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