1 //===--- FrontendActions.cpp ----------------------------------------------===// 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 #include "clang/Frontend/FrontendActions.h" 11 #include "clang/AST/ASTConsumer.h" 12 #include "clang/Basic/FileManager.h" 13 #include "clang/Frontend/ASTConsumers.h" 14 #include "clang/Frontend/CompilerInstance.h" 15 #include "clang/Frontend/FrontendDiagnostic.h" 16 #include "clang/Frontend/MultiplexConsumer.h" 17 #include "clang/Frontend/Utils.h" 18 #include "clang/Lex/HeaderSearch.h" 19 #include "clang/Lex/Preprocessor.h" 20 #include "clang/Lex/PreprocessorOptions.h" 21 #include "clang/Sema/TemplateInstCallback.h" 22 #include "clang/Serialization/ASTReader.h" 23 #include "clang/Serialization/ASTWriter.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include "llvm/Support/Path.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include "llvm/Support/YAMLTraits.h" 29 #include <memory> 30 #include <system_error> 31 32 using namespace clang; 33 34 namespace { 35 CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) { 36 return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer() 37 : nullptr; 38 } 39 40 void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) { 41 if (Action.hasCodeCompletionSupport() && 42 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) 43 CI.createCodeCompletionConsumer(); 44 45 if (!CI.hasSema()) 46 CI.createSema(Action.getTranslationUnitKind(), 47 GetCodeCompletionConsumer(CI)); 48 } 49 } // namespace 50 51 //===----------------------------------------------------------------------===// 52 // Custom Actions 53 //===----------------------------------------------------------------------===// 54 55 std::unique_ptr<ASTConsumer> 56 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 57 return llvm::make_unique<ASTConsumer>(); 58 } 59 60 void InitOnlyAction::ExecuteAction() { 61 } 62 63 //===----------------------------------------------------------------------===// 64 // AST Consumer Actions 65 //===----------------------------------------------------------------------===// 66 67 std::unique_ptr<ASTConsumer> 68 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 69 if (std::unique_ptr<raw_ostream> OS = 70 CI.createDefaultOutputFile(false, InFile)) 71 return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter); 72 return nullptr; 73 } 74 75 std::unique_ptr<ASTConsumer> 76 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 77 return CreateASTDumper(nullptr /*Dump to stdout.*/, 78 CI.getFrontendOpts().ASTDumpFilter, 79 CI.getFrontendOpts().ASTDumpDecls, 80 CI.getFrontendOpts().ASTDumpAll, 81 CI.getFrontendOpts().ASTDumpLookups); 82 } 83 84 std::unique_ptr<ASTConsumer> 85 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 86 return CreateASTDeclNodeLister(); 87 } 88 89 std::unique_ptr<ASTConsumer> 90 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 91 return CreateASTViewer(); 92 } 93 94 std::unique_ptr<ASTConsumer> 95 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, 96 StringRef InFile) { 97 return CreateDeclContextPrinter(); 98 } 99 100 std::unique_ptr<ASTConsumer> 101 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 102 std::string Sysroot; 103 if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot)) 104 return nullptr; 105 106 std::string OutputFile; 107 std::unique_ptr<raw_pwrite_stream> OS = 108 CreateOutputFile(CI, InFile, /*ref*/ OutputFile); 109 if (!OS) 110 return nullptr; 111 112 if (!CI.getFrontendOpts().RelocatablePCH) 113 Sysroot.clear(); 114 115 const auto &FrontendOpts = CI.getFrontendOpts(); 116 auto Buffer = std::make_shared<PCHBuffer>(); 117 std::vector<std::unique_ptr<ASTConsumer>> Consumers; 118 Consumers.push_back(llvm::make_unique<PCHGenerator>( 119 CI.getPreprocessor(), OutputFile, Sysroot, 120 Buffer, FrontendOpts.ModuleFileExtensions, 121 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, 122 FrontendOpts.IncludeTimestamps)); 123 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( 124 CI, InFile, OutputFile, std::move(OS), Buffer)); 125 126 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); 127 } 128 129 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, 130 std::string &Sysroot) { 131 Sysroot = CI.getHeaderSearchOpts().Sysroot; 132 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { 133 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); 134 return false; 135 } 136 137 return true; 138 } 139 140 std::unique_ptr<llvm::raw_pwrite_stream> 141 GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile, 142 std::string &OutputFile) { 143 // We use createOutputFile here because this is exposed via libclang, and we 144 // must disable the RemoveFileOnSignal behavior. 145 // We use a temporary to avoid race conditions. 146 std::unique_ptr<raw_pwrite_stream> OS = 147 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 148 /*RemoveFileOnSignal=*/false, InFile, 149 /*Extension=*/"", /*useTemporary=*/true); 150 if (!OS) 151 return nullptr; 152 153 OutputFile = CI.getFrontendOpts().OutputFile; 154 return OS; 155 } 156 157 bool GeneratePCHAction::shouldEraseOutputFiles() { 158 if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors) 159 return false; 160 return ASTFrontendAction::shouldEraseOutputFiles(); 161 } 162 163 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) { 164 CI.getLangOpts().CompilingPCH = true; 165 return true; 166 } 167 168 std::unique_ptr<ASTConsumer> 169 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, 170 StringRef InFile) { 171 std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile); 172 if (!OS) 173 return nullptr; 174 175 std::string OutputFile = CI.getFrontendOpts().OutputFile; 176 std::string Sysroot; 177 178 auto Buffer = std::make_shared<PCHBuffer>(); 179 std::vector<std::unique_ptr<ASTConsumer>> Consumers; 180 181 Consumers.push_back(llvm::make_unique<PCHGenerator>( 182 CI.getPreprocessor(), OutputFile, Sysroot, 183 Buffer, CI.getFrontendOpts().ModuleFileExtensions, 184 /*AllowASTWithErrors=*/false, 185 /*IncludeTimestamps=*/ 186 +CI.getFrontendOpts().BuildingImplicitModule)); 187 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( 188 CI, InFile, OutputFile, std::move(OS), Buffer)); 189 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); 190 } 191 192 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction( 193 CompilerInstance &CI) { 194 if (!CI.getLangOpts().Modules) { 195 CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules); 196 return false; 197 } 198 199 return GenerateModuleAction::BeginSourceFileAction(CI); 200 } 201 202 std::unique_ptr<raw_pwrite_stream> 203 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI, 204 StringRef InFile) { 205 // If no output file was provided, figure out where this module would go 206 // in the module cache. 207 if (CI.getFrontendOpts().OutputFile.empty()) { 208 StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap; 209 if (ModuleMapFile.empty()) 210 ModuleMapFile = InFile; 211 212 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 213 CI.getFrontendOpts().OutputFile = 214 HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule, 215 ModuleMapFile); 216 } 217 218 // We use createOutputFile here because this is exposed via libclang, and we 219 // must disable the RemoveFileOnSignal behavior. 220 // We use a temporary to avoid race conditions. 221 return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 222 /*RemoveFileOnSignal=*/false, InFile, 223 /*Extension=*/"", /*useTemporary=*/true, 224 /*CreateMissingDirectories=*/true); 225 } 226 227 bool GenerateModuleInterfaceAction::BeginSourceFileAction( 228 CompilerInstance &CI) { 229 if (!CI.getLangOpts().ModulesTS) { 230 CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts); 231 return false; 232 } 233 234 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface); 235 236 return GenerateModuleAction::BeginSourceFileAction(CI); 237 } 238 239 std::unique_ptr<raw_pwrite_stream> 240 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI, 241 StringRef InFile) { 242 return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm"); 243 } 244 245 SyntaxOnlyAction::~SyntaxOnlyAction() { 246 } 247 248 std::unique_ptr<ASTConsumer> 249 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 250 return llvm::make_unique<ASTConsumer>(); 251 } 252 253 std::unique_ptr<ASTConsumer> 254 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI, 255 StringRef InFile) { 256 return llvm::make_unique<ASTConsumer>(); 257 } 258 259 std::unique_ptr<ASTConsumer> 260 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 261 return llvm::make_unique<ASTConsumer>(); 262 } 263 264 void VerifyPCHAction::ExecuteAction() { 265 CompilerInstance &CI = getCompilerInstance(); 266 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 267 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; 268 std::unique_ptr<ASTReader> Reader(new ASTReader( 269 CI.getPreprocessor(), &CI.getASTContext(), CI.getPCHContainerReader(), 270 CI.getFrontendOpts().ModuleFileExtensions, 271 Sysroot.empty() ? "" : Sysroot.c_str(), 272 /*DisableValidation*/ false, 273 /*AllowPCHWithCompilerErrors*/ false, 274 /*AllowConfigurationMismatch*/ true, 275 /*ValidateSystemInputs*/ true)); 276 277 Reader->ReadAST(getCurrentFile(), 278 Preamble ? serialization::MK_Preamble 279 : serialization::MK_PCH, 280 SourceLocation(), 281 ASTReader::ARR_ConfigurationMismatch); 282 } 283 284 namespace { 285 struct TemplightEntry { 286 std::string Name; 287 std::string Kind; 288 std::string Event; 289 std::string DefinitionLocation; 290 std::string PointOfInstantiation; 291 }; 292 } // namespace 293 294 namespace llvm { 295 namespace yaml { 296 template <> struct MappingTraits<TemplightEntry> { 297 static void mapping(IO &io, TemplightEntry &fields) { 298 io.mapRequired("name", fields.Name); 299 io.mapRequired("kind", fields.Kind); 300 io.mapRequired("event", fields.Event); 301 io.mapRequired("orig", fields.DefinitionLocation); 302 io.mapRequired("poi", fields.PointOfInstantiation); 303 } 304 }; 305 } // namespace yaml 306 } // namespace llvm 307 308 namespace { 309 class DefaultTemplateInstCallback : public TemplateInstantiationCallback { 310 using CodeSynthesisContext = Sema::CodeSynthesisContext; 311 312 public: 313 void initialize(const Sema &) override {} 314 315 void finalize(const Sema &) override {} 316 317 void atTemplateBegin(const Sema &TheSema, 318 const CodeSynthesisContext &Inst) override { 319 displayTemplightEntry<true>(llvm::outs(), TheSema, Inst); 320 } 321 322 void atTemplateEnd(const Sema &TheSema, 323 const CodeSynthesisContext &Inst) override { 324 displayTemplightEntry<false>(llvm::outs(), TheSema, Inst); 325 } 326 327 private: 328 static std::string toString(CodeSynthesisContext::SynthesisKind Kind) { 329 switch (Kind) { 330 case CodeSynthesisContext::TemplateInstantiation: 331 return "TemplateInstantiation"; 332 case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: 333 return "DefaultTemplateArgumentInstantiation"; 334 case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: 335 return "DefaultFunctionArgumentInstantiation"; 336 case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: 337 return "ExplicitTemplateArgumentSubstitution"; 338 case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: 339 return "DeducedTemplateArgumentSubstitution"; 340 case CodeSynthesisContext::PriorTemplateArgumentSubstitution: 341 return "PriorTemplateArgumentSubstitution"; 342 case CodeSynthesisContext::DefaultTemplateArgumentChecking: 343 return "DefaultTemplateArgumentChecking"; 344 case CodeSynthesisContext::ExceptionSpecEvaluation: 345 return "ExceptionSpecEvaluation"; 346 case CodeSynthesisContext::ExceptionSpecInstantiation: 347 return "ExceptionSpecInstantiation"; 348 case CodeSynthesisContext::DeclaringSpecialMember: 349 return "DeclaringSpecialMember"; 350 case CodeSynthesisContext::DefiningSynthesizedFunction: 351 return "DefiningSynthesizedFunction"; 352 case CodeSynthesisContext::Memoization: 353 return "Memoization"; 354 } 355 return ""; 356 } 357 358 template <bool BeginInstantiation> 359 static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema, 360 const CodeSynthesisContext &Inst) { 361 std::string YAML; 362 { 363 llvm::raw_string_ostream OS(YAML); 364 llvm::yaml::Output YO(OS); 365 TemplightEntry Entry = 366 getTemplightEntry<BeginInstantiation>(TheSema, Inst); 367 llvm::yaml::EmptyContext Context; 368 llvm::yaml::yamlize(YO, Entry, true, Context); 369 } 370 Out << "---" << YAML << "\n"; 371 } 372 373 template <bool BeginInstantiation> 374 static TemplightEntry getTemplightEntry(const Sema &TheSema, 375 const CodeSynthesisContext &Inst) { 376 TemplightEntry Entry; 377 Entry.Kind = toString(Inst.Kind); 378 Entry.Event = BeginInstantiation ? "Begin" : "End"; 379 if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) { 380 llvm::raw_string_ostream OS(Entry.Name); 381 NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true); 382 const PresumedLoc DefLoc = 383 TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation()); 384 if(!DefLoc.isInvalid()) 385 Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" + 386 std::to_string(DefLoc.getLine()) + ":" + 387 std::to_string(DefLoc.getColumn()); 388 } 389 const PresumedLoc PoiLoc = 390 TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation); 391 if (!PoiLoc.isInvalid()) { 392 Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" + 393 std::to_string(PoiLoc.getLine()) + ":" + 394 std::to_string(PoiLoc.getColumn()); 395 } 396 return Entry; 397 } 398 }; 399 } // namespace 400 401 std::unique_ptr<ASTConsumer> 402 TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 403 return llvm::make_unique<ASTConsumer>(); 404 } 405 406 void TemplightDumpAction::ExecuteAction() { 407 CompilerInstance &CI = getCompilerInstance(); 408 409 // This part is normally done by ASTFrontEndAction, but needs to happen 410 // before Templight observers can be created 411 // FIXME: Move the truncation aspect of this into Sema, we delayed this till 412 // here so the source manager would be initialized. 413 EnsureSemaIsCreated(CI, *this); 414 415 CI.getSema().TemplateInstCallbacks.push_back( 416 llvm::make_unique<DefaultTemplateInstCallback>()); 417 ASTFrontendAction::ExecuteAction(); 418 } 419 420 namespace { 421 /// AST reader listener that dumps module information for a module 422 /// file. 423 class DumpModuleInfoListener : public ASTReaderListener { 424 llvm::raw_ostream &Out; 425 426 public: 427 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { } 428 429 #define DUMP_BOOLEAN(Value, Text) \ 430 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n" 431 432 bool ReadFullVersionInformation(StringRef FullVersion) override { 433 Out.indent(2) 434 << "Generated by " 435 << (FullVersion == getClangFullRepositoryVersion()? "this" 436 : "a different") 437 << " Clang: " << FullVersion << "\n"; 438 return ASTReaderListener::ReadFullVersionInformation(FullVersion); 439 } 440 441 void ReadModuleName(StringRef ModuleName) override { 442 Out.indent(2) << "Module name: " << ModuleName << "\n"; 443 } 444 void ReadModuleMapFile(StringRef ModuleMapPath) override { 445 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n"; 446 } 447 448 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 449 bool AllowCompatibleDifferences) override { 450 Out.indent(2) << "Language options:\n"; 451 #define LANGOPT(Name, Bits, Default, Description) \ 452 DUMP_BOOLEAN(LangOpts.Name, Description); 453 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 454 Out.indent(4) << Description << ": " \ 455 << static_cast<unsigned>(LangOpts.get##Name()) << "\n"; 456 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 457 Out.indent(4) << Description << ": " << LangOpts.Name << "\n"; 458 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 459 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 460 #include "clang/Basic/LangOptions.def" 461 462 if (!LangOpts.ModuleFeatures.empty()) { 463 Out.indent(4) << "Module features:\n"; 464 for (StringRef Feature : LangOpts.ModuleFeatures) 465 Out.indent(6) << Feature << "\n"; 466 } 467 468 return false; 469 } 470 471 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 472 bool AllowCompatibleDifferences) override { 473 Out.indent(2) << "Target options:\n"; 474 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n"; 475 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n"; 476 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n"; 477 478 if (!TargetOpts.FeaturesAsWritten.empty()) { 479 Out.indent(4) << "Target features:\n"; 480 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); 481 I != N; ++I) { 482 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n"; 483 } 484 } 485 486 return false; 487 } 488 489 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, 490 bool Complain) override { 491 Out.indent(2) << "Diagnostic options:\n"; 492 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name); 493 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 494 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n"; 495 #define VALUE_DIAGOPT(Name, Bits, Default) \ 496 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n"; 497 #include "clang/Basic/DiagnosticOptions.def" 498 499 Out.indent(4) << "Diagnostic flags:\n"; 500 for (const std::string &Warning : DiagOpts->Warnings) 501 Out.indent(6) << "-W" << Warning << "\n"; 502 for (const std::string &Remark : DiagOpts->Remarks) 503 Out.indent(6) << "-R" << Remark << "\n"; 504 505 return false; 506 } 507 508 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 509 StringRef SpecificModuleCachePath, 510 bool Complain) override { 511 Out.indent(2) << "Header search options:\n"; 512 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n"; 513 Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n"; 514 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n"; 515 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes, 516 "Use builtin include directories [-nobuiltininc]"); 517 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes, 518 "Use standard system include directories [-nostdinc]"); 519 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes, 520 "Use standard C++ include directories [-nostdinc++]"); 521 DUMP_BOOLEAN(HSOpts.UseLibcxx, 522 "Use libc++ (rather than libstdc++) [-stdlib=]"); 523 return false; 524 } 525 526 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 527 bool Complain, 528 std::string &SuggestedPredefines) override { 529 Out.indent(2) << "Preprocessor options:\n"; 530 DUMP_BOOLEAN(PPOpts.UsePredefines, 531 "Uses compiler/target-specific predefines [-undef]"); 532 DUMP_BOOLEAN(PPOpts.DetailedRecord, 533 "Uses detailed preprocessing record (for indexing)"); 534 535 if (!PPOpts.Macros.empty()) { 536 Out.indent(4) << "Predefined macros:\n"; 537 } 538 539 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator 540 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end(); 541 I != IEnd; ++I) { 542 Out.indent(6); 543 if (I->second) 544 Out << "-U"; 545 else 546 Out << "-D"; 547 Out << I->first << "\n"; 548 } 549 return false; 550 } 551 552 /// Indicates that a particular module file extension has been read. 553 void readModuleFileExtension( 554 const ModuleFileExtensionMetadata &Metadata) override { 555 Out.indent(2) << "Module file extension '" 556 << Metadata.BlockName << "' " << Metadata.MajorVersion 557 << "." << Metadata.MinorVersion; 558 if (!Metadata.UserInfo.empty()) { 559 Out << ": "; 560 Out.write_escaped(Metadata.UserInfo); 561 } 562 563 Out << "\n"; 564 } 565 566 /// Tells the \c ASTReaderListener that we want to receive the 567 /// input files of the AST file via \c visitInputFile. 568 bool needsInputFileVisitation() override { return true; } 569 570 /// Tells the \c ASTReaderListener that we want to receive the 571 /// input files of the AST file via \c visitInputFile. 572 bool needsSystemInputFileVisitation() override { return true; } 573 574 /// Indicates that the AST file contains particular input file. 575 /// 576 /// \returns true to continue receiving the next input file, false to stop. 577 bool visitInputFile(StringRef Filename, bool isSystem, 578 bool isOverridden, bool isExplicitModule) override { 579 580 Out.indent(2) << "Input file: " << Filename; 581 582 if (isSystem || isOverridden || isExplicitModule) { 583 Out << " ["; 584 if (isSystem) { 585 Out << "System"; 586 if (isOverridden || isExplicitModule) 587 Out << ", "; 588 } 589 if (isOverridden) { 590 Out << "Overridden"; 591 if (isExplicitModule) 592 Out << ", "; 593 } 594 if (isExplicitModule) 595 Out << "ExplicitModule"; 596 597 Out << "]"; 598 } 599 600 Out << "\n"; 601 602 return true; 603 } 604 605 /// Returns true if this \c ASTReaderListener wants to receive the 606 /// imports of the AST file via \c visitImport, false otherwise. 607 bool needsImportVisitation() const override { return true; } 608 609 /// If needsImportVisitation returns \c true, this is called for each 610 /// AST file imported by this AST file. 611 void visitImport(StringRef ModuleName, StringRef Filename) override { 612 Out.indent(2) << "Imports module '" << ModuleName 613 << "': " << Filename.str() << "\n"; 614 } 615 #undef DUMP_BOOLEAN 616 }; 617 } 618 619 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) { 620 // The Object file reader also supports raw ast files and there is no point in 621 // being strict about the module file format in -module-file-info mode. 622 CI.getHeaderSearchOpts().ModuleFormat = "obj"; 623 return true; 624 } 625 626 void DumpModuleInfoAction::ExecuteAction() { 627 // Set up the output file. 628 std::unique_ptr<llvm::raw_fd_ostream> OutFile; 629 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile; 630 if (!OutputFileName.empty() && OutputFileName != "-") { 631 std::error_code EC; 632 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, 633 llvm::sys::fs::F_Text)); 634 } 635 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs(); 636 637 Out << "Information for module file '" << getCurrentFile() << "':\n"; 638 auto &FileMgr = getCompilerInstance().getFileManager(); 639 auto Buffer = FileMgr.getBufferForFile(getCurrentFile()); 640 StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer(); 641 bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' && 642 Magic[2] == 'C' && Magic[3] == 'H'); 643 Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n"; 644 645 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 646 DumpModuleInfoListener Listener(Out); 647 HeaderSearchOptions &HSOpts = 648 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 649 ASTReader::readASTFileControlBlock( 650 getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(), 651 /*FindModuleFileExtensions=*/true, Listener, 652 HSOpts.ModulesValidateDiagnosticOptions); 653 } 654 655 //===----------------------------------------------------------------------===// 656 // Preprocessor Actions 657 //===----------------------------------------------------------------------===// 658 659 void DumpRawTokensAction::ExecuteAction() { 660 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 661 SourceManager &SM = PP.getSourceManager(); 662 663 // Start lexing the specified input file. 664 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); 665 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); 666 RawLex.SetKeepWhitespaceMode(true); 667 668 Token RawTok; 669 RawLex.LexFromRawLexer(RawTok); 670 while (RawTok.isNot(tok::eof)) { 671 PP.DumpToken(RawTok, true); 672 llvm::errs() << "\n"; 673 RawLex.LexFromRawLexer(RawTok); 674 } 675 } 676 677 void DumpTokensAction::ExecuteAction() { 678 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 679 // Start preprocessing the specified input file. 680 Token Tok; 681 PP.EnterMainSourceFile(); 682 do { 683 PP.Lex(Tok); 684 PP.DumpToken(Tok, true); 685 llvm::errs() << "\n"; 686 } while (Tok.isNot(tok::eof)); 687 } 688 689 void GeneratePTHAction::ExecuteAction() { 690 CompilerInstance &CI = getCompilerInstance(); 691 std::unique_ptr<raw_pwrite_stream> OS = 692 CI.createDefaultOutputFile(true, getCurrentFile()); 693 if (!OS) 694 return; 695 696 CacheTokens(CI.getPreprocessor(), OS.get()); 697 } 698 699 void PreprocessOnlyAction::ExecuteAction() { 700 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 701 702 // Ignore unknown pragmas. 703 PP.IgnorePragmas(); 704 705 Token Tok; 706 // Start parsing the specified input file. 707 PP.EnterMainSourceFile(); 708 do { 709 PP.Lex(Tok); 710 } while (Tok.isNot(tok::eof)); 711 } 712 713 void PrintPreprocessedAction::ExecuteAction() { 714 CompilerInstance &CI = getCompilerInstance(); 715 // Output file may need to be set to 'Binary', to avoid converting Unix style 716 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). 717 // 718 // Look to see what type of line endings the file uses. If there's a 719 // CRLF, then we won't open the file up in binary mode. If there is 720 // just an LF or CR, then we will open the file up in binary mode. 721 // In this fashion, the output format should match the input format, unless 722 // the input format has inconsistent line endings. 723 // 724 // This should be a relatively fast operation since most files won't have 725 // all of their source code on a single line. However, that is still a 726 // concern, so if we scan for too long, we'll just assume the file should 727 // be opened in binary mode. 728 bool BinaryMode = true; 729 bool InvalidFile = false; 730 const SourceManager& SM = CI.getSourceManager(); 731 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), 732 &InvalidFile); 733 if (!InvalidFile) { 734 const char *cur = Buffer->getBufferStart(); 735 const char *end = Buffer->getBufferEnd(); 736 const char *next = (cur != end) ? cur + 1 : end; 737 738 // Limit ourselves to only scanning 256 characters into the source 739 // file. This is mostly a sanity check in case the file has no 740 // newlines whatsoever. 741 if (end - cur > 256) end = cur + 256; 742 743 while (next < end) { 744 if (*cur == 0x0D) { // CR 745 if (*next == 0x0A) // CRLF 746 BinaryMode = false; 747 748 break; 749 } else if (*cur == 0x0A) // LF 750 break; 751 752 ++cur; 753 ++next; 754 } 755 } 756 757 std::unique_ptr<raw_ostream> OS = 758 CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); 759 if (!OS) return; 760 761 // If we're preprocessing a module map, start by dumping the contents of the 762 // module itself before switching to the input buffer. 763 auto &Input = getCurrentInput(); 764 if (Input.getKind().getFormat() == InputKind::ModuleMap) { 765 if (Input.isFile()) { 766 (*OS) << "# 1 \""; 767 OS->write_escaped(Input.getFile()); 768 (*OS) << "\"\n"; 769 } 770 // FIXME: Include additional information here so that we don't need the 771 // original source files to exist on disk. 772 getCurrentModule()->print(*OS); 773 (*OS) << "#pragma clang module contents\n"; 774 } 775 776 DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(), 777 CI.getPreprocessorOutputOpts()); 778 } 779 780 void PrintPreambleAction::ExecuteAction() { 781 switch (getCurrentFileKind().getLanguage()) { 782 case InputKind::C: 783 case InputKind::CXX: 784 case InputKind::ObjC: 785 case InputKind::ObjCXX: 786 case InputKind::OpenCL: 787 case InputKind::CUDA: 788 case InputKind::HIP: 789 break; 790 791 case InputKind::Unknown: 792 case InputKind::Asm: 793 case InputKind::LLVM_IR: 794 case InputKind::RenderScript: 795 // We can't do anything with these. 796 return; 797 } 798 799 // We don't expect to find any #include directives in a preprocessed input. 800 if (getCurrentFileKind().isPreprocessed()) 801 return; 802 803 CompilerInstance &CI = getCompilerInstance(); 804 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile()); 805 if (Buffer) { 806 unsigned Preamble = 807 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size; 808 llvm::outs().write((*Buffer)->getBufferStart(), Preamble); 809 } 810 } 811 812 void DumpCompilerOptionsAction::ExecuteAction() { 813 CompilerInstance &CI = getCompilerInstance(); 814 std::unique_ptr<raw_ostream> OSP = 815 CI.createDefaultOutputFile(false, getCurrentFile()); 816 if (!OSP) 817 return; 818 819 raw_ostream &OS = *OSP; 820 const Preprocessor &PP = CI.getPreprocessor(); 821 const LangOptions &LangOpts = PP.getLangOpts(); 822 823 // FIXME: Rather than manually format the JSON (which is awkward due to 824 // needing to remove trailing commas), this should make use of a JSON library. 825 // FIXME: Instead of printing enums as an integral value and specifying the 826 // type as a separate field, use introspection to print the enumerator. 827 828 OS << "{\n"; 829 OS << "\n\"features\" : [\n"; 830 { 831 llvm::SmallString<128> Str; 832 #define FEATURE(Name, Predicate) \ 833 ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \ 834 .toVector(Str); 835 #include "clang/Basic/Features.def" 836 #undef FEATURE 837 // Remove the newline and comma from the last entry to ensure this remains 838 // valid JSON. 839 OS << Str.substr(0, Str.size() - 2); 840 } 841 OS << "\n],\n"; 842 843 OS << "\n\"extensions\" : [\n"; 844 { 845 llvm::SmallString<128> Str; 846 #define EXTENSION(Name, Predicate) \ 847 ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \ 848 .toVector(Str); 849 #include "clang/Basic/Features.def" 850 #undef EXTENSION 851 // Remove the newline and comma from the last entry to ensure this remains 852 // valid JSON. 853 OS << Str.substr(0, Str.size() - 2); 854 } 855 OS << "\n]\n"; 856 857 OS << "}"; 858 } 859