1 //===--- CrossTranslationUnit.h - -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides an interface to load binary AST dumps on demand. This
11 // feature can be utilized for tools that require cross translation unit
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
16 #define LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/Error.h"
25 class CompilerInstance;
32 class TranslationUnitDecl;
36 enum class index_error_code {
43 failed_to_get_external_ast,
44 failed_to_generate_usr
47 class IndexError : public llvm::ErrorInfo<IndexError> {
50 IndexError(index_error_code C) : Code(C), LineNo(0) {}
51 IndexError(index_error_code C, std::string FileName, int LineNo = 0)
52 : Code(C), FileName(std::move(FileName)), LineNo(LineNo) {}
53 void log(raw_ostream &OS) const override;
54 std::error_code convertToErrorCode() const override;
55 index_error_code getCode() const { return Code; }
56 int getLineNum() const { return LineNo; }
57 std::string getFileName() const { return FileName; }
60 index_error_code Code;
65 /// This function parses an index file that determines which
66 /// translation unit contains which definition.
68 /// The index file format is the following:
69 /// each line consists of an USR and a filepath separated by a space.
71 /// \return Returns a map where the USR is the key and the filepath is the value
73 llvm::Expected<llvm::StringMap<std::string>>
74 parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir);
76 std::string createCrossTUIndexString(const llvm::StringMap<std::string> &Index);
78 /// This class is used for tools that requires cross translation
81 /// This class can load function definitions from external AST files.
82 /// The loaded definition will be merged back to the original AST using the
84 /// In order to use this class, an index file is required that describes
85 /// the locations of the AST files for each function definition.
87 /// Note that this class also implements caching.
88 class CrossTranslationUnitContext {
90 CrossTranslationUnitContext(CompilerInstance &CI);
91 ~CrossTranslationUnitContext();
93 /// This function loads a function definition from an external AST
94 /// file and merge it into the original AST.
96 /// This method should only be used on functions that have no definitions in
97 /// the current translation unit. A function definition with the same
98 /// declaration will be looked up in the index file which should be in the
99 /// \p CrossTUDir directory, called \p IndexName. In case the declaration is
100 /// found in the index the corresponding AST file will be loaded and the
101 /// definition of the function will be merged into the original AST using
102 /// the AST Importer.
104 /// \return The declaration with the definition will be returned.
105 /// If no suitable definition is found in the index file or multiple
106 /// definitions found error will be returned.
108 /// Note that the AST files should also be in the \p CrossTUDir.
109 llvm::Expected<const FunctionDecl *>
110 getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir,
111 StringRef IndexName);
113 /// This function loads a function definition from an external AST
116 /// A function definition with the same declaration will be looked up in the
117 /// index file which should be in the \p CrossTUDir directory, called
118 /// \p IndexName. In case the declaration is found in the index the
119 /// corresponding AST file will be loaded.
121 /// \return Returns an ASTUnit that contains the definition of the looked up
124 /// Note that the AST files should also be in the \p CrossTUDir.
125 llvm::Expected<ASTUnit *> loadExternalAST(StringRef LookupName,
126 StringRef CrossTUDir,
127 StringRef IndexName);
129 /// This function merges a definition from a separate AST Unit into
130 /// the current one which was created by the compiler instance that
131 /// was passed to the constructor.
133 /// \return Returns the resulting definition or an error.
134 llvm::Expected<const FunctionDecl *> importDefinition(const FunctionDecl *FD);
136 /// Get a name to identify a function.
137 static std::string getLookupName(const NamedDecl *ND);
139 /// Emit diagnostics for the user for potential configuration errors.
140 void emitCrossTUDiagnostics(const IndexError &IE);
143 ASTImporter &getOrCreateASTImporter(ASTContext &From);
144 const FunctionDecl *findFunctionInDeclContext(const DeclContext *DC,
145 StringRef LookupFnName);
147 llvm::StringMap<std::unique_ptr<clang::ASTUnit>> FileASTUnitMap;
148 llvm::StringMap<clang::ASTUnit *> FunctionASTUnitMap;
149 llvm::StringMap<std::string> FunctionFileMap;
150 llvm::DenseMap<TranslationUnitDecl *, std::unique_ptr<ASTImporter>>
152 CompilerInstance &CI;
156 } // namespace cross_tu
159 #endif // LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H