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(NumTripleMismatch, "The # of triple mismatches");
44 STATISTIC(NumLangMismatch, "The # of language mismatches");
45 
46 // Same as Triple's equality operator, but we check a field only if that is
47 // known in both instances.
48 bool hasEqualKnownFields(const llvm::Triple &Lhs, const llvm::Triple &Rhs) {
49   using llvm::Triple;
50   if (Lhs.getArch() != Triple::UnknownArch &&
51       Rhs.getArch() != Triple::UnknownArch && Lhs.getArch() != Rhs.getArch())
52     return false;
53   if (Lhs.getSubArch() != Triple::NoSubArch &&
54       Rhs.getSubArch() != Triple::NoSubArch &&
55       Lhs.getSubArch() != Rhs.getSubArch())
56     return false;
57   if (Lhs.getVendor() != Triple::UnknownVendor &&
58       Rhs.getVendor() != Triple::UnknownVendor &&
59       Lhs.getVendor() != Rhs.getVendor())
60     return false;
61   if (!Lhs.isOSUnknown() && !Rhs.isOSUnknown() &&
62       Lhs.getOS() != Rhs.getOS())
63     return false;
64   if (Lhs.getEnvironment() != Triple::UnknownEnvironment &&
65       Rhs.getEnvironment() != Triple::UnknownEnvironment &&
66       Lhs.getEnvironment() != Rhs.getEnvironment())
67     return false;
68   if (Lhs.getObjectFormat() != Triple::UnknownObjectFormat &&
69       Rhs.getObjectFormat() != Triple::UnknownObjectFormat &&
70       Lhs.getObjectFormat() != Rhs.getObjectFormat())
71     return false;
72   return true;
73 }
74 
75 // FIXME: This class is will be removed after the transition to llvm::Error.
76 class IndexErrorCategory : public std::error_category {
77 public:
78   const char *name() const noexcept override { return "clang.index"; }
79 
80   std::string message(int Condition) const override {
81     switch (static_cast<index_error_code>(Condition)) {
82     case index_error_code::unspecified:
83       return "An unknown error has occurred.";
84     case index_error_code::missing_index_file:
85       return "The index file is missing.";
86     case index_error_code::invalid_index_format:
87       return "Invalid index file format.";
88     case index_error_code::multiple_definitions:
89       return "Multiple definitions in the index file.";
90     case index_error_code::missing_definition:
91       return "Missing definition from the index file.";
92     case index_error_code::failed_import:
93       return "Failed to import the definition.";
94     case index_error_code::failed_to_get_external_ast:
95       return "Failed to load external AST source.";
96     case index_error_code::failed_to_generate_usr:
97       return "Failed to generate USR.";
98     case index_error_code::triple_mismatch:
99       return "Triple mismatch";
100     case index_error_code::lang_mismatch:
101       return "Language mismatch";
102     }
103     llvm_unreachable("Unrecognized index_error_code.");
104   }
105 };
106 
107 static llvm::ManagedStatic<IndexErrorCategory> Category;
108 } // end anonymous namespace
109 
110 char IndexError::ID;
111 
112 void IndexError::log(raw_ostream &OS) const {
113   OS << Category->message(static_cast<int>(Code)) << '\n';
114 }
115 
116 std::error_code IndexError::convertToErrorCode() const {
117   return std::error_code(static_cast<int>(Code), *Category);
118 }
119 
120 llvm::Expected<llvm::StringMap<std::string>>
121 parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir) {
122   std::ifstream ExternalMapFile(IndexPath);
123   if (!ExternalMapFile)
124     return llvm::make_error<IndexError>(index_error_code::missing_index_file,
125                                         IndexPath.str());
126 
127   llvm::StringMap<std::string> Result;
128   std::string Line;
129   unsigned LineNo = 1;
130   while (std::getline(ExternalMapFile, Line)) {
131     const size_t Pos = Line.find(" ");
132     if (Pos > 0 && Pos != std::string::npos) {
133       StringRef LineRef{Line};
134       StringRef LookupName = LineRef.substr(0, Pos);
135       if (Result.count(LookupName))
136         return llvm::make_error<IndexError>(
137             index_error_code::multiple_definitions, IndexPath.str(), LineNo);
138       StringRef FileName = LineRef.substr(Pos + 1);
139       SmallString<256> FilePath = CrossTUDir;
140       llvm::sys::path::append(FilePath, FileName);
141       Result[LookupName] = FilePath.str().str();
142     } else
143       return llvm::make_error<IndexError>(
144           index_error_code::invalid_index_format, IndexPath.str(), LineNo);
145     LineNo++;
146   }
147   return Result;
148 }
149 
150 std::string
151 createCrossTUIndexString(const llvm::StringMap<std::string> &Index) {
152   std::ostringstream Result;
153   for (const auto &E : Index)
154     Result << E.getKey().str() << " " << E.getValue() << '\n';
155   return Result.str();
156 }
157 
158 CrossTranslationUnitContext::CrossTranslationUnitContext(CompilerInstance &CI)
159     : CI(CI), Context(CI.getASTContext()) {}
160 
161 CrossTranslationUnitContext::~CrossTranslationUnitContext() {}
162 
163 std::string CrossTranslationUnitContext::getLookupName(const NamedDecl *ND) {
164   SmallString<128> DeclUSR;
165   bool Ret = index::generateUSRForDecl(ND, DeclUSR); (void)Ret;
166   assert(!Ret && "Unable to generate USR");
167   return DeclUSR.str();
168 }
169 
170 /// Recursively visits the function decls of a DeclContext, and looks up a
171 /// function based on USRs.
172 const FunctionDecl *
173 CrossTranslationUnitContext::findFunctionInDeclContext(const DeclContext *DC,
174                                                        StringRef LookupFnName) {
175   assert(DC && "Declaration Context must not be null");
176   for (const Decl *D : DC->decls()) {
177     const auto *SubDC = dyn_cast<DeclContext>(D);
178     if (SubDC)
179       if (const auto *FD = findFunctionInDeclContext(SubDC, LookupFnName))
180         return FD;
181 
182     const auto *ND = dyn_cast<FunctionDecl>(D);
183     const FunctionDecl *ResultDecl;
184     if (!ND || !ND->hasBody(ResultDecl))
185       continue;
186     if (getLookupName(ResultDecl) != LookupFnName)
187       continue;
188     return ResultDecl;
189   }
190   return nullptr;
191 }
192 
193 llvm::Expected<const FunctionDecl *>
194 CrossTranslationUnitContext::getCrossTUDefinition(const FunctionDecl *FD,
195                                                   StringRef CrossTUDir,
196                                                   StringRef IndexName,
197                                                   bool DisplayCTUProgress) {
198   assert(FD && "FD is missing, bad call to this function!");
199   assert(!FD->hasBody() && "FD has a definition in current translation unit!");
200   ++NumGetCTUCalled;
201   const std::string LookupFnName = getLookupName(FD);
202   if (LookupFnName.empty())
203     return llvm::make_error<IndexError>(
204         index_error_code::failed_to_generate_usr);
205   llvm::Expected<ASTUnit *> ASTUnitOrError =
206       loadExternalAST(LookupFnName, CrossTUDir, IndexName, DisplayCTUProgress);
207   if (!ASTUnitOrError)
208     return ASTUnitOrError.takeError();
209   ASTUnit *Unit = *ASTUnitOrError;
210   assert(&Unit->getFileManager() ==
211          &Unit->getASTContext().getSourceManager().getFileManager());
212 
213   const llvm::Triple &TripleTo = Context.getTargetInfo().getTriple();
214   const llvm::Triple &TripleFrom =
215       Unit->getASTContext().getTargetInfo().getTriple();
216   // The imported AST had been generated for a different target.
217   // Some parts of the triple in the loaded ASTContext can be unknown while the
218   // very same parts in the target ASTContext are known. Thus we check for the
219   // known parts only.
220   if (!hasEqualKnownFields(TripleTo, TripleFrom)) {
221     // TODO: Pass the SourceLocation of the CallExpression for more precise
222     // diagnostics.
223     ++NumTripleMismatch;
224     return llvm::make_error<IndexError>(index_error_code::triple_mismatch,
225                                         Unit->getMainFileName(), TripleTo.str(),
226                                         TripleFrom.str());
227   }
228 
229   const auto &LangTo = Context.getLangOpts();
230   const auto &LangFrom = Unit->getASTContext().getLangOpts();
231   // FIXME: Currenty we do not support CTU across C++ and C and across
232   // different dialects of C++.
233   if (LangTo.CPlusPlus != LangFrom.CPlusPlus) {
234     ++NumLangMismatch;
235     return llvm::make_error<IndexError>(index_error_code::lang_mismatch);
236   }
237 
238   TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
239   if (const FunctionDecl *ResultDecl =
240           findFunctionInDeclContext(TU, LookupFnName))
241     return importDefinition(ResultDecl);
242   return llvm::make_error<IndexError>(index_error_code::failed_import);
243 }
244 
245 void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE) {
246   switch (IE.getCode()) {
247   case index_error_code::missing_index_file:
248     Context.getDiagnostics().Report(diag::err_ctu_error_opening)
249         << IE.getFileName();
250     break;
251   case index_error_code::invalid_index_format:
252     Context.getDiagnostics().Report(diag::err_extdefmap_parsing)
253         << IE.getFileName() << IE.getLineNum();
254     break;
255   case index_error_code::multiple_definitions:
256     Context.getDiagnostics().Report(diag::err_multiple_def_index)
257         << IE.getLineNum();
258     break;
259   case index_error_code::triple_mismatch:
260     Context.getDiagnostics().Report(diag::warn_ctu_incompat_triple)
261         << IE.getFileName() << IE.getTripleToName() << IE.getTripleFromName();
262     break;
263   default:
264     break;
265   }
266 }
267 
268 llvm::Expected<ASTUnit *> CrossTranslationUnitContext::loadExternalAST(
269     StringRef LookupName, StringRef CrossTUDir, StringRef IndexName,
270     bool DisplayCTUProgress) {
271   // FIXME: The current implementation only supports loading functions with
272   //        a lookup name from a single translation unit. If multiple
273   //        translation units contains functions with the same lookup name an
274   //        error will be returned.
275   ASTUnit *Unit = nullptr;
276   auto FnUnitCacheEntry = FunctionASTUnitMap.find(LookupName);
277   if (FnUnitCacheEntry == FunctionASTUnitMap.end()) {
278     if (FunctionFileMap.empty()) {
279       SmallString<256> IndexFile = CrossTUDir;
280       if (llvm::sys::path::is_absolute(IndexName))
281         IndexFile = IndexName;
282       else
283         llvm::sys::path::append(IndexFile, IndexName);
284       llvm::Expected<llvm::StringMap<std::string>> IndexOrErr =
285           parseCrossTUIndex(IndexFile, CrossTUDir);
286       if (IndexOrErr)
287         FunctionFileMap = *IndexOrErr;
288       else
289         return IndexOrErr.takeError();
290     }
291 
292     auto It = FunctionFileMap.find(LookupName);
293     if (It == FunctionFileMap.end()) {
294       ++NumNotInOtherTU;
295       return llvm::make_error<IndexError>(index_error_code::missing_definition);
296     }
297     StringRef ASTFileName = It->second;
298     auto ASTCacheEntry = FileASTUnitMap.find(ASTFileName);
299     if (ASTCacheEntry == FileASTUnitMap.end()) {
300       IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
301       TextDiagnosticPrinter *DiagClient =
302           new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
303       IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
304       IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
305           new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
306 
307       std::unique_ptr<ASTUnit> LoadedUnit(ASTUnit::LoadFromASTFile(
308           ASTFileName, CI.getPCHContainerOperations()->getRawReader(),
309           ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts()));
310       Unit = LoadedUnit.get();
311       FileASTUnitMap[ASTFileName] = std::move(LoadedUnit);
312       if (DisplayCTUProgress) {
313         llvm::errs() << "CTU loaded AST file: "
314                      << ASTFileName << "\n";
315       }
316     } else {
317       Unit = ASTCacheEntry->second.get();
318     }
319     FunctionASTUnitMap[LookupName] = Unit;
320   } else {
321     Unit = FnUnitCacheEntry->second;
322   }
323   if (!Unit)
324     return llvm::make_error<IndexError>(
325         index_error_code::failed_to_get_external_ast);
326   return Unit;
327 }
328 
329 llvm::Expected<const FunctionDecl *>
330 CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD) {
331   assert(FD->hasBody() && "Functions to be imported should have body.");
332 
333   ASTImporter &Importer = getOrCreateASTImporter(FD->getASTContext());
334   auto *ToDecl =
335       cast_or_null<FunctionDecl>(Importer.Import(const_cast<FunctionDecl *>(FD)));
336   if (!ToDecl)
337     return llvm::make_error<IndexError>(index_error_code::failed_import);
338   assert(ToDecl->hasBody());
339   assert(FD->hasBody() && "Functions already imported should have body.");
340   ++NumGetCTUSuccess;
341   return ToDecl;
342 }
343 
344 void CrossTranslationUnitContext::lazyInitLookupTable(
345     TranslationUnitDecl *ToTU) {
346   if (!LookupTable)
347     LookupTable = llvm::make_unique<ASTImporterLookupTable>(*ToTU);
348 }
349 
350 ASTImporter &
351 CrossTranslationUnitContext::getOrCreateASTImporter(ASTContext &From) {
352   auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl());
353   if (I != ASTUnitImporterMap.end())
354     return *I->second;
355   lazyInitLookupTable(Context.getTranslationUnitDecl());
356   ASTImporter *NewImporter = new ASTImporter(
357       Context, Context.getSourceManager().getFileManager(), From,
358       From.getSourceManager().getFileManager(), false, LookupTable.get());
359   ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter);
360   return *NewImporter;
361 }
362 
363 } // namespace cross_tu
364 } // namespace clang
365