1 //===--- CrossTranslationUnit.h - -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides an interface to load binary AST dumps on demand. This
10 // feature can be utilized for tools that require cross translation unit
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
15 #define LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
17 #include "clang/AST/ASTImporterSharedState.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;
33 class TranslationUnitDecl;
37 enum class index_error_code {
44 failed_to_get_external_ast,
45 failed_to_generate_usr,
48 lang_dialect_mismatch,
49 load_threshold_reached
52 class IndexError : public llvm::ErrorInfo<IndexError> {
55 IndexError(index_error_code C) : Code(C), LineNo(0) {}
56 IndexError(index_error_code C, std::string FileName, int LineNo = 0)
57 : Code(C), FileName(std::move(FileName)), LineNo(LineNo) {}
58 IndexError(index_error_code C, std::string FileName, std::string TripleToName,
59 std::string TripleFromName)
60 : Code(C), FileName(std::move(FileName)),
61 TripleToName(std::move(TripleToName)),
62 TripleFromName(std::move(TripleFromName)) {}
63 void log(raw_ostream &OS) const override;
64 std::error_code convertToErrorCode() const override;
65 index_error_code getCode() const { return Code; }
66 int getLineNum() const { return LineNo; }
67 std::string getFileName() const { return FileName; }
68 std::string getTripleToName() const { return TripleToName; }
69 std::string getTripleFromName() const { return TripleFromName; }
72 index_error_code Code;
75 std::string TripleToName;
76 std::string TripleFromName;
79 /// This function parses an index file that determines which
80 /// translation unit contains which definition.
82 /// The index file format is the following:
83 /// each line consists of an USR and a filepath separated by a space.
85 /// \return Returns a map where the USR is the key and the filepath is the value
87 llvm::Expected<llvm::StringMap<std::string>>
88 parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir);
90 std::string createCrossTUIndexString(const llvm::StringMap<std::string> &Index);
92 // Returns true if the variable or any field of a record variable is const.
93 bool containsConst(const VarDecl *VD, const ASTContext &ACtx);
95 /// This class is used for tools that requires cross translation
98 /// This class can load definitions from external AST files.
99 /// The loaded definition will be merged back to the original AST using the
101 /// In order to use this class, an index file is required that describes
102 /// the locations of the AST files for each definition.
104 /// Note that this class also implements caching.
105 class CrossTranslationUnitContext {
107 CrossTranslationUnitContext(CompilerInstance &CI);
108 ~CrossTranslationUnitContext();
110 /// This function loads a function or variable definition from an
111 /// external AST file and merges it into the original AST.
113 /// This method should only be used on functions that have no definitions or
114 /// variables that have no initializer in
115 /// the current translation unit. A function definition with the same
116 /// declaration will be looked up in the index file which should be in the
117 /// \p CrossTUDir directory, called \p IndexName. In case the declaration is
118 /// found in the index the corresponding AST file will be loaded and the
119 /// definition will be merged into the original AST using the AST Importer.
121 /// \return The declaration with the definition will be returned.
122 /// If no suitable definition is found in the index file or multiple
123 /// definitions found error will be returned.
125 /// Note that the AST files should also be in the \p CrossTUDir.
126 llvm::Expected<const FunctionDecl *>
127 getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir,
128 StringRef IndexName, bool DisplayCTUProgress = false);
129 llvm::Expected<const VarDecl *>
130 getCrossTUDefinition(const VarDecl *VD, StringRef CrossTUDir,
131 StringRef IndexName, bool DisplayCTUProgress = false);
133 /// This function loads a definition from an external AST file.
135 /// A definition with the same declaration will be looked up in the
136 /// index file which should be in the \p CrossTUDir directory, called
137 /// \p IndexName. In case the declaration is found in the index the
138 /// corresponding AST file will be loaded. If the number of TUs imported
139 /// reaches \p CTULoadTreshold, no loading is performed.
141 /// \return Returns a pointer to the ASTUnit that contains the definition of
142 /// the looked up name or an Error.
143 /// The returned pointer is never a nullptr.
145 /// Note that the AST files should also be in the \p CrossTUDir.
146 llvm::Expected<ASTUnit *> loadExternalAST(StringRef LookupName,
147 StringRef CrossTUDir,
149 bool DisplayCTUProgress = false);
151 /// This function merges a definition from a separate AST Unit into
152 /// the current one which was created by the compiler instance that
153 /// was passed to the constructor.
155 /// \return Returns the resulting definition or an error.
156 llvm::Expected<const FunctionDecl *> importDefinition(const FunctionDecl *FD);
157 llvm::Expected<const VarDecl *> importDefinition(const VarDecl *VD);
159 /// Get a name to identify a named decl.
160 static std::string getLookupName(const NamedDecl *ND);
162 /// Emit diagnostics for the user for potential configuration errors.
163 void emitCrossTUDiagnostics(const IndexError &IE);
166 void lazyInitImporterSharedSt(TranslationUnitDecl *ToTU);
167 ASTImporter &getOrCreateASTImporter(ASTContext &From);
168 template <typename T>
169 llvm::Expected<const T *> getCrossTUDefinitionImpl(const T *D,
170 StringRef CrossTUDir,
172 bool DisplayCTUProgress);
173 template <typename T>
174 const T *findDefInDeclContext(const DeclContext *DC,
175 StringRef LookupName);
176 template <typename T>
177 llvm::Expected<const T *> importDefinitionImpl(const T *D);
179 llvm::StringMap<std::unique_ptr<clang::ASTUnit>> FileASTUnitMap;
180 llvm::StringMap<clang::ASTUnit *> NameASTUnitMap;
181 llvm::StringMap<std::string> NameFileMap;
182 llvm::DenseMap<TranslationUnitDecl *, std::unique_ptr<ASTImporter>>
184 CompilerInstance &CI;
186 std::shared_ptr<ASTImporterSharedState> ImporterSharedSt;
187 /// \p CTULoadTreshold should serve as an upper limit to the number of TUs
188 /// imported in order to reduce the memory footprint of CTU analysis.
189 const unsigned CTULoadThreshold;
190 unsigned NumASTLoaded{0u};
193 } // namespace cross_tu
196 #endif // LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H