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/AST/ASTImporterLookupTable.h"
19 #include "clang/Basic/LLVM.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"
26 class CompilerInstance;
33 class TranslationUnitDecl;
37 enum class index_error_code {
44 failed_to_get_external_ast,
45 failed_to_generate_usr,
50 class IndexError : public llvm::ErrorInfo<IndexError> {
53 IndexError(index_error_code C) : Code(C), LineNo(0) {}
54 IndexError(index_error_code C, std::string FileName, int LineNo = 0)
55 : Code(C), FileName(std::move(FileName)), LineNo(LineNo) {}
56 IndexError(index_error_code C, std::string FileName, std::string TripleToName,
57 std::string TripleFromName)
58 : Code(C), FileName(std::move(FileName)),
59 TripleToName(std::move(TripleToName)),
60 TripleFromName(std::move(TripleFromName)) {}
61 void log(raw_ostream &OS) const override;
62 std::error_code convertToErrorCode() const override;
63 index_error_code getCode() const { return Code; }
64 int getLineNum() const { return LineNo; }
65 std::string getFileName() const { return FileName; }
66 std::string getTripleToName() const { return TripleToName; }
67 std::string getTripleFromName() const { return TripleFromName; }
70 index_error_code Code;
73 std::string TripleToName;
74 std::string TripleFromName;
77 /// This function parses an index file that determines which
78 /// translation unit contains which definition.
80 /// The index file format is the following:
81 /// each line consists of an USR and a filepath separated by a space.
83 /// \return Returns a map where the USR is the key and the filepath is the value
85 llvm::Expected<llvm::StringMap<std::string>>
86 parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir);
88 std::string createCrossTUIndexString(const llvm::StringMap<std::string> &Index);
90 /// This class is used for tools that requires cross translation
93 /// This class can load definitions from external AST files.
94 /// The loaded definition will be merged back to the original AST using the
96 /// In order to use this class, an index file is required that describes
97 /// the locations of the AST files for each definition.
99 /// Note that this class also implements caching.
100 class CrossTranslationUnitContext {
102 CrossTranslationUnitContext(CompilerInstance &CI);
103 ~CrossTranslationUnitContext();
105 /// This function loads a function definition from an external AST
106 /// file and merge it into the original AST.
108 /// This method should only be used on functions that have no definitions in
109 /// the current translation unit. A function definition with the same
110 /// declaration will be looked up in the index file which should be in the
111 /// \p CrossTUDir directory, called \p IndexName. In case the declaration is
112 /// found in the index the corresponding AST file will be loaded and the
113 /// definition of the function will be merged into the original AST using
114 /// the AST Importer.
116 /// \return The declaration with the definition will be returned.
117 /// If no suitable definition is found in the index file or multiple
118 /// definitions found error will be returned.
120 /// Note that the AST files should also be in the \p CrossTUDir.
121 llvm::Expected<const FunctionDecl *>
122 getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir,
123 StringRef IndexName, bool DisplayCTUProgress = false);
125 /// This function loads a function definition from an external AST
128 /// A function definition with the same declaration will be looked up in the
129 /// index file which should be in the \p CrossTUDir directory, called
130 /// \p IndexName. In case the declaration is found in the index the
131 /// corresponding AST file will be loaded.
133 /// \return Returns a pointer to the ASTUnit that contains the definition of
134 /// the looked up function or an Error.
135 /// The returned pointer is never a nullptr.
137 /// Note that the AST files should also be in the \p CrossTUDir.
138 llvm::Expected<ASTUnit *> loadExternalAST(StringRef LookupName,
139 StringRef CrossTUDir,
141 bool DisplayCTUProgress = false);
143 /// This function merges a definition from a separate AST Unit into
144 /// the current one which was created by the compiler instance that
145 /// was passed to the constructor.
147 /// \return Returns the resulting definition or an error.
148 llvm::Expected<const FunctionDecl *> importDefinition(const FunctionDecl *FD);
150 /// Get a name to identify a function.
151 static std::string getLookupName(const NamedDecl *ND);
153 /// Emit diagnostics for the user for potential configuration errors.
154 void emitCrossTUDiagnostics(const IndexError &IE);
157 void lazyInitLookupTable(TranslationUnitDecl *ToTU);
158 ASTImporter &getOrCreateASTImporter(ASTContext &From);
159 const FunctionDecl *findFunctionInDeclContext(const DeclContext *DC,
160 StringRef LookupFnName);
162 llvm::StringMap<std::unique_ptr<clang::ASTUnit>> FileASTUnitMap;
163 llvm::StringMap<clang::ASTUnit *> FunctionASTUnitMap;
164 llvm::StringMap<std::string> FunctionFileMap;
165 llvm::DenseMap<TranslationUnitDecl *, std::unique_ptr<ASTImporter>>
167 CompilerInstance &CI;
169 std::unique_ptr<ASTImporterLookupTable> LookupTable;
172 } // namespace cross_tu
175 #endif // LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H