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