14#ifndef LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
15#define LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/Path.h"
66 : Code(
C), FileName(
std::move(FileName)), LineNo(LineNo) {}
68 std::string ConfigFromName)
69 : Code(
C), FileName(
std::move(FileName)),
70 ConfigToName(
std::move(ConfigToName)),
71 ConfigFromName(
std::move(ConfigFromName)) {}
72 void log(raw_ostream &OS)
const override;
84 std::string ConfigToName;
85 std::string ConfigFromName;
109 StringRef FileContent,
110 llvm::sys::path::Style PathStyle = llvm::sys::path::Style::posix,
111 StringRef FilePath =
"");
151 StringRef IndexName,
bool DisplayCTUProgress =
false);
154 StringRef IndexName,
bool DisplayCTUProgress =
false);
170 StringRef CrossTUDir,
172 bool DisplayCTUProgress =
false);
197 std::optional<clang::MacroExpansionContext>
212 template <
typename T>
214 StringRef CrossTUDir,
216 bool DisplayCTUProgress);
217 template <
typename T>
218 const T *findDefInDeclContext(
const DeclContext *DC,
219 StringRef LookupName);
220 template <
typename T>
223 using ImporterMapTy =
224 llvm::DenseMap<TranslationUnitDecl *, std::unique_ptr<ASTImporter>>;
226 ImporterMapTy ASTUnitImporterMap;
229 std::shared_ptr<ASTImporterSharedState> ImporterSharedSt;
237 StringRef InvocationListFilePath);
243 LoadResultTy load(StringRef Identifier);
247 llvm::Error lazyInitInvocationList();
252 const llvm::sys::path::Style PathStyle = llvm::sys::path::Style::posix;
255 LoadResultTy loadFromDump(StringRef Identifier);
257 LoadResultTy loadFromSource(StringRef Identifier);
264 StringRef InvocationListFilePath;
267 std::optional<InvocationListTy> InvocationList;
268 std::optional<IndexError> PreviousError;
274 ASTLoadGuard(
unsigned Limit) : Limit(Limit) {}
278 operator bool()
const {
return Count < Limit; }
281 void indicateLoadSuccess() { ++Count; }
287 const unsigned Limit;
292 class ASTUnitStorage {
307 StringRef CrossTUDir,
309 bool DisplayCTUProgress);
321 StringRef CrossTUDir,
322 StringRef IndexName);
325 llvm::Error ensureCTUIndexLoaded(StringRef CrossTUDir, StringRef IndexName);
327 bool DisplayCTUProgress);
329 template <
typename... T>
using BaseMapTy = llvm::StringMap<T...>;
330 using OwningMapTy = BaseMapTy<std::unique_ptr<clang::ASTUnit>>;
331 using NonOwningMapTy = BaseMapTy<clang::ASTUnit *>;
333 OwningMapTy FileASTUnitMap;
334 NonOwningMapTy NameASTUnitMap;
336 using IndexMapTy = BaseMapTy<std::string>;
337 IndexMapTy NameFileMap;
346 ASTLoadGuard LoadGuard;
349 ASTUnitStorage ASTStorage;
351 bool HasEmittedLoadThresholdRemark =
false;
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Utility class for loading a ASTContext from an AST file.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Decl - This represents one declaration (or definition), e.g.
Represents a function declaration or definition.
This represents a decl that may have a name.
Encodes a location in the source.
The top declaration context.
Represents a variable declaration or definition.
llvm::Expected< const FunctionDecl * > getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir, StringRef IndexName, bool DisplayCTUProgress=false)
This function loads a function or variable definition from an external AST file and merges it into th...
llvm::Expected< const FunctionDecl * > importDefinition(const FunctionDecl *FD, ASTUnit *Unit)
This function merges a definition from a separate AST Unit into the current one which was created by ...
CrossTranslationUnitContext(CompilerInstance &CI)
void emitCrossTUDiagnostics(const IndexError &IE, SourceLocation Loc)
Emit diagnostics for the user for potential configuration errors.
static std::optional< std::string > getLookupName(const Decl *D)
Get a name to identify a decl.
std::optional< clang::MacroExpansionContext > getMacroExpansionContextForSourceLocation(const clang::SourceLocation &ToLoc) const
Returns the MacroExpansionContext for the imported TU to which the given source-location corresponds.
bool hasError(const Decl *ToDecl) const
Returns true if the given Decl is mapped (or created) during an import but there was an unrecoverable...
bool isImportedAsNew(const Decl *ToDecl) const
Returns true if the given Decl is newly created during the import.
~CrossTranslationUnitContext()
llvm::Expected< ASTUnit * > loadExternalAST(StringRef LookupName, StringRef CrossTUDir, StringRef IndexName, bool DisplayCTUProgress=false)
This function loads a definition from an external AST file.
index_error_code getCode() const
IndexError(index_error_code C, std::string FileName, int LineNo=0)
std::error_code convertToErrorCode() const override
std::string getConfigFromName() const
std::string getConfigToName() const
IndexError(index_error_code C, std::string FileName, std::string ConfigToName, std::string ConfigFromName)
std::string getFileName() const
IndexError(index_error_code C)
bool shouldImport(const VarDecl *VD, const ASTContext &ACtx)
Returns true if it makes sense to import a foreign variable definition.
llvm::Expected< llvm::StringMap< std::string > > parseCrossTUIndex(StringRef IndexPath)
This function parses an index file that determines which translation unit contains which definition.
std::string createCrossTUIndexString(const llvm::StringMap< std::string > &Index)
@ invocation_list_wrong_format
@ invocation_list_file_not_found
@ invocation_list_lookup_unsuccessful
@ failed_to_get_external_ast
@ invocation_list_ambiguous
llvm::Expected< InvocationListTy > parseInvocationList(StringRef FileContent, llvm::sys::path::Style PathStyle=llvm::sys::path::Style::posix, StringRef FilePath="")
Parse the YAML formatted invocation list file content FileContent.
llvm::StringMap< llvm::SmallVector< std::string, 32 > > InvocationListTy
The JSON file list parser is used to communicate input to InstallAPI.