clang  8.0.0svn
CrossTranslationUnit.h
Go to the documentation of this file.
1 //===--- CrossTranslationUnit.h - -------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // support.
13 //
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
16 #define LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
17 
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"
23 
24 namespace clang {
25 class CompilerInstance;
26 class ASTContext;
27 class ASTImporter;
28 class ASTUnit;
29 class DeclContext;
30 class FunctionDecl;
31 class NamedDecl;
32 class TranslationUnitDecl;
33 
34 namespace cross_tu {
35 
36 enum class index_error_code {
37  unspecified = 1,
47 };
48 
49 class IndexError : public llvm::ErrorInfo<IndexError> {
50 public:
51  static char ID;
52  IndexError(index_error_code C) : Code(C), LineNo(0) {}
53  IndexError(index_error_code C, std::string FileName, int LineNo = 0)
54  : Code(C), FileName(std::move(FileName)), LineNo(LineNo) {}
55  IndexError(index_error_code C, std::string FileName, std::string TripleToName,
56  std::string TripleFromName)
57  : Code(C), FileName(std::move(FileName)),
58  TripleToName(std::move(TripleToName)),
59  TripleFromName(std::move(TripleFromName)) {}
60  void log(raw_ostream &OS) const override;
61  std::error_code convertToErrorCode() const override;
62  index_error_code getCode() const { return Code; }
63  int getLineNum() const { return LineNo; }
64  std::string getFileName() const { return FileName; }
65  std::string getTripleToName() const { return TripleToName; }
66  std::string getTripleFromName() const { return TripleFromName; }
67 
68 private:
69  index_error_code Code;
70  std::string FileName;
71  int LineNo;
72  std::string TripleToName;
73  std::string TripleFromName;
74 };
75 
76 /// This function parses an index file that determines which
77 /// translation unit contains which definition.
78 ///
79 /// The index file format is the following:
80 /// each line consists of an USR and a filepath separated by a space.
81 ///
82 /// \return Returns a map where the USR is the key and the filepath is the value
83 /// or an error.
85 parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir);
86 
87 std::string createCrossTUIndexString(const llvm::StringMap<std::string> &Index);
88 
89 /// This class is used for tools that requires cross translation
90 /// unit capability.
91 ///
92 /// This class can load function definitions from external AST files.
93 /// The loaded definition will be merged back to the original AST using the
94 /// AST Importer.
95 /// In order to use this class, an index file is required that describes
96 /// the locations of the AST files for each function definition.
97 ///
98 /// Note that this class also implements caching.
100 public:
103 
104  /// This function loads a function definition from an external AST
105  /// file and merge it into the original AST.
106  ///
107  /// This method should only be used on functions that have no definitions in
108  /// the current translation unit. A function definition with the same
109  /// declaration will be looked up in the index file which should be in the
110  /// \p CrossTUDir directory, called \p IndexName. In case the declaration is
111  /// found in the index the corresponding AST file will be loaded and the
112  /// definition of the function will be merged into the original AST using
113  /// the AST Importer.
114  ///
115  /// \return The declaration with the definition will be returned.
116  /// If no suitable definition is found in the index file or multiple
117  /// definitions found error will be returned.
118  ///
119  /// Note that the AST files should also be in the \p CrossTUDir.
121  getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir,
122  StringRef IndexName, bool DisplayCTUProgress = false);
123 
124  /// This function loads a function definition from an external AST
125  /// file.
126  ///
127  /// A function definition with the same declaration will be looked up in the
128  /// index file which should be in the \p CrossTUDir directory, called
129  /// \p IndexName. In case the declaration is found in the index the
130  /// corresponding AST file will be loaded.
131  ///
132  /// \return Returns an ASTUnit that contains the definition of the looked up
133  /// function.
134  ///
135  /// Note that the AST files should also be in the \p CrossTUDir.
136  llvm::Expected<ASTUnit *> loadExternalAST(StringRef LookupName,
137  StringRef CrossTUDir,
138  StringRef IndexName,
139  bool DisplayCTUProgress = false);
140 
141  /// This function merges a definition from a separate AST Unit into
142  /// the current one which was created by the compiler instance that
143  /// was passed to the constructor.
144  ///
145  /// \return Returns the resulting definition or an error.
146  llvm::Expected<const FunctionDecl *> importDefinition(const FunctionDecl *FD);
147 
148  /// Get a name to identify a function.
149  static std::string getLookupName(const NamedDecl *ND);
150 
151  /// Emit diagnostics for the user for potential configuration errors.
152  void emitCrossTUDiagnostics(const IndexError &IE);
153 
154 private:
155  ASTImporter &getOrCreateASTImporter(ASTContext &From);
156  const FunctionDecl *findFunctionInDeclContext(const DeclContext *DC,
157  StringRef LookupFnName);
158 
159  llvm::StringMap<std::unique_ptr<clang::ASTUnit>> FileASTUnitMap;
160  llvm::StringMap<clang::ASTUnit *> FunctionASTUnitMap;
161  llvm::StringMap<std::string> FunctionFileMap;
162  llvm::DenseMap<TranslationUnitDecl *, std::unique_ptr<ASTImporter>>
163  ASTUnitImporterMap;
164  CompilerInstance &CI;
165  ASTContext &Context;
166 };
167 
168 } // namespace cross_tu
169 } // namespace clang
170 
171 #endif // LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
Represents a function declaration or definition.
Definition: Decl.h:1739
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Definition: Format.h:2072
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
std::string createCrossTUIndexString(const llvm::StringMap< std::string > &Index)
llvm::Expected< llvm::StringMap< std::string > > parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir)
This function parses an index file that determines which translation unit contains which definition...
#define log(__x)
Definition: tgmath.h:476
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
IndexError(index_error_code C, std::string FileName, std::string TripleToName, std::string TripleFromName)
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
This class is used for tools that requires cross translation unit capability.
index_error_code getCode() const
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:82
IndexError(index_error_code C, std::string FileName, int LineNo=0)
This represents a decl that may have a name.
Definition: Decl.h:248
std::string getTripleToName() const
std::string getTripleFromName() const