CrossTranslationUnit.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- CrossTranslationUnit.h - -------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file provides an interface to load binary AST dumps on demand. This
  15. // feature can be utilized for tools that require cross translation unit
  16. // support.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
  20. #define LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
  21. #include "clang/AST/ASTImporterSharedState.h"
  22. #include "clang/Analysis/MacroExpansionContext.h"
  23. #include "clang/Basic/LLVM.h"
  24. #include "llvm/ADT/DenseMap.h"
  25. #include "llvm/ADT/Optional.h"
  26. #include "llvm/ADT/SmallPtrSet.h"
  27. #include "llvm/ADT/StringMap.h"
  28. #include "llvm/Support/Error.h"
  29. #include "llvm/Support/Path.h"
  30. namespace clang {
  31. class CompilerInstance;
  32. class ASTContext;
  33. class ASTImporter;
  34. class ASTUnit;
  35. class DeclContext;
  36. class FunctionDecl;
  37. class VarDecl;
  38. class NamedDecl;
  39. class TranslationUnitDecl;
  40. namespace cross_tu {
  41. enum class index_error_code {
  42. success = 0,
  43. unspecified = 1,
  44. missing_index_file,
  45. invalid_index_format,
  46. multiple_definitions,
  47. missing_definition,
  48. failed_import,
  49. failed_to_get_external_ast,
  50. failed_to_generate_usr,
  51. triple_mismatch,
  52. lang_mismatch,
  53. lang_dialect_mismatch,
  54. load_threshold_reached,
  55. invocation_list_ambiguous,
  56. invocation_list_file_not_found,
  57. invocation_list_empty,
  58. invocation_list_wrong_format,
  59. invocation_list_lookup_unsuccessful
  60. };
  61. class IndexError : public llvm::ErrorInfo<IndexError> {
  62. public:
  63. static char ID;
  64. IndexError(index_error_code C) : Code(C), LineNo(0) {}
  65. IndexError(index_error_code C, std::string FileName, int LineNo = 0)
  66. : Code(C), FileName(std::move(FileName)), LineNo(LineNo) {}
  67. IndexError(index_error_code C, std::string FileName, std::string TripleToName,
  68. std::string TripleFromName)
  69. : Code(C), FileName(std::move(FileName)),
  70. TripleToName(std::move(TripleToName)),
  71. TripleFromName(std::move(TripleFromName)) {}
  72. void log(raw_ostream &OS) const override;
  73. std::error_code convertToErrorCode() const override;
  74. index_error_code getCode() const { return Code; }
  75. int getLineNum() const { return LineNo; }
  76. std::string getFileName() const { return FileName; }
  77. std::string getTripleToName() const { return TripleToName; }
  78. std::string getTripleFromName() const { return TripleFromName; }
  79. private:
  80. index_error_code Code;
  81. std::string FileName;
  82. int LineNo;
  83. std::string TripleToName;
  84. std::string TripleFromName;
  85. };
  86. /// This function parses an index file that determines which
  87. /// translation unit contains which definition. The IndexPath is not prefixed
  88. /// with CTUDir, so an absolute path is expected for consistent results.
  89. ///
  90. /// The index file format is the following:
  91. /// each line consists of an USR and a filepath separated by a space.
  92. ///
  93. /// \return Returns a map where the USR is the key and the filepath is the value
  94. /// or an error.
  95. llvm::Expected<llvm::StringMap<std::string>>
  96. parseCrossTUIndex(StringRef IndexPath);
  97. std::string createCrossTUIndexString(const llvm::StringMap<std::string> &Index);
  98. using InvocationListTy = llvm::StringMap<llvm::SmallVector<std::string, 32>>;
  99. /// Parse the YAML formatted invocation list file content \p FileContent.
  100. /// The format is expected to be a mapping from from absolute source file
  101. /// paths in the filesystem to a list of command-line parts, which
  102. /// constitute the invocation needed to compile that file. That invocation
  103. /// will be used to produce the AST of the TU.
  104. llvm::Expected<InvocationListTy> parseInvocationList(
  105. StringRef FileContent,
  106. llvm::sys::path::Style PathStyle = llvm::sys::path::Style::posix);
  107. // Returns true if the variable or any field of a record variable is const.
  108. bool containsConst(const VarDecl *VD, const ASTContext &ACtx);
  109. /// This class is used for tools that requires cross translation
  110. /// unit capability.
  111. ///
  112. /// This class can load definitions from external AST sources.
  113. /// The loaded definition will be merged back to the original AST using the
  114. /// AST Importer.
  115. /// In order to use this class, an index file is required that describes
  116. /// the locations of the AST files for each definition.
  117. ///
  118. /// Note that this class also implements caching.
  119. class CrossTranslationUnitContext {
  120. public:
  121. CrossTranslationUnitContext(CompilerInstance &CI);
  122. ~CrossTranslationUnitContext();
  123. /// This function loads a function or variable definition from an
  124. /// external AST file and merges it into the original AST.
  125. ///
  126. /// This method should only be used on functions that have no definitions or
  127. /// variables that have no initializer in
  128. /// the current translation unit. A function definition with the same
  129. /// declaration will be looked up in the index file which should be in the
  130. /// \p CrossTUDir directory, called \p IndexName. In case the declaration is
  131. /// found in the index the corresponding AST will be loaded and the
  132. /// definition will be merged into the original AST using the AST Importer.
  133. ///
  134. /// \return The declaration with the definition will be returned.
  135. /// If no suitable definition is found in the index file or multiple
  136. /// definitions found error will be returned.
  137. ///
  138. /// Note that the AST files should also be in the \p CrossTUDir.
  139. llvm::Expected<const FunctionDecl *>
  140. getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir,
  141. StringRef IndexName, bool DisplayCTUProgress = false);
  142. llvm::Expected<const VarDecl *>
  143. getCrossTUDefinition(const VarDecl *VD, StringRef CrossTUDir,
  144. StringRef IndexName, bool DisplayCTUProgress = false);
  145. /// This function loads a definition from an external AST file.
  146. ///
  147. /// A definition with the same declaration will be looked up in the
  148. /// index file which should be in the \p CrossTUDir directory, called
  149. /// \p IndexName. In case the declaration is found in the index the
  150. /// corresponding AST will be loaded. If the number of TUs imported
  151. /// reaches \p CTULoadTreshold, no loading is performed.
  152. ///
  153. /// \return Returns a pointer to the ASTUnit that contains the definition of
  154. /// the looked up name or an Error.
  155. /// The returned pointer is never a nullptr.
  156. ///
  157. /// Note that the AST files should also be in the \p CrossTUDir.
  158. llvm::Expected<ASTUnit *> loadExternalAST(StringRef LookupName,
  159. StringRef CrossTUDir,
  160. StringRef IndexName,
  161. bool DisplayCTUProgress = false);
  162. /// This function merges a definition from a separate AST Unit into
  163. /// the current one which was created by the compiler instance that
  164. /// was passed to the constructor.
  165. ///
  166. /// \return Returns the resulting definition or an error.
  167. llvm::Expected<const FunctionDecl *> importDefinition(const FunctionDecl *FD,
  168. ASTUnit *Unit);
  169. llvm::Expected<const VarDecl *> importDefinition(const VarDecl *VD,
  170. ASTUnit *Unit);
  171. /// Get a name to identify a named decl.
  172. static llvm::Optional<std::string> getLookupName(const NamedDecl *ND);
  173. /// Emit diagnostics for the user for potential configuration errors.
  174. void emitCrossTUDiagnostics(const IndexError &IE);
  175. /// Returns the MacroExpansionContext for the imported TU to which the given
  176. /// source-location corresponds.
  177. /// \p ToLoc Source location in the imported-to AST.
  178. /// \note If any error happens such as \p ToLoc is a non-imported
  179. /// source-location, empty is returned.
  180. /// \note Macro expansion tracking for imported TUs is not implemented yet.
  181. /// It returns empty unconditionally.
  182. llvm::Optional<clang::MacroExpansionContext>
  183. getMacroExpansionContextForSourceLocation(
  184. const clang::SourceLocation &ToLoc) const;
  185. private:
  186. void lazyInitImporterSharedSt(TranslationUnitDecl *ToTU);
  187. ASTImporter &getOrCreateASTImporter(ASTUnit *Unit);
  188. template <typename T>
  189. llvm::Expected<const T *> getCrossTUDefinitionImpl(const T *D,
  190. StringRef CrossTUDir,
  191. StringRef IndexName,
  192. bool DisplayCTUProgress);
  193. template <typename T>
  194. const T *findDefInDeclContext(const DeclContext *DC,
  195. StringRef LookupName);
  196. template <typename T>
  197. llvm::Expected<const T *> importDefinitionImpl(const T *D, ASTUnit *Unit);
  198. using ImporterMapTy =
  199. llvm::DenseMap<TranslationUnitDecl *, std::unique_ptr<ASTImporter>>;
  200. ImporterMapTy ASTUnitImporterMap;
  201. ASTContext &Context;
  202. std::shared_ptr<ASTImporterSharedState> ImporterSharedSt;
  203. using LoadResultTy = llvm::Expected<std::unique_ptr<ASTUnit>>;
  204. /// Loads ASTUnits from AST-dumps or source-files.
  205. class ASTLoader {
  206. public:
  207. ASTLoader(CompilerInstance &CI, StringRef CTUDir,
  208. StringRef InvocationListFilePath);
  209. /// Load the ASTUnit by its identifier found in the index file. If the
  210. /// indentifier is suffixed with '.ast' it is considered a dump. Otherwise
  211. /// it is treated as source-file, and on-demand parsed. Relative paths are
  212. /// prefixed with CTUDir.
  213. LoadResultTy load(StringRef Identifier);
  214. /// Lazily initialize the invocation list information, which is needed for
  215. /// on-demand parsing.
  216. llvm::Error lazyInitInvocationList();
  217. private:
  218. /// The style used for storage and lookup of filesystem paths.
  219. /// Defaults to posix.
  220. const llvm::sys::path::Style PathStyle = llvm::sys::path::Style::posix;
  221. /// Loads an AST from a pch-dump.
  222. LoadResultTy loadFromDump(StringRef Identifier);
  223. /// Loads an AST from a source-file.
  224. LoadResultTy loadFromSource(StringRef Identifier);
  225. CompilerInstance &CI;
  226. StringRef CTUDir;
  227. /// The path to the file containing the invocation list, which is in YAML
  228. /// format, and contains a mapping from source files to compiler invocations
  229. /// that produce the AST used for analysis.
  230. StringRef InvocationListFilePath;
  231. /// In case of on-demand parsing, the invocations for parsing the source
  232. /// files is stored.
  233. llvm::Optional<InvocationListTy> InvocationList;
  234. index_error_code PreviousParsingResult = index_error_code::success;
  235. };
  236. /// Maintain number of AST loads and check for reaching the load limit.
  237. class ASTLoadGuard {
  238. public:
  239. ASTLoadGuard(unsigned Limit) : Limit(Limit) {}
  240. /// Indicates, whether a new load operation is permitted, it is within the
  241. /// threshold.
  242. operator bool() const { return Count < Limit; }
  243. /// Tell that a new AST was loaded successfully.
  244. void indicateLoadSuccess() { ++Count; }
  245. private:
  246. /// The number of ASTs actually imported.
  247. unsigned Count{0u};
  248. /// The limit (threshold) value for number of loaded ASTs.
  249. const unsigned Limit;
  250. };
  251. /// Storage and load of ASTUnits, cached access, and providing searchability
  252. /// are the concerns of ASTUnitStorage class.
  253. class ASTUnitStorage {
  254. public:
  255. ASTUnitStorage(CompilerInstance &CI);
  256. /// Loads an ASTUnit for a function.
  257. ///
  258. /// \param FunctionName USR name of the function.
  259. /// \param CrossTUDir Path to the directory used to store CTU related files.
  260. /// \param IndexName Name of the file inside \p CrossTUDir which maps
  261. /// function USR names to file paths. These files contain the corresponding
  262. /// AST-dumps.
  263. /// \param DisplayCTUProgress Display a message about loading new ASTs.
  264. ///
  265. /// \return An Expected instance which contains the ASTUnit pointer or the
  266. /// error occured during the load.
  267. llvm::Expected<ASTUnit *> getASTUnitForFunction(StringRef FunctionName,
  268. StringRef CrossTUDir,
  269. StringRef IndexName,
  270. bool DisplayCTUProgress);
  271. /// Identifies the path of the file which can be used to load the ASTUnit
  272. /// for a given function.
  273. ///
  274. /// \param FunctionName USR name of the function.
  275. /// \param CrossTUDir Path to the directory used to store CTU related files.
  276. /// \param IndexName Name of the file inside \p CrossTUDir which maps
  277. /// function USR names to file paths. These files contain the corresponding
  278. /// AST-dumps.
  279. ///
  280. /// \return An Expected instance containing the filepath.
  281. llvm::Expected<std::string> getFileForFunction(StringRef FunctionName,
  282. StringRef CrossTUDir,
  283. StringRef IndexName);
  284. private:
  285. llvm::Error ensureCTUIndexLoaded(StringRef CrossTUDir, StringRef IndexName);
  286. llvm::Expected<ASTUnit *> getASTUnitForFile(StringRef FileName,
  287. bool DisplayCTUProgress);
  288. template <typename... T> using BaseMapTy = llvm::StringMap<T...>;
  289. using OwningMapTy = BaseMapTy<std::unique_ptr<clang::ASTUnit>>;
  290. using NonOwningMapTy = BaseMapTy<clang::ASTUnit *>;
  291. OwningMapTy FileASTUnitMap;
  292. NonOwningMapTy NameASTUnitMap;
  293. using IndexMapTy = BaseMapTy<std::string>;
  294. IndexMapTy NameFileMap;
  295. /// Loads the AST based on the identifier found in the index.
  296. ASTLoader Loader;
  297. /// Limit the number of loaded ASTs. It is used to limit the memory usage
  298. /// of the CrossTranslationUnitContext. The ASTUnitStorage has the
  299. /// information whether the AST to load is actually loaded or returned from
  300. /// cache. This information is needed to maintain the counter.
  301. ASTLoadGuard LoadGuard;
  302. };
  303. ASTUnitStorage ASTStorage;
  304. };
  305. } // namespace cross_tu
  306. } // namespace clang
  307. #endif // LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
  308. #ifdef __GNUC__
  309. #pragma GCC diagnostic pop
  310. #endif