clang 23.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//===----------------------------------------------------------------------===//
14#include "clang/AST/Decl.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/Option/ArgList.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/IOSandbox.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/Path.h"
30#include "llvm/Support/YAMLParser.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/TargetParser/Triple.h"
33#include <algorithm>
34#include <fstream>
35#include <optional>
36#include <sstream>
37#include <tuple>
38
39namespace clang {
40namespace cross_tu {
41
42namespace {
43
44#define DEBUG_TYPE "CrossTranslationUnit"
45STATISTIC(NumGetCTUCalled, "The # of getCTUDefinition function called");
47 NumNotInOtherTU,
48 "The # of getCTUDefinition called but the function is not in any other TU");
49STATISTIC(NumGetCTUSuccess,
50 "The # of getCTUDefinition successfully returned the "
51 "requested function's body");
52STATISTIC(NumUnsupportedNodeFound, "The # of imports when the ASTImporter "
53 "encountered an unsupported AST Node");
54STATISTIC(NumNameConflicts, "The # of imports when the ASTImporter "
55 "encountered an ODR error");
56STATISTIC(NumTripleMismatch, "The # of triple mismatches");
57STATISTIC(NumLangMismatch, "The # of language mismatches");
58STATISTIC(NumLangDialectMismatch, "The # of language dialect mismatches");
59STATISTIC(NumASTLoadThresholdReached,
60 "The # of ASTs not loaded because of threshold");
61
62// Same as Triple's equality operator, but we check a field only if that is
63// known in both instances.
64bool hasEqualKnownFields(const llvm::Triple &Lhs, const llvm::Triple &Rhs) {
65 using llvm::Triple;
66 if (Lhs.getArch() != Triple::UnknownArch &&
67 Rhs.getArch() != Triple::UnknownArch && Lhs.getArch() != Rhs.getArch())
68 return false;
69 if (Lhs.getSubArch() != Triple::NoSubArch &&
70 Rhs.getSubArch() != Triple::NoSubArch &&
71 Lhs.getSubArch() != Rhs.getSubArch())
72 return false;
73 if (Lhs.getVendor() != Triple::UnknownVendor &&
74 Rhs.getVendor() != Triple::UnknownVendor &&
75 Lhs.getVendor() != Rhs.getVendor())
76 return false;
77 if (!Lhs.isOSUnknown() && !Rhs.isOSUnknown() &&
78 Lhs.getOS() != Rhs.getOS())
79 return false;
80 if (Lhs.getEnvironment() != Triple::UnknownEnvironment &&
81 Rhs.getEnvironment() != Triple::UnknownEnvironment &&
82 Lhs.getEnvironment() != Rhs.getEnvironment())
83 return false;
84 if (Lhs.getObjectFormat() != Triple::UnknownObjectFormat &&
85 Rhs.getObjectFormat() != Triple::UnknownObjectFormat &&
86 Lhs.getObjectFormat() != Rhs.getObjectFormat())
87 return false;
88 return true;
89}
90
91// FIXME: This class is will be removed after the transition to llvm::Error.
92class IndexErrorCategory : public std::error_category {
93public:
94 const char *name() const noexcept override { return "clang.index"; }
95
96 std::string message(int Condition) const override {
97 switch (static_cast<index_error_code>(Condition)) {
99 // There should not be a success error. Jump to unreachable directly.
100 // Add this case to make the compiler stop complaining.
101 break;
103 return "An unknown error has occurred.";
105 return "The index file is missing.";
107 return "Invalid index file format.";
109 return "Multiple definitions in the index file.";
111 return "Missing definition from the index file.";
113 return "Failed to import the definition.";
115 return "Failed to load external AST source.";
117 return "Failed to generate USR.";
119 return "Triple mismatch";
121 return "Language mismatch";
123 return "Language dialect mismatch";
125 return "Load threshold reached";
127 return "Invocation list file contains multiple references to the same "
128 "source file.";
130 return "Invocation list file is not found.";
132 return "Invocation list file is empty.";
134 return "Invocation list file is in wrong format.";
136 return "Invocation list file does not contain the requested source file.";
137 }
138 llvm_unreachable("Unrecognized index_error_code.");
139 }
140};
141
142static llvm::ManagedStatic<IndexErrorCategory> Category;
143} // end anonymous namespace
144
145/// Returns a human-readable language/dialect description for diagnostics.
146/// Checks flags from highest to lowest standard since they are cumulative
147/// (e.g. CPlusPlus20 implies CPlusPlus17).
148/// This does not cover all possible languages (e.g. Obj-C or flavors of C),
149/// because CTU currently does not differentiate between them.
150static std::string getLangDescription(const LangOptions &LO) {
151 if (!LO.CPlusPlus)
152 return "non-C++";
153 if (LO.CPlusPlus26)
154 return "C++26";
155 if (LO.CPlusPlus23)
156 return "C++23";
157 if (LO.CPlusPlus20)
158 return "C++20";
159 if (LO.CPlusPlus17)
160 return "C++17";
161 if (LO.CPlusPlus14)
162 return "C++14";
163 if (LO.CPlusPlus11)
164 return "C++11";
165 return "C++98";
166}
167
168char IndexError::ID;
169
170void IndexError::log(raw_ostream &OS) const {
171 OS << Category->message(static_cast<int>(Code)) << '\n';
172}
173
174std::error_code IndexError::convertToErrorCode() const {
175 return std::error_code(static_cast<int>(Code), *Category);
176}
177
178/// Parse one line of the input CTU index file.
179///
180/// @param[in] LineRef The input CTU index item in format
181/// "<USR-Length>:<USR> <File-Path>".
182/// @param[out] LookupName The lookup name in format "<USR-Length>:<USR>".
183/// @param[out] FilePath The file path "<File-Path>".
184static bool parseCrossTUIndexItem(StringRef LineRef, StringRef &LookupName,
185 StringRef &FilePath) {
186 // `LineRef` is "<USR-Length>:<USR> <File-Path>" now.
187
188 size_t USRLength = 0;
189 if (LineRef.consumeInteger(10, USRLength))
190 return false;
191 assert(USRLength && "USRLength should be greater than zero.");
192
193 if (!LineRef.consume_front(":"))
194 return false;
195
196 // `LineRef` is now just "<USR> <File-Path>".
197
198 // Check LookupName length out of bound and incorrect delimiter.
199 if (USRLength >= LineRef.size() || ' ' != LineRef[USRLength])
200 return false;
201
202 LookupName = LineRef.substr(0, USRLength);
203 FilePath = LineRef.substr(USRLength + 1);
204 return true;
205}
206
208parseCrossTUIndex(StringRef IndexPath) {
209 std::ifstream ExternalMapFile{std::string(IndexPath)};
210 if (!ExternalMapFile)
211 return llvm::make_error<IndexError>(index_error_code::missing_index_file,
212 IndexPath.str());
213
214 llvm::StringMap<std::string> Result;
215 std::string Line;
216 unsigned LineNo = 1;
217 while (std::getline(ExternalMapFile, Line)) {
218 // Split lookup name and file path
219 StringRef LookupName, FilePathInIndex;
220 if (!parseCrossTUIndexItem(Line, LookupName, FilePathInIndex))
221 return llvm::make_error<IndexError>(
222 index_error_code::invalid_index_format, IndexPath.str(), LineNo);
223
224 // Store paths with posix-style directory separator.
225 SmallString<32> FilePath(FilePathInIndex);
226 llvm::sys::path::native(FilePath, llvm::sys::path::Style::posix);
227
228 bool InsertionOccurred;
229 std::tie(std::ignore, InsertionOccurred) =
230 Result.try_emplace(LookupName, FilePath.begin(), FilePath.end());
231 if (!InsertionOccurred)
232 return llvm::make_error<IndexError>(
233 index_error_code::multiple_definitions, IndexPath.str(), LineNo);
234
235 ++LineNo;
236 }
237 return Result;
238}
239
240std::string
241createCrossTUIndexString(const llvm::StringMap<std::string> &Index) {
242 std::ostringstream Result;
243 for (const auto &E : Index)
244 Result << E.getKey().size() << ':' << E.getKey().str() << ' '
245 << E.getValue() << '\n';
246 return Result.str();
247}
248
249bool shouldImport(const VarDecl *VD, const ASTContext &ACtx) {
250 CanQualType CT = ACtx.getCanonicalType(VD->getType());
251 return CT.isConstQualified() && VD->getType().isTrivialType(ACtx);
252}
253
254static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD) {
255 return D->hasBody(DefD);
256}
257static bool hasBodyOrInit(const VarDecl *D, const VarDecl *&DefD) {
258 return D->getAnyInitializer(DefD);
259}
260template <typename T> static bool hasBodyOrInit(const T *D) {
261 const T *Unused;
262 return hasBodyOrInit(D, Unused);
263}
264
266 : Context(CI.getASTContext()), ASTStorage(CI) {
268 !CI.getAnalyzerOpts().CTUDir.empty()) {
269 auto S = CI.getVirtualFileSystem().status(CI.getAnalyzerOpts().CTUDir);
270 if (!S || S->getType() != llvm::sys::fs::file_type::directory_file)
271 CI.getDiagnostics().Report(diag::err_analyzer_config_invalid_input)
272 << "ctu-dir"
273 << "a filename";
274 }
275}
276
278
279std::optional<std::string>
281 SmallString<128> DeclUSR;
282 bool Ret = index::generateUSRForDecl(D, DeclUSR);
283 if (Ret)
284 return {};
285 return std::string(DeclUSR);
286}
287
288/// Recursively visits the decls of a DeclContext, and returns one with the
289/// given USR.
290template <typename T>
291const T *
292CrossTranslationUnitContext::findDefInDeclContext(const DeclContext *DC,
293 StringRef LookupName) {
294 assert(DC && "Declaration Context must not be null");
295 for (const Decl *D : DC->decls()) {
296 const auto *SubDC = dyn_cast<DeclContext>(D);
297 if (SubDC)
298 if (const auto *ND = findDefInDeclContext<T>(SubDC, LookupName))
299 return ND;
300
301 const auto *ND = dyn_cast<T>(D);
302 const T *ResultDecl;
303 if (!ND || !hasBodyOrInit(ND, ResultDecl))
304 continue;
305 std::optional<std::string> ResultLookupName = getLookupName(ResultDecl);
306 if (!ResultLookupName || *ResultLookupName != LookupName)
307 continue;
308 return ResultDecl;
309 }
310 return nullptr;
311}
312
313template <typename T>
314llvm::Expected<const T *> CrossTranslationUnitContext::getCrossTUDefinitionImpl(
315 const T *D, StringRef CrossTUDir, StringRef IndexName,
316 bool DisplayCTUProgress) {
317 assert(D && "D is missing, bad call to this function!");
318 assert(!hasBodyOrInit(D) &&
319 "D has a body or init in current translation unit!");
320 ++NumGetCTUCalled;
321 const std::optional<std::string> LookupName = getLookupName(D);
322 if (!LookupName)
323 return llvm::make_error<IndexError>(
325 llvm::Expected<ASTUnit *> ASTUnitOrError =
326 loadExternalAST(*LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
327 if (!ASTUnitOrError)
328 return ASTUnitOrError.takeError();
329 ASTUnit *Unit = *ASTUnitOrError;
330 assert(&Unit->getFileManager() ==
331 &Unit->getASTContext().getSourceManager().getFileManager());
332
333 const llvm::Triple &TripleTo = Context.getTargetInfo().getTriple();
334 const llvm::Triple &TripleFrom =
335 Unit->getASTContext().getTargetInfo().getTriple();
336 // The imported AST had been generated for a different target.
337 // Some parts of the triple in the loaded ASTContext can be unknown while the
338 // very same parts in the target ASTContext are known. Thus we check for the
339 // known parts only.
340 if (!hasEqualKnownFields(TripleTo, TripleFrom)) {
341 // TODO: Pass the SourceLocation of the CallExpression for more precise
342 // diagnostics.
343 ++NumTripleMismatch;
344 return llvm::make_error<IndexError>(index_error_code::triple_mismatch,
345 std::string(Unit->getMainFileName()),
346 TripleTo.str(), TripleFrom.str());
347 }
348
349 const auto &LangTo = Context.getLangOpts();
350 const auto &LangFrom = Unit->getASTContext().getLangOpts();
351
352 // FIXME: Currenty we do not support CTU across C++ and C and across
353 // different dialects of C++.
354 if (LangTo.CPlusPlus != LangFrom.CPlusPlus) {
355 ++NumLangMismatch;
356 return llvm::make_error<IndexError>(
357 index_error_code::lang_mismatch, std::string(Unit->getMainFileName()),
358 getLangDescription(LangTo), getLangDescription(LangFrom));
359 }
360
361 // If CPP dialects are different then return with error.
362 //
363 // Consider this STL code:
364 // template<typename _Alloc>
365 // struct __alloc_traits
366 // #if __cplusplus >= 201103L
367 // : std::allocator_traits<_Alloc>
368 // #endif
369 // { // ...
370 // };
371 // This class template would create ODR errors during merging the two units,
372 // since in one translation unit the class template has a base class, however
373 // in the other unit it has none.
374 if (LangTo.CPlusPlus11 != LangFrom.CPlusPlus11 ||
375 LangTo.CPlusPlus14 != LangFrom.CPlusPlus14 ||
376 LangTo.CPlusPlus17 != LangFrom.CPlusPlus17 ||
377 LangTo.CPlusPlus20 != LangFrom.CPlusPlus20) {
378 ++NumLangDialectMismatch;
379 return llvm::make_error<IndexError>(index_error_code::lang_dialect_mismatch,
380 std::string(Unit->getMainFileName()),
381 getLangDescription(LangTo),
382 getLangDescription(LangFrom));
383 }
384
385 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
386 if (const T *ResultDecl = findDefInDeclContext<T>(TU, *LookupName))
387 return importDefinition(ResultDecl, Unit);
388 return llvm::make_error<IndexError>(index_error_code::failed_import);
389}
390
391llvm::Expected<const FunctionDecl *>
393 StringRef CrossTUDir,
394 StringRef IndexName,
395 bool DisplayCTUProgress) {
396 return getCrossTUDefinitionImpl(FD, CrossTUDir, IndexName,
397 DisplayCTUProgress);
398}
399
402 StringRef CrossTUDir,
403 StringRef IndexName,
404 bool DisplayCTUProgress) {
405 return getCrossTUDefinitionImpl(VD, CrossTUDir, IndexName,
406 DisplayCTUProgress);
407}
408
410 SourceLocation Loc) {
411 switch (IE.getCode()) {
414 // If the external def-map refers to source files, you must provide an
415 // invocation list file. Otherwise, CTU does not work at all, so you should
416 // check your build and analysis configuration.
417 Context.getDiagnostics().Report(Loc, diag::err_ctu_error_opening)
418 << IE.getFileName();
419 return;
420
422 Context.getDiagnostics().Report(Loc, diag::err_extdefmap_parsing)
423 << IE.getFileName() << IE.getLineNum();
424 return;
425
427 Context.getDiagnostics().Report(Loc, diag::err_multiple_def_index)
428 << IE.getLineNum();
429 return;
430
432 Context.getDiagnostics().Report(Loc, diag::warn_ctu_incompat_triple)
433 << IE.getFileName() << IE.getConfigToName() << IE.getConfigFromName();
434 return;
435
437 // Ignore missing definitions because it is very common to have some symbols
438 // defined outside of the analysis scope: they may be defined in 3-rd party
439 // and standard libraries, generated code, and files excluded from the
440 // analysis.
441 // Even ignoring it with Ignored diagnostic might generate too much traffic.
442 return;
443
446 // Not clear what happened exactly, but the outcome is a missing definition
447 // This is not a big deal, and is expected since ASTImporter is incomplete.
448 Context.getDiagnostics().Report(Loc, diag::warn_ctu_import_failure)
449 << Category->message(static_cast<int>(IE.getCode()));
450 return;
451
453 // This is unlikely, so it is worth looking into, hence an error.
455 // This is suspicious, since the external AST is mentioned in the external
456 // defmap, so it should exist.
457 Context.getDiagnostics().Report(Loc, diag::err_ctu_import_failure)
458 << Category->message(static_cast<int>(IE.getCode()));
459 return;
460
462 // This is expected. It is still useful to be aware of, but it is normal
463 // operation. Emit the remark only once to avoid noise.
464 if (!HasEmittedLoadThresholdRemark) {
465 HasEmittedLoadThresholdRemark = true;
466 Context.getDiagnostics().Report(
467 Loc, diag::remark_ctu_import_threshold_reached);
468 }
469 return;
470
473 // Similar to target triple mismatch.
474 Context.getDiagnostics().Report(Loc, diag::warn_ctu_incompat_lang)
475 << IE.getFileName() << IE.getConfigToName() << IE.getConfigFromName();
476 return;
477
480 // Without parsable invocation list, CTU cannot function.
481 Context.getDiagnostics().Report(Loc, diag::err_invlist_parsing)
482 << IE.getFileName() << IE.getLineNum();
483 return;
484
486 // For automatically generated invocation lists, it is common to list
487 // multiple invocations, if a file is compiled in multiple contexts. No need
488 // to block CTU because of this.
489 Context.getDiagnostics().Report(Loc, diag::warn_multiple_entries_invlist)
490 << IE.getFileName();
491 return;
492
494 // Some files might be missing in the invocation list. It is sad but not
495 // fatal, and CTU can take advantage of the definitions in files with known
496 // invocations.
497 Context.getDiagnostics().Report(Loc, diag::warn_invlist_missing_file)
498 << IE.getFileName();
499 return;
500
502 llvm_unreachable("Success is not an error.");
503 return;
504 }
505 llvm_unreachable("Unrecognized index_error_code.");
506}
507
508CrossTranslationUnitContext::ASTUnitStorage::ASTUnitStorage(
510 : Loader(CI, CI.getAnalyzerOpts().CTUDir,
511 CI.getAnalyzerOpts().CTUInvocationList),
512 LoadGuard(CI.getASTContext().getLangOpts().CPlusPlus
513 ? CI.getAnalyzerOpts().CTUImportCppThreshold
514 : CI.getAnalyzerOpts().CTUImportThreshold) {}
515
517CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFile(
518 StringRef FileName, bool DisplayCTUProgress) {
519 // Try the cache first.
520 auto ASTCacheEntry = FileASTUnitMap.find(FileName);
521 if (ASTCacheEntry == FileASTUnitMap.end()) {
522
523 // Do not load if the limit is reached.
524 if (!LoadGuard) {
525 ++NumASTLoadThresholdReached;
526 return llvm::make_error<IndexError>(
528 }
529
530 auto LoadAttempt = Loader.load(FileName);
531
532 if (!LoadAttempt)
533 return LoadAttempt.takeError();
534
535 std::unique_ptr<ASTUnit> LoadedUnit = std::move(LoadAttempt.get());
536
537 // Need the raw pointer and the unique_ptr as well.
538 ASTUnit *Unit = LoadedUnit.get();
539
540 // Update the cache.
541 FileASTUnitMap[FileName] = std::move(LoadedUnit);
542
543 LoadGuard.indicateLoadSuccess();
544
545 if (DisplayCTUProgress)
546 llvm::errs() << "CTU loaded AST file: " << FileName << "\n";
547
548 return Unit;
549
550 } else {
551 // Found in the cache.
552 return ASTCacheEntry->second.get();
553 }
554}
555
556llvm::Expected<ASTUnit *>
557CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFunction(
558 StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName,
559 bool DisplayCTUProgress) {
560 // Try the cache first.
561 auto ASTCacheEntry = NameASTUnitMap.find(FunctionName);
562 if (ASTCacheEntry == NameASTUnitMap.end()) {
563 // Load the ASTUnit from the pre-dumped AST file specified by ASTFileName.
564
565 // Ensure that the Index is loaded, as we need to search in it.
566 if (llvm::Error IndexLoadError =
567 ensureCTUIndexLoaded(CrossTUDir, IndexName))
568 return std::move(IndexLoadError);
569
570 // Check if there is an entry in the index for the function.
571 auto It = NameFileMap.find(FunctionName);
572 if (It == NameFileMap.end()) {
573 ++NumNotInOtherTU;
574 return llvm::make_error<IndexError>(index_error_code::missing_definition);
575 }
576
577 // Search in the index for the filename where the definition of FunctionName
578 // resides.
579 if (llvm::Expected<ASTUnit *> FoundForFile =
580 getASTUnitForFile(It->second, DisplayCTUProgress)) {
581
582 // Update the cache.
583 NameASTUnitMap[FunctionName] = *FoundForFile;
584 return *FoundForFile;
585
586 } else {
587 return FoundForFile.takeError();
588 }
589 } else {
590 // Found in the cache.
591 return ASTCacheEntry->second;
592 }
593}
594
595llvm::Expected<std::string>
596CrossTranslationUnitContext::ASTUnitStorage::getFileForFunction(
597 StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName) {
598 if (llvm::Error IndexLoadError = ensureCTUIndexLoaded(CrossTUDir, IndexName))
599 return std::move(IndexLoadError);
600 return NameFileMap[FunctionName];
601}
602
603llvm::Error CrossTranslationUnitContext::ASTUnitStorage::ensureCTUIndexLoaded(
604 StringRef CrossTUDir, StringRef IndexName) {
605 // Dont initialize if the map is filled.
606 if (!NameFileMap.empty())
607 return llvm::Error::success();
608
609 // Get the absolute path to the index file.
610 SmallString<256> IndexFile = CrossTUDir;
611 if (llvm::sys::path::is_absolute(IndexName))
612 IndexFile = IndexName;
613 else
614 llvm::sys::path::append(IndexFile, IndexName);
615
616 if (auto IndexMapping = parseCrossTUIndex(IndexFile)) {
617 // Initialize member map.
618 NameFileMap = *IndexMapping;
619 return llvm::Error::success();
620 } else {
621 // Error while parsing CrossTU index file.
622 return IndexMapping.takeError();
623 };
624}
625
627 StringRef LookupName, StringRef CrossTUDir, StringRef IndexName,
628 bool DisplayCTUProgress) {
629 // FIXME: The current implementation only supports loading decls with
630 // a lookup name from a single translation unit. If multiple
631 // translation units contains decls with the same lookup name an
632 // error will be returned.
633
634 // Try to get the value from the heavily cached storage.
635 llvm::Expected<ASTUnit *> Unit = ASTStorage.getASTUnitForFunction(
636 LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
637
638 if (!Unit)
639 return Unit.takeError();
640
641 // Check whether the backing pointer of the Expected is a nullptr.
642 if (!*Unit)
643 return llvm::make_error<IndexError>(
645
646 return Unit;
647}
648
649CrossTranslationUnitContext::ASTLoader::ASTLoader(
650 CompilerInstance &CI, StringRef CTUDir, StringRef InvocationListFilePath)
651 : CI(CI), CTUDir(CTUDir), InvocationListFilePath(InvocationListFilePath) {}
652
653CrossTranslationUnitContext::LoadResultTy
654CrossTranslationUnitContext::ASTLoader::load(StringRef Identifier) {
656 if (llvm::sys::path::is_absolute(Identifier, PathStyle)) {
657 Path = Identifier;
658 } else {
659 Path = CTUDir;
660 llvm::sys::path::append(Path, PathStyle, Identifier);
661 }
662
663 // The path is stored in the InvocationList member in posix style. To
664 // successfully lookup an entry based on filepath, it must be converted.
665 llvm::sys::path::native(Path, PathStyle);
666
667 // Normalize by removing relative path components.
668 llvm::sys::path::remove_dots(Path, /*remove_dot_dot*/ true, PathStyle);
669
670 if (Path.ends_with(".ast"))
671 return loadFromDump(Path);
672 else
673 return loadFromSource(Path);
674}
675
676CrossTranslationUnitContext::LoadResultTy
677CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) {
678 auto DiagOpts = std::make_shared<DiagnosticOptions>();
679 TextDiagnosticPrinter *DiagClient =
680 new TextDiagnosticPrinter(llvm::errs(), *DiagOpts);
681 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(
682 DiagnosticIDs::create(), *DiagOpts, DiagClient);
684 ASTDumpPath, CI.getPCHContainerOperations()->getRawReader(),
685 ASTUnit::LoadEverything, CI.getVirtualFileSystemPtr(), DiagOpts, Diags,
686 CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
687}
688
689/// Load the AST from a source-file, which is supposed to be located inside the
690/// YAML formatted invocation list file under the filesystem path specified by
691/// \p InvocationList. The invocation list should contain absolute paths.
692/// \p SourceFilePath is the absolute path of the source file that contains the
693/// function definition the analysis is looking for. The Index is built by the
694/// \p clang-extdef-mapping tool, which is also supposed to be generating
695/// absolute paths.
696///
697/// Proper diagnostic emission requires absolute paths, so even if a future
698/// change introduces the handling of relative paths, this must be taken into
699/// consideration.
700CrossTranslationUnitContext::LoadResultTy
701CrossTranslationUnitContext::ASTLoader::loadFromSource(
702 StringRef SourceFilePath) {
703
704 if (llvm::Error InitError = lazyInitInvocationList())
705 return std::move(InitError);
706 assert(InvocationList);
707
708 auto Invocation = InvocationList->find(SourceFilePath);
709 if (Invocation == InvocationList->end())
710 return llvm::make_error<IndexError>(
712 SourceFilePath.str());
713
714 const InvocationListTy::mapped_type &InvocationCommand = Invocation->second;
715
716 SmallVector<const char *, 32> CommandLineArgs(InvocationCommand.size());
717 std::transform(InvocationCommand.begin(), InvocationCommand.end(),
718 CommandLineArgs.begin(),
719 [](auto &&CmdPart) { return CmdPart.c_str(); });
720
721 auto DiagOpts = std::make_shared<DiagnosticOptions>(CI.getDiagnosticOpts());
722 auto *DiagClient = new ForwardingDiagnosticConsumer{CI.getDiagnosticClient()};
723 IntrusiveRefCntPtr<DiagnosticIDs> DiagID{
724 CI.getDiagnostics().getDiagnosticIDs()};
725 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(DiagID, *DiagOpts,
726 DiagClient);
727
728 // This runs the driver which isn't expected to be free of sandbox violations.
729 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
731 CommandLineArgs.begin(), (CommandLineArgs.end()),
732 CI.getPCHContainerOperations(), DiagOpts, Diags,
733 CI.getHeaderSearchOpts().ResourceDir);
734}
735
736llvm::Expected<InvocationListTy>
737parseInvocationList(StringRef FileContent, llvm::sys::path::Style PathStyle,
738 StringRef FilePath) {
739 InvocationListTy InvocationList;
740
741 /// LLVM YAML parser is used to extract information from invocation list file.
742 llvm::SourceMgr SM;
743 llvm::yaml::Stream InvocationFile(FileContent, SM);
744
745 auto GetLine = [&SM](const llvm::yaml::Node *N) -> int {
746 return N ? SM.FindLineNumber(N->getSourceRange().Start) : 0;
747 };
748 auto WrongFormatError = [&](const llvm::yaml::Node *N) {
749 return llvm::make_error<IndexError>(
751 GetLine(N));
752 };
753
754 /// Only the first document is processed.
755 llvm::yaml::document_iterator FirstInvocationFile = InvocationFile.begin();
756
757 /// There has to be at least one document available.
758 if (FirstInvocationFile == InvocationFile.end())
759 return llvm::make_error<IndexError>(
761
762 llvm::yaml::Node *DocumentRoot = FirstInvocationFile->getRoot();
763 if (!DocumentRoot)
764 return llvm::make_error<IndexError>(
766
767 /// According to the format specified the document must be a mapping, where
768 /// the keys are paths to source files, and values are sequences of invocation
769 /// parts.
770 auto *Mappings = dyn_cast<llvm::yaml::MappingNode>(DocumentRoot);
771 if (!Mappings)
772 return WrongFormatError(DocumentRoot);
773
774 for (auto &NextMapping : *Mappings) {
775 /// The keys should be strings, which represent a source-file path.
776 auto *Key =
777 dyn_cast_if_present<llvm::yaml::ScalarNode>(NextMapping.getKey());
778 if (!Key)
779 return WrongFormatError(NextMapping.getKey());
780
781 SmallString<32> ValueStorage;
782 StringRef SourcePath = Key->getValue(ValueStorage);
783
784 // Store paths with PathStyle directory separator.
785 SmallString<32> NativeSourcePath(SourcePath);
786 llvm::sys::path::native(NativeSourcePath, PathStyle);
787
788 StringRef InvocationKey = NativeSourcePath;
789
790 if (InvocationList.contains(InvocationKey))
791 return llvm::make_error<IndexError>(
793
794 /// The values should be sequences of strings, each representing a part of
795 /// the invocation.
796 auto *Args =
797 dyn_cast_if_present<llvm::yaml::SequenceNode>(NextMapping.getValue());
798 if (!Args)
799 return WrongFormatError(NextMapping.getValue());
800
801 for (auto &Arg : *Args) {
802 auto *CmdString = dyn_cast<llvm::yaml::ScalarNode>(&Arg);
803 if (!CmdString)
804 return WrongFormatError(&Arg);
805 /// Every conversion starts with an empty working storage, as it is not
806 /// clear if this is a requirement of the YAML parser.
807 ValueStorage.clear();
808 InvocationList[InvocationKey].emplace_back(
809 CmdString->getValue(ValueStorage));
810 }
811
812 if (InvocationList[InvocationKey].empty())
813 return WrongFormatError(Key);
814 }
815
816 return InvocationList;
817}
818
819llvm::Error CrossTranslationUnitContext::ASTLoader::lazyInitInvocationList() {
820 /// Lazily initialize the invocation list member used for on-demand parsing.
821 if (InvocationList)
822 return llvm::Error::success();
823 if (PreviousError)
824 return llvm::make_error<IndexError>(*PreviousError);
825
826 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileContent =
827 CI.getVirtualFileSystem().getBufferForFile(InvocationListFilePath);
828 if (!FileContent) {
829 PreviousError = IndexError(index_error_code::invocation_list_file_not_found,
830 InvocationListFilePath.str());
831 return llvm::make_error<IndexError>(*PreviousError);
832 }
833 std::unique_ptr<llvm::MemoryBuffer> ContentBuffer = std::move(*FileContent);
834 assert(ContentBuffer && "If no error was produced after loading, the pointer "
835 "should not be nullptr.");
836
838 ContentBuffer->getBuffer(), PathStyle, InvocationListFilePath);
839
840 if (!ExpectedInvocationList) {
841 llvm::handleAllErrors(
842 ExpectedInvocationList.takeError(),
843 [this](const IndexError &E) { this->PreviousError = E; });
844 return llvm::make_error<IndexError>(*PreviousError);
845 }
846
847 InvocationList = *ExpectedInvocationList;
848
849 return llvm::Error::success();
850}
851
852template <typename T>
853llvm::Expected<const T *>
854CrossTranslationUnitContext::importDefinitionImpl(const T *D, ASTUnit *Unit) {
855 assert(hasBodyOrInit(D) && "Decls to be imported should have body or init.");
856
857 assert(&D->getASTContext() == &Unit->getASTContext() &&
858 "ASTContext of Decl and the unit should match.");
859 ASTImporter &Importer = getOrCreateASTImporter(Unit);
860
861 auto ToDeclOrError = Importer.Import(D);
862 if (!ToDeclOrError) {
863 handleAllErrors(ToDeclOrError.takeError(), [&](const ASTImportError &IE) {
864 switch (IE.Error) {
865 case ASTImportError::NameConflict:
866 ++NumNameConflicts;
867 break;
868 case ASTImportError::UnsupportedConstruct:
869 ++NumUnsupportedNodeFound;
870 break;
871 case ASTImportError::Unknown:
872 llvm_unreachable("Unknown import error happened.");
873 break;
874 }
875 });
876 return llvm::make_error<IndexError>(index_error_code::failed_import);
877 }
878 auto *ToDecl = cast<T>(*ToDeclOrError);
879 assert(hasBodyOrInit(ToDecl) && "Imported Decl should have body or init.");
880 ++NumGetCTUSuccess;
881
882 // Parent map is invalidated after changing the AST.
883 ToDecl->getASTContext().getParentMapContext().clear();
884
885 return ToDecl;
886}
887
888llvm::Expected<const FunctionDecl *>
890 ASTUnit *Unit) {
891 return importDefinitionImpl(FD, Unit);
892}
893
896 ASTUnit *Unit) {
897 return importDefinitionImpl(VD, Unit);
898}
899
900void CrossTranslationUnitContext::lazyInitImporterSharedSt(
901 TranslationUnitDecl *ToTU) {
902 if (!ImporterSharedSt)
903 ImporterSharedSt = std::make_shared<ASTImporterSharedState>(*ToTU);
904}
905
907CrossTranslationUnitContext::getOrCreateASTImporter(ASTUnit *Unit) {
908 ASTContext &From = Unit->getASTContext();
909
910 auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl());
911 if (I != ASTUnitImporterMap.end())
912 return *I->second;
913 lazyInitImporterSharedSt(Context.getTranslationUnitDecl());
914 ASTImporter *NewImporter = new ASTImporter(
915 Context, Context.getSourceManager().getFileManager(), From,
916 From.getSourceManager().getFileManager(), false, ImporterSharedSt);
917 ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter);
918 return *NewImporter;
919}
920
921std::optional<clang::MacroExpansionContext>
923 const clang::SourceLocation &ToLoc) const {
924 // FIXME: Implement: Record such a context for every imported ASTUnit; lookup.
925 return std::nullopt;
926}
927
929 if (!ImporterSharedSt)
930 return false;
931 return ImporterSharedSt->isNewDecl(const_cast<Decl *>(ToDecl));
932}
933
935 if (!ImporterSharedSt)
936 return false;
937 return static_cast<bool>(
938 ImporterSharedSt->getImportDeclErrorIfAny(const_cast<Decl *>(ToDecl)));
939}
940
941} // namespace cross_tu
942} // namespace clang
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges")
#define SM(sm)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
SourceManager & getSourceManager()
Definition ASTContext.h:859
TranslationUnitDecl * getTranslationUnitDecl() const
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition ASTImporter.h:62
Utility class for loading a ASTContext from an AST file.
Definition ASTUnit.h:93
static std::unique_ptr< ASTUnit > LoadFromASTFile(StringRef Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, std::shared_ptr< DiagnosticOptions > DiagOpts, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts=nullptr, bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, bool AllowASTWithCompilerErrors=false, bool UserFilesAreVolatile=false)
Create a ASTUnit from an AST file.
Definition ASTUnit.cpp:692
@ LoadEverything
Load everything, including Sema.
Definition ASTUnit.h:716
const ASTContext & getASTContext() const
Definition ASTUnit.h:451
unsigned ShouldEmitErrorsOnInvalidConfigValue
bool isConstQualified() const
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
AnalyzerOptions & getAnalyzerOpts()
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1462
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2386
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
static llvm::IntrusiveRefCntPtr< DiagnosticIDs > create()
Represents a function declaration or definition.
Definition Decl.h:2015
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3201
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool isTrivialType(const ASTContext &Context) const
Return true if this is a trivial type per (C++0x [basic.types]p9)
Definition Type.cpp:2805
Encodes a location in the source.
FileManager & getFileManager() const
The top declaration context.
Definition Decl.h:105
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1373
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...
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 ...
void emitCrossTUDiagnostics(const IndexError &IE, SourceLocation Loc)
Emit diagnostics for the user for potential configuration errors.
static std::optional< std::string > getLookupName(const Decl *D)
Get a name to identify a decl.
std::optional< clang::MacroExpansionContext > getMacroExpansionContextForSourceLocation(const clang::SourceLocation &ToLoc) const
Returns the MacroExpansionContext for the imported TU to which the given source-location corresponds.
bool hasError(const Decl *ToDecl) const
Returns true if the given Decl is mapped (or created) during an import but there was an unrecoverable...
bool isImportedAsNew(const Decl *ToDecl) const
Returns true if the given Decl is newly created during the import.
llvm::Expected< ASTUnit * > loadExternalAST(StringRef LookupName, StringRef CrossTUDir, StringRef IndexName, bool DisplayCTUProgress=false)
This function loads a definition from an external AST file.
index_error_code getCode() const
std::error_code convertToErrorCode() const override
void log(raw_ostream &OS) const override
std::string getConfigFromName() const
std::string getConfigToName() const
Defines the clang::TargetInfo interface.
bool shouldImport(const VarDecl *VD, const ASTContext &ACtx)
Returns true if it makes sense to import a foreign variable definition.
static std::string getLangDescription(const LangOptions &LO)
Returns a human-readable language/dialect description for diagnostics.
llvm::Expected< llvm::StringMap< std::string > > parseCrossTUIndex(StringRef IndexPath)
This function parses an index file that determines which translation unit contains which definition.
std::string createCrossTUIndexString(const llvm::StringMap< std::string > &Index)
static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD)
static bool parseCrossTUIndexItem(StringRef LineRef, StringRef &LookupName, StringRef &FilePath)
Parse one line of the input CTU index file.
llvm::Expected< InvocationListTy > parseInvocationList(StringRef FileContent, llvm::sys::path::Style PathStyle=llvm::sys::path::Style::posix, StringRef FilePath="")
Parse the YAML formatted invocation list file content FileContent.
llvm::StringMap< llvm::SmallVector< std::string, 32 > > InvocationListTy
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
@ CPlusPlus
std::unique_ptr< ASTUnit > CreateASTUnitFromCommandLine(const char **ArgBegin, const char **ArgEnd, std::shared_ptr< PCHContainerOperations > PCHContainerOps, std::shared_ptr< DiagnosticOptions > DiagOpts, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, StringRef ResourceFilesPath, bool StorePreamblesInMemory=false, StringRef PreambleStoragePath=StringRef(), bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, ArrayRef< ASTUnit::RemappedFile > RemappedFiles={}, bool RemappedFilesKeepOriginalName=true, unsigned PrecompilePreambleAfterNParses=0, TranslationUnitKind TUKind=TU_Complete, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool AllowPCHWithCompilerErrors=false, SkipFunctionBodiesScope SkipFunctionBodies=SkipFunctionBodiesScope::None, bool SingleFileParse=false, bool UserFilesAreVolatile=false, bool ForSerialization=false, bool RetainExcludedConditionalBlocks=false, std::optional< StringRef > ModuleFormat=std::nullopt, std::unique_ptr< ASTUnit > *ErrAST=nullptr, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Create an ASTUnit from a vector of command line arguments, which must specify exactly one source file...
@ Result
The result type of a method or function.
Definition TypeBase.h:905
U cast(CodeGen::Address addr)
Definition Address.h:327