clang  9.0.0svn
CrossTranslationUnit.cpp
Go to the documentation of this file.
1 //===--- CrossTranslationUnit.cpp - -----------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the CrossTranslationUnit interface.
10 //
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTImporter.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Frontend/ASTUnit.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <fstream>
28 #include <sstream>
29 
30 namespace clang {
31 namespace cross_tu {
32 
33 namespace {
34 
35 #define DEBUG_TYPE "CrossTranslationUnit"
36 STATISTIC(NumGetCTUCalled, "The # of getCTUDefinition function called");
37 STATISTIC(
38  NumNotInOtherTU,
39  "The # of getCTUDefinition called but the function is not in any other TU");
40 STATISTIC(NumGetCTUSuccess,
41  "The # of getCTUDefinition successfully returned the "
42  "requested function's body");
43 STATISTIC(NumUnsupportedNodeFound, "The # of imports when the ASTImporter "
44  "encountered an unsupported AST Node");
45 STATISTIC(NumNameConflicts, "The # of imports when the ASTImporter "
46  "encountered an ODR error");
47 STATISTIC(NumTripleMismatch, "The # of triple mismatches");
48 STATISTIC(NumLangMismatch, "The # of language mismatches");
49 STATISTIC(NumLangDialectMismatch, "The # of language dialect mismatches");
50 STATISTIC(NumASTLoadThresholdReached,
51  "The # of ASTs not loaded because of threshold");
52 
53 // Same as Triple's equality operator, but we check a field only if that is
54 // known in both instances.
55 bool hasEqualKnownFields(const llvm::Triple &Lhs, const llvm::Triple &Rhs) {
56  using llvm::Triple;
57  if (Lhs.getArch() != Triple::UnknownArch &&
58  Rhs.getArch() != Triple::UnknownArch && Lhs.getArch() != Rhs.getArch())
59  return false;
60  if (Lhs.getSubArch() != Triple::NoSubArch &&
61  Rhs.getSubArch() != Triple::NoSubArch &&
62  Lhs.getSubArch() != Rhs.getSubArch())
63  return false;
64  if (Lhs.getVendor() != Triple::UnknownVendor &&
65  Rhs.getVendor() != Triple::UnknownVendor &&
66  Lhs.getVendor() != Rhs.getVendor())
67  return false;
68  if (!Lhs.isOSUnknown() && !Rhs.isOSUnknown() &&
69  Lhs.getOS() != Rhs.getOS())
70  return false;
71  if (Lhs.getEnvironment() != Triple::UnknownEnvironment &&
72  Rhs.getEnvironment() != Triple::UnknownEnvironment &&
73  Lhs.getEnvironment() != Rhs.getEnvironment())
74  return false;
75  if (Lhs.getObjectFormat() != Triple::UnknownObjectFormat &&
76  Rhs.getObjectFormat() != Triple::UnknownObjectFormat &&
77  Lhs.getObjectFormat() != Rhs.getObjectFormat())
78  return false;
79  return true;
80 }
81 
82 // FIXME: This class is will be removed after the transition to llvm::Error.
83 class IndexErrorCategory : public std::error_category {
84 public:
85  const char *name() const noexcept override { return "clang.index"; }
86 
87  std::string message(int Condition) const override {
88  switch (static_cast<index_error_code>(Condition)) {
90  return "An unknown error has occurred.";
92  return "The index file is missing.";
94  return "Invalid index file format.";
96  return "Multiple definitions in the index file.";
98  return "Missing definition from the index file.";
100  return "Failed to import the definition.";
102  return "Failed to load external AST source.";
104  return "Failed to generate USR.";
106  return "Triple mismatch";
108  return "Language mismatch";
110  return "Language dialect mismatch";
112  return "Load threshold reached";
113  }
114  llvm_unreachable("Unrecognized index_error_code.");
115  }
116 };
117 
118 static llvm::ManagedStatic<IndexErrorCategory> Category;
119 } // end anonymous namespace
120 
121 char IndexError::ID;
122 
123 void IndexError::log(raw_ostream &OS) const {
124  OS << Category->message(static_cast<int>(Code)) << '\n';
125 }
126 
127 std::error_code IndexError::convertToErrorCode() const {
128  return std::error_code(static_cast<int>(Code), *Category);
129 }
130 
132 parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir) {
133  std::ifstream ExternalMapFile(IndexPath);
134  if (!ExternalMapFile)
135  return llvm::make_error<IndexError>(index_error_code::missing_index_file,
136  IndexPath.str());
137 
138  llvm::StringMap<std::string> Result;
139  std::string Line;
140  unsigned LineNo = 1;
141  while (std::getline(ExternalMapFile, Line)) {
142  const size_t Pos = Line.find(" ");
143  if (Pos > 0 && Pos != std::string::npos) {
144  StringRef LineRef{Line};
145  StringRef LookupName = LineRef.substr(0, Pos);
146  if (Result.count(LookupName))
147  return llvm::make_error<IndexError>(
148  index_error_code::multiple_definitions, IndexPath.str(), LineNo);
149  StringRef FileName = LineRef.substr(Pos + 1);
150  SmallString<256> FilePath = CrossTUDir;
151  llvm::sys::path::append(FilePath, FileName);
152  Result[LookupName] = FilePath.str().str();
153  } else
154  return llvm::make_error<IndexError>(
155  index_error_code::invalid_index_format, IndexPath.str(), LineNo);
156  LineNo++;
157  }
158  return Result;
159 }
160 
161 std::string
162 createCrossTUIndexString(const llvm::StringMap<std::string> &Index) {
163  std::ostringstream Result;
164  for (const auto &E : Index)
165  Result << E.getKey().str() << " " << E.getValue() << '\n';
166  return Result.str();
167 }
168 
169 bool containsConst(const VarDecl *VD, const ASTContext &ACtx) {
170  CanQualType CT = ACtx.getCanonicalType(VD->getType());
171  if (!CT.isConstQualified()) {
172  const RecordType *RTy = CT->getAs<RecordType>();
173  if (!RTy || !RTy->hasConstFields())
174  return false;
175  }
176  return true;
177 }
178 
179 static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD) {
180  return D->hasBody(DefD);
181 }
182 static bool hasBodyOrInit(const VarDecl *D, const VarDecl *&DefD) {
183  return D->getAnyInitializer(DefD);
184 }
185 template <typename T> static bool hasBodyOrInit(const T *D) {
186  const T *Unused;
187  return hasBodyOrInit(D, Unused);
188 }
189 
191  : CI(CI), Context(CI.getASTContext()),
192  CTULoadThreshold(CI.getAnalyzerOpts()->CTUImportThreshold) {}
193 
195 
197  SmallString<128> DeclUSR;
198  bool Ret = index::generateUSRForDecl(ND, DeclUSR);
199  (void)Ret;
200  assert(!Ret && "Unable to generate USR");
201  return DeclUSR.str();
202 }
203 
204 /// Recursively visits the decls of a DeclContext, and returns one with the
205 /// given USR.
206 template <typename T>
207 const T *
208 CrossTranslationUnitContext::findDefInDeclContext(const DeclContext *DC,
209  StringRef LookupName) {
210  assert(DC && "Declaration Context must not be null");
211  for (const Decl *D : DC->decls()) {
212  const auto *SubDC = dyn_cast<DeclContext>(D);
213  if (SubDC)
214  if (const auto *ND = findDefInDeclContext<T>(SubDC, LookupName))
215  return ND;
216 
217  const auto *ND = dyn_cast<T>(D);
218  const T *ResultDecl;
219  if (!ND || !hasBodyOrInit(ND, ResultDecl))
220  continue;
221  if (getLookupName(ResultDecl) != LookupName)
222  continue;
223  return ResultDecl;
224  }
225  return nullptr;
226 }
227 
228 template <typename T>
229 llvm::Expected<const T *> CrossTranslationUnitContext::getCrossTUDefinitionImpl(
230  const T *D, StringRef CrossTUDir, StringRef IndexName,
231  bool DisplayCTUProgress) {
232  assert(D && "D is missing, bad call to this function!");
233  assert(!hasBodyOrInit(D) &&
234  "D has a body or init in current translation unit!");
235  ++NumGetCTUCalled;
236  const std::string LookupName = getLookupName(D);
237  if (LookupName.empty())
238  return llvm::make_error<IndexError>(
241  LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
242  if (!ASTUnitOrError)
243  return ASTUnitOrError.takeError();
244  ASTUnit *Unit = *ASTUnitOrError;
245  assert(&Unit->getFileManager() ==
247 
248  const llvm::Triple &TripleTo = Context.getTargetInfo().getTriple();
249  const llvm::Triple &TripleFrom =
251  // The imported AST had been generated for a different target.
252  // Some parts of the triple in the loaded ASTContext can be unknown while the
253  // very same parts in the target ASTContext are known. Thus we check for the
254  // known parts only.
255  if (!hasEqualKnownFields(TripleTo, TripleFrom)) {
256  // TODO: Pass the SourceLocation of the CallExpression for more precise
257  // diagnostics.
258  ++NumTripleMismatch;
259  return llvm::make_error<IndexError>(index_error_code::triple_mismatch,
260  Unit->getMainFileName(), TripleTo.str(),
261  TripleFrom.str());
262  }
263 
264  const auto &LangTo = Context.getLangOpts();
265  const auto &LangFrom = Unit->getASTContext().getLangOpts();
266 
267  // FIXME: Currenty we do not support CTU across C++ and C and across
268  // different dialects of C++.
269  if (LangTo.CPlusPlus != LangFrom.CPlusPlus) {
270  ++NumLangMismatch;
271  return llvm::make_error<IndexError>(index_error_code::lang_mismatch);
272  }
273 
274  // If CPP dialects are different then return with error.
275  //
276  // Consider this STL code:
277  // template<typename _Alloc>
278  // struct __alloc_traits
279  // #if __cplusplus >= 201103L
280  // : std::allocator_traits<_Alloc>
281  // #endif
282  // { // ...
283  // };
284  // This class template would create ODR errors during merging the two units,
285  // since in one translation unit the class template has a base class, however
286  // in the other unit it has none.
287  if (LangTo.CPlusPlus11 != LangFrom.CPlusPlus11 ||
288  LangTo.CPlusPlus14 != LangFrom.CPlusPlus14 ||
289  LangTo.CPlusPlus17 != LangFrom.CPlusPlus17 ||
290  LangTo.CPlusPlus2a != LangFrom.CPlusPlus2a) {
291  ++NumLangDialectMismatch;
292  return llvm::make_error<IndexError>(
294  }
295 
297  if (const T *ResultDecl = findDefInDeclContext<T>(TU, LookupName))
298  return importDefinition(ResultDecl);
299  return llvm::make_error<IndexError>(index_error_code::failed_import);
300 }
301 
304  StringRef CrossTUDir,
305  StringRef IndexName,
306  bool DisplayCTUProgress) {
307  return getCrossTUDefinitionImpl(FD, CrossTUDir, IndexName,
308  DisplayCTUProgress);
309 }
310 
313  StringRef CrossTUDir,
314  StringRef IndexName,
315  bool DisplayCTUProgress) {
316  return getCrossTUDefinitionImpl(VD, CrossTUDir, IndexName,
317  DisplayCTUProgress);
318 }
319 
321  switch (IE.getCode()) {
323  Context.getDiagnostics().Report(diag::err_ctu_error_opening)
324  << IE.getFileName();
325  break;
327  Context.getDiagnostics().Report(diag::err_extdefmap_parsing)
328  << IE.getFileName() << IE.getLineNum();
329  break;
331  Context.getDiagnostics().Report(diag::err_multiple_def_index)
332  << IE.getLineNum();
333  break;
335  Context.getDiagnostics().Report(diag::warn_ctu_incompat_triple)
336  << IE.getFileName() << IE.getTripleToName() << IE.getTripleFromName();
337  break;
338  default:
339  break;
340  }
341 }
342 
344  StringRef LookupName, StringRef CrossTUDir, StringRef IndexName,
345  bool DisplayCTUProgress) {
346  // FIXME: The current implementation only supports loading decls with
347  // a lookup name from a single translation unit. If multiple
348  // translation units contains decls with the same lookup name an
349  // error will be returned.
350 
351  if (NumASTLoaded >= CTULoadThreshold) {
352  ++NumASTLoadThresholdReached;
353  return llvm::make_error<IndexError>(
355  }
356 
357  ASTUnit *Unit = nullptr;
358  auto NameUnitCacheEntry = NameASTUnitMap.find(LookupName);
359  if (NameUnitCacheEntry == NameASTUnitMap.end()) {
360  if (NameFileMap.empty()) {
361  SmallString<256> IndexFile = CrossTUDir;
362  if (llvm::sys::path::is_absolute(IndexName))
363  IndexFile = IndexName;
364  else
365  llvm::sys::path::append(IndexFile, IndexName);
367  parseCrossTUIndex(IndexFile, CrossTUDir);
368  if (IndexOrErr)
369  NameFileMap = *IndexOrErr;
370  else
371  return IndexOrErr.takeError();
372  }
373 
374  auto It = NameFileMap.find(LookupName);
375  if (It == NameFileMap.end()) {
376  ++NumNotInOtherTU;
377  return llvm::make_error<IndexError>(index_error_code::missing_definition);
378  }
379  StringRef ASTFileName = It->second;
380  auto ASTCacheEntry = FileASTUnitMap.find(ASTFileName);
381  if (ASTCacheEntry == FileASTUnitMap.end()) {
383  TextDiagnosticPrinter *DiagClient =
384  new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
387  new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
388 
389  std::unique_ptr<ASTUnit> LoadedUnit(ASTUnit::LoadFromASTFile(
390  ASTFileName, CI.getPCHContainerOperations()->getRawReader(),
392  Unit = LoadedUnit.get();
393  FileASTUnitMap[ASTFileName] = std::move(LoadedUnit);
394  ++NumASTLoaded;
395  if (DisplayCTUProgress) {
396  llvm::errs() << "CTU loaded AST file: "
397  << ASTFileName << "\n";
398  }
399  } else {
400  Unit = ASTCacheEntry->second.get();
401  }
402  NameASTUnitMap[LookupName] = Unit;
403  } else {
404  Unit = NameUnitCacheEntry->second;
405  }
406  if (!Unit)
407  return llvm::make_error<IndexError>(
409  return Unit;
410 }
411 
412 template <typename T>
414 CrossTranslationUnitContext::importDefinitionImpl(const T *D) {
415  assert(hasBodyOrInit(D) && "Decls to be imported should have body or init.");
416 
417  ASTImporter &Importer = getOrCreateASTImporter(D->getASTContext());
418  auto ToDeclOrError = Importer.Import(D);
419  if (!ToDeclOrError) {
420  handleAllErrors(ToDeclOrError.takeError(),
421  [&](const ImportError &IE) {
422  switch (IE.Error) {
424  ++NumNameConflicts;
425  break;
427  ++NumUnsupportedNodeFound;
428  break;
430  llvm_unreachable("Unknown import error happened.");
431  break;
432  }
433  });
434  return llvm::make_error<IndexError>(index_error_code::failed_import);
435  }
436  auto *ToDecl = cast<T>(*ToDeclOrError);
437  assert(hasBodyOrInit(ToDecl) && "Imported Decl should have body or init.");
438  ++NumGetCTUSuccess;
439 
440  return ToDecl;
441 }
442 
445  return importDefinitionImpl(FD);
446 }
447 
450  return importDefinitionImpl(VD);
451 }
452 
453 void CrossTranslationUnitContext::lazyInitImporterSharedSt(
454  TranslationUnitDecl *ToTU) {
455  if (!ImporterSharedSt)
456  ImporterSharedSt = std::make_shared<ASTImporterSharedState>(*ToTU);
457 }
458 
459 ASTImporter &
460 CrossTranslationUnitContext::getOrCreateASTImporter(ASTContext &From) {
461  auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl());
462  if (I != ASTUnitImporterMap.end())
463  return *I->second;
464  lazyInitImporterSharedSt(Context.getTranslationUnitDecl());
465  ASTImporter *NewImporter = new ASTImporter(
466  Context, Context.getSourceManager().getFileManager(), From,
467  From.getSourceManager().getFileManager(), false, ImporterSharedSt);
468  ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter);
469  return *NewImporter;
470 }
471 
472 } // namespace cross_tu
473 } // namespace clang
Represents a function declaration or definition.
Definition: Decl.h:1748
bool isConstQualified() const
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:985
Load everything, including Sema.
Definition: ASTUnit.h:678
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
const FileManager & getFileManager() const
Definition: ASTUnit.h:476
llvm::Expected< ASTUnit * > loadExternalAST(StringRef LookupName, StringRef CrossTUDir, StringRef IndexName, bool DisplayCTUProgress=false)
This function loads a definition from an external AST file.
void log(raw_ostream &OS) const override
DiagnosticsEngine & getDiagnostics() const
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1297
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:693
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr or CxxCtorInitializer) selects the name&#39;s to...
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to...
Definition: Decl.h:1209
Not supported node or case.
Definition: ASTImporter.h:56
Represents a variable declaration or definition.
Definition: Decl.h:812
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2028
FileManager & getFileManager() const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:89
int Category
Definition: Format.cpp:1714
StringRef getMainFileName() const
Definition: ASTUnit.cpp:1463
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, bool UseDebugInfo=false, bool OnlyLocalDecls=false, ArrayRef< RemappedFile > RemappedFiles=None, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, bool AllowPCHWithCompilerErrors=false, bool UserFilesAreVolatile=false)
Create a ASTUnit from an AST file.
Definition: ASTUnit.cpp:755
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
bool containsConst(const VarDecl *VD, const ASTContext &ACtx)
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges")
const AnnotatedLine * Line
std::string createCrossTUIndexString(const llvm::StringMap< std::string > &Index)
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:2772
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...
FileSystemOptions & getFileSystemOpts()
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Options for controlling the compiler diagnostics engine.
llvm::Expected< QualType > Import(QualType FromT)
Import the given type from the "from" context into the "to" context.
static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD)
Naming ambiguity (likely ODR violation).
Definition: ASTImporter.h:55
bool hasConstFields() const
Recursively check all fields in the record for const-ness.
Definition: Type.cpp:3232
CanProxy< U > getAs() const
Retrieve a canonical type pointer with a different static type, upcasting or downcasting as needed...
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:1271
Used for handling and querying diagnostic IDs.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4438
index_error_code getCode() const
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:84
llvm::Expected< const FunctionDecl * > importDefinition(const FunctionDecl *FD)
This function merges a definition from a separate AST Unit into the current one which was created by ...
const ASTContext & getASTContext() const
Definition: ASTUnit.h:439
SourceManager & getSourceManager()
Definition: ASTContext.h:665
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2280
llvm::Expected< const FunctionDecl * > getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir, StringRef IndexName, bool DisplayCTUProgress=false)
This function loads a function or variable definition from an external AST file and merges it into th...
std::error_code convertToErrorCode() const override
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1007
Defines the clang::TargetInfo interface.
static std::string getLookupName(const NamedDecl *ND)
Get a name to identify a named decl.
void emitCrossTUDiagnostics(const IndexError &IE)
Emit diagnostics for the user for potential configuration errors.
The top declaration context.
Definition: Decl.h:107
QualType getType() const
Definition: Decl.h:647
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
This represents a decl that may have a name.
Definition: Decl.h:248
std::string getTripleToName() const
const LangOptions & getLangOpts() const
Definition: ASTContext.h:710
std::string getTripleFromName() const