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