1 //===--- FrontendAction.cpp -----------------------------------------------===// 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 #include "clang/Frontend/FrontendAction.h" 10 #include "clang/AST/ASTConsumer.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/AST/DeclGroup.h" 13 #include "clang/Basic/Builtins.h" 14 #include "clang/Basic/DiagnosticOptions.h" 15 #include "clang/Basic/FileEntry.h" 16 #include "clang/Basic/LangOptions.h" 17 #include "clang/Basic/LangStandard.h" 18 #include "clang/Basic/Sarif.h" 19 #include "clang/Basic/SourceLocation.h" 20 #include "clang/Basic/SourceManager.h" 21 #include "clang/Basic/Stack.h" 22 #include "clang/Basic/TokenKinds.h" 23 #include "clang/Frontend/ASTUnit.h" 24 #include "clang/Frontend/CompilerInstance.h" 25 #include "clang/Frontend/FrontendDiagnostic.h" 26 #include "clang/Frontend/FrontendPluginRegistry.h" 27 #include "clang/Frontend/LayoutOverrideSource.h" 28 #include "clang/Frontend/MultiplexConsumer.h" 29 #include "clang/Frontend/SARIFDiagnosticPrinter.h" 30 #include "clang/Frontend/Utils.h" 31 #include "clang/Lex/HeaderSearch.h" 32 #include "clang/Lex/LiteralSupport.h" 33 #include "clang/Lex/Preprocessor.h" 34 #include "clang/Lex/PreprocessorOptions.h" 35 #include "clang/Parse/ParseAST.h" 36 #include "clang/Sema/HLSLExternalSemaSource.h" 37 #include "clang/Sema/MultiplexExternalSemaSource.h" 38 #include "clang/Serialization/ASTDeserializationListener.h" 39 #include "clang/Serialization/ASTReader.h" 40 #include "clang/Serialization/GlobalModuleIndex.h" 41 #include "llvm/ADT/ScopeExit.h" 42 #include "llvm/ADT/StringRef.h" 43 #include "llvm/Support/BuryPointer.h" 44 #include "llvm/Support/ErrorHandling.h" 45 #include "llvm/Support/FileSystem.h" 46 #include "llvm/Support/Path.h" 47 #include "llvm/Support/Timer.h" 48 #include "llvm/Support/raw_ostream.h" 49 #include <memory> 50 #include <system_error> 51 using namespace clang; 52 53 LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry) 54 55 namespace { 56 57 /// DeserializedDeclsLineRangePrinter dumps ranges of deserialized declarations 58 /// to aid debugging and bug minimization. It implements ASTConsumer and 59 /// ASTDeserializationListener, so that an object of 60 /// DeserializedDeclsLineRangePrinter registers as its own listener. The 61 /// ASTDeserializationListener interface provides the DeclRead callback that we 62 /// use to collect the deserialized Decls. Note that printing or otherwise 63 /// processing them as this point is dangerous, since that could trigger 64 /// additional deserialization and crash compilation. Therefore, we process the 65 /// collected Decls in HandleTranslationUnit method of ASTConsumer. This is a 66 /// safe point, since we know that by this point all the Decls needed by the 67 /// compiler frontend have been deserialized. In case our processing causes 68 /// further deserialization, DeclRead from the listener might be called again. 69 /// However, at that point we don't accept any more Decls for processing. 70 class DeserializedDeclsSourceRangePrinter : public ASTConsumer, 71 ASTDeserializationListener { 72 public: 73 explicit DeserializedDeclsSourceRangePrinter( 74 SourceManager &SM, std::unique_ptr<llvm::raw_fd_ostream> OS) 75 : ASTDeserializationListener(), SM(SM), OS(std::move(OS)) {} 76 77 ASTDeserializationListener *GetASTDeserializationListener() override { 78 return this; 79 } 80 81 void DeclRead(GlobalDeclID ID, const Decl *D) override { 82 if (!IsCollectingDecls) 83 return; 84 if (!D || isa<TranslationUnitDecl>(D) || isa<LinkageSpecDecl>(D) || 85 isa<NamespaceDecl>(D)) { 86 // These decls cover a lot of nested declarations that might not be used, 87 // reducing the granularity and making the output less useful. 88 return; 89 } 90 if (auto *DC = D->getDeclContext(); !DC || !DC->isFileContext()) { 91 // We choose to work at namespace level to reduce complexity and the 92 // number of cases we care about. 93 return; 94 } 95 PendingDecls.push_back(D); 96 } 97 98 struct Position { 99 unsigned Line; 100 unsigned Column; 101 102 bool operator<(const Position &other) const { 103 return std::tie(Line, Column) < std::tie(other.Line, other.Column); 104 } 105 106 static Position GetBeginSpelling(const SourceManager &SM, 107 const CharSourceRange &R) { 108 SourceLocation Begin = R.getBegin(); 109 return {SM.getSpellingLineNumber(Begin), 110 SM.getSpellingColumnNumber(Begin)}; 111 } 112 113 static Position GetEndSpelling(const SourceManager &SM, 114 const CharSourceRange &Range, 115 const LangOptions &LangOpts) { 116 // For token ranges, compute end location for end character of the range. 117 CharSourceRange R = Lexer::getAsCharRange(Range, SM, LangOpts); 118 SourceLocation End = R.getEnd(); 119 // Relex the token past the end location of the last token in the source 120 // range. If it's a semicolon, advance the location by one token. 121 Token PossiblySemi; 122 Lexer::getRawToken(End, PossiblySemi, SM, LangOpts, true); 123 if (PossiblySemi.is(tok::semi)) 124 End = End.getLocWithOffset(1); 125 // Column number of the returned end position is exclusive. 126 return {SM.getSpellingLineNumber(End), SM.getSpellingColumnNumber(End)}; 127 } 128 }; 129 130 struct RequiredRanges { 131 StringRef Filename; 132 std::vector<std::pair<Position, Position>> FromTo; 133 }; 134 void HandleTranslationUnit(ASTContext &Context) override { 135 assert(IsCollectingDecls && "HandleTranslationUnit called twice?"); 136 IsCollectingDecls = false; 137 138 // Merge ranges in each of the files. 139 struct FileData { 140 std::vector<std::pair<Position, Position>> FromTo; 141 OptionalFileEntryRef Ref; 142 }; 143 llvm::DenseMap<const FileEntry *, FileData> FileToRanges; 144 for (const Decl *D : PendingDecls) { 145 CharSourceRange R = SM.getExpansionRange(D->getSourceRange()); 146 if (!R.isValid()) 147 continue; 148 149 auto *F = SM.getFileEntryForID(SM.getFileID(R.getBegin())); 150 if (F != SM.getFileEntryForID(SM.getFileID(R.getEnd()))) { 151 // Such cases are rare and difficult to handle. 152 continue; 153 } 154 155 auto &Data = FileToRanges[F]; 156 if (!Data.Ref) 157 Data.Ref = SM.getFileEntryRefForID(SM.getFileID(R.getBegin())); 158 Data.FromTo.push_back( 159 {Position::GetBeginSpelling(SM, R), 160 Position::GetEndSpelling(SM, R, D->getLangOpts())}); 161 } 162 163 // To simplify output, merge consecutive and intersecting ranges. 164 std::vector<RequiredRanges> Result; 165 for (auto &[F, Data] : FileToRanges) { 166 auto &FromTo = Data.FromTo; 167 assert(!FromTo.empty()); 168 169 if (!Data.Ref) 170 continue; 171 172 llvm::sort(FromTo); 173 174 std::vector<std::pair<Position, Position>> MergedRanges; 175 MergedRanges.push_back(FromTo.front()); 176 for (auto It = FromTo.begin() + 1; It < FromTo.end(); ++It) { 177 if (MergedRanges.back().second < It->first) { 178 MergedRanges.push_back(*It); 179 continue; 180 } 181 if (MergedRanges.back().second < It->second) 182 MergedRanges.back().second = It->second; 183 } 184 Result.push_back({Data.Ref->getName(), std::move(MergedRanges)}); 185 } 186 printJson(Result); 187 } 188 189 private: 190 std::vector<const Decl *> PendingDecls; 191 bool IsCollectingDecls = true; 192 const SourceManager &SM; 193 std::unique_ptr<llvm::raw_ostream> OS; 194 195 void printJson(llvm::ArrayRef<RequiredRanges> Result) { 196 *OS << "{\n"; 197 *OS << R"( "required_ranges": [)" << "\n"; 198 for (size_t I = 0; I < Result.size(); ++I) { 199 auto &F = Result[I].Filename; 200 auto &MergedRanges = Result[I].FromTo; 201 *OS << R"( {)" << "\n"; 202 *OS << R"( "file": ")" << F << "\"," << "\n"; 203 *OS << R"( "range": [)" << "\n"; 204 for (size_t J = 0; J < MergedRanges.size(); ++J) { 205 auto &From = MergedRanges[J].first; 206 auto &To = MergedRanges[J].second; 207 *OS << R"( {)" << "\n"; 208 *OS << R"( "from": {)" << "\n"; 209 *OS << R"( "line": )" << From.Line << ",\n"; 210 *OS << R"( "column": )" << From.Column << "\n" 211 << R"( },)" << "\n"; 212 *OS << R"( "to": {)" << "\n"; 213 *OS << R"( "line": )" << To.Line << ",\n"; 214 *OS << R"( "column": )" << To.Column << "\n" 215 << R"( })" << "\n"; 216 *OS << R"( })"; 217 if (J < MergedRanges.size() - 1) { 218 *OS << ","; 219 } 220 *OS << "\n"; 221 } 222 *OS << " ]" << "\n" << " }"; 223 if (I < Result.size() - 1) 224 *OS << ","; 225 *OS << "\n"; 226 } 227 *OS << " ]\n"; 228 *OS << "}\n"; 229 } 230 }; 231 232 /// Dumps deserialized declarations. 233 class DeserializedDeclsDumper : public DelegatingDeserializationListener { 234 public: 235 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous, 236 bool DeletePrevious) 237 : DelegatingDeserializationListener(Previous, DeletePrevious) {} 238 239 void DeclRead(GlobalDeclID ID, const Decl *D) override { 240 llvm::outs() << "PCH DECL: " << D->getDeclKindName(); 241 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 242 llvm::outs() << " - "; 243 ND->printQualifiedName(llvm::outs()); 244 } 245 llvm::outs() << "\n"; 246 247 DelegatingDeserializationListener::DeclRead(ID, D); 248 } 249 }; 250 251 /// Checks deserialized declarations and emits error if a name 252 /// matches one given in command-line using -error-on-deserialized-decl. 253 class DeserializedDeclsChecker : public DelegatingDeserializationListener { 254 ASTContext &Ctx; 255 std::set<std::string> NamesToCheck; 256 257 public: 258 DeserializedDeclsChecker(ASTContext &Ctx, 259 const std::set<std::string> &NamesToCheck, 260 ASTDeserializationListener *Previous, 261 bool DeletePrevious) 262 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx), 263 NamesToCheck(NamesToCheck) {} 264 265 void DeclRead(GlobalDeclID ID, const Decl *D) override { 266 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 267 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) { 268 unsigned DiagID 269 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, 270 "%0 was deserialized"); 271 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID) 272 << ND; 273 } 274 275 DelegatingDeserializationListener::DeclRead(ID, D); 276 } 277 }; 278 279 } // end anonymous namespace 280 281 FrontendAction::FrontendAction() : Instance(nullptr) {} 282 283 FrontendAction::~FrontendAction() {} 284 285 void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput, 286 std::unique_ptr<ASTUnit> AST) { 287 this->CurrentInput = CurrentInput; 288 CurrentASTUnit = std::move(AST); 289 } 290 291 Module *FrontendAction::getCurrentModule() const { 292 CompilerInstance &CI = getCompilerInstance(); 293 return CI.getPreprocessor().getHeaderSearchInfo().lookupModule( 294 CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch*/false); 295 } 296 297 std::unique_ptr<ASTConsumer> 298 FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI, 299 StringRef InFile) { 300 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile); 301 if (!Consumer) 302 return nullptr; 303 304 std::vector<std::unique_ptr<ASTConsumer>> Consumers; 305 llvm::StringRef DumpDeserializedDeclarationRangesPath = 306 CI.getFrontendOpts().DumpMinimizationHintsPath; 307 if (!DumpDeserializedDeclarationRangesPath.empty()) { 308 std::error_code ErrorCode; 309 auto FileStream = std::make_unique<llvm::raw_fd_ostream>( 310 DumpDeserializedDeclarationRangesPath, ErrorCode, 311 llvm::sys::fs::OF_TextWithCRLF); 312 if (!ErrorCode) { 313 Consumers.push_back(std::make_unique<DeserializedDeclsSourceRangePrinter>( 314 CI.getSourceManager(), std::move(FileStream))); 315 } else { 316 llvm::errs() << "Failed to create output file for " 317 "-dump-minimization-hints flag, file path: " 318 << DumpDeserializedDeclarationRangesPath 319 << ", error: " << ErrorCode.message() << "\n"; 320 } 321 } 322 323 // Validate -add-plugin args. 324 bool FoundAllPlugins = true; 325 for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) { 326 bool Found = false; 327 for (const FrontendPluginRegistry::entry &Plugin : 328 FrontendPluginRegistry::entries()) { 329 if (Plugin.getName() == Arg) 330 Found = true; 331 } 332 if (!Found) { 333 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg; 334 FoundAllPlugins = false; 335 } 336 } 337 if (!FoundAllPlugins) 338 return nullptr; 339 340 // If this is a code completion run, avoid invoking the plugin consumers 341 if (CI.hasCodeCompletionConsumer()) 342 return Consumer; 343 344 // Collect the list of plugins that go before the main action (in Consumers) 345 // or after it (in AfterConsumers) 346 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers; 347 for (const FrontendPluginRegistry::entry &Plugin : 348 FrontendPluginRegistry::entries()) { 349 std::unique_ptr<PluginASTAction> P = Plugin.instantiate(); 350 PluginASTAction::ActionType ActionType = P->getActionType(); 351 if (ActionType == PluginASTAction::CmdlineAfterMainAction || 352 ActionType == PluginASTAction::CmdlineBeforeMainAction) { 353 // This is O(|plugins| * |add_plugins|), but since both numbers are 354 // way below 50 in practice, that's ok. 355 if (llvm::is_contained(CI.getFrontendOpts().AddPluginActions, 356 Plugin.getName())) { 357 if (ActionType == PluginASTAction::CmdlineBeforeMainAction) 358 ActionType = PluginASTAction::AddBeforeMainAction; 359 else 360 ActionType = PluginASTAction::AddAfterMainAction; 361 } 362 } 363 if ((ActionType == PluginASTAction::AddBeforeMainAction || 364 ActionType == PluginASTAction::AddAfterMainAction) && 365 P->ParseArgs( 366 CI, 367 CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) { 368 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile); 369 if (ActionType == PluginASTAction::AddBeforeMainAction) { 370 Consumers.push_back(std::move(PluginConsumer)); 371 } else { 372 AfterConsumers.push_back(std::move(PluginConsumer)); 373 } 374 } 375 } 376 377 // Add to Consumers the main consumer, then all the plugins that go after it 378 Consumers.push_back(std::move(Consumer)); 379 if (!AfterConsumers.empty()) { 380 // If we have plugins after the main consumer, which may be the codegen 381 // action, they likely will need the ASTContext, so don't clear it in the 382 // codegen action. 383 CI.getCodeGenOpts().ClearASTBeforeBackend = false; 384 for (auto &C : AfterConsumers) 385 Consumers.push_back(std::move(C)); 386 } 387 388 assert(Consumers.size() >= 1 && "should have added the main consumer"); 389 if (Consumers.size() == 1) 390 return std::move(Consumers.front()); 391 return std::make_unique<MultiplexConsumer>(std::move(Consumers)); 392 } 393 394 /// For preprocessed files, if the first line is the linemarker and specifies 395 /// the original source file name, use that name as the input file name. 396 /// Returns the location of the first token after the line marker directive. 397 /// 398 /// \param CI The compiler instance. 399 /// \param InputFile Populated with the filename from the line marker. 400 /// \param IsModuleMap If \c true, add a line note corresponding to this line 401 /// directive. (We need to do this because the directive will not be 402 /// visited by the preprocessor.) 403 static SourceLocation ReadOriginalFileName(CompilerInstance &CI, 404 std::string &InputFile, 405 bool IsModuleMap = false) { 406 auto &SourceMgr = CI.getSourceManager(); 407 auto MainFileID = SourceMgr.getMainFileID(); 408 409 auto MainFileBuf = SourceMgr.getBufferOrNone(MainFileID); 410 if (!MainFileBuf) 411 return SourceLocation(); 412 413 std::unique_ptr<Lexer> RawLexer( 414 new Lexer(MainFileID, *MainFileBuf, SourceMgr, CI.getLangOpts())); 415 416 // If the first line has the syntax of 417 // 418 // # NUM "FILENAME" 419 // 420 // we use FILENAME as the input file name. 421 Token T; 422 if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash) 423 return SourceLocation(); 424 if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() || 425 T.getKind() != tok::numeric_constant) 426 return SourceLocation(); 427 428 unsigned LineNo; 429 SourceLocation LineNoLoc = T.getLocation(); 430 if (IsModuleMap) { 431 llvm::SmallString<16> Buffer; 432 if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts()) 433 .getAsInteger(10, LineNo)) 434 return SourceLocation(); 435 } 436 437 RawLexer->LexFromRawLexer(T); 438 if (T.isAtStartOfLine() || T.getKind() != tok::string_literal) 439 return SourceLocation(); 440 441 StringLiteralParser Literal(T, CI.getPreprocessor()); 442 if (Literal.hadError) 443 return SourceLocation(); 444 RawLexer->LexFromRawLexer(T); 445 if (T.isNot(tok::eof) && !T.isAtStartOfLine()) 446 return SourceLocation(); 447 InputFile = Literal.GetString().str(); 448 449 if (IsModuleMap) 450 CI.getSourceManager().AddLineNote( 451 LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false, 452 false, SrcMgr::C_User_ModuleMap); 453 454 return T.getLocation(); 455 } 456 457 static SmallVectorImpl<char> & 458 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) { 459 Includes.append(RHS.begin(), RHS.end()); 460 return Includes; 461 } 462 463 static void addHeaderInclude(StringRef HeaderName, 464 SmallVectorImpl<char> &Includes, 465 const LangOptions &LangOpts, 466 bool IsExternC) { 467 if (IsExternC && LangOpts.CPlusPlus) 468 Includes += "extern \"C\" {\n"; 469 if (LangOpts.ObjC) 470 Includes += "#import \""; 471 else 472 Includes += "#include \""; 473 474 Includes += HeaderName; 475 476 Includes += "\"\n"; 477 if (IsExternC && LangOpts.CPlusPlus) 478 Includes += "}\n"; 479 } 480 481 /// Collect the set of header includes needed to construct the given 482 /// module and update the TopHeaders file set of the module. 483 /// 484 /// \param Module The module we're collecting includes from. 485 /// 486 /// \param Includes Will be augmented with the set of \#includes or \#imports 487 /// needed to load all of the named headers. 488 static std::error_code collectModuleHeaderIncludes( 489 const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag, 490 ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) { 491 // Don't collect any headers for unavailable modules. 492 if (!Module->isAvailable()) 493 return std::error_code(); 494 495 // Resolve all lazy header directives to header files. 496 ModMap.resolveHeaderDirectives(Module, /*File=*/std::nullopt); 497 498 // If any headers are missing, we can't build this module. In most cases, 499 // diagnostics for this should have already been produced; we only get here 500 // if explicit stat information was provided. 501 // FIXME: If the name resolves to a file with different stat information, 502 // produce a better diagnostic. 503 if (!Module->MissingHeaders.empty()) { 504 auto &MissingHeader = Module->MissingHeaders.front(); 505 Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing) 506 << MissingHeader.IsUmbrella << MissingHeader.FileName; 507 return std::error_code(); 508 } 509 510 // Add includes for each of these headers. 511 for (auto HK : {Module::HK_Normal, Module::HK_Private}) { 512 for (const Module::Header &H : Module->getHeaders(HK)) { 513 Module->addTopHeader(H.Entry); 514 // Use the path as specified in the module map file. We'll look for this 515 // file relative to the module build directory (the directory containing 516 // the module map file) so this will find the same file that we found 517 // while parsing the module map. 518 addHeaderInclude(H.PathRelativeToRootModuleDirectory, Includes, LangOpts, 519 Module->IsExternC); 520 } 521 } 522 // Note that Module->PrivateHeaders will not be a TopHeader. 523 524 if (std::optional<Module::Header> UmbrellaHeader = 525 Module->getUmbrellaHeaderAsWritten()) { 526 Module->addTopHeader(UmbrellaHeader->Entry); 527 if (Module->Parent) 528 // Include the umbrella header for submodules. 529 addHeaderInclude(UmbrellaHeader->PathRelativeToRootModuleDirectory, 530 Includes, LangOpts, Module->IsExternC); 531 } else if (std::optional<Module::DirectoryName> UmbrellaDir = 532 Module->getUmbrellaDirAsWritten()) { 533 // Add all of the headers we find in this subdirectory. 534 std::error_code EC; 535 SmallString<128> DirNative; 536 llvm::sys::path::native(UmbrellaDir->Entry.getName(), DirNative); 537 538 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); 539 SmallVector<std::pair<std::string, FileEntryRef>, 8> Headers; 540 for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End; 541 Dir != End && !EC; Dir.increment(EC)) { 542 // Check whether this entry has an extension typically associated with 543 // headers. 544 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path())) 545 .Cases(".h", ".H", ".hh", ".hpp", true) 546 .Default(false)) 547 continue; 548 549 auto Header = FileMgr.getOptionalFileRef(Dir->path()); 550 // FIXME: This shouldn't happen unless there is a file system race. Is 551 // that worth diagnosing? 552 if (!Header) 553 continue; 554 555 // If this header is marked 'unavailable' in this module, don't include 556 // it. 557 if (ModMap.isHeaderUnavailableInModule(*Header, Module)) 558 continue; 559 560 // Compute the relative path from the directory to this file. 561 SmallVector<StringRef, 16> Components; 562 auto PathIt = llvm::sys::path::rbegin(Dir->path()); 563 for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt) 564 Components.push_back(*PathIt); 565 SmallString<128> RelativeHeader( 566 UmbrellaDir->PathRelativeToRootModuleDirectory); 567 for (auto It = Components.rbegin(), End = Components.rend(); It != End; 568 ++It) 569 llvm::sys::path::append(RelativeHeader, *It); 570 571 std::string RelName = RelativeHeader.c_str(); 572 Headers.push_back(std::make_pair(RelName, *Header)); 573 } 574 575 if (EC) 576 return EC; 577 578 // Sort header paths and make the header inclusion order deterministic 579 // across different OSs and filesystems. 580 llvm::sort(Headers, llvm::less_first()); 581 for (auto &H : Headers) { 582 // Include this header as part of the umbrella directory. 583 Module->addTopHeader(H.second); 584 addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC); 585 } 586 } 587 588 // Recurse into submodules. 589 for (auto *Submodule : Module->submodules()) 590 if (std::error_code Err = collectModuleHeaderIncludes( 591 LangOpts, FileMgr, Diag, ModMap, Submodule, Includes)) 592 return Err; 593 594 return std::error_code(); 595 } 596 597 static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem, 598 bool IsPreprocessed, 599 std::string &PresumedModuleMapFile, 600 unsigned &Offset) { 601 auto &SrcMgr = CI.getSourceManager(); 602 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 603 604 // Map the current input to a file. 605 FileID ModuleMapID = SrcMgr.getMainFileID(); 606 OptionalFileEntryRef ModuleMap = SrcMgr.getFileEntryRefForID(ModuleMapID); 607 assert(ModuleMap && "MainFileID without FileEntry"); 608 609 // If the module map is preprocessed, handle the initial line marker; 610 // line directives are not part of the module map syntax in general. 611 Offset = 0; 612 if (IsPreprocessed) { 613 SourceLocation EndOfLineMarker = 614 ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true); 615 if (EndOfLineMarker.isValid()) 616 Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second; 617 } 618 619 // Load the module map file. 620 if (HS.parseAndLoadModuleMapFile(*ModuleMap, IsSystem, ModuleMapID, &Offset, 621 PresumedModuleMapFile)) 622 return true; 623 624 if (SrcMgr.getBufferOrFake(ModuleMapID).getBufferSize() == Offset) 625 Offset = 0; 626 627 // Infer framework module if possible. 628 if (HS.getModuleMap().canInferFrameworkModule(ModuleMap->getDir())) { 629 SmallString<128> InferredFrameworkPath = ModuleMap->getDir().getName(); 630 llvm::sys::path::append(InferredFrameworkPath, 631 CI.getLangOpts().ModuleName + ".framework"); 632 if (auto Dir = 633 CI.getFileManager().getOptionalDirectoryRef(InferredFrameworkPath)) 634 (void)HS.getModuleMap().inferFrameworkModule(*Dir, IsSystem, nullptr); 635 } 636 637 return false; 638 } 639 640 static Module *prepareToBuildModule(CompilerInstance &CI, 641 StringRef ModuleMapFilename) { 642 if (CI.getLangOpts().CurrentModule.empty()) { 643 CI.getDiagnostics().Report(diag::err_missing_module_name); 644 645 // FIXME: Eventually, we could consider asking whether there was just 646 // a single module described in the module map, and use that as a 647 // default. Then it would be fairly trivial to just "compile" a module 648 // map with a single module (the common case). 649 return nullptr; 650 } 651 652 // Dig out the module definition. 653 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 654 Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(), 655 /*AllowSearch=*/true); 656 if (!M) { 657 CI.getDiagnostics().Report(diag::err_missing_module) 658 << CI.getLangOpts().CurrentModule << ModuleMapFilename; 659 660 return nullptr; 661 } 662 663 // Check whether we can build this module at all. 664 if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(), *M, 665 CI.getDiagnostics())) 666 return nullptr; 667 668 // Inform the preprocessor that includes from within the input buffer should 669 // be resolved relative to the build directory of the module map file. 670 CI.getPreprocessor().setMainFileDir(*M->Directory); 671 672 // If the module was inferred from a different module map (via an expanded 673 // umbrella module definition), track that fact. 674 // FIXME: It would be preferable to fill this in as part of processing 675 // the module map, rather than adding it after the fact. 676 StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap; 677 if (!OriginalModuleMapName.empty()) { 678 auto OriginalModuleMap = 679 CI.getFileManager().getOptionalFileRef(OriginalModuleMapName, 680 /*openFile*/ true); 681 if (!OriginalModuleMap) { 682 CI.getDiagnostics().Report(diag::err_module_map_not_found) 683 << OriginalModuleMapName; 684 return nullptr; 685 } 686 if (*OriginalModuleMap != CI.getSourceManager().getFileEntryRefForID( 687 CI.getSourceManager().getMainFileID())) { 688 auto FileCharacter = 689 M->IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap; 690 FileID OriginalModuleMapFID = CI.getSourceManager().getOrCreateFileID( 691 *OriginalModuleMap, FileCharacter); 692 CI.getPreprocessor() 693 .getHeaderSearchInfo() 694 .getModuleMap() 695 .setInferredModuleAllowedBy(M, OriginalModuleMapFID); 696 } 697 } 698 699 // If we're being run from the command-line, the module build stack will not 700 // have been filled in yet, so complete it now in order to allow us to detect 701 // module cycles. 702 SourceManager &SourceMgr = CI.getSourceManager(); 703 if (SourceMgr.getModuleBuildStack().empty()) 704 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule, 705 FullSourceLoc(SourceLocation(), SourceMgr)); 706 return M; 707 } 708 709 /// Compute the input buffer that should be used to build the specified module. 710 static std::unique_ptr<llvm::MemoryBuffer> 711 getInputBufferForModule(CompilerInstance &CI, Module *M) { 712 FileManager &FileMgr = CI.getFileManager(); 713 714 // Collect the set of #includes we need to build the module. 715 SmallString<256> HeaderContents; 716 std::error_code Err = std::error_code(); 717 if (std::optional<Module::Header> UmbrellaHeader = 718 M->getUmbrellaHeaderAsWritten()) 719 addHeaderInclude(UmbrellaHeader->PathRelativeToRootModuleDirectory, 720 HeaderContents, CI.getLangOpts(), M->IsExternC); 721 Err = collectModuleHeaderIncludes( 722 CI.getLangOpts(), FileMgr, CI.getDiagnostics(), 723 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M, 724 HeaderContents); 725 726 if (Err) { 727 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes) 728 << M->getFullModuleName() << Err.message(); 729 return nullptr; 730 } 731 732 return llvm::MemoryBuffer::getMemBufferCopy( 733 HeaderContents, Module::getModuleInputBufferName()); 734 } 735 736 bool FrontendAction::BeginSourceFile(CompilerInstance &CI, 737 const FrontendInputFile &RealInput) { 738 FrontendInputFile Input(RealInput); 739 assert(!Instance && "Already processing a source file!"); 740 assert(!Input.isEmpty() && "Unexpected empty filename!"); 741 setCurrentInput(Input); 742 setCompilerInstance(&CI); 743 744 bool HasBegunSourceFile = false; 745 bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled && 746 usesPreprocessorOnly(); 747 748 // If we fail, reset state since the client will not end up calling the 749 // matching EndSourceFile(). All paths that return true should release this. 750 auto FailureCleanup = llvm::make_scope_exit([&]() { 751 if (HasBegunSourceFile) 752 CI.getDiagnosticClient().EndSourceFile(); 753 CI.setASTConsumer(nullptr); 754 CI.clearOutputFiles(/*EraseFiles=*/true); 755 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None); 756 setCurrentInput(FrontendInputFile()); 757 setCompilerInstance(nullptr); 758 }); 759 760 if (!BeginInvocation(CI)) 761 return false; 762 763 // If we're replaying the build of an AST file, import it and set up 764 // the initial state from its build. 765 if (ReplayASTFile) { 766 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics()); 767 768 // The AST unit populates its own diagnostics engine rather than ours. 769 IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(new DiagnosticsEngine( 770 Diags->getDiagnosticIDs(), Diags->getDiagnosticOptions())); 771 ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false); 772 773 // FIXME: What if the input is a memory buffer? 774 StringRef InputFile = Input.getFile(); 775 776 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( 777 InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly, 778 nullptr, ASTDiags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts()); 779 if (!AST) 780 return false; 781 782 // Options relating to how we treat the input (but not what we do with it) 783 // are inherited from the AST unit. 784 CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts(); 785 CI.getPreprocessorOpts() = AST->getPreprocessorOpts(); 786 CI.getLangOpts() = AST->getLangOpts(); 787 788 // Set the shared objects, these are reset when we finish processing the 789 // file, otherwise the CompilerInstance will happily destroy them. 790 CI.setFileManager(&AST->getFileManager()); 791 CI.createSourceManager(CI.getFileManager()); 792 CI.getSourceManager().initializeForReplay(AST->getSourceManager()); 793 794 // Preload all the module files loaded transitively by the AST unit. Also 795 // load all module map files that were parsed as part of building the AST 796 // unit. 797 if (auto ASTReader = AST->getASTReader()) { 798 auto &MM = ASTReader->getModuleManager(); 799 auto &PrimaryModule = MM.getPrimaryModule(); 800 801 for (serialization::ModuleFile &MF : MM) 802 if (&MF != &PrimaryModule) 803 CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName); 804 805 ASTReader->visitTopLevelModuleMaps(PrimaryModule, [&](FileEntryRef FE) { 806 CI.getFrontendOpts().ModuleMapFiles.push_back( 807 std::string(FE.getName())); 808 }); 809 } 810 811 // Set up the input file for replay purposes. 812 auto Kind = AST->getInputKind(); 813 if (Kind.getFormat() == InputKind::ModuleMap) { 814 Module *ASTModule = 815 AST->getPreprocessor().getHeaderSearchInfo().lookupModule( 816 AST->getLangOpts().CurrentModule, SourceLocation(), 817 /*AllowSearch*/ false); 818 assert(ASTModule && "module file does not define its own module"); 819 Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind); 820 } else { 821 auto &OldSM = AST->getSourceManager(); 822 FileID ID = OldSM.getMainFileID(); 823 if (auto File = OldSM.getFileEntryRefForID(ID)) 824 Input = FrontendInputFile(File->getName(), Kind); 825 else 826 Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind); 827 } 828 setCurrentInput(Input, std::move(AST)); 829 } 830 831 // AST files follow a very different path, since they share objects via the 832 // AST unit. 833 if (Input.getKind().getFormat() == InputKind::Precompiled) { 834 assert(!usesPreprocessorOnly() && "this case was handled above"); 835 assert(hasASTFileSupport() && 836 "This action does not have AST file support!"); 837 838 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics()); 839 840 // FIXME: What if the input is a memory buffer? 841 StringRef InputFile = Input.getFile(); 842 843 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( 844 InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, nullptr, 845 Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts(), 846 &CI.getLangOpts()); 847 848 if (!AST) 849 return false; 850 851 // Inform the diagnostic client we are processing a source file. 852 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); 853 HasBegunSourceFile = true; 854 855 // Set the shared objects, these are reset when we finish processing the 856 // file, otherwise the CompilerInstance will happily destroy them. 857 CI.setFileManager(&AST->getFileManager()); 858 CI.setSourceManager(&AST->getSourceManager()); 859 CI.setPreprocessor(AST->getPreprocessorPtr()); 860 Preprocessor &PP = CI.getPreprocessor(); 861 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), 862 PP.getLangOpts()); 863 CI.setASTContext(&AST->getASTContext()); 864 865 setCurrentInput(Input, std::move(AST)); 866 867 // Initialize the action. 868 if (!BeginSourceFileAction(CI)) 869 return false; 870 871 // Create the AST consumer. 872 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile)); 873 if (!CI.hasASTConsumer()) 874 return false; 875 876 FailureCleanup.release(); 877 return true; 878 } 879 880 // Set up the file and source managers, if needed. 881 if (!CI.hasFileManager()) { 882 if (!CI.createFileManager()) { 883 return false; 884 } 885 } 886 if (!CI.hasSourceManager()) { 887 CI.createSourceManager(CI.getFileManager()); 888 if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) { 889 static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient()) 890 ->setSarifWriter( 891 std::make_unique<SarifDocumentWriter>(CI.getSourceManager())); 892 } 893 } 894 895 // Set up embedding for any specified files. Do this before we load any 896 // source files, including the primary module map for the compilation. 897 for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) { 898 if (auto FE = CI.getFileManager().getOptionalFileRef(F, /*openFile*/true)) 899 CI.getSourceManager().setFileIsTransient(*FE); 900 else 901 CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F; 902 } 903 if (CI.getFrontendOpts().ModulesEmbedAllFiles) 904 CI.getSourceManager().setAllFilesAreTransient(true); 905 906 // IR files bypass the rest of initialization. 907 if (Input.getKind().getLanguage() == Language::LLVM_IR) { 908 if (!hasIRSupport()) { 909 CI.getDiagnostics().Report(diag::err_ast_action_on_llvm_ir) 910 << Input.getFile(); 911 return false; 912 } 913 914 // Inform the diagnostic client we are processing a source file. 915 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); 916 HasBegunSourceFile = true; 917 918 // Initialize the action. 919 if (!BeginSourceFileAction(CI)) 920 return false; 921 922 // Initialize the main file entry. 923 if (!CI.InitializeSourceManager(CurrentInput)) 924 return false; 925 926 FailureCleanup.release(); 927 return true; 928 } 929 930 // If the implicit PCH include is actually a directory, rather than 931 // a single file, search for a suitable PCH file in that directory. 932 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { 933 FileManager &FileMgr = CI.getFileManager(); 934 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); 935 StringRef PCHInclude = PPOpts.ImplicitPCHInclude; 936 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath(); 937 if (auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude)) { 938 std::error_code EC; 939 SmallString<128> DirNative; 940 llvm::sys::path::native(PCHDir->getName(), DirNative); 941 bool Found = false; 942 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); 943 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), 944 DirEnd; 945 Dir != DirEnd && !EC; Dir.increment(EC)) { 946 // Check whether this is an acceptable AST file. 947 if (ASTReader::isAcceptableASTFile( 948 Dir->path(), FileMgr, CI.getModuleCache(), 949 CI.getPCHContainerReader(), CI.getLangOpts(), 950 CI.getTargetOpts(), CI.getPreprocessorOpts(), 951 SpecificModuleCachePath, /*RequireStrictOptionMatches=*/true)) { 952 PPOpts.ImplicitPCHInclude = std::string(Dir->path()); 953 Found = true; 954 break; 955 } 956 } 957 958 if (!Found) { 959 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude; 960 return false; 961 } 962 } 963 } 964 965 // Set up the preprocessor if needed. When parsing model files the 966 // preprocessor of the original source is reused. 967 if (!isModelParsingAction()) 968 CI.createPreprocessor(getTranslationUnitKind()); 969 970 // Inform the diagnostic client we are processing a source file. 971 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 972 &CI.getPreprocessor()); 973 HasBegunSourceFile = true; 974 975 // Handle C++20 header units. 976 // Here, the user has the option to specify that the header name should be 977 // looked up in the pre-processor search paths (and the main filename as 978 // passed by the driver might therefore be incomplete until that look-up). 979 if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() && 980 !Input.getKind().isPreprocessed()) { 981 StringRef FileName = Input.getFile(); 982 InputKind Kind = Input.getKind(); 983 if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) { 984 assert(CI.hasPreprocessor() && 985 "trying to build a header unit without a Pre-processor?"); 986 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 987 // Relative searches begin from CWD. 988 auto Dir = CI.getFileManager().getOptionalDirectoryRef("."); 989 SmallVector<std::pair<OptionalFileEntryRef, DirectoryEntryRef>, 1> CWD; 990 CWD.push_back({std::nullopt, *Dir}); 991 OptionalFileEntryRef FE = 992 HS.LookupFile(FileName, SourceLocation(), 993 /*Angled*/ Input.getKind().getHeaderUnitKind() == 994 InputKind::HeaderUnit_System, 995 nullptr, nullptr, CWD, nullptr, nullptr, nullptr, 996 nullptr, nullptr, nullptr); 997 if (!FE) { 998 CI.getDiagnostics().Report(diag::err_module_header_file_not_found) 999 << FileName; 1000 return false; 1001 } 1002 // We now have the filename... 1003 FileName = FE->getName(); 1004 // ... still a header unit, but now use the path as written. 1005 Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs); 1006 Input = FrontendInputFile(FileName, Kind, Input.isSystem()); 1007 } 1008 // Unless the user has overridden the name, the header unit module name is 1009 // the pathname for the file. 1010 if (CI.getLangOpts().ModuleName.empty()) 1011 CI.getLangOpts().ModuleName = std::string(FileName); 1012 CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName; 1013 } 1014 1015 if (!CI.InitializeSourceManager(Input)) 1016 return false; 1017 1018 if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() && 1019 Input.getKind().isPreprocessed() && !usesPreprocessorOnly()) { 1020 // We have an input filename like foo.iih, but we want to find the right 1021 // module name (and original file, to build the map entry). 1022 // Check if the first line specifies the original source file name with a 1023 // linemarker. 1024 std::string PresumedInputFile = std::string(getCurrentFileOrBufferName()); 1025 ReadOriginalFileName(CI, PresumedInputFile); 1026 // Unless the user overrides this, the module name is the name by which the 1027 // original file was known. 1028 if (CI.getLangOpts().ModuleName.empty()) 1029 CI.getLangOpts().ModuleName = std::string(PresumedInputFile); 1030 CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName; 1031 } 1032 1033 // For module map files, we first parse the module map and synthesize a 1034 // "<module-includes>" buffer before more conventional processing. 1035 if (Input.getKind().getFormat() == InputKind::ModuleMap) { 1036 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap); 1037 1038 std::string PresumedModuleMapFile; 1039 unsigned OffsetToContents; 1040 if (loadModuleMapForModuleBuild(CI, Input.isSystem(), 1041 Input.isPreprocessed(), 1042 PresumedModuleMapFile, OffsetToContents)) 1043 return false; 1044 1045 auto *CurrentModule = prepareToBuildModule(CI, Input.getFile()); 1046 if (!CurrentModule) 1047 return false; 1048 1049 CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile; 1050 1051 if (OffsetToContents) 1052 // If the module contents are in the same file, skip to them. 1053 CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true); 1054 else { 1055 // Otherwise, convert the module description to a suitable input buffer. 1056 auto Buffer = getInputBufferForModule(CI, CurrentModule); 1057 if (!Buffer) 1058 return false; 1059 1060 // Reinitialize the main file entry to refer to the new input. 1061 auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 1062 auto &SourceMgr = CI.getSourceManager(); 1063 auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind); 1064 assert(BufferID.isValid() && "couldn't create module buffer ID"); 1065 SourceMgr.setMainFileID(BufferID); 1066 } 1067 } 1068 1069 // Initialize the action. 1070 if (!BeginSourceFileAction(CI)) 1071 return false; 1072 1073 // If we were asked to load any module map files, do so now. 1074 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) { 1075 if (auto File = CI.getFileManager().getOptionalFileRef(Filename)) 1076 CI.getPreprocessor().getHeaderSearchInfo().parseAndLoadModuleMapFile( 1077 *File, /*IsSystem*/ false); 1078 else 1079 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename; 1080 } 1081 1082 // If compiling implementation of a module, load its module map file now. 1083 (void)CI.getPreprocessor().getCurrentModuleImplementation(); 1084 1085 // Add a module declaration scope so that modules from -fmodule-map-file 1086 // arguments may shadow modules found implicitly in search paths. 1087 CI.getPreprocessor() 1088 .getHeaderSearchInfo() 1089 .getModuleMap() 1090 .finishModuleDeclarationScope(); 1091 1092 // Create the AST context and consumer unless this is a preprocessor only 1093 // action. 1094 if (!usesPreprocessorOnly()) { 1095 // Parsing a model file should reuse the existing ASTContext. 1096 if (!isModelParsingAction()) 1097 CI.createASTContext(); 1098 1099 // For preprocessed files, check if the first line specifies the original 1100 // source file name with a linemarker. 1101 std::string PresumedInputFile = std::string(getCurrentFileOrBufferName()); 1102 if (Input.isPreprocessed()) 1103 ReadOriginalFileName(CI, PresumedInputFile); 1104 1105 std::unique_ptr<ASTConsumer> Consumer = 1106 CreateWrappedASTConsumer(CI, PresumedInputFile); 1107 if (!Consumer) 1108 return false; 1109 1110 // FIXME: should not overwrite ASTMutationListener when parsing model files? 1111 if (!isModelParsingAction()) 1112 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener()); 1113 1114 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) { 1115 // Convert headers to PCH and chain them. 1116 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader; 1117 source = createChainedIncludesSource(CI, FinalReader); 1118 if (!source) 1119 return false; 1120 CI.setASTReader(static_cast<ASTReader *>(FinalReader.get())); 1121 CI.getASTContext().setExternalSource(source); 1122 } else if (CI.getLangOpts().Modules || 1123 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { 1124 // Use PCM or PCH. 1125 assert(hasPCHSupport() && "This action does not have PCH support!"); 1126 ASTDeserializationListener *DeserialListener = 1127 Consumer->GetASTDeserializationListener(); 1128 bool DeleteDeserialListener = false; 1129 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) { 1130 DeserialListener = new DeserializedDeclsDumper(DeserialListener, 1131 DeleteDeserialListener); 1132 DeleteDeserialListener = true; 1133 } 1134 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) { 1135 DeserialListener = new DeserializedDeclsChecker( 1136 CI.getASTContext(), 1137 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn, 1138 DeserialListener, DeleteDeserialListener); 1139 DeleteDeserialListener = true; 1140 } 1141 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { 1142 CI.createPCHExternalASTSource( 1143 CI.getPreprocessorOpts().ImplicitPCHInclude, 1144 CI.getPreprocessorOpts().DisablePCHOrModuleValidation, 1145 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, 1146 DeserialListener, DeleteDeserialListener); 1147 if (!CI.getASTContext().getExternalSource()) 1148 return false; 1149 } 1150 // If modules are enabled, create the AST reader before creating 1151 // any builtins, so that all declarations know that they might be 1152 // extended by an external source. 1153 if (CI.getLangOpts().Modules || !CI.hasASTContext() || 1154 !CI.getASTContext().getExternalSource()) { 1155 CI.createASTReader(); 1156 CI.getASTReader()->setDeserializationListener(DeserialListener, 1157 DeleteDeserialListener); 1158 } 1159 } 1160 1161 CI.setASTConsumer(std::move(Consumer)); 1162 if (!CI.hasASTConsumer()) 1163 return false; 1164 } 1165 1166 // Initialize built-in info as long as we aren't using an external AST 1167 // source. 1168 if (CI.getLangOpts().Modules || !CI.hasASTContext() || 1169 !CI.getASTContext().getExternalSource()) { 1170 Preprocessor &PP = CI.getPreprocessor(); 1171 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), 1172 PP.getLangOpts()); 1173 } else { 1174 // FIXME: If this is a problem, recover from it by creating a multiplex 1175 // source. 1176 assert((!CI.getLangOpts().Modules || CI.getASTReader()) && 1177 "modules enabled but created an external source that " 1178 "doesn't support modules"); 1179 } 1180 1181 // If we were asked to load any module files, do so now. 1182 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles) { 1183 serialization::ModuleFile *Loaded = nullptr; 1184 if (!CI.loadModuleFile(ModuleFile, Loaded)) 1185 return false; 1186 1187 if (Loaded && Loaded->StandardCXXModule) 1188 CI.getDiagnostics().Report( 1189 diag::warn_eagerly_load_for_standard_cplusplus_modules); 1190 } 1191 1192 // If there is a layout overrides file, attach an external AST source that 1193 // provides the layouts from that file. 1194 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() && 1195 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) { 1196 IntrusiveRefCntPtr<ExternalASTSource> 1197 Override(new LayoutOverrideSource( 1198 CI.getFrontendOpts().OverrideRecordLayoutsFile)); 1199 CI.getASTContext().setExternalSource(Override); 1200 } 1201 1202 // Setup HLSL External Sema Source 1203 if (CI.getLangOpts().HLSL && CI.hasASTContext()) { 1204 IntrusiveRefCntPtr<ExternalSemaSource> HLSLSema( 1205 new HLSLExternalSemaSource()); 1206 if (auto *SemaSource = dyn_cast_if_present<ExternalSemaSource>( 1207 CI.getASTContext().getExternalSource())) { 1208 IntrusiveRefCntPtr<ExternalSemaSource> MultiSema( 1209 new MultiplexExternalSemaSource(SemaSource, HLSLSema.get())); 1210 CI.getASTContext().setExternalSource(MultiSema); 1211 } else 1212 CI.getASTContext().setExternalSource(HLSLSema); 1213 } 1214 1215 FailureCleanup.release(); 1216 return true; 1217 } 1218 1219 llvm::Error FrontendAction::Execute() { 1220 CompilerInstance &CI = getCompilerInstance(); 1221 ExecuteAction(); 1222 1223 // If we are supposed to rebuild the global module index, do so now unless 1224 // there were any module-build failures. 1225 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() && 1226 CI.hasPreprocessor()) { 1227 StringRef Cache = 1228 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 1229 if (!Cache.empty()) { 1230 if (llvm::Error Err = GlobalModuleIndex::writeIndex( 1231 CI.getFileManager(), CI.getPCHContainerReader(), Cache)) { 1232 // FIXME this drops the error on the floor, but 1233 // Index/pch-from-libclang.c seems to rely on dropping at least some of 1234 // the error conditions! 1235 consumeError(std::move(Err)); 1236 } 1237 } 1238 } 1239 1240 return llvm::Error::success(); 1241 } 1242 1243 void FrontendAction::EndSourceFile() { 1244 CompilerInstance &CI = getCompilerInstance(); 1245 1246 // Inform the preprocessor we are done. 1247 if (CI.hasPreprocessor()) 1248 CI.getPreprocessor().EndSourceFile(); 1249 1250 // Inform the diagnostic client we are done with this source file. 1251 // Do this after notifying the preprocessor, so that end-of-file preprocessor 1252 // callbacks can report diagnostics. 1253 CI.getDiagnosticClient().EndSourceFile(); 1254 1255 // Finalize the action. 1256 EndSourceFileAction(); 1257 1258 // Sema references the ast consumer, so reset sema first. 1259 // 1260 // FIXME: There is more per-file stuff we could just drop here? 1261 bool DisableFree = CI.getFrontendOpts().DisableFree; 1262 if (DisableFree) { 1263 CI.resetAndLeakSema(); 1264 CI.resetAndLeakASTContext(); 1265 llvm::BuryPointer(CI.takeASTConsumer().get()); 1266 } else { 1267 CI.setSema(nullptr); 1268 CI.setASTContext(nullptr); 1269 CI.setASTConsumer(nullptr); 1270 } 1271 1272 if (CI.getFrontendOpts().ShowStats) { 1273 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n"; 1274 if (CI.hasPreprocessor()) { 1275 CI.getPreprocessor().PrintStats(); 1276 CI.getPreprocessor().getIdentifierTable().PrintStats(); 1277 CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); 1278 } 1279 if (CI.hasSourceManager()) { 1280 CI.getSourceManager().PrintStats(); 1281 } 1282 llvm::errs() << "\n"; 1283 } 1284 1285 // Cleanup the output streams, and erase the output files if instructed by the 1286 // FrontendAction. 1287 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles()); 1288 1289 // The resources are owned by AST when the current file is AST. 1290 // So we reset the resources here to avoid users accessing it 1291 // accidently. 1292 if (isCurrentFileAST()) { 1293 if (DisableFree) { 1294 CI.resetAndLeakPreprocessor(); 1295 CI.resetAndLeakSourceManager(); 1296 CI.resetAndLeakFileManager(); 1297 llvm::BuryPointer(std::move(CurrentASTUnit)); 1298 } else { 1299 CI.setPreprocessor(nullptr); 1300 CI.setSourceManager(nullptr); 1301 CI.setFileManager(nullptr); 1302 } 1303 } 1304 1305 setCompilerInstance(nullptr); 1306 setCurrentInput(FrontendInputFile()); 1307 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None); 1308 } 1309 1310 bool FrontendAction::shouldEraseOutputFiles() { 1311 return getCompilerInstance().getDiagnostics().hasErrorOccurred(); 1312 } 1313 1314 //===----------------------------------------------------------------------===// 1315 // Utility Actions 1316 //===----------------------------------------------------------------------===// 1317 1318 void ASTFrontendAction::ExecuteAction() { 1319 CompilerInstance &CI = getCompilerInstance(); 1320 if (!CI.hasPreprocessor()) 1321 return; 1322 // This is a fallback: If the client forgets to invoke this, we mark the 1323 // current stack as the bottom. Though not optimal, this could help prevent 1324 // stack overflow during deep recursion. 1325 clang::noteBottomOfStack(); 1326 1327 // FIXME: Move the truncation aspect of this into Sema, we delayed this till 1328 // here so the source manager would be initialized. 1329 if (hasCodeCompletionSupport() && 1330 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) 1331 CI.createCodeCompletionConsumer(); 1332 1333 // Use a code completion consumer? 1334 CodeCompleteConsumer *CompletionConsumer = nullptr; 1335 if (CI.hasCodeCompletionConsumer()) 1336 CompletionConsumer = &CI.getCodeCompletionConsumer(); 1337 1338 if (!CI.hasSema()) 1339 CI.createSema(getTranslationUnitKind(), CompletionConsumer); 1340 1341 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats, 1342 CI.getFrontendOpts().SkipFunctionBodies); 1343 } 1344 1345 void PluginASTAction::anchor() { } 1346 1347 std::unique_ptr<ASTConsumer> 1348 PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, 1349 StringRef InFile) { 1350 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); 1351 } 1352 1353 bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) { 1354 return WrappedAction->PrepareToExecuteAction(CI); 1355 } 1356 std::unique_ptr<ASTConsumer> 1357 WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI, 1358 StringRef InFile) { 1359 return WrappedAction->CreateASTConsumer(CI, InFile); 1360 } 1361 bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) { 1362 return WrappedAction->BeginInvocation(CI); 1363 } 1364 bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) { 1365 WrappedAction->setCurrentInput(getCurrentInput()); 1366 WrappedAction->setCompilerInstance(&CI); 1367 auto Ret = WrappedAction->BeginSourceFileAction(CI); 1368 // BeginSourceFileAction may change CurrentInput, e.g. during module builds. 1369 setCurrentInput(WrappedAction->getCurrentInput()); 1370 return Ret; 1371 } 1372 void WrapperFrontendAction::ExecuteAction() { 1373 WrappedAction->ExecuteAction(); 1374 } 1375 void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); } 1376 void WrapperFrontendAction::EndSourceFileAction() { 1377 WrappedAction->EndSourceFileAction(); 1378 } 1379 bool WrapperFrontendAction::shouldEraseOutputFiles() { 1380 return WrappedAction->shouldEraseOutputFiles(); 1381 } 1382 1383 bool WrapperFrontendAction::usesPreprocessorOnly() const { 1384 return WrappedAction->usesPreprocessorOnly(); 1385 } 1386 TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() { 1387 return WrappedAction->getTranslationUnitKind(); 1388 } 1389 bool WrapperFrontendAction::hasPCHSupport() const { 1390 return WrappedAction->hasPCHSupport(); 1391 } 1392 bool WrapperFrontendAction::hasASTFileSupport() const { 1393 return WrappedAction->hasASTFileSupport(); 1394 } 1395 bool WrapperFrontendAction::hasIRSupport() const { 1396 return WrappedAction->hasIRSupport(); 1397 } 1398 bool WrapperFrontendAction::hasCodeCompletionSupport() const { 1399 return WrappedAction->hasCodeCompletionSupport(); 1400 } 1401 1402 WrapperFrontendAction::WrapperFrontendAction( 1403 std::unique_ptr<FrontendAction> WrappedAction) 1404 : WrappedAction(std::move(WrappedAction)) {} 1405