diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Serialization/GlobalModuleIndex.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/Serialization/GlobalModuleIndex.cpp | 116 |
1 files changed, 74 insertions, 42 deletions
diff --git a/contrib/llvm/tools/clang/lib/Serialization/GlobalModuleIndex.cpp b/contrib/llvm/tools/clang/lib/Serialization/GlobalModuleIndex.cpp index fb647b0..9858122 100644 --- a/contrib/llvm/tools/clang/lib/Serialization/GlobalModuleIndex.cpp +++ b/contrib/llvm/tools/clang/lib/Serialization/GlobalModuleIndex.cpp @@ -13,7 +13,7 @@ #include "ASTReaderInternals.h" #include "clang/Basic/FileManager.h" -#include "clang/Basic/OnDiskHashTable.h" +#include "clang/Lex/HeaderSearch.h" #include "clang/Serialization/ASTBitCodes.h" #include "clang/Serialization/GlobalModuleIndex.h" #include "clang/Serialization/Module.h" @@ -26,6 +26,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/LockFileManager.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/OnDiskHashTable.h" #include "llvm/Support/Path.h" #include <cstdio> using namespace clang; @@ -71,20 +72,22 @@ public: typedef StringRef external_key_type; typedef StringRef internal_key_type; typedef SmallVector<unsigned, 2> data_type; + typedef unsigned hash_value_type; + typedef unsigned offset_type; static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { return a == b; } - static unsigned ComputeHash(const internal_key_type& a) { + static hash_value_type ComputeHash(const internal_key_type& a) { return llvm::HashString(a); } static std::pair<unsigned, unsigned> ReadKeyDataLength(const unsigned char*& d) { - using namespace clang::io; - unsigned KeyLen = ReadUnalignedLE16(d); - unsigned DataLen = ReadUnalignedLE16(d); + using namespace llvm::support; + unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); + unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); return std::make_pair(KeyLen, DataLen); } @@ -101,11 +104,11 @@ public: static data_type ReadData(const internal_key_type& k, const unsigned char* d, unsigned DataLen) { - using namespace clang::io; + using namespace llvm::support; data_type Result; while (DataLen > 0) { - unsigned ID = ReadUnalignedLE32(d); + unsigned ID = endian::readNext<uint32_t, little, unaligned>(d); Result.push_back(ID); DataLen -= 4; } @@ -114,7 +117,8 @@ public: } }; -typedef OnDiskChainedHashTable<IdentifierIndexReaderTrait> IdentifierIndexTable; +typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait> + IdentifierIndexTable; } @@ -202,7 +206,12 @@ GlobalModuleIndex::GlobalModuleIndex(llvm::MemoryBuffer *Buffer, assert(Idx == Record.size() && "More module info?"); // Record this module as an unresolved module. - UnresolvedModules[llvm::sys::path::stem(Modules[ID].FileName)] = ID; + // FIXME: this doesn't work correctly for module names containing path + // separators. + StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName); + // Remove the -<hash of ModuleMapPath> + ModuleName = ModuleName.rsplit('-').first; + UnresolvedModules[ModuleName] = ID; break; } @@ -210,16 +219,18 @@ GlobalModuleIndex::GlobalModuleIndex(llvm::MemoryBuffer *Buffer, // Wire up the identifier index. if (Record[0]) { IdentifierIndex = IdentifierIndexTable::Create( - (const unsigned char *)Blob.data() + Record[0], - (const unsigned char *)Blob.data(), - IdentifierIndexReaderTrait()); + (const unsigned char *)Blob.data() + Record[0], + (const unsigned char *)Blob.data() + sizeof(uint32_t), + (const unsigned char *)Blob.data(), IdentifierIndexReaderTrait()); } break; } } } -GlobalModuleIndex::~GlobalModuleIndex() { } +GlobalModuleIndex::~GlobalModuleIndex() { + delete static_cast<IdentifierIndexTable *>(IdentifierIndex); +} std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> GlobalModuleIndex::readIndex(StringRef Path) { @@ -228,10 +239,11 @@ GlobalModuleIndex::readIndex(StringRef Path) { IndexPath += Path; llvm::sys::path::append(IndexPath, IndexFileName); - llvm::OwningPtr<llvm::MemoryBuffer> Buffer; - if (llvm::MemoryBuffer::getFile(IndexPath.c_str(), Buffer) != - llvm::errc::success) - return std::make_pair((GlobalModuleIndex *)0, EC_NotFound); + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr = + llvm::MemoryBuffer::getFile(IndexPath.c_str()); + if (!BufferOrErr) + return std::make_pair(nullptr, EC_NotFound); + std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get()); /// \brief The bitstream reader from which we'll read the AST file. llvm::BitstreamReader Reader((const unsigned char *)Buffer->getBufferStart(), @@ -245,10 +257,11 @@ GlobalModuleIndex::readIndex(StringRef Path) { Cursor.Read(8) != 'C' || Cursor.Read(8) != 'G' || Cursor.Read(8) != 'I') { - return std::make_pair((GlobalModuleIndex *)0, EC_IOError); + return std::make_pair(nullptr, EC_IOError); } - - return std::make_pair(new GlobalModuleIndex(Buffer.take(), Cursor), EC_None); + + return std::make_pair(new GlobalModuleIndex(Buffer.release(), Cursor), + EC_None); } void @@ -306,7 +319,7 @@ bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) { bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) { // Look for the module in the global module index based on the module name. - StringRef Name = llvm::sys::path::stem(File->FileName); + StringRef Name = File->ModuleName; llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name); if (Known == UnresolvedModules.end()) { return true; @@ -341,6 +354,19 @@ void GlobalModuleIndex::printStats() { std::fprintf(stderr, "\n"); } +void GlobalModuleIndex::dump() { + llvm::errs() << "*** Global Module Index Dump:\n"; + llvm::errs() << "Module files:\n"; + for (auto &MI : Modules) { + llvm::errs() << "** " << MI.FileName << "\n"; + if (MI.File) + MI.File->dump(); + else + llvm::errs() << "\n"; + } + llvm::errs() << "\n"; +} + //----------------------------------------------------------------------------// // Global module index writer. //----------------------------------------------------------------------------// @@ -411,7 +437,7 @@ static void emitBlockID(unsigned ID, const char *Name, Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); // Emit the block name if present. - if (Name == 0 || Name[0] == 0) return; + if (!Name || Name[0] == 0) return; Record.clear(); while (*Name) Record.push_back(*Name++); @@ -458,8 +484,8 @@ namespace { unsigned DataLen) { // The first bit indicates whether this identifier is interesting. // That's all we care about. - using namespace clang::io; - unsigned RawID = ReadUnalignedLE32(d); + using namespace llvm::support; + unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); bool IsInteresting = RawID & 0x01; return std::make_pair(k, IsInteresting); } @@ -468,7 +494,7 @@ namespace { bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { // Open the module file. - OwningPtr<llvm::MemoryBuffer> Buffer; + std::unique_ptr<llvm::MemoryBuffer> Buffer; std::string ErrorStr; Buffer.reset(FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true)); if (!Buffer) { @@ -590,12 +616,13 @@ bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { // Handle the identifier table if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) { - typedef OnDiskChainedHashTable<InterestingASTIdentifierLookupTrait> - InterestingIdentifierTable; - llvm::OwningPtr<InterestingIdentifierTable> - Table(InterestingIdentifierTable::Create( - (const unsigned char *)Blob.data() + Record[0], - (const unsigned char *)Blob.data())); + typedef llvm::OnDiskIterableChainedHashTable< + InterestingASTIdentifierLookupTrait> InterestingIdentifierTable; + std::unique_ptr<InterestingIdentifierTable> Table( + InterestingIdentifierTable::Create( + (const unsigned char *)Blob.data() + Record[0], + (const unsigned char *)Blob.data() + sizeof(uint32_t), + (const unsigned char *)Blob.data())); for (InterestingIdentifierTable::data_iterator D = Table->data_begin(), DEnd = Table->data_end(); D != DEnd; ++D) { @@ -623,17 +650,21 @@ public: typedef StringRef key_type_ref; typedef SmallVector<unsigned, 2> data_type; typedef const SmallVector<unsigned, 2> &data_type_ref; + typedef unsigned hash_value_type; + typedef unsigned offset_type; - static unsigned ComputeHash(key_type_ref Key) { + static hash_value_type ComputeHash(key_type_ref Key) { return llvm::HashString(Key); } std::pair<unsigned,unsigned> EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) { + using namespace llvm::support; + endian::Writer<little> LE(Out); unsigned KeyLen = Key.size(); unsigned DataLen = Data.size() * 4; - clang::io::Emit16(Out, KeyLen); - clang::io::Emit16(Out, DataLen); + LE.write<uint16_t>(KeyLen); + LE.write<uint16_t>(DataLen); return std::make_pair(KeyLen, DataLen); } @@ -643,8 +674,9 @@ public: void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data, unsigned DataLen) { + using namespace llvm::support; for (unsigned I = 0, N = Data.size(); I != N; ++I) - clang::io::Emit32(Out, Data[I]); + endian::Writer<little>(Out).write<uint32_t>(Data[I]); } }; @@ -692,7 +724,7 @@ void GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) { // Write the identifier -> module file mapping. { - OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator; + llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator; IdentifierIndexWriterTrait Trait; // Populate the hash table. @@ -706,9 +738,10 @@ void GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) { SmallString<4096> IdentifierTable; uint32_t BucketOffset; { + using namespace llvm::support; llvm::raw_svector_ostream Out(IdentifierTable); // Make sure that no bucket is at offset 0 - clang::io::Emit32(Out, 0); + endian::Writer<little>(Out).write<uint32_t>(0); BucketOffset = Generator.Emit(Out, Trait); } @@ -756,7 +789,7 @@ GlobalModuleIndex::writeIndex(FileManager &FileMgr, StringRef Path) { GlobalModuleIndexBuilder Builder(FileMgr); // Load each of the module files. - llvm::error_code EC; + std::error_code EC; for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd; D != DEnd && !EC; D.increment(EC)) { @@ -807,13 +840,12 @@ GlobalModuleIndex::writeIndex(FileManager &FileMgr, StringRef Path) { return EC_IOError; // Remove the old index file. It isn't relevant any more. - bool OldIndexExisted; - llvm::sys::fs::remove(IndexPath.str(), OldIndexExisted); + llvm::sys::fs::remove(IndexPath.str()); // Rename the newly-written index file to the proper name. if (llvm::sys::fs::rename(IndexTmpPath.str(), IndexPath.str())) { // Rename failed; just remove the - llvm::sys::fs::remove(IndexTmpPath.str(), OldIndexExisted); + llvm::sys::fs::remove(IndexTmpPath.str()); return EC_IOError; } @@ -835,7 +867,7 @@ namespace { End = Idx.key_end(); } - virtual StringRef Next() { + StringRef Next() override { if (Current == End) return StringRef(); |