1 //===-- CompilerInstance.h - Clang Compiler Instance ------------*- C++ -*-===// 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 #ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ 10 #define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ 11 12 #include "clang/AST/ASTConsumer.h" 13 #include "clang/Basic/Diagnostic.h" 14 #include "clang/Basic/SourceManager.h" 15 #include "clang/Frontend/CompilerInvocation.h" 16 #include "clang/Frontend/PCHContainerOperations.h" 17 #include "clang/Frontend/Utils.h" 18 #include "clang/Lex/HeaderSearchOptions.h" 19 #include "clang/Lex/ModuleLoader.h" 20 #include "llvm/ADT/ArrayRef.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/IntrusiveRefCntPtr.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/Support/BuryPointer.h" 25 #include "llvm/Support/FileSystem.h" 26 #include <cassert> 27 #include <list> 28 #include <memory> 29 #include <string> 30 #include <utility> 31 32 namespace llvm { 33 class raw_fd_ostream; 34 class Timer; 35 class TimerGroup; 36 } 37 38 namespace clang { 39 class ASTContext; 40 class ASTReader; 41 class CodeCompleteConsumer; 42 class DiagnosticsEngine; 43 class DiagnosticConsumer; 44 class FileManager; 45 class FrontendAction; 46 class InMemoryModuleCache; 47 class Module; 48 class Preprocessor; 49 class Sema; 50 class SourceManager; 51 class TargetInfo; 52 enum class DisableValidationForModuleKind; 53 54 /// CompilerInstance - Helper class for managing a single instance of the Clang 55 /// compiler. 56 /// 57 /// The CompilerInstance serves two purposes: 58 /// (1) It manages the various objects which are necessary to run the compiler, 59 /// for example the preprocessor, the target information, and the AST 60 /// context. 61 /// (2) It provides utility routines for constructing and manipulating the 62 /// common Clang objects. 63 /// 64 /// The compiler instance generally owns the instance of all the objects that it 65 /// manages. However, clients can still share objects by manually setting the 66 /// object and retaking ownership prior to destroying the CompilerInstance. 67 /// 68 /// The compiler instance is intended to simplify clients, but not to lock them 69 /// in to the compiler instance for everything. When possible, utility functions 70 /// come in two forms; a short form that reuses the CompilerInstance objects, 71 /// and a long form that takes explicit instances of any required objects. 72 class CompilerInstance : public ModuleLoader { 73 /// The options used in this compiler instance. 74 std::shared_ptr<CompilerInvocation> Invocation; 75 76 /// The diagnostics engine instance. 77 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics; 78 79 /// The target being compiled for. 80 IntrusiveRefCntPtr<TargetInfo> Target; 81 82 /// Auxiliary Target info. 83 IntrusiveRefCntPtr<TargetInfo> AuxTarget; 84 85 /// The file manager. 86 IntrusiveRefCntPtr<FileManager> FileMgr; 87 88 /// The source manager. 89 IntrusiveRefCntPtr<SourceManager> SourceMgr; 90 91 /// The cache of PCM files. 92 IntrusiveRefCntPtr<InMemoryModuleCache> ModuleCache; 93 94 /// The preprocessor. 95 std::shared_ptr<Preprocessor> PP; 96 97 /// The AST context. 98 IntrusiveRefCntPtr<ASTContext> Context; 99 100 /// An optional sema source that will be attached to sema. 101 IntrusiveRefCntPtr<ExternalSemaSource> ExternalSemaSrc; 102 103 /// The AST consumer. 104 std::unique_ptr<ASTConsumer> Consumer; 105 106 /// The code completion consumer. 107 std::unique_ptr<CodeCompleteConsumer> CompletionConsumer; 108 109 /// The semantic analysis object. 110 std::unique_ptr<Sema> TheSema; 111 112 /// The frontend timer group. 113 std::unique_ptr<llvm::TimerGroup> FrontendTimerGroup; 114 115 /// The frontend timer. 116 std::unique_ptr<llvm::Timer> FrontendTimer; 117 118 /// The ASTReader, if one exists. 119 IntrusiveRefCntPtr<ASTReader> TheASTReader; 120 121 /// The module dependency collector for crashdumps 122 std::shared_ptr<ModuleDependencyCollector> ModuleDepCollector; 123 124 /// The module provider. 125 std::shared_ptr<PCHContainerOperations> ThePCHContainerOperations; 126 127 std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors; 128 129 /// The set of top-level modules that has already been built on the 130 /// fly as part of this overall compilation action. 131 std::map<std::string, std::string, std::less<>> BuiltModules; 132 133 /// Should we delete the BuiltModules when we're done? 134 bool DeleteBuiltModules = true; 135 136 /// The location of the module-import keyword for the last module 137 /// import. 138 SourceLocation LastModuleImportLoc; 139 140 /// The result of the last module import. 141 /// 142 ModuleLoadResult LastModuleImportResult; 143 144 /// Whether we should (re)build the global module index once we 145 /// have finished with this translation unit. 146 bool BuildGlobalModuleIndex = false; 147 148 /// We have a full global module index, with all modules. 149 bool HaveFullGlobalModuleIndex = false; 150 151 /// One or more modules failed to build. 152 bool DisableGeneratingGlobalModuleIndex = false; 153 154 /// The stream for verbose output if owned, otherwise nullptr. 155 std::unique_ptr<raw_ostream> OwnedVerboseOutputStream; 156 157 /// The stream for verbose output. 158 raw_ostream *VerboseOutputStream = &llvm::errs(); 159 160 /// Holds information about the output file. 161 /// 162 /// If TempFilename is not empty we must rename it to Filename at the end. 163 /// TempFilename may be empty and Filename non-empty if creating the temporary 164 /// failed. 165 struct OutputFile { 166 std::string Filename; 167 Optional<llvm::sys::fs::TempFile> File; 168 169 OutputFile(std::string filename, Optional<llvm::sys::fs::TempFile> file) 170 : Filename(std::move(filename)), File(std::move(file)) {} 171 }; 172 173 /// The list of active output files. 174 std::list<OutputFile> OutputFiles; 175 176 /// Force an output buffer. 177 std::unique_ptr<llvm::raw_pwrite_stream> OutputStream; 178 179 CompilerInstance(const CompilerInstance &) = delete; 180 void operator=(const CompilerInstance &) = delete; 181 public: 182 explicit CompilerInstance( 183 std::shared_ptr<PCHContainerOperations> PCHContainerOps = 184 std::make_shared<PCHContainerOperations>(), 185 InMemoryModuleCache *SharedModuleCache = nullptr); 186 ~CompilerInstance() override; 187 188 /// @name High-Level Operations 189 /// { 190 191 /// ExecuteAction - Execute the provided action against the compiler's 192 /// CompilerInvocation object. 193 /// 194 /// This function makes the following assumptions: 195 /// 196 /// - The invocation options should be initialized. This function does not 197 /// handle the '-help' or '-version' options, clients should handle those 198 /// directly. 199 /// 200 /// - The diagnostics engine should have already been created by the client. 201 /// 202 /// - No other CompilerInstance state should have been initialized (this is 203 /// an unchecked error). 204 /// 205 /// - Clients should have initialized any LLVM target features that may be 206 /// required. 207 /// 208 /// - Clients should eventually call llvm_shutdown() upon the completion of 209 /// this routine to ensure that any managed objects are properly destroyed. 210 /// 211 /// Note that this routine may write output to 'stderr'. 212 /// 213 /// \param Act - The action to execute. 214 /// \return - True on success. 215 // 216 // FIXME: Eliminate the llvm_shutdown requirement, that should either be part 217 // of the context or else not CompilerInstance specific. 218 bool ExecuteAction(FrontendAction &Act); 219 220 /// Load the list of plugins requested in the \c FrontendOptions. 221 void LoadRequestedPlugins(); 222 223 /// } 224 /// @name Compiler Invocation and Options 225 /// { 226 227 bool hasInvocation() const { return Invocation != nullptr; } 228 229 CompilerInvocation &getInvocation() { 230 assert(Invocation && "Compiler instance has no invocation!"); 231 return *Invocation; 232 } 233 234 /// setInvocation - Replace the current invocation. 235 void setInvocation(std::shared_ptr<CompilerInvocation> Value); 236 237 /// Indicates whether we should (re)build the global module index. 238 bool shouldBuildGlobalModuleIndex() const; 239 240 /// Set the flag indicating whether we should (re)build the global 241 /// module index. 242 void setBuildGlobalModuleIndex(bool Build) { 243 BuildGlobalModuleIndex = Build; 244 } 245 246 /// } 247 /// @name Forwarding Methods 248 /// { 249 250 AnalyzerOptionsRef getAnalyzerOpts() { 251 return Invocation->getAnalyzerOpts(); 252 } 253 254 CodeGenOptions &getCodeGenOpts() { 255 return Invocation->getCodeGenOpts(); 256 } 257 const CodeGenOptions &getCodeGenOpts() const { 258 return Invocation->getCodeGenOpts(); 259 } 260 261 DependencyOutputOptions &getDependencyOutputOpts() { 262 return Invocation->getDependencyOutputOpts(); 263 } 264 const DependencyOutputOptions &getDependencyOutputOpts() const { 265 return Invocation->getDependencyOutputOpts(); 266 } 267 268 DiagnosticOptions &getDiagnosticOpts() { 269 return Invocation->getDiagnosticOpts(); 270 } 271 const DiagnosticOptions &getDiagnosticOpts() const { 272 return Invocation->getDiagnosticOpts(); 273 } 274 275 FileSystemOptions &getFileSystemOpts() { 276 return Invocation->getFileSystemOpts(); 277 } 278 const FileSystemOptions &getFileSystemOpts() const { 279 return Invocation->getFileSystemOpts(); 280 } 281 282 FrontendOptions &getFrontendOpts() { 283 return Invocation->getFrontendOpts(); 284 } 285 const FrontendOptions &getFrontendOpts() const { 286 return Invocation->getFrontendOpts(); 287 } 288 289 HeaderSearchOptions &getHeaderSearchOpts() { 290 return Invocation->getHeaderSearchOpts(); 291 } 292 const HeaderSearchOptions &getHeaderSearchOpts() const { 293 return Invocation->getHeaderSearchOpts(); 294 } 295 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const { 296 return Invocation->getHeaderSearchOptsPtr(); 297 } 298 299 LangOptions &getLangOpts() { 300 return *Invocation->getLangOpts(); 301 } 302 const LangOptions &getLangOpts() const { 303 return *Invocation->getLangOpts(); 304 } 305 306 PreprocessorOptions &getPreprocessorOpts() { 307 return Invocation->getPreprocessorOpts(); 308 } 309 const PreprocessorOptions &getPreprocessorOpts() const { 310 return Invocation->getPreprocessorOpts(); 311 } 312 313 PreprocessorOutputOptions &getPreprocessorOutputOpts() { 314 return Invocation->getPreprocessorOutputOpts(); 315 } 316 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const { 317 return Invocation->getPreprocessorOutputOpts(); 318 } 319 320 TargetOptions &getTargetOpts() { 321 return Invocation->getTargetOpts(); 322 } 323 const TargetOptions &getTargetOpts() const { 324 return Invocation->getTargetOpts(); 325 } 326 327 /// } 328 /// @name Diagnostics Engine 329 /// { 330 331 bool hasDiagnostics() const { return Diagnostics != nullptr; } 332 333 /// Get the current diagnostics engine. 334 DiagnosticsEngine &getDiagnostics() const { 335 assert(Diagnostics && "Compiler instance has no diagnostics!"); 336 return *Diagnostics; 337 } 338 339 /// setDiagnostics - Replace the current diagnostics engine. 340 void setDiagnostics(DiagnosticsEngine *Value); 341 342 DiagnosticConsumer &getDiagnosticClient() const { 343 assert(Diagnostics && Diagnostics->getClient() && 344 "Compiler instance has no diagnostic client!"); 345 return *Diagnostics->getClient(); 346 } 347 348 /// } 349 /// @name VerboseOutputStream 350 /// } 351 352 /// Replace the current stream for verbose output. 353 void setVerboseOutputStream(raw_ostream &Value); 354 355 /// Replace the current stream for verbose output. 356 void setVerboseOutputStream(std::unique_ptr<raw_ostream> Value); 357 358 /// Get the current stream for verbose output. 359 raw_ostream &getVerboseOutputStream() { 360 return *VerboseOutputStream; 361 } 362 363 /// } 364 /// @name Target Info 365 /// { 366 367 bool hasTarget() const { return Target != nullptr; } 368 369 TargetInfo &getTarget() const { 370 assert(Target && "Compiler instance has no target!"); 371 return *Target; 372 } 373 374 /// Replace the current Target. 375 void setTarget(TargetInfo *Value); 376 377 /// } 378 /// @name AuxTarget Info 379 /// { 380 381 TargetInfo *getAuxTarget() const { return AuxTarget.get(); } 382 383 /// Replace the current AuxTarget. 384 void setAuxTarget(TargetInfo *Value); 385 386 // Create Target and AuxTarget based on current options 387 bool createTarget(); 388 389 /// } 390 /// @name Virtual File System 391 /// { 392 393 llvm::vfs::FileSystem &getVirtualFileSystem() const; 394 395 /// } 396 /// @name File Manager 397 /// { 398 399 bool hasFileManager() const { return FileMgr != nullptr; } 400 401 /// Return the current file manager to the caller. 402 FileManager &getFileManager() const { 403 assert(FileMgr && "Compiler instance has no file manager!"); 404 return *FileMgr; 405 } 406 407 void resetAndLeakFileManager() { 408 llvm::BuryPointer(FileMgr.get()); 409 FileMgr.resetWithoutRelease(); 410 } 411 412 /// Replace the current file manager and virtual file system. 413 void setFileManager(FileManager *Value); 414 415 /// } 416 /// @name Source Manager 417 /// { 418 419 bool hasSourceManager() const { return SourceMgr != nullptr; } 420 421 /// Return the current source manager. 422 SourceManager &getSourceManager() const { 423 assert(SourceMgr && "Compiler instance has no source manager!"); 424 return *SourceMgr; 425 } 426 427 void resetAndLeakSourceManager() { 428 llvm::BuryPointer(SourceMgr.get()); 429 SourceMgr.resetWithoutRelease(); 430 } 431 432 /// setSourceManager - Replace the current source manager. 433 void setSourceManager(SourceManager *Value); 434 435 /// } 436 /// @name Preprocessor 437 /// { 438 439 bool hasPreprocessor() const { return PP != nullptr; } 440 441 /// Return the current preprocessor. 442 Preprocessor &getPreprocessor() const { 443 assert(PP && "Compiler instance has no preprocessor!"); 444 return *PP; 445 } 446 447 std::shared_ptr<Preprocessor> getPreprocessorPtr() { return PP; } 448 449 void resetAndLeakPreprocessor() { 450 llvm::BuryPointer(new std::shared_ptr<Preprocessor>(PP)); 451 } 452 453 /// Replace the current preprocessor. 454 void setPreprocessor(std::shared_ptr<Preprocessor> Value); 455 456 /// } 457 /// @name ASTContext 458 /// { 459 460 bool hasASTContext() const { return Context != nullptr; } 461 462 ASTContext &getASTContext() const { 463 assert(Context && "Compiler instance has no AST context!"); 464 return *Context; 465 } 466 467 void resetAndLeakASTContext() { 468 llvm::BuryPointer(Context.get()); 469 Context.resetWithoutRelease(); 470 } 471 472 /// setASTContext - Replace the current AST context. 473 void setASTContext(ASTContext *Value); 474 475 /// Replace the current Sema; the compiler instance takes ownership 476 /// of S. 477 void setSema(Sema *S); 478 479 /// } 480 /// @name ASTConsumer 481 /// { 482 483 bool hasASTConsumer() const { return (bool)Consumer; } 484 485 ASTConsumer &getASTConsumer() const { 486 assert(Consumer && "Compiler instance has no AST consumer!"); 487 return *Consumer; 488 } 489 490 /// takeASTConsumer - Remove the current AST consumer and give ownership to 491 /// the caller. 492 std::unique_ptr<ASTConsumer> takeASTConsumer() { return std::move(Consumer); } 493 494 /// setASTConsumer - Replace the current AST consumer; the compiler instance 495 /// takes ownership of \p Value. 496 void setASTConsumer(std::unique_ptr<ASTConsumer> Value); 497 498 /// } 499 /// @name Semantic analysis 500 /// { 501 bool hasSema() const { return (bool)TheSema; } 502 503 Sema &getSema() const { 504 assert(TheSema && "Compiler instance has no Sema object!"); 505 return *TheSema; 506 } 507 508 std::unique_ptr<Sema> takeSema(); 509 void resetAndLeakSema(); 510 511 /// } 512 /// @name Module Management 513 /// { 514 515 IntrusiveRefCntPtr<ASTReader> getASTReader() const; 516 void setASTReader(IntrusiveRefCntPtr<ASTReader> Reader); 517 518 std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const; 519 void setModuleDepCollector( 520 std::shared_ptr<ModuleDependencyCollector> Collector); 521 522 std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const { 523 return ThePCHContainerOperations; 524 } 525 526 /// Return the appropriate PCHContainerWriter depending on the 527 /// current CodeGenOptions. 528 const PCHContainerWriter &getPCHContainerWriter() const { 529 assert(Invocation && "cannot determine module format without invocation"); 530 StringRef Format = getHeaderSearchOpts().ModuleFormat; 531 auto *Writer = ThePCHContainerOperations->getWriterOrNull(Format); 532 if (!Writer) { 533 if (Diagnostics) 534 Diagnostics->Report(diag::err_module_format_unhandled) << Format; 535 llvm::report_fatal_error("unknown module format"); 536 } 537 return *Writer; 538 } 539 540 /// Return the appropriate PCHContainerReader depending on the 541 /// current CodeGenOptions. 542 const PCHContainerReader &getPCHContainerReader() const { 543 assert(Invocation && "cannot determine module format without invocation"); 544 StringRef Format = getHeaderSearchOpts().ModuleFormat; 545 auto *Reader = ThePCHContainerOperations->getReaderOrNull(Format); 546 if (!Reader) { 547 if (Diagnostics) 548 Diagnostics->Report(diag::err_module_format_unhandled) << Format; 549 llvm::report_fatal_error("unknown module format"); 550 } 551 return *Reader; 552 } 553 554 /// } 555 /// @name Code Completion 556 /// { 557 558 bool hasCodeCompletionConsumer() const { return (bool)CompletionConsumer; } 559 560 CodeCompleteConsumer &getCodeCompletionConsumer() const { 561 assert(CompletionConsumer && 562 "Compiler instance has no code completion consumer!"); 563 return *CompletionConsumer; 564 } 565 566 /// setCodeCompletionConsumer - Replace the current code completion consumer; 567 /// the compiler instance takes ownership of \p Value. 568 void setCodeCompletionConsumer(CodeCompleteConsumer *Value); 569 570 /// } 571 /// @name Frontend timer 572 /// { 573 574 bool hasFrontendTimer() const { return (bool)FrontendTimer; } 575 576 llvm::Timer &getFrontendTimer() const { 577 assert(FrontendTimer && "Compiler instance has no frontend timer!"); 578 return *FrontendTimer; 579 } 580 581 /// } 582 /// @name Output Files 583 /// { 584 585 /// clearOutputFiles - Clear the output file list. The underlying output 586 /// streams must have been closed beforehand. 587 /// 588 /// \param EraseFiles - If true, attempt to erase the files from disk. 589 void clearOutputFiles(bool EraseFiles); 590 591 /// } 592 /// @name Construction Utility Methods 593 /// { 594 595 /// Create the diagnostics engine using the invocation's diagnostic options 596 /// and replace any existing one with it. 597 /// 598 /// Note that this routine also replaces the diagnostic client, 599 /// allocating one if one is not provided. 600 /// 601 /// \param Client If non-NULL, a diagnostic client that will be 602 /// attached to (and, then, owned by) the DiagnosticsEngine inside this AST 603 /// unit. 604 /// 605 /// \param ShouldOwnClient If Client is non-NULL, specifies whether 606 /// the diagnostic object should take ownership of the client. 607 void createDiagnostics(DiagnosticConsumer *Client = nullptr, 608 bool ShouldOwnClient = true); 609 610 /// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter. 611 /// 612 /// If no diagnostic client is provided, this creates a 613 /// DiagnosticConsumer that is owned by the returned diagnostic 614 /// object, if using directly the caller is responsible for 615 /// releasing the returned DiagnosticsEngine's client eventually. 616 /// 617 /// \param Opts - The diagnostic options; note that the created text 618 /// diagnostic object contains a reference to these options. 619 /// 620 /// \param Client If non-NULL, a diagnostic client that will be 621 /// attached to (and, then, owned by) the returned DiagnosticsEngine 622 /// object. 623 /// 624 /// \param CodeGenOpts If non-NULL, the code gen options in use, which may be 625 /// used by some diagnostics printers (for logging purposes only). 626 /// 627 /// \return The new object on success, or null on failure. 628 static IntrusiveRefCntPtr<DiagnosticsEngine> 629 createDiagnostics(DiagnosticOptions *Opts, 630 DiagnosticConsumer *Client = nullptr, 631 bool ShouldOwnClient = true, 632 const CodeGenOptions *CodeGenOpts = nullptr); 633 634 /// Create the file manager and replace any existing one with it. 635 /// 636 /// \return The new file manager on success, or null on failure. 637 FileManager * 638 createFileManager(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr); 639 640 /// Create the source manager and replace any existing one with it. 641 void createSourceManager(FileManager &FileMgr); 642 643 /// Create the preprocessor, using the invocation, file, and source managers, 644 /// and replace any existing one with it. 645 void createPreprocessor(TranslationUnitKind TUKind); 646 647 std::string getSpecificModuleCachePath(StringRef ModuleHash); 648 std::string getSpecificModuleCachePath() { 649 return getSpecificModuleCachePath(getInvocation().getModuleHash()); 650 } 651 652 /// Create the AST context. 653 void createASTContext(); 654 655 /// Create an external AST source to read a PCH file and attach it to the AST 656 /// context. 657 void createPCHExternalASTSource( 658 StringRef Path, DisableValidationForModuleKind DisableValidation, 659 bool AllowPCHWithCompilerErrors, void *DeserializationListener, 660 bool OwnDeserializationListener); 661 662 /// Create an external AST source to read a PCH file. 663 /// 664 /// \return - The new object on success, or null on failure. 665 static IntrusiveRefCntPtr<ASTReader> createPCHExternalASTSource( 666 StringRef Path, StringRef Sysroot, 667 DisableValidationForModuleKind DisableValidation, 668 bool AllowPCHWithCompilerErrors, Preprocessor &PP, 669 InMemoryModuleCache &ModuleCache, ASTContext &Context, 670 const PCHContainerReader &PCHContainerRdr, 671 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 672 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors, 673 void *DeserializationListener, bool OwnDeserializationListener, 674 bool Preamble, bool UseGlobalModuleIndex); 675 676 /// Create a code completion consumer using the invocation; note that this 677 /// will cause the source manager to truncate the input source file at the 678 /// completion point. 679 void createCodeCompletionConsumer(); 680 681 /// Create a code completion consumer to print code completion results, at 682 /// \p Filename, \p Line, and \p Column, to the given output stream \p OS. 683 static CodeCompleteConsumer *createCodeCompletionConsumer( 684 Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column, 685 const CodeCompleteOptions &Opts, raw_ostream &OS); 686 687 /// Create the Sema object to be used for parsing. 688 void createSema(TranslationUnitKind TUKind, 689 CodeCompleteConsumer *CompletionConsumer); 690 691 /// Create the frontend timer and replace any existing one with it. 692 void createFrontendTimer(); 693 694 /// Create the default output file (from the invocation's options) and add it 695 /// to the list of tracked output files. 696 /// 697 /// The files created by this are usually removed on signal, and, depending 698 /// on FrontendOptions, may also use a temporary file (that is, the data is 699 /// written to a temporary file which will atomically replace the target 700 /// output on success). 701 /// 702 /// \return - Null on error. 703 std::unique_ptr<raw_pwrite_stream> createDefaultOutputFile( 704 bool Binary = true, StringRef BaseInput = "", StringRef Extension = "", 705 bool RemoveFileOnSignal = true, bool CreateMissingDirectories = false, 706 bool ForceUseTemporary = false); 707 708 /// Create a new output file, optionally deriving the output path name, and 709 /// add it to the list of tracked output files. 710 /// 711 /// \return - Null on error. 712 std::unique_ptr<raw_pwrite_stream> 713 createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, 714 bool UseTemporary, bool CreateMissingDirectories = false); 715 716 private: 717 /// Create a new output file and add it to the list of tracked output files. 718 /// 719 /// If \p OutputPath is empty, then createOutputFile will derive an output 720 /// path location as \p BaseInput, with any suffix removed, and \p Extension 721 /// appended. If \p OutputPath is not stdout and \p UseTemporary 722 /// is true, createOutputFile will create a new temporary file that must be 723 /// renamed to \p OutputPath in the end. 724 /// 725 /// \param OutputPath - If given, the path to the output file. 726 /// \param Binary - The mode to open the file in. 727 /// \param RemoveFileOnSignal - Whether the file should be registered with 728 /// llvm::sys::RemoveFileOnSignal. Note that this is not safe for 729 /// multithreaded use, as the underlying signal mechanism is not reentrant 730 /// \param UseTemporary - Create a new temporary file that must be renamed to 731 /// OutputPath in the end. 732 /// \param CreateMissingDirectories - When \p UseTemporary is true, create 733 /// missing directories in the output path. 734 Expected<std::unique_ptr<raw_pwrite_stream>> 735 createOutputFileImpl(StringRef OutputPath, bool Binary, 736 bool RemoveFileOnSignal, bool UseTemporary, 737 bool CreateMissingDirectories); 738 739 public: 740 std::unique_ptr<raw_pwrite_stream> createNullOutputFile(); 741 742 /// } 743 /// @name Initialization Utility Methods 744 /// { 745 746 /// InitializeSourceManager - Initialize the source manager to set InputFile 747 /// as the main file. 748 /// 749 /// \return True on success. 750 bool InitializeSourceManager(const FrontendInputFile &Input); 751 752 /// InitializeSourceManager - Initialize the source manager to set InputFile 753 /// as the main file. 754 /// 755 /// \return True on success. 756 static bool InitializeSourceManager(const FrontendInputFile &Input, 757 DiagnosticsEngine &Diags, 758 FileManager &FileMgr, 759 SourceManager &SourceMgr); 760 761 /// } 762 763 void setOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> OutStream) { 764 OutputStream = std::move(OutStream); 765 } 766 767 std::unique_ptr<llvm::raw_pwrite_stream> takeOutputStream() { 768 return std::move(OutputStream); 769 } 770 771 void createASTReader(); 772 773 bool loadModuleFile(StringRef FileName); 774 775 private: 776 /// Find a module, potentially compiling it, before reading its AST. This is 777 /// the guts of loadModule. 778 /// 779 /// For prebuilt modules, the Module is not expected to exist in 780 /// HeaderSearch's ModuleMap. If a ModuleFile by that name is in the 781 /// ModuleManager, then it will be loaded and looked up. 782 /// 783 /// For implicit modules, the Module is expected to already be in the 784 /// ModuleMap. First attempt to load it from the given path on disk. If that 785 /// fails, defer to compileModuleAndReadAST, which will first build and then 786 /// load it. 787 ModuleLoadResult findOrCompileModuleAndReadAST(StringRef ModuleName, 788 SourceLocation ImportLoc, 789 SourceLocation ModuleNameLoc, 790 bool IsInclusionDirective); 791 792 public: 793 ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, 794 Module::NameVisibilityKind Visibility, 795 bool IsInclusionDirective) override; 796 797 void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName, 798 StringRef Source) override; 799 800 void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility, 801 SourceLocation ImportLoc) override; 802 803 bool hadModuleLoaderFatalFailure() const { 804 return ModuleLoader::HadFatalFailure; 805 } 806 807 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override; 808 809 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override; 810 811 void addDependencyCollector(std::shared_ptr<DependencyCollector> Listener) { 812 DependencyCollectors.push_back(std::move(Listener)); 813 } 814 815 void setExternalSemaSource(IntrusiveRefCntPtr<ExternalSemaSource> ESS); 816 817 InMemoryModuleCache &getModuleCache() const { return *ModuleCache; } 818 }; 819 820 } // end namespace clang 821 822 #endif 823