clang 10.0.0git

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 //===----------------------------------------------------------------------===//
12 #include "clang/CrossTU/CrossTranslationUnit.h"
13 #include "clang/AST/ASTImporter.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/CrossTU/CrossTUDiagnostic.h"
17 #include "clang/Frontend/ASTUnit.h"
18 #include "clang/Frontend/CompilerInstance.h"
19 #include "clang/Frontend/TextDiagnosticPrinter.h"
20 #include "clang/Index/USRGeneration.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)) {
89 case index_error_code::unspecified:
90 return "An unknown error has occurred.";
91 case index_error_code::missing_index_file:
92 return "The index file is missing.";
93 case index_error_code::invalid_index_format:
94 return "Invalid index file format.";
95 case index_error_code::multiple_definitions:
96 return "Multiple definitions in the index file.";
97 case index_error_code::missing_definition:
98 return "Missing definition from the index file.";
99 case index_error_code::failed_import:
100 return "Failed to import the definition.";
101 case index_error_code::failed_to_get_external_ast:
102 return "Failed to load external AST source.";
103 case index_error_code::failed_to_generate_usr:
104 return "Failed to generate USR.";
105 case index_error_code::triple_mismatch:
106 return "Triple mismatch";
107 case index_error_code::lang_mismatch:
108 return "Language mismatch";
109 case index_error_code::lang_dialect_mismatch:
110 return "Language dialect mismatch";
111 case index_error_code::load_threshold_reached:
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
131 llvm::Expected<llvm::StringMap<std::string>>
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
190 CrossTranslationUnitContext::CrossTranslationUnitContext(CompilerInstance &CI)
191 : Context(CI.getASTContext()), ASTStorage(CI) {}
192
193 CrossTranslationUnitContext::~CrossTranslationUnitContext() {}
194
195 llvm::Optional<std::string>
196 CrossTranslationUnitContext::getLookupName(const NamedDecl *ND) {
197 SmallString<128> DeclUSR;
198 bool Ret = index::generateUSRForDecl(ND, DeclUSR);
199 if (Ret)
200 return {};
201 return std::string(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 llvm::Optional<std::string> ResultLookupName = getLookupName(ResultDecl);
222 if (!ResultLookupName || *ResultLookupName != LookupName)
223 continue;
224 return ResultDecl;
225 }
226 return nullptr;
227 }
228
229 template <typename T>
230 llvm::Expected<const T *> CrossTranslationUnitContext::getCrossTUDefinitionImpl(
231 const T *D, StringRef CrossTUDir, StringRef IndexName,
232 bool DisplayCTUProgress) {
233 assert(D && "D is missing, bad call to this function!");
234 assert(!hasBodyOrInit(D) &&
235 "D has a body or init in current translation unit!");
236 ++NumGetCTUCalled;
237 const llvm::Optional<std::string> LookupName = getLookupName(D);
238 if (!LookupName)
239 return llvm::make_error<IndexError>(
240 index_error_code::failed_to_generate_usr);
241 llvm::Expected<ASTUnit *> ASTUnitOrError =
242 loadExternalAST(*LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
243 if (!ASTUnitOrError)
244 return ASTUnitOrError.takeError();
245 ASTUnit *Unit = *ASTUnitOrError;
246 assert(&Unit->getFileManager() ==
247 &Unit->getASTContext().getSourceManager().getFileManager());
248
249 const llvm::Triple &TripleTo = Context.getTargetInfo().getTriple();
250 const llvm::Triple &TripleFrom =
251 Unit->getASTContext().getTargetInfo().getTriple();
252 // The imported AST had been generated for a different target.
253 // Some parts of the triple in the loaded ASTContext can be unknown while the
254 // very same parts in the target ASTContext are known. Thus we check for the
255 // known parts only.
256 if (!hasEqualKnownFields(TripleTo, TripleFrom)) {
257 // TODO: Pass the SourceLocation of the CallExpression for more precise
258 // diagnostics.
259 ++NumTripleMismatch;
260 return llvm::make_error<IndexError>(index_error_code::triple_mismatch,
261 Unit->getMainFileName(), TripleTo.str(),
262 TripleFrom.str());
263 }
264
265 const auto &LangTo = Context.getLangOpts();
266 const auto &LangFrom = Unit->getASTContext().getLangOpts();
267
268 // FIXME: Currenty we do not support CTU across C++ and C and across
269 // different dialects of C++.
270 if (LangTo.CPlusPlus != LangFrom.CPlusPlus) {
271 ++NumLangMismatch;
272 return llvm::make_error<IndexError>(index_error_code::lang_mismatch);
273 }
274
275 // If CPP dialects are different then return with error.
276 //
277 // Consider this STL code:
278 // template<typename _Alloc>
279 // struct __alloc_traits
280 // #if __cplusplus >= 201103L
281 // : std::allocator_traits<_Alloc>
282 // #endif
283 // { // ...
284 // };
285 // This class template would create ODR errors during merging the two units,
286 // since in one translation unit the class template has a base class, however
287 // in the other unit it has none.
288 if (LangTo.CPlusPlus11 != LangFrom.CPlusPlus11 ||
289 LangTo.CPlusPlus14 != LangFrom.CPlusPlus14 ||
290 LangTo.CPlusPlus17 != LangFrom.CPlusPlus17 ||
291 LangTo.CPlusPlus2a != LangFrom.CPlusPlus2a) {
292 ++NumLangDialectMismatch;
293 return llvm::make_error<IndexError>(
294 index_error_code::lang_dialect_mismatch);
295 }
296
297 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
298 if (const T *ResultDecl = findDefInDeclContext<T>(TU, *LookupName))
299 return importDefinition(ResultDecl, Unit);
300 return llvm::make_error<IndexError>(index_error_code::failed_import);
301 }
302
303 llvm::Expected<const FunctionDecl *>
304 CrossTranslationUnitContext::getCrossTUDefinition(const FunctionDecl *FD,
305 StringRef CrossTUDir,
306 StringRef IndexName,
307 bool DisplayCTUProgress) {
308 return getCrossTUDefinitionImpl(FD, CrossTUDir, IndexName,
309 DisplayCTUProgress);
310 }
311
312 llvm::Expected<const VarDecl *>
313 CrossTranslationUnitContext::getCrossTUDefinition(const VarDecl *VD,
314 StringRef CrossTUDir,
315 StringRef IndexName,
316 bool DisplayCTUProgress) {
317 return getCrossTUDefinitionImpl(VD, CrossTUDir, IndexName,
318 DisplayCTUProgress);
319 }
320
321 void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE) {
322 switch (IE.getCode()) {
323 case index_error_code::missing_index_file:
324 Context.getDiagnostics().Report(diag::err_ctu_error_opening)
325 << IE.getFileName();
326 break;
327 case index_error_code::invalid_index_format:
328 Context.getDiagnostics().Report(diag::err_extdefmap_parsing)
329 << IE.getFileName() << IE.getLineNum();
330 break;
331 case index_error_code::multiple_definitions:
332 Context.getDiagnostics().Report(diag::err_multiple_def_index)
333 << IE.getLineNum();
334 break;
335 case index_error_code::triple_mismatch:
336 Context.getDiagnostics().Report(diag::warn_ctu_incompat_triple)
337 << IE.getFileName() << IE.getTripleToName() << IE.getTripleFromName();
338 break;
339 default:
340 break;
341 }
342 }
343
344 CrossTranslationUnitContext::ASTFileLoader::ASTFileLoader(
345 const CompilerInstance &CI)
346 : CI(CI) {}
347
348 std::unique_ptr<ASTUnit>
349 CrossTranslationUnitContext::ASTFileLoader::operator()(StringRef ASTFilePath) {
350 // Load AST from ast-dump.
351 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
352 TextDiagnosticPrinter *DiagClient =
353 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
354 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
355 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
356 new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
357
358 return ASTUnit::LoadFromASTFile(
359 ASTFilePath, CI.getPCHContainerOperations()->getRawReader(),
360 ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts());
361 }
362
363 CrossTranslationUnitContext::ASTUnitStorage::ASTUnitStorage(
364 const CompilerInstance &CI)
365 : FileAccessor(CI), LoadGuard(const_cast<CompilerInstance &>(CI)
366 .getAnalyzerOpts()
367 ->CTUImportThreshold) {}
368
369 llvm::Expected<ASTUnit *>
370 CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFile(
371 StringRef FileName, bool DisplayCTUProgress) {
372 // Try the cache first.
373 auto ASTCacheEntry = FileASTUnitMap.find(FileName);
374 if (ASTCacheEntry == FileASTUnitMap.end()) {
375
376 // Do not load if the limit is reached.
377 if (!LoadGuard) {
378 ++NumASTLoadThresholdReached;
379 return llvm::make_error<IndexError>(
380 index_error_code::load_threshold_reached);
381 }
382
383 // Load the ASTUnit from the pre-dumped AST file specified by ASTFileName.
384 std::unique_ptr<ASTUnit> LoadedUnit = FileAccessor(FileName);
385
386 // Need the raw pointer and the unique_ptr as well.
387 ASTUnit *Unit = LoadedUnit.get();
388
389 // Update the cache.
390 FileASTUnitMap[FileName] = std::move(LoadedUnit);
391
392 LoadGuard.indicateLoadSuccess();
393
394 if (DisplayCTUProgress)
395 llvm::errs() << "CTU loaded AST file: " << FileName << "\n";
396
397 return Unit;
398
399 } else {
400 // Found in the cache.
401 return ASTCacheEntry->second.get();
402 }
403 }
404
405 llvm::Expected<ASTUnit *>
406 CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFunction(
407 StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName,
408 bool DisplayCTUProgress) {
409 // Try the cache first.
410 auto ASTCacheEntry = NameASTUnitMap.find(FunctionName);
411 if (ASTCacheEntry == NameASTUnitMap.end()) {
412 // Load the ASTUnit from the pre-dumped AST file specified by ASTFileName.
413
414 // Ensure that the Index is loaded, as we need to search in it.
415 if (llvm::Error IndexLoadError =
416 ensureCTUIndexLoaded(CrossTUDir, IndexName))
417 return std::move(IndexLoadError);
418
419 // Check if there is and entry in the index for the function.
420 if (!NameFileMap.count(FunctionName)) {
421 ++NumNotInOtherTU;
422 return llvm::make_error<IndexError>(index_error_code::missing_definition);
423 }
424
425 // Search in the index for the filename where the definition of FuncitonName
426 // resides.
427 if (llvm::Expected<ASTUnit *> FoundForFile =
428 getASTUnitForFile(NameFileMap[FunctionName], DisplayCTUProgress)) {
429
430 // Update the cache.
431 NameASTUnitMap[FunctionName] = *FoundForFile;
432 return *FoundForFile;
433
434 } else {
435 return FoundForFile.takeError();
436 }
437 } else {
438 // Found in the cache.
439 return ASTCacheEntry->second;
440 }
441 }
442
443 llvm::Expected<std::string>
444 CrossTranslationUnitContext::ASTUnitStorage::getFileForFunction(
445 StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName) {
446 if (llvm::Error IndexLoadError = ensureCTUIndexLoaded(CrossTUDir, IndexName))
447 return std::move(IndexLoadError);
448 return NameFileMap[FunctionName];
449 }
450
451 llvm::Error CrossTranslationUnitContext::ASTUnitStorage::ensureCTUIndexLoaded(
452 StringRef CrossTUDir, StringRef IndexName) {
453 // Dont initialize if the map is filled.
454 if (!NameFileMap.empty())
455 return llvm::Error::success();
456
457 // Get the absolute path to the index file.
458 SmallString<256> IndexFile = CrossTUDir;
459 if (llvm::sys::path::is_absolute(IndexName))
460 IndexFile = IndexName;
461 else
462 llvm::sys::path::append(IndexFile, IndexName);
463
464 if (auto IndexMapping = parseCrossTUIndex(IndexFile, CrossTUDir)) {
465 // Initialize member map.
466 NameFileMap = *IndexMapping;
467 return llvm::Error::success();
468 } else {
469 // Error while parsing CrossTU index file.
470 return IndexMapping.takeError();
471 };
472 }
473
474 llvm::Expected<ASTUnit *> CrossTranslationUnitContext::loadExternalAST(
475 StringRef LookupName, StringRef CrossTUDir, StringRef IndexName,
476 bool DisplayCTUProgress) {
477 // FIXME: The current implementation only supports loading decls with
478 // a lookup name from a single translation unit. If multiple
479 // translation units contains decls with the same lookup name an
480 // error will be returned.
481
482 // Try to get the value from the heavily cached storage.
483 llvm::Expected<ASTUnit *> Unit = ASTStorage.getASTUnitForFunction(
484 LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
485
486 if (!Unit)
487 return Unit.takeError();
488
489 // Check whether the backing pointer of the Expected is a nullptr.
490 if (!*Unit)
491 return llvm::make_error<IndexError>(
492 index_error_code::failed_to_get_external_ast);
493
494 return Unit;
495 }
496
497 template <typename T>
498 llvm::Expected<const T *>
499 CrossTranslationUnitContext::importDefinitionImpl(const T *D, ASTUnit *Unit) {
500 assert(hasBodyOrInit(D) && "Decls to be imported should have body or init.");
501
502 assert(&D->getASTContext() == &Unit->getASTContext() &&
503 "ASTContext of Decl and the unit should match.");
504 ASTImporter &Importer = getOrCreateASTImporter(Unit);
505
506 auto ToDeclOrError = Importer.Import(D);
507 if (!ToDeclOrError) {
508 handleAllErrors(ToDeclOrError.takeError(),
509 [&](const ImportError &IE) {
510 switch (IE.Error) {
511 case ImportError::NameConflict:
512 ++NumNameConflicts;
513 break;
514 case ImportError::UnsupportedConstruct:
515 ++NumUnsupportedNodeFound;
516 break;
517 case ImportError::Unknown:
518 llvm_unreachable("Unknown import error happened.");
519 break;
520 }
521 });
522 return llvm::make_error<IndexError>(index_error_code::failed_import);
523 }
524 auto *ToDecl = cast<T>(*ToDeclOrError);
525 assert(hasBodyOrInit(ToDecl) && "Imported Decl should have body or init.");
526 ++NumGetCTUSuccess;
527
528 return ToDecl;
529 }
530
531 llvm::Expected<const FunctionDecl *>
532 CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD,
533 ASTUnit *Unit) {
534 return importDefinitionImpl(FD, Unit);
535 }
536
537 llvm::Expected<const VarDecl *>
538 CrossTranslationUnitContext::importDefinition(const VarDecl *VD,
539 ASTUnit *Unit) {
540 return importDefinitionImpl(VD, Unit);
541 }
542
543 void CrossTranslationUnitContext::lazyInitImporterSharedSt(
544 TranslationUnitDecl *ToTU) {
545 if (!ImporterSharedSt)
546 ImporterSharedSt = std::make_shared<ASTImporterSharedState>(*ToTU);
547 }
548
549 ASTImporter &
550 CrossTranslationUnitContext::getOrCreateASTImporter(ASTUnit *Unit) {
551 ASTContext &From = Unit->getASTContext();
552
553 auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl());
554 if (I != ASTUnitImporterMap.end())
555 return *I->second;
556 lazyInitImporterSharedSt(Context.getTranslationUnitDecl());
557 ASTImporter *NewImporter = new ASTImporter(
558 Context, Context.getSourceManager().getFileManager(), From,
559 From.getSourceManager().getFileManager(), false, ImporterSharedSt);
560 NewImporter->setFileIDImportHandler([this, Unit](FileID ToID, FileID FromID) {
561 assert(ImportedFileIDs.find(ToID) == ImportedFileIDs.end() &&
562 "FileID already imported, should not happen.");
563 ImportedFileIDs[ToID] = std::make_pair(FromID, Unit);
564 });
565 ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter);
566 return *NewImporter;
567 }
568
569 llvm::Optional<std::pair<SourceLocation, ASTUnit *>>
570 CrossTranslationUnitContext::getImportedFromSourceLocation(
571 const clang::SourceLocation &ToLoc) const {
572 const SourceManager &SM = Context.getSourceManager();
573 auto DecToLoc = SM.getDecomposedLoc(ToLoc);
574
575 auto I = ImportedFileIDs.find(DecToLoc.first);
576 if (I == ImportedFileIDs.end())
577 return {};
578
579 FileID FromID = I->second.first;
580 clang::ASTUnit *Unit = I->second.second;
581 SourceLocation FromLoc =
582 Unit->getSourceManager().getComposedLoc(FromID, DecToLoc.second);
583
584 return std::make_pair(FromLoc, Unit);
585 }
586
587 } // namespace cross_tu
588 } // namespace clang
CrossTranslationUnit.h
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1783
clang::cross_tu::index_error_code::unspecified
clang::cross_tu::CrossTranslationUnitContext::CrossTranslationUnitContext
CrossTranslationUnitContext(CompilerInstance &CI)
Definition: CrossTranslationUnit.cpp:190
clang::CanQual::isConstQualified
bool isConstQualified() const
Definition: CanonicalType.h:137
clang::TargetInfo::getTriple
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:994
clang::ASTUnit::LoadEverything
Load everything, including Sema.
Definition: ASTUnit.h:678
clang::CompilerInstance::getPCHContainerOperations
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
Definition: CompilerInstance.h:524
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::ASTUnit::getFileManager
const FileManager & getFileManager() const
Definition: ASTUnit.h:476
clang::cross_tu::CrossTranslationUnitContext::loadExternalAST
llvm::Expected< ASTUnit * > loadExternalAST(StringRef LookupName, StringRef CrossTUDir, StringRef IndexName, bool DisplayCTUProgress=false)
This function loads a definition from an external AST file.
Definition: CrossTranslationUnit.cpp:474
clang::cross_tu::IndexError::log
void log(raw_ostream &OS) const override
Definition: CrossTranslationUnit.cpp:123
clang::ASTContext::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Definition: ASTContext.cpp:1516
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1300
clang::ASTContext::getTargetInfo
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:707
clang::VarDecl::getAnyInitializer
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to...
Definition: Decl.h:1219
clang::ImportError::Unknown
Not supported node or case.
Definition: ASTImporter.h:56
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:820
clang::cross_tu::index_error_code::missing_definition
clang::DeclContext::decls
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2033
clang::SourceManager::getFileManager
FileManager & getFileManager() const
Definition: SourceManager.h:773
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::ASTUnit
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:89
llvm::Optional< std::string >
Category
int Category
Definition: Format.cpp:1828
clang::ASTUnit::getMainFileName
StringRef getMainFileName() const
Definition: ASTUnit.cpp:1464
clang::SourceManager::getComposedLoc
SourceLocation getComposedLoc(FileID FID, unsigned Offset) const
Form a SourceLocation from a FileID and Offset pair.
Definition: SourceManager.h:1223
llvm::Expected
Definition: LLVM.h:38
clang::ASTUnit::LoadFromASTFile
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:756
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
clang::cross_tu::index_error_code::lang_mismatch
clang::cross_tu::index_error_code::invalid_index_format
clang::transformer::name
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr or CxxCtorInitializer) selects the name&#39;s to...
Definition: RangeSelector.cpp:186
clang::ImportError
Definition: ASTImporter.h:50
llvm::SmallString
Definition: LLVM.h:34
clang::cross_tu::CrossTranslationUnitContext::getLookupName
static llvm::Optional< std::string > getLookupName(const NamedDecl *ND)
Get a name to identify a named decl.
Definition: CrossTranslationUnit.cpp:196
Unused
Definition: ObjCUnusedIVarsChecker.cpp:29
clang::cross_tu::index_error_code::failed_to_generate_usr
CompilerInstance.h
clang::cross_tu::CrossTranslationUnitContext::importDefinition
llvm::Expected< const FunctionDecl * > importDefinition(const FunctionDecl *FD, ASTUnit *Unit)
This function merges a definition from a separate AST Unit into the current one which was created by ...
Definition: CrossTranslationUnit.cpp:532
clang::cross_tu::containsConst
bool containsConst(const VarDecl *VD, const ASTContext &ACtx)
Definition: CrossTranslationUnit.cpp:169
STATISTIC
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges")
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:68
clang::cross_tu::createCrossTUIndexString
std::string createCrossTUIndexString(const llvm::StringMap< std::string > &Index)
Definition: CrossTranslationUnit.cpp:162
clang::cross_tu::index_error_code::multiple_definitions
clang::cross_tu::index_error_code::lang_dialect_mismatch
clang::FunctionDecl::hasBody
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:2859
clang::cross_tu::IndexError::getFileName
std::string getFileName() const
Definition: CrossTranslationUnit.h:68
clang::DiagnosticLevelMask::Error
clang::cross_tu::parseCrossTUIndex
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...
Definition: CrossTranslationUnit.cpp:132
SM
const SourceManager & SM
Definition: Format.cpp:1685
clang::cross_tu::CrossTranslationUnitContext::~CrossTranslationUnitContext
~CrossTranslationUnitContext()
Definition: CrossTranslationUnit.cpp:193
clang::cross_tu::index_error_code::failed_import
clang::CompilerInstance::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Definition: CompilerInstance.h:278
clang::ASTImporter::setFileIDImportHandler
void setFileIDImportHandler(FileIDImportHandlerType H)
Set a callback function for FileID import handling.
Definition: ASTImporter.h:329
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:44
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
USRGeneration.h
Ret
static bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
Definition: Interp.cpp:34
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:63
clang::CanQual< Type >
clang::cross_tu::index_error_code::load_threshold_reached
clang::cross_tu::hasBodyOrInit
static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD)
Definition: CrossTranslationUnit.cpp:179
clang::ImportError::UnsupportedConstruct
Naming ambiguity (likely ODR violation).
Definition: ASTImporter.h:55
clang::RecordType::hasConstFields
bool hasConstFields() const
Recursively check all fields in the record for const-ness.
Definition: Type.cpp:3304
clang::CanQual::getAs
CanProxy< U > getAs() const
Retrieve a canonical type pointer with a different static type, upcasting or downcasting as needed...
Definition: CanonicalType.h:651
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:38
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
CrossTUDiagnostic.h
clang::TextDiagnosticPrinter
Definition: TextDiagnosticPrinter.h:27
clang::DiagnosticIDs
Used for handling and querying diagnostic IDs.
Definition: DiagnosticIDs.h:163
TextDiagnosticPrinter.h
ASTImporter.h
ASTUnit.h
clang::RecordType
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4495
clang::cross_tu::IndexError::getCode
index_error_code getCode() const
Definition: CrossTranslationUnit.h:66
clang::ASTImporter
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:88
clang::cross_tu::IndexError
Definition: CrossTranslationUnit.h:53
clang::cross_tu::index_error_code::missing_index_file
clang::cross_tu::index_error_code::triple_mismatch
clang::ImportError::NameConflict
Definition: ASTImporter.h:54
clang::ASTUnit::getASTContext
const ASTContext & getASTContext() const
Definition: ASTUnit.h:439
clang::cross_tu::IndexError::ID
static char ID
Definition: CrossTranslationUnit.h:55
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:679
clang::ASTContext::getCanonicalType
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2305
clang::cross_tu::CrossTranslationUnitContext::getCrossTUDefinition
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...
Definition: CrossTranslationUnit.cpp:304
clang::cross_tu::IndexError::convertToErrorCode
std::error_code convertToErrorCode() const override
Definition: CrossTranslationUnit.cpp:127
clang::ASTContext::getTranslationUnitDecl
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1009
clang::ASTUnit::getSourceManager
const SourceManager & getSourceManager() const
Definition: ASTUnit.h:432
clang::cross_tu::IndexError::getLineNum
int getLineNum() const
Definition: CrossTranslationUnit.h:67
TargetInfo.h
Defines the clang::TargetInfo interface.
clang::cross_tu::CrossTranslationUnitContext::emitCrossTUDiagnostics
void emitCrossTUDiagnostics(const IndexError &IE)
Emit diagnostics for the user for potential configuration errors.
Definition: CrossTranslationUnit.cpp:321
clang::cross_tu::CrossTranslationUnitContext::getImportedFromSourceLocation
llvm::Optional< std::pair< SourceLocation, ASTUnit * > > getImportedFromSourceLocation(const clang::SourceLocation &ToLoc) const
Determine the original source location in the original TU for an imported source location.
Definition: CrossTranslationUnit.cpp:570
clang::TranslationUnitDecl
The top declaration context.
Definition: Decl.h:82
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:630
clang::index::generateUSRForDecl
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
Definition: USRGeneration.cpp:1073
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:223
clang::cross_tu::IndexError::getTripleToName
std::string getTripleToName() const
Definition: CrossTranslationUnit.h:69
clang::cross_tu::index_error_code::failed_to_get_external_ast
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:724
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:596
clang::SourceManager::getDecomposedLoc
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
Definition: SourceManager.h:1238
clang::cross_tu::IndexError::getTripleFromName
std::string getTripleFromName() const
Definition: CrossTranslationUnit.h:70