From afd1b1c9896ff3bab823bc90ff96f04573c15355 Mon Sep 17 00:00:00 2001 From: Eugene Zelenko Date: Wed, 6 Dec 2017 23:18:41 +0000 Subject: [Lex] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC). llvm-svn: 319986 --- clang/lib/Lex/PTHLexer.cpp | 89 ++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 34 deletions(-) (limited to 'clang/lib/Lex/PTHLexer.cpp') diff --git a/clang/lib/Lex/PTHLexer.cpp b/clang/lib/Lex/PTHLexer.cpp index ec806e84453..d6c20a13d27 100644 --- a/clang/lib/Lex/PTHLexer.cpp +++ b/clang/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 +#include +#include +#include +#include #include -#include +#include + 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 internal_key_type; - typedef unsigned hash_value_type; - typedef unsigned offset_type; + using internal_key_type = std::pair; + 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 ReadKeyDataLength(const unsigned char*& d) { using namespace llvm::support; + unsigned keyLen = (unsigned)endian::readNext(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(d); uint32_t y = endian::readNext(d); return PTHFileData(x, y); @@ -367,11 +383,11 @@ public: class PTHManager::PTHStringLookupTrait { public: - typedef uint32_t data_type; - typedef const std::pair 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; + 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 ReadKeyDataLength(const unsigned char*& d) { using namespace llvm::support; + return std::make_pair( (unsigned)endian::readNext(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(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 *Mem = - Alloc.Allocate >(); + Alloc.Allocate>(); 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 CacheTy; + using CacheTy = llvm::OnDiskChainedHashTable; + CacheTy Cache; public: @@ -720,7 +740,8 @@ public: return CacheExists; } }; -} + +} // namespace clang std::unique_ptr PTHManager::createStatCache() { return llvm::make_unique(*FileLookup); -- cgit v1.2.3