blob: 7391d7132daf4bcd91c4ac4c8a22e5245969b4c6 [file] [log] [blame]
Gabor Horvathe350b0a2017-09-22 11:11:01 +00001//===--- CrossTranslationUnit.cpp - -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Gabor Horvathe350b0a2017-09-22 11:11:01 +00006//
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"
Gabor Horvathe350b0a2017-09-22 11:11:01 +000019#include "clang/Frontend/TextDiagnosticPrinter.h"
20#include "clang/Index/USRGeneration.h"
21#include "llvm/ADT/Triple.h"
Gabor Marton700a29a2018-12-07 11:55:22 +000022#include "llvm/ADT/Statistic.h"
Gabor Horvathe350b0a2017-09-22 11:11:01 +000023#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
30namespace clang {
31namespace cross_tu {
32
33namespace {
Gabor Marton32aff2e2018-12-07 16:32:43 +000034
Gabor Marton700a29a2018-12-07 11:55:22 +000035#define DEBUG_TYPE "CrossTranslationUnit"
36STATISTIC(NumGetCTUCalled, "The # of getCTUDefinition function called");
37STATISTIC(
38 NumNotInOtherTU,
39 "The # of getCTUDefinition called but the function is not in any other TU");
40STATISTIC(NumGetCTUSuccess,
41 "The # of getCTUDefinition successfully returned the "
42 "requested function's body");
Gabor Marton8ab8a602019-06-28 08:08:51 +000043STATISTIC(NumUnsupportedNodeFound, "The # of imports when the ASTImporter "
44 "encountered an unsupported AST Node");
45STATISTIC(NumNameConflicts, "The # of imports when the ASTImporter "
46 "encountered an ODR error");
Gabor Marton32aff2e2018-12-07 16:32:43 +000047STATISTIC(NumTripleMismatch, "The # of triple mismatches");
48STATISTIC(NumLangMismatch, "The # of language mismatches");
Gabor Martona006b802019-02-28 15:24:59 +000049STATISTIC(NumLangDialectMismatch, "The # of language dialect mismatches");
Endre Fulop0752d122019-07-08 12:37:10 +000050STATISTIC(NumASTLoadThresholdReached,
51 "The # of ASTs not loaded because of threshold");
Gabor Marton32aff2e2018-12-07 16:32:43 +000052
53// Same as Triple's equality operator, but we check a field only if that is
54// known in both instances.
55bool 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}
Gabor Marton700a29a2018-12-07 11:55:22 +000081
Gabor Horvathe350b0a2017-09-22 11:11:01 +000082// FIXME: This class is will be removed after the transition to llvm::Error.
83class IndexErrorCategory : public std::error_category {
84public:
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.";
Gabor Marton32aff2e2018-12-07 16:32:43 +0000105 case index_error_code::triple_mismatch:
106 return "Triple mismatch";
107 case index_error_code::lang_mismatch:
108 return "Language mismatch";
Gabor Martona006b802019-02-28 15:24:59 +0000109 case index_error_code::lang_dialect_mismatch:
110 return "Language dialect mismatch";
Endre Fulop0752d122019-07-08 12:37:10 +0000111 case index_error_code::load_threshold_reached:
112 return "Load threshold reached";
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000113 }
114 llvm_unreachable("Unrecognized index_error_code.");
115 }
116};
117
118static llvm::ManagedStatic<IndexErrorCategory> Category;
119} // end anonymous namespace
120
121char IndexError::ID;
122
123void IndexError::log(raw_ostream &OS) const {
124 OS << Category->message(static_cast<int>(Code)) << '\n';
125}
126
127std::error_code IndexError::convertToErrorCode() const {
128 return std::error_code(static_cast<int>(Code), *Category);
129}
130
131llvm::Expected<llvm::StringMap<std::string>>
132parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir) {
Rafael Stahl8c487052019-01-10 17:44:04 +0000133 std::ifstream ExternalMapFile(IndexPath);
134 if (!ExternalMapFile)
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000135 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;
Rafael Stahl8c487052019-01-10 17:44:04 +0000141 while (std::getline(ExternalMapFile, Line)) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000142 const size_t Pos = Line.find(" ");
143 if (Pos > 0 && Pos != std::string::npos) {
144 StringRef LineRef{Line};
Rafael Stahl8c487052019-01-10 17:44:04 +0000145 StringRef LookupName = LineRef.substr(0, Pos);
146 if (Result.count(LookupName))
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000147 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;
Gabor Horvath724beac2017-10-27 12:53:37 +0000151 llvm::sys::path::append(FilePath, FileName);
Rafael Stahl8c487052019-01-10 17:44:04 +0000152 Result[LookupName] = FilePath.str().str();
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000153 } 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
161std::string
162createCrossTUIndexString(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
Rafael Stahl850361f2019-04-23 11:04:41 +0000169bool 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
179static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD) {
180 return D->hasBody(DefD);
181}
182static bool hasBodyOrInit(const VarDecl *D, const VarDecl *&DefD) {
183 return D->getAnyInitializer(DefD);
184}
185template <typename T> static bool hasBodyOrInit(const T *D) {
186 const T *Unused;
187 return hasBodyOrInit(D, Unused);
188}
189
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000190CrossTranslationUnitContext::CrossTranslationUnitContext(CompilerInstance &CI)
Balazs Keri4b9d2002019-08-12 07:15:29 +0000191 : Context(CI.getASTContext()), ASTStorage(CI) {}
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000192
193CrossTranslationUnitContext::~CrossTranslationUnitContext() {}
194
Balazs Keri4e790972019-08-06 12:10:16 +0000195llvm::Optional<std::string>
196CrossTranslationUnitContext::getLookupName(const NamedDecl *ND) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000197 SmallString<128> DeclUSR;
Rafael Stahl850361f2019-04-23 11:04:41 +0000198 bool Ret = index::generateUSRForDecl(ND, DeclUSR);
Balazs Keri4e790972019-08-06 12:10:16 +0000199 if (Ret)
200 return {};
201 return std::string(DeclUSR.str());
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000202}
203
Rafael Stahl850361f2019-04-23 11:04:41 +0000204/// Recursively visits the decls of a DeclContext, and returns one with the
205/// given USR.
206template <typename T>
207const T *
208CrossTranslationUnitContext::findDefInDeclContext(const DeclContext *DC,
209 StringRef LookupName) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000210 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)
Rafael Stahl850361f2019-04-23 11:04:41 +0000214 if (const auto *ND = findDefInDeclContext<T>(SubDC, LookupName))
215 return ND;
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000216
Rafael Stahl850361f2019-04-23 11:04:41 +0000217 const auto *ND = dyn_cast<T>(D);
218 const T *ResultDecl;
219 if (!ND || !hasBodyOrInit(ND, ResultDecl))
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000220 continue;
Balazs Keri4e790972019-08-06 12:10:16 +0000221 llvm::Optional<std::string> ResultLookupName = getLookupName(ResultDecl);
222 if (!ResultLookupName || *ResultLookupName != LookupName)
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000223 continue;
224 return ResultDecl;
225 }
226 return nullptr;
227}
228
Rafael Stahl850361f2019-04-23 11:04:41 +0000229template <typename T>
230llvm::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!");
Gabor Marton700a29a2018-12-07 11:55:22 +0000236 ++NumGetCTUCalled;
Balazs Keri4e790972019-08-06 12:10:16 +0000237 const llvm::Optional<std::string> LookupName = getLookupName(D);
238 if (!LookupName)
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000239 return llvm::make_error<IndexError>(
240 index_error_code::failed_to_generate_usr);
Endre Fulop0492fd42019-08-05 11:06:41 +0000241 llvm::Expected<ASTUnit *> ASTUnitOrError =
Balazs Keri4e790972019-08-06 12:10:16 +0000242 loadExternalAST(*LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000243 if (!ASTUnitOrError)
244 return ASTUnitOrError.takeError();
245 ASTUnit *Unit = *ASTUnitOrError;
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000246 assert(&Unit->getFileManager() ==
247 &Unit->getASTContext().getSourceManager().getFileManager());
248
Gabor Marton32aff2e2018-12-07 16:32:43 +0000249 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();
Gabor Martona006b802019-02-28 15:24:59 +0000267
Gabor Marton32aff2e2018-12-07 16:32:43 +0000268 // 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
Gabor Martona006b802019-02-28 15:24:59 +0000275 // 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
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000297 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
Balazs Keri4e790972019-08-06 12:10:16 +0000298 if (const T *ResultDecl = findDefInDeclContext<T>(TU, *LookupName))
Balazs Kerid22f8772019-07-24 10:16:37 +0000299 return importDefinition(ResultDecl, Unit);
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000300 return llvm::make_error<IndexError>(index_error_code::failed_import);
301}
302
Rafael Stahl850361f2019-04-23 11:04:41 +0000303llvm::Expected<const FunctionDecl *>
304CrossTranslationUnitContext::getCrossTUDefinition(const FunctionDecl *FD,
305 StringRef CrossTUDir,
306 StringRef IndexName,
307 bool DisplayCTUProgress) {
308 return getCrossTUDefinitionImpl(FD, CrossTUDir, IndexName,
309 DisplayCTUProgress);
310}
311
312llvm::Expected<const VarDecl *>
313CrossTranslationUnitContext::getCrossTUDefinition(const VarDecl *VD,
314 StringRef CrossTUDir,
315 StringRef IndexName,
316 bool DisplayCTUProgress) {
317 return getCrossTUDefinitionImpl(VD, CrossTUDir, IndexName,
318 DisplayCTUProgress);
319}
320
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000321void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE) {
322 switch (IE.getCode()) {
323 case index_error_code::missing_index_file:
Richard Trieu0f25c742018-12-14 03:35:10 +0000324 Context.getDiagnostics().Report(diag::err_ctu_error_opening)
325 << IE.getFileName();
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000326 break;
327 case index_error_code::invalid_index_format:
Rafael Stahl8c487052019-01-10 17:44:04 +0000328 Context.getDiagnostics().Report(diag::err_extdefmap_parsing)
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000329 << IE.getFileName() << IE.getLineNum();
Simon Pilgrim554ab532017-09-24 15:17:46 +0000330 break;
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000331 case index_error_code::multiple_definitions:
332 Context.getDiagnostics().Report(diag::err_multiple_def_index)
333 << IE.getLineNum();
334 break;
Gabor Marton32aff2e2018-12-07 16:32:43 +0000335 case index_error_code::triple_mismatch:
336 Context.getDiagnostics().Report(diag::warn_ctu_incompat_triple)
337 << IE.getFileName() << IE.getTripleToName() << IE.getTripleFromName();
338 break;
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000339 default:
340 break;
341 }
342}
343
Endre Fulop0492fd42019-08-05 11:06:41 +0000344CrossTranslationUnitContext::ASTFileLoader::ASTFileLoader(
345 const CompilerInstance &CI)
346 : CI(CI) {}
347
348std::unique_ptr<ASTUnit>
349CrossTranslationUnitContext::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
363CrossTranslationUnitContext::ASTUnitStorage::ASTUnitStorage(
364 const CompilerInstance &CI)
Balazs Keri4b9d2002019-08-12 07:15:29 +0000365 : FileAccessor(CI), LoadGuard(const_cast<CompilerInstance &>(CI)
366 .getAnalyzerOpts()
367 ->CTUImportThreshold) {}
Endre Fulop0492fd42019-08-05 11:06:41 +0000368
369llvm::Expected<ASTUnit *>
Balazs Keri4b9d2002019-08-12 07:15:29 +0000370CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFile(
371 StringRef FileName, bool DisplayCTUProgress) {
Endre Fulop0492fd42019-08-05 11:06:41 +0000372 // Try the cache first.
373 auto ASTCacheEntry = FileASTUnitMap.find(FileName);
374 if (ASTCacheEntry == FileASTUnitMap.end()) {
Balazs Keri4b9d2002019-08-12 07:15:29 +0000375
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
Endre Fulop0492fd42019-08-05 11:06:41 +0000383 // 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.
Balazs Keri4b9d2002019-08-12 07:15:29 +0000387 ASTUnit *Unit = LoadedUnit.get();
Endre Fulop0492fd42019-08-05 11:06:41 +0000388
389 // Update the cache.
390 FileASTUnitMap[FileName] = std::move(LoadedUnit);
Balazs Keri4b9d2002019-08-12 07:15:29 +0000391
392 LoadGuard.indicateLoadSuccess();
393
394 if (DisplayCTUProgress)
395 llvm::errs() << "CTU loaded AST file: " << FileName << "\n";
396
Endre Fulop0492fd42019-08-05 11:06:41 +0000397 return Unit;
398
399 } else {
400 // Found in the cache.
401 return ASTCacheEntry->second.get();
402 }
403}
404
405llvm::Expected<ASTUnit *>
406CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFunction(
Balazs Keri4b9d2002019-08-12 07:15:29 +0000407 StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName,
408 bool DisplayCTUProgress) {
Endre Fulop0492fd42019-08-05 11:06:41 +0000409 // 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 =
Balazs Keri4b9d2002019-08-12 07:15:29 +0000428 getASTUnitForFile(NameFileMap[FunctionName], DisplayCTUProgress)) {
Endre Fulop0492fd42019-08-05 11:06:41 +0000429
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
443llvm::Expected<std::string>
444CrossTranslationUnitContext::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
451llvm::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
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000474llvm::Expected<ASTUnit *> CrossTranslationUnitContext::loadExternalAST(
Gabor Marton9419eb42018-12-07 14:56:02 +0000475 StringRef LookupName, StringRef CrossTUDir, StringRef IndexName,
476 bool DisplayCTUProgress) {
Rafael Stahl850361f2019-04-23 11:04:41 +0000477 // FIXME: The current implementation only supports loading decls with
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000478 // a lookup name from a single translation unit. If multiple
Rafael Stahl850361f2019-04-23 11:04:41 +0000479 // translation units contains decls with the same lookup name an
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000480 // error will be returned.
Endre Fulop0752d122019-07-08 12:37:10 +0000481
Endre Fulop0492fd42019-08-05 11:06:41 +0000482 // Try to get the value from the heavily cached storage.
Balazs Keri4b9d2002019-08-12 07:15:29 +0000483 llvm::Expected<ASTUnit *> Unit = ASTStorage.getASTUnitForFunction(
484 LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000485
Gabor Marton30816902019-01-07 14:05:19 +0000486 if (!Unit)
Endre Fulop0492fd42019-08-05 11:06:41 +0000487 return Unit.takeError();
488
489 // Check whether the backing pointer of the Expected is a nullptr.
490 if (!*Unit)
Gabor Marton30816902019-01-07 14:05:19 +0000491 return llvm::make_error<IndexError>(
492 index_error_code::failed_to_get_external_ast);
Endre Fulop0492fd42019-08-05 11:06:41 +0000493
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000494 return Unit;
495}
496
Rafael Stahl850361f2019-04-23 11:04:41 +0000497template <typename T>
498llvm::Expected<const T *>
Balazs Kerid22f8772019-07-24 10:16:37 +0000499CrossTranslationUnitContext::importDefinitionImpl(const T *D, ASTUnit *Unit) {
Rafael Stahl850361f2019-04-23 11:04:41 +0000500 assert(hasBodyOrInit(D) && "Decls to be imported should have body or init.");
Gabor Martonb7f30dd2018-12-07 12:21:43 +0000501
Balazs Kerid22f8772019-07-24 10:16:37 +0000502 assert(&D->getASTContext() == &Unit->getASTContext() &&
503 "ASTContext of Decl and the unit should match.");
504 ASTImporter &Importer = getOrCreateASTImporter(Unit);
505
Gabor Marton5ac6d492019-05-15 10:29:48 +0000506 auto ToDeclOrError = Importer.Import(D);
Balazs Keria1f6b102019-04-08 13:59:15 +0000507 if (!ToDeclOrError) {
508 handleAllErrors(ToDeclOrError.takeError(),
509 [&](const ImportError &IE) {
510 switch (IE.Error) {
511 case ImportError::NameConflict:
Gabor Marton8ab8a602019-06-28 08:08:51 +0000512 ++NumNameConflicts;
Balazs Keria1f6b102019-04-08 13:59:15 +0000513 break;
514 case ImportError::UnsupportedConstruct:
Gabor Marton8ab8a602019-06-28 08:08:51 +0000515 ++NumUnsupportedNodeFound;
Balazs Keria1f6b102019-04-08 13:59:15 +0000516 break;
517 case ImportError::Unknown:
518 llvm_unreachable("Unknown import error happened.");
519 break;
520 }
521 });
Gabor Martonb87251d2018-12-07 16:05:58 +0000522 return llvm::make_error<IndexError>(index_error_code::failed_import);
Balazs Keria1f6b102019-04-08 13:59:15 +0000523 }
Rafael Stahl850361f2019-04-23 11:04:41 +0000524 auto *ToDecl = cast<T>(*ToDeclOrError);
525 assert(hasBodyOrInit(ToDecl) && "Imported Decl should have body or init.");
Gabor Marton700a29a2018-12-07 11:55:22 +0000526 ++NumGetCTUSuccess;
Balazs Keria1f6b102019-04-08 13:59:15 +0000527
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000528 return ToDecl;
529}
530
Rafael Stahl850361f2019-04-23 11:04:41 +0000531llvm::Expected<const FunctionDecl *>
Balazs Kerid22f8772019-07-24 10:16:37 +0000532CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD,
533 ASTUnit *Unit) {
534 return importDefinitionImpl(FD, Unit);
Rafael Stahl850361f2019-04-23 11:04:41 +0000535}
536
537llvm::Expected<const VarDecl *>
Balazs Kerid22f8772019-07-24 10:16:37 +0000538CrossTranslationUnitContext::importDefinition(const VarDecl *VD,
539 ASTUnit *Unit) {
540 return importDefinitionImpl(VD, Unit);
Rafael Stahl850361f2019-04-23 11:04:41 +0000541}
542
Gabor Marton2afbfb62019-07-01 15:37:07 +0000543void CrossTranslationUnitContext::lazyInitImporterSharedSt(
Gabor Marton54058b52018-12-17 13:53:12 +0000544 TranslationUnitDecl *ToTU) {
Gabor Marton2afbfb62019-07-01 15:37:07 +0000545 if (!ImporterSharedSt)
546 ImporterSharedSt = std::make_shared<ASTImporterSharedState>(*ToTU);
Gabor Marton54058b52018-12-17 13:53:12 +0000547}
548
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000549ASTImporter &
Balazs Kerid22f8772019-07-24 10:16:37 +0000550CrossTranslationUnitContext::getOrCreateASTImporter(ASTUnit *Unit) {
551 ASTContext &From = Unit->getASTContext();
552
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000553 auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl());
554 if (I != ASTUnitImporterMap.end())
555 return *I->second;
Gabor Marton2afbfb62019-07-01 15:37:07 +0000556 lazyInitImporterSharedSt(Context.getTranslationUnitDecl());
Ilya Biryukovabc744d2019-07-18 15:43:26 +0000557 ASTImporter *NewImporter = new ASTImporter(
558 Context, Context.getSourceManager().getFileManager(), From,
559 From.getSourceManager().getFileManager(), false, ImporterSharedSt);
Balazs Kerid22f8772019-07-24 10:16:37 +0000560 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 });
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000565 ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter);
566 return *NewImporter;
567}
568
Balazs Kerid22f8772019-07-24 10:16:37 +0000569llvm::Optional<std::pair<SourceLocation, ASTUnit *>>
570CrossTranslationUnitContext::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
Gabor Horvathe350b0a2017-09-22 11:11:01 +0000587} // namespace cross_tu
588} // namespace clang