diff options
author | Eugene Zelenko <eugene.zelenko@gmail.com> | 2017-12-06 23:18:41 +0000 |
---|---|---|
committer | Eugene Zelenko <eugene.zelenko@gmail.com> | 2017-12-06 23:18:41 +0000 |
commit | 08fe1221761fe36d59a47304d2b5e6ba66c61c87 (patch) | |
tree | 83ecc978c82d82c9ca24eda318d3162a71562e2c /lib/Lex/PTHLexer.cpp | |
parent | 1dae00ad5ffbc58dc0cf00953ca9c991adfd93fd (diff) |
[Lex] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319986 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/PTHLexer.cpp')
-rw-r--r-- | lib/Lex/PTHLexer.cpp | 89 |
1 files changed, 55 insertions, 34 deletions
diff --git a/lib/Lex/PTHLexer.cpp b/lib/Lex/PTHLexer.cpp index ec806e8445..d6c20a13d2 100644 --- a/lib/Lex/PTHLexer.cpp +++ b/lib/Lex/PTHLexer.cpp @@ -1,4 +1,4 @@ -//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===// +//===- PTHLexer.cpp - Lex from a token stream -----------------------------===// // // The LLVM Compiler Infrastructure // @@ -12,19 +12,32 @@ //===----------------------------------------------------------------------===// #include "clang/Lex/PTHLexer.h" +#include "clang/Basic/Diagnostic.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/FileSystemStatCache.h" #include "clang/Basic/IdentifierTable.h" +#include "clang/Basic/SourceManager.h" #include "clang/Basic/TokenKinds.h" #include "clang/Lex/LexDiagnostic.h" #include "clang/Lex/PTHManager.h" #include "clang/Lex/Preprocessor.h" #include "clang/Lex/Token.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" -#include "llvm/Support/EndianStream.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/OnDiskHashTable.h" +#include <cassert> +#include <cstdint> +#include <cstdlib> +#include <cstring> +#include <ctime> #include <memory> -#include <system_error> +#include <utility> + using namespace clang; static const unsigned StoredTokenSize = 1 + 1 + 2 + 4 + 4; @@ -35,9 +48,8 @@ static const unsigned StoredTokenSize = 1 + 1 + 2 + 4 + 4; PTHLexer::PTHLexer(Preprocessor &PP, FileID FID, const unsigned char *D, const unsigned char *ppcond, PTHManager &PM) - : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), LastHashTokPtr(nullptr), - PPCond(ppcond), CurPPCondPtr(ppcond), PTHMgr(PM) { - + : PreprocessorLexer(&PP, FID), TokBuf(D), CurPtr(D), PPCond(ppcond), + CurPPCondPtr(ppcond), PTHMgr(PM) { FileStartLoc = PP.getSourceManager().getLocForStartOfFile(FID); } @@ -167,7 +179,7 @@ void PTHLexer::DiscardToEndOfLine() { // We don't need to actually reconstruct full tokens from the token buffer. // This saves some copies and it also reduces IdentifierInfo* lookup. const unsigned char* p = CurPtr; - while (1) { + while (true) { // Read the token kind. Are we at the end of the file? tok::TokenKind x = (tok::TokenKind) (uint8_t) *p; if (x == tok::eof) break; @@ -186,6 +198,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."); @@ -303,23 +316,24 @@ SourceLocation PTHLexer::getSourceLocation() { /// to map from FileEntry objects managed by FileManager to offsets within /// the PTH file. namespace { + class PTHFileData { const uint32_t TokenOff; const uint32_t PPCondOff; + public: PTHFileData(uint32_t tokenOff, uint32_t ppCondOff) - : TokenOff(tokenOff), PPCondOff(ppCondOff) {} + : TokenOff(tokenOff), PPCondOff(ppCondOff) {} uint32_t getTokenOffset() const { return TokenOff; } uint32_t getPPCondOffset() const { return PPCondOff; } }; - class PTHFileLookupCommonTrait { public: - typedef std::pair<unsigned char, StringRef> internal_key_type; - typedef unsigned hash_value_type; - typedef unsigned offset_type; + using internal_key_type = std::pair<unsigned char, StringRef>; + using hash_value_type = unsigned; + using offset_type = unsigned; static hash_value_type ComputeHash(internal_key_type x) { return llvm::HashString(x.second); @@ -328,6 +342,7 @@ public: static std::pair<unsigned, unsigned> ReadKeyDataLength(const unsigned char*& d) { using namespace llvm::support; + unsigned keyLen = (unsigned)endian::readNext<uint16_t, little, unaligned>(d); unsigned dataLen = (unsigned) *(d++); @@ -340,12 +355,12 @@ public: } }; -} // end anonymous namespace +} // namespace class PTHManager::PTHFileLookupTrait : public PTHFileLookupCommonTrait { public: - typedef const FileEntry* external_key_type; - typedef PTHFileData data_type; + using external_key_type = const FileEntry *; + using data_type = PTHFileData; static internal_key_type GetInternalKey(const FileEntry* FE) { return std::make_pair((unsigned char) 0x1, FE->getName()); @@ -357,8 +372,9 @@ public: static PTHFileData ReadData(const internal_key_type& k, const unsigned char* d, unsigned) { - assert(k.first == 0x1 && "Only file lookups can match!"); using namespace llvm::support; + + assert(k.first == 0x1 && "Only file lookups can match!"); uint32_t x = endian::readNext<uint32_t, little, unaligned>(d); uint32_t y = endian::readNext<uint32_t, little, unaligned>(d); return PTHFileData(x, y); @@ -367,11 +383,11 @@ public: class PTHManager::PTHStringLookupTrait { public: - typedef uint32_t data_type; - typedef const std::pair<const char*, unsigned> external_key_type; - typedef external_key_type internal_key_type; - typedef uint32_t hash_value_type; - typedef unsigned offset_type; + using data_type = uint32_t; + using external_key_type = const std::pair<const char *, unsigned>; + using internal_key_type = external_key_type; + using hash_value_type = uint32_t; + using offset_type = unsigned; static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { @@ -390,6 +406,7 @@ public: static std::pair<unsigned, unsigned> ReadKeyDataLength(const unsigned char*& d) { using namespace llvm::support; + return std::make_pair( (unsigned)endian::readNext<uint16_t, little, unaligned>(d), sizeof(uint32_t)); @@ -404,6 +421,7 @@ public: static uint32_t ReadData(const internal_key_type& k, const unsigned char* d, unsigned) { using namespace llvm::support; + return endian::readNext<uint32_t, little, unaligned>(d); } }; @@ -420,11 +438,10 @@ PTHManager::PTHManager( const unsigned char *spellingBase, const char *originalSourceFile) : Buf(std::move(buf)), PerIDCache(std::move(perIDCache)), FileLookup(std::move(fileLookup)), IdDataTable(idDataTable), - StringIdLookup(std::move(stringIdLookup)), NumIds(numIds), PP(nullptr), + StringIdLookup(std::move(stringIdLookup)), NumIds(numIds), SpellingBase(spellingBase), OriginalSourceFile(originalSourceFile) {} -PTHManager::~PTHManager() { -} +PTHManager::~PTHManager() = default; static void InvalidPTH(DiagnosticsEngine &Diags, const char *Msg) { Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) << Msg; @@ -557,6 +574,7 @@ PTHManager *PTHManager::Create(StringRef file, DiagnosticsEngine &Diags) { 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 = @@ -566,7 +584,7 @@ IdentifierInfo* PTHManager::LazilyCreateIdentifierInfo(unsigned PersistentID) { // Allocate the object. std::pair<IdentifierInfo,const unsigned char*> *Mem = - Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >(); + Alloc.Allocate<std::pair<IdentifierInfo, const unsigned char *>>(); Mem->second = IDData; assert(IDData[0] != '\0'); @@ -626,26 +644,26 @@ PTHLexer *PTHManager::CreateLexer(FileID FID) { //===----------------------------------------------------------------------===// namespace { + class PTHStatData { public: uint64_t Size; time_t ModTime; llvm::sys::fs::UniqueID UniqueID; - const bool HasData; + const bool HasData = false; bool IsDirectory; + PTHStatData() = default; PTHStatData(uint64_t Size, time_t ModTime, llvm::sys::fs::UniqueID UniqueID, bool IsDirectory) : Size(Size), ModTime(ModTime), UniqueID(UniqueID), HasData(true), IsDirectory(IsDirectory) {} - - PTHStatData() : HasData(false) {} }; class PTHStatLookupTrait : public PTHFileLookupCommonTrait { public: - typedef StringRef external_key_type; // const char* - typedef PTHStatData data_type; + using external_key_type = StringRef; // const char* + using data_type = PTHStatData; static internal_key_type GetInternalKey(StringRef path) { // The key 'kind' doesn't matter here because it is ignored in EqualKey. @@ -660,7 +678,6 @@ public: static data_type ReadData(const internal_key_type& k, const unsigned char* d, unsigned) { - if (k.first /* File or Directory */) { bool IsDirectory = true; if (k.first == 0x1 /* File */) { @@ -682,11 +699,14 @@ public: return data_type(); } }; -} // end anonymous namespace + +} // namespace namespace clang { + class PTHStatCache : public FileSystemStatCache { - typedef llvm::OnDiskChainedHashTable<PTHStatLookupTrait> CacheTy; + using CacheTy = llvm::OnDiskChainedHashTable<PTHStatLookupTrait>; + CacheTy Cache; public: @@ -720,7 +740,8 @@ public: return CacheExists; } }; -} + +} // namespace clang std::unique_ptr<FileSystemStatCache> PTHManager::createStatCache() { return llvm::make_unique<PTHStatCache>(*FileLookup); |