gamesh411 updated this revision to Diff 211363.
gamesh411 added a comment.
Too much autoformat fixed
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D64753/new/
https://reviews.llvm.org/D64753
Files:
clang/include/clang/CrossTU/CrossTranslationUnit.h
clang/lib/CrossTU/CrossTranslationUnit.cpp
Index: clang/lib/CrossTU/CrossTranslationUnit.cpp
===================================================================
--- clang/lib/CrossTU/CrossTranslationUnit.cpp
+++ clang/lib/CrossTU/CrossTranslationUnit.cpp
@@ -188,8 +188,8 @@
}
CrossTranslationUnitContext::CrossTranslationUnitContext(CompilerInstance &CI)
- : CI(CI), Context(CI.getASTContext()),
- CTULoadThreshold(CI.getAnalyzerOpts()->CTUImportThreshold) {}
+ : CI(CI), Context(CI.getASTContext()), ASTStorage(CI),
+ CTULoadGuard(CI.getAnalyzerOpts()->CTUImportThreshold) {}
CrossTranslationUnitContext::~CrossTranslationUnitContext() {}
@@ -237,8 +237,8 @@
if (LookupName.empty())
return llvm::make_error<IndexError>(
index_error_code::failed_to_generate_usr);
- llvm::Expected<ASTUnit *> ASTUnitOrError = loadExternalAST(
- LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
+ llvm::Expected<ASTUnit *> ASTUnitOrError =
+ loadExternalAST(LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
if (!ASTUnitOrError)
return ASTUnitOrError.takeError();
ASTUnit *Unit = *ASTUnitOrError;
@@ -340,6 +340,144 @@
}
}
+CrossTranslationUnitContext::LoadGuard::LoadGuard(unsigned CTULoadThreshold)
+ : CTULoadThreshold(CTULoadThreshold), NumASTLoaded(0u) {}
+
+CrossTranslationUnitContext::LoadPass
+CrossTranslationUnitContext::LoadGuard::beginLoad() {
+ bool CanBegin = NumASTLoaded < CTULoadThreshold;
+ return {NumASTLoaded, CanBegin};
+}
+
+CrossTranslationUnitContext::LoadPass::LoadPass(unsigned &NumASTLoaded,
+ bool CanBegin)
+ : NumASTLoaded(NumASTLoaded), CanBegin(CanBegin), WasSuccessful(false) {}
+
+CrossTranslationUnitContext::LoadPass::~LoadPass() {
+ if (WasSuccessful)
+ ++NumASTLoaded;
+}
+
+void CrossTranslationUnitContext::LoadPass::wasSuccessful() {
+ WasSuccessful = true;
+}
+
+CrossTranslationUnitContext::LoadPass::operator bool() const {
+ return CanBegin;
+}
+
+CrossTranslationUnitContext::ASTDumpLoader::ASTDumpLoader(
+ const CompilerInstance &CI)
+ : CI(CI) {}
+
+std::unique_ptr<ASTUnit>
+CrossTranslationUnitContext::ASTDumpLoader::operator()(StringRef ASTFileName) {
+ // Load AST from ast-dump.
+ IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+ TextDiagnosticPrinter *DiagClient =
+ new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
+ IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
+ IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
+ new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
+
+ return ASTUnit::LoadFromASTFile(
+ ASTFileName, CI.getPCHContainerOperations()->getRawReader(),
+ ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts());
+}
+
+CrossTranslationUnitContext::ASTUnitStorage::ASTUnitStorage(
+ const CompilerInstance &CI)
+ : FileAccessor(CI) {}
+
+llvm::Expected<ASTUnit *>
+CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFile(StringRef FileName) {
+ // Try the cache first.
+ auto ASTCacheEntry = FileASTUnitMap.find(FileName);
+ if (ASTCacheEntry == FileASTUnitMap.end()) {
+ // Load the ASTUnit from the pre-dumped AST file specified by ASTFileName.
+ std::unique_ptr<ASTUnit> LoadedUnit = FileAccessor(FileName);
+
+ // Need the raw pointer and the unique_ptr as well.
+ ASTUnit* Unit = LoadedUnit.get();
+
+ // Update the cache.
+ FileASTUnitMap[FileName] = std::move(LoadedUnit);
+ return Unit;
+
+ } else {
+ // Found in the cache.
+ return ASTCacheEntry->second.get();
+ }
+}
+
+llvm::Expected<ASTUnit *>
+CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFunction(
+ StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName) {
+ // Try the cache first.
+ auto ASTCacheEntry = NameASTUnitMap.find(FunctionName);
+ if (ASTCacheEntry == NameASTUnitMap.end()) {
+ // Load the ASTUnit from the pre-dumped AST file specified by ASTFileName.
+
+ // Ensure that the Index is loaded, as we need to search in it.
+ if (llvm::Error IndexLoadError =
+ ensureCTUIndexLoaded(CrossTUDir, IndexName))
+ return std::move(IndexLoadError);
+
+ // Check if there is and entry in the index for the function.
+ if (!NameFileMap.count(FunctionName)) {
+ ++NumNotInOtherTU;
+ return llvm::make_error<IndexError>(index_error_code::missing_definition);
+ }
+
+ // Search in the index for the filename where the definition of FuncitonName
+ // resides.
+ if (llvm::Expected<ASTUnit *> FoundForFile =
+ getASTUnitForFile(NameFileMap[FunctionName])) {
+
+ // Update the cache.
+ NameASTUnitMap[FunctionName] = *FoundForFile;
+ return *FoundForFile;
+
+ } else {
+ return FoundForFile.takeError();
+ }
+ } else {
+ // Found in the cache.
+ return ASTCacheEntry->second;
+ }
+}
+
+llvm::Expected<std::string>
+CrossTranslationUnitContext::ASTUnitStorage::getFileForFunction(
+ StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName) {
+ if (llvm::Error IndexLoadError = ensureCTUIndexLoaded(CrossTUDir, IndexName))
+ return std::move(IndexLoadError);
+ return NameFileMap[FunctionName];
+}
+
+llvm::Error CrossTranslationUnitContext::ASTUnitStorage::ensureCTUIndexLoaded(
+ StringRef CrossTUDir, StringRef IndexName) {
+ // Dont initialize if the map is filled.
+ if (!NameFileMap.empty())
+ return llvm::Error::success();
+
+ // Get the absolute path to the index file.
+ SmallString<256> IndexFile = CrossTUDir;
+ if (llvm::sys::path::is_absolute(IndexName))
+ IndexFile = IndexName;
+ else
+ llvm::sys::path::append(IndexFile, IndexName);
+
+ if (auto IndexMapping = parseCrossTUIndex(IndexFile, CrossTUDir)) {
+ // Initialize member map.
+ NameFileMap = *IndexMapping;
+ return llvm::Error::success();
+ } else {
+ // Error while parsing CrossTU index file.
+ return IndexMapping.takeError();
+ };
+}
+
llvm::Expected<ASTUnit *> CrossTranslationUnitContext::loadExternalAST(
StringRef LookupName, StringRef CrossTUDir, StringRef IndexName,
bool DisplayCTUProgress) {
@@ -348,64 +486,41 @@
// translation units contains decls with the same lookup name an
// error will be returned.
- if (NumASTLoaded >= CTULoadThreshold) {
+ using LoadPass = CrossTranslationUnitContext::LoadPass;
+
+ // RAII incrementing counter is used to count successful loads.
+ LoadPass LoadAttempt = CTULoadGuard.beginLoad();
+
+ // If import threshold is reached, don't import anything.
+ if (!LoadAttempt) {
++NumASTLoadThresholdReached;
return llvm::make_error<IndexError>(
index_error_code::load_threshold_reached);
}
- ASTUnit *Unit = nullptr;
- auto NameUnitCacheEntry = NameASTUnitMap.find(LookupName);
- if (NameUnitCacheEntry == NameASTUnitMap.end()) {
- if (NameFileMap.empty()) {
- SmallString<256> IndexFile = CrossTUDir;
- if (llvm::sys::path::is_absolute(IndexName))
- IndexFile = IndexName;
- else
- llvm::sys::path::append(IndexFile, IndexName);
- llvm::Expected<llvm::StringMap<std::string>> IndexOrErr =
- parseCrossTUIndex(IndexFile, CrossTUDir);
- if (IndexOrErr)
- NameFileMap = *IndexOrErr;
- else
- return IndexOrErr.takeError();
- }
+ // Try to get the value from the heavily cached storage.
+ llvm::Expected<ASTUnit *> Unit =
+ ASTStorage.getASTUnitForFunction(LookupName, CrossTUDir, IndexName);
- auto It = NameFileMap.find(LookupName);
- if (It == NameFileMap.end()) {
- ++NumNotInOtherTU;
- return llvm::make_error<IndexError>(index_error_code::missing_definition);
- }
- StringRef ASTFileName = It->second;
- auto ASTCacheEntry = FileASTUnitMap.find(ASTFileName);
- if (ASTCacheEntry == FileASTUnitMap.end()) {
- IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
- TextDiagnosticPrinter *DiagClient =
- new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
- IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
- new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
-
- std::unique_ptr<ASTUnit> LoadedUnit(ASTUnit::LoadFromASTFile(
- ASTFileName, CI.getPCHContainerOperations()->getRawReader(),
- ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts()));
- Unit = LoadedUnit.get();
- FileASTUnitMap[ASTFileName] = std::move(LoadedUnit);
- ++NumASTLoaded;
- if (DisplayCTUProgress) {
- llvm::errs() << "CTU loaded AST file: "
- << ASTFileName << "\n";
- }
- } else {
- Unit = ASTCacheEntry->second.get();
- }
- NameASTUnitMap[LookupName] = Unit;
- } else {
- Unit = NameUnitCacheEntry->second;
- }
if (!Unit)
+ return Unit.takeError();
+
+ // Check whether the backing pointer of the Expected is a nullptr.
+ if (!*Unit)
return llvm::make_error<IndexError>(
index_error_code::failed_to_get_external_ast);
+
+ // The backing pointer is not null, loading was successful.
+ LoadAttempt.wasSuccessful();
+
+ if (DisplayCTUProgress) {
+ if (llvm::Expected<std::string> FileName =
+ ASTStorage.getFileForFunction(LookupName, CrossTUDir, IndexName))
+ llvm::errs() << "CTU loaded AST file: " << *FileName << "\n";
+ else
+ return FileName.takeError();
+ }
+
return Unit;
}
Index: clang/include/clang/CrossTU/CrossTranslationUnit.h
===================================================================
--- clang/include/clang/CrossTU/CrossTranslationUnit.h
+++ clang/include/clang/CrossTU/CrossTranslationUnit.h
@@ -176,18 +176,91 @@
template <typename T>
llvm::Expected<const T *> importDefinitionImpl(const T *D);
- llvm::StringMap<std::unique_ptr<clang::ASTUnit>> FileASTUnitMap;
- llvm::StringMap<clang::ASTUnit *> NameASTUnitMap;
- llvm::StringMap<std::string> NameFileMap;
llvm::DenseMap<TranslationUnitDecl *, std::unique_ptr<ASTImporter>>
ASTUnitImporterMap;
CompilerInstance &CI;
ASTContext &Context;
std::shared_ptr<ASTImporterSharedState> ImporterSharedSt;
- /// \p CTULoadTreshold should serve as an upper limit to the number of TUs
- /// imported in order to reduce the memory footprint of CTU analysis.
- const unsigned CTULoadThreshold;
- unsigned NumASTLoaded{0u};
+
+ /// Cached access to ASTUnit mapping information is implemented in this
+ /// section.
+
+ /// Functor for loading ASTUnits from AST-dump files.
+ class ASTDumpLoader {
+ public:
+ ASTDumpLoader(const CompilerInstance &CI);
+ std::unique_ptr<ASTUnit> operator()(StringRef ASTFileName);
+
+ private:
+ const CompilerInstance &CI;
+ };
+
+ /// Storage for ASTUnits, cached access, and providing searchability are the
+ /// concerns of ASTUnitStorage class.
+ class ASTUnitStorage {
+ public:
+ ASTUnitStorage(const CompilerInstance &CI);
+ llvm::Expected<ASTUnit *> getASTUnitForFunction(StringRef FunctionName,
+ StringRef CrossTUDir,
+ StringRef IndexName);
+ llvm::Expected<std::string> getFileForFunction(StringRef FunctionName,
+ StringRef CrossTUDir,
+ StringRef IndexName);
+
+ private:
+ llvm::Error ensureCTUIndexLoaded(StringRef CrossTUDir, StringRef IndexName);
+ llvm::Expected<ASTUnit *> getASTUnitForFile(StringRef FileName);
+
+ template <typename... T> using BaseMapTy = llvm::StringMap<T...>;
+ using OwningMapTy = BaseMapTy<std::unique_ptr<clang::ASTUnit>>;
+ using NonOwningMapTy = BaseMapTy<clang::ASTUnit *>;
+
+ OwningMapTy FileASTUnitMap;
+ NonOwningMapTy NameASTUnitMap;
+
+ using IndexMapTy = BaseMapTy<std::string>;
+ IndexMapTy NameFileMap;
+
+ ASTDumpLoader FileAccessor;
+ };
+
+ ASTUnitStorage ASTStorage;
+
+ /// The thresholding of AST-loads is implemented in this section.
+
+ /// RAII counter to signal threshold reached condition, and to increment the
+ /// counter on successful load. Member `CanBegin` is used to signal, that the
+ /// import attempt should be made at the beginning. Member `WasSuccesful`
+ /// signifies whether the load is successfully finished. The counter is
+ /// incremented if the instance is destroyed while `WasSuccessful` is true.
+ class LoadPass {
+ public:
+ LoadPass(unsigned &NumASTLoaded, bool CanBegin);
+ ~LoadPass();
+ void wasSuccessful();
+ operator bool() const;
+
+ private:
+ unsigned &NumASTLoaded;
+ bool CanBegin;
+ bool WasSuccessful;
+ };
+
+ /// An ASTLoadGuard instance manages the threshold and AST loaded values, and
+ /// it is responsible for handing out LoadPass instances.
+ class LoadGuard {
+ public:
+ LoadGuard(unsigned CTULoadThreshold);
+ LoadPass beginLoad();
+
+ private:
+ /// \p CTULoadTreshold should serve as an upper limit to the number of TUs
+ /// imported in order to reduce the memory footprint of CTU analysis.
+ const unsigned CTULoadThreshold;
+ unsigned NumASTLoaded;
+ };
+
+ LoadGuard CTULoadGuard;
};
} // namespace cross_tu
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits