summaryrefslogtreecommitdiffstats
path: root/lib/Lex/PTHLexer.cpp
diff options
context:
space:
mode:
authorJustin Bogner <mail@justinbogner.com>2014-03-28 22:03:24 +0000
committerJustin Bogner <mail@justinbogner.com>2014-03-28 22:03:24 +0000
commitd60a851dd59b8888acbd9e1a917ac4e8802a4729 (patch)
tree0e979a7088c67652b2e481c556bc9880cdfbb1c4 /lib/Lex/PTHLexer.cpp
parent73433a87fa9458b99a92a8f88a27ef47ff0d0ded (diff)
Reapply "OnDiskHashTable: Use Endian.h to read little endian ostreams"
Committed this by accident before it was done last time. Original message: Rather than rolling our own functions to read little endian data from a buffer, we can use the support in llvm's Endian.h. No functional change. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205062 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/PTHLexer.cpp')
-rw-r--r--lib/Lex/PTHLexer.cpp86
1 files changed, 57 insertions, 29 deletions
diff --git a/lib/Lex/PTHLexer.cpp b/lib/Lex/PTHLexer.cpp
index b671f9b4c1..1ca16d39b4 100644
--- a/lib/Lex/PTHLexer.cpp
+++ b/lib/Lex/PTHLexer.cpp
@@ -48,14 +48,17 @@ bool PTHLexer::Lex(Token& Tok) {
//===--------------------------------------==//
// Read the raw token data.
//===--------------------------------------==//
+ using namespace llvm::support;
// Shadow CurPtr into an automatic variable.
const unsigned char *CurPtrShadow = CurPtr;
// Read in the data for the token.
- unsigned Word0 = ReadLE32(CurPtrShadow);
- uint32_t IdentifierID = ReadLE32(CurPtrShadow);
- uint32_t FileOffset = ReadLE32(CurPtrShadow);
+ unsigned Word0 = endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
+ uint32_t IdentifierID =
+ endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
+ uint32_t FileOffset =
+ endian::readNext<uint32_t, little, aligned>(CurPtrShadow);
tok::TokenKind TKind = (tok::TokenKind) (Word0 & 0xFF);
Token::TokenFlags TFlags = (Token::TokenFlags) ((Word0 >> 8) & 0xFF);
@@ -185,6 +188,7 @@ void PTHLexer::DiscardToEndOfLine() {
/// SkipBlock - Used by Preprocessor to skip the current conditional block.
bool PTHLexer::SkipBlock() {
+ using namespace llvm::support;
assert(CurPPCondPtr && "No cached PP conditional information.");
assert(LastHashTokPtr && "No known '#' token.");
@@ -193,10 +197,10 @@ bool PTHLexer::SkipBlock() {
do {
// Read the token offset from the side-table.
- uint32_t Offset = ReadLE32(CurPPCondPtr);
+ uint32_t Offset = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
// Read the target table index from the side-table.
- TableIdx = ReadLE32(CurPPCondPtr);
+ TableIdx = endian::readNext<uint32_t, little, aligned>(CurPPCondPtr);
// Compute the actual memory address of the '#' token data for this entry.
HashEntryI = TokBuf + Offset;
@@ -213,12 +217,13 @@ bool PTHLexer::SkipBlock() {
PPCond + TableIdx*(sizeof(uint32_t)*2);
assert(NextPPCondPtr >= CurPPCondPtr);
// Read where we should jump to.
- const unsigned char* HashEntryJ = TokBuf + ReadLE32(NextPPCondPtr);
+ const unsigned char *HashEntryJ =
+ TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
if (HashEntryJ <= LastHashTokPtr) {
// Jump directly to the next entry in the side table.
HashEntryI = HashEntryJ;
- TableIdx = ReadLE32(NextPPCondPtr);
+ TableIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
CurPPCondPtr = NextPPCondPtr;
}
}
@@ -233,8 +238,9 @@ bool PTHLexer::SkipBlock() {
CurPPCondPtr = NextPPCondPtr;
// Read where we should jump to.
- HashEntryI = TokBuf + ReadLE32(NextPPCondPtr);
- uint32_t NextIdx = ReadLE32(NextPPCondPtr);
+ HashEntryI =
+ TokBuf + endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
+ uint32_t NextIdx = endian::readNext<uint32_t, little, aligned>(NextPPCondPtr);
// By construction NextIdx will be zero if this is a #endif. This is useful
// to know to obviate lexing another token.
@@ -283,8 +289,10 @@ SourceLocation PTHLexer::getSourceLocation() {
// handling a #included file. Just read the necessary data from the token
// data buffer to construct the SourceLocation object.
// NOTE: This is a virtual function; hence it is defined out-of-line.
+ using namespace llvm::support;
+
const unsigned char *OffsetPtr = CurPtr + (DISK_TOKEN_SIZE - 4);
- uint32_t Offset = ReadLE32(OffsetPtr);
+ uint32_t Offset = endian::readNext<uint32_t, little, aligned>(OffsetPtr);
return FileStartLoc.getLocWithOffset(Offset);
}
@@ -318,7 +326,9 @@ public:
static std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char*& d) {
- unsigned keyLen = (unsigned) ReadUnalignedLE16(d);
+ using namespace llvm::support;
+ unsigned keyLen =
+ (unsigned)endian::readNext<uint16_t, little, unaligned>(d);
unsigned dataLen = (unsigned) *(d++);
return std::make_pair(keyLen, dataLen);
}
@@ -345,8 +355,9 @@ public:
static PTHFileData ReadData(const internal_key_type& k,
const unsigned char* d, unsigned) {
assert(k.first == 0x1 && "Only file lookups can match!");
- uint32_t x = ::ReadUnalignedLE32(d);
- uint32_t y = ::ReadUnalignedLE32(d);
+ using namespace llvm::support;
+ uint32_t x = endian::readNext<uint32_t, little, unaligned>(d);
+ uint32_t y = endian::readNext<uint32_t, little, unaligned>(d);
return PTHFileData(x, y);
}
};
@@ -377,7 +388,10 @@ public:
static std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char*& d) {
- return std::make_pair((unsigned) ReadUnalignedLE16(d), sizeof(uint32_t));
+ using namespace llvm::support;
+ return std::make_pair(
+ (unsigned)endian::readNext<uint16_t, little, unaligned>(d),
+ sizeof(uint32_t));
}
static std::pair<const char*, unsigned>
@@ -388,7 +402,8 @@ public:
static uint32_t ReadData(const internal_key_type& k, const unsigned char* d,
unsigned) {
- return ::ReadUnalignedLE32(d);
+ using namespace llvm::support;
+ return endian::readNext<uint32_t, little, unaligned>(d);
}
};
@@ -434,6 +449,8 @@ PTHManager *PTHManager::Create(const std::string &file,
return 0;
}
+ using namespace llvm::support;
+
// Get the buffer ranges and check if there are at least three 32-bit
// words at the end of the file.
const unsigned char *BufBeg = (const unsigned char*)File->getBufferStart();
@@ -448,7 +465,7 @@ PTHManager *PTHManager::Create(const std::string &file,
// Read the PTH version.
const unsigned char *p = BufBeg + (sizeof("cfe-pth"));
- unsigned Version = ReadLE32(p);
+ unsigned Version = endian::readNext<uint32_t, little, aligned>(p);
if (Version < PTHManager::Version) {
InvalidPTH(Diags,
@@ -469,7 +486,8 @@ PTHManager *PTHManager::Create(const std::string &file,
// Construct the file lookup table. This will be used for mapping from
// FileEntry*'s to cached tokens.
const unsigned char* FileTableOffset = PrologueOffset + sizeof(uint32_t)*2;
- const unsigned char* FileTable = BufBeg + ReadLE32(FileTableOffset);
+ const unsigned char *FileTable =
+ BufBeg + endian::readNext<uint32_t, little, aligned>(FileTableOffset);
if (!(FileTable > BufBeg && FileTable < BufEnd)) {
Diags.Report(diag::err_invalid_pth_file) << file;
@@ -486,7 +504,8 @@ PTHManager *PTHManager::Create(const std::string &file,
// Get the location of the table mapping from persistent ids to the
// data needed to reconstruct identifiers.
const unsigned char* IDTableOffset = PrologueOffset + sizeof(uint32_t)*0;
- const unsigned char* IData = BufBeg + ReadLE32(IDTableOffset);
+ const unsigned char *IData =
+ BufBeg + endian::readNext<uint32_t, little, aligned>(IDTableOffset);
if (!(IData >= BufBeg && IData < BufEnd)) {
Diags.Report(diag::err_invalid_pth_file) << file;
@@ -496,7 +515,8 @@ PTHManager *PTHManager::Create(const std::string &file,
// Get the location of the hashtable mapping between strings and
// persistent IDs.
const unsigned char* StringIdTableOffset = PrologueOffset + sizeof(uint32_t)*1;
- const unsigned char* StringIdTable = BufBeg + ReadLE32(StringIdTableOffset);
+ const unsigned char *StringIdTable =
+ BufBeg + endian::readNext<uint32_t, little, aligned>(StringIdTableOffset);
if (!(StringIdTable >= BufBeg && StringIdTable < BufEnd)) {
Diags.Report(diag::err_invalid_pth_file) << file;
return 0;
@@ -507,14 +527,15 @@ PTHManager *PTHManager::Create(const std::string &file,
// Get the location of the spelling cache.
const unsigned char* spellingBaseOffset = PrologueOffset + sizeof(uint32_t)*3;
- const unsigned char* spellingBase = BufBeg + ReadLE32(spellingBaseOffset);
+ const unsigned char *spellingBase =
+ BufBeg + endian::readNext<uint32_t, little, aligned>(spellingBaseOffset);
if (!(spellingBase >= BufBeg && spellingBase < BufEnd)) {
Diags.Report(diag::err_invalid_pth_file) << file;
return 0;
}
// Get the number of IdentifierInfos and pre-allocate the identifier cache.
- uint32_t NumIds = ReadLE32(IData);
+ uint32_t NumIds = endian::readNext<uint32_t, little, aligned>(IData);
// Pre-allocate the persistent ID -> IdentifierInfo* cache. We use calloc()
// so that we in the best case only zero out memory once when the OS returns
@@ -531,7 +552,8 @@ PTHManager *PTHManager::Create(const std::string &file,
// Compute the address of the original source file.
const unsigned char* originalSourceBase = PrologueOffset + sizeof(uint32_t)*4;
- unsigned len = ReadUnalignedLE16(originalSourceBase);
+ unsigned len =
+ endian::readNext<uint16_t, little, unaligned>(originalSourceBase);
if (!len) originalSourceBase = 0;
// Create the new PTHManager.
@@ -541,10 +563,12 @@ PTHManager *PTHManager::Create(const std::string &file,
}
IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) {
+ using namespace llvm::support;
// Look in the PTH file for the string data for the IdentifierInfo object.
const unsigned char* TableEntry = IdDataTable + sizeof(uint32_t)*PersistentID;
- const unsigned char* IDData =
- (const unsigned char*)Buf->getBufferStart() + ReadLE32(TableEntry);
+ const unsigned char *IDData =
+ (const unsigned char *)Buf->getBufferStart() +
+ endian::readNext<uint32_t, little, aligned>(TableEntry);
assert(IDData < (const unsigned char*)Buf->getBufferEnd());
// Allocate the object.
@@ -580,6 +604,8 @@ PTHLexer *PTHManager::CreateLexer(FileID FID) {
if (!FE)
return 0;
+ using namespace llvm::support;
+
// Lookup the FileEntry object in our file lookup data structure. It will
// return a variant that indicates whether or not there is an offset within
// the PTH file that contains cached tokens.
@@ -597,7 +623,7 @@ PTHLexer *PTHManager::CreateLexer(FileID FID) {
// Get the location of pp-conditional table.
const unsigned char* ppcond = BufStart + FileData.getPPCondOffset();
- uint32_t Len = ReadLE32(ppcond);
+ uint32_t Len = endian::readNext<uint32_t, little, aligned>(ppcond);
if (Len == 0) ppcond = 0;
assert(PP && "No preprocessor set yet!");
@@ -651,11 +677,13 @@ public:
d += 4 * 2; // Skip the first 2 words.
}
- uint64_t File = ReadUnalignedLE64(d);
- uint64_t Device = ReadUnalignedLE64(d);
+ using namespace llvm::support;
+
+ uint64_t File = endian::readNext<uint64_t, little, unaligned>(d);
+ uint64_t Device = endian::readNext<uint64_t, little, unaligned>(d);
llvm::sys::fs::UniqueID UniqueID(File, Device);
- time_t ModTime = ReadUnalignedLE64(d);
- uint64_t Size = ReadUnalignedLE64(d);
+ time_t ModTime = endian::readNext<uint64_t, little, unaligned>(d);
+ uint64_t Size = endian::readNext<uint64_t, little, unaligned>(d);
return data_type(Size, ModTime, UniqueID, IsDirectory);
}