CompilerInstance.h revision cbdbbd1ba105e65c5c1afa30c6c64f44adf56ad4
1ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//===-- CompilerInstance.h - Clang Compiler Instance ------------*- C++ -*-===// 2ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// 3ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// The LLVM Compiler Infrastructure 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details. 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Diagnostic.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/SourceManager.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Frontend/CompilerInvocation.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/ModuleLoader.h" 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/ArrayRef.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/DenseMap.h" 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/IntrusiveRefCntPtr.h" 20ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "llvm/ADT/OwningPtr.h" 21ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "llvm/ADT/StringRef.h" 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <cassert> 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <list> 24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include <string> 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <utility> 26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace llvm { 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class raw_fd_ostream; 29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass Timer; 30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace clang { 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ASTContext; 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ASTConsumer; 35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass ASTReader; 36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass CodeCompleteConsumer; 37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass DiagnosticsEngine; 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class DiagnosticConsumer; 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ExternalASTSource; 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FileEntry; 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FileManager; 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FrontendAction; 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class Module; 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Preprocessor; 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Sema; 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SourceManager; 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TargetInfo; 48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// CompilerInstance - Helper class for managing a single instance of the Clang 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// compiler. 51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// The CompilerInstance serves two purposes: 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// (1) It manages the various objects which are necessary to run the compiler, 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// for example the preprocessor, the target information, and the AST 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// context. 56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// (2) It provides utility routines for constructing and manipulating the 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// common Clang objects. 58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// 59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// The compiler instance generally owns the instance of all the objects that it 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// manages. However, clients can still share objects by manually setting the 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// object and retaking ownership prior to destroying the CompilerInstance. 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// The compiler instance is intended to simplify clients, but not to lock them 64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// in to the compiler instance for everything. When possible, utility functions 65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// come in two forms; a short form that reuses the CompilerInstance objects, 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// and a long form that takes explicit instances of any required objects. 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class CompilerInstance : public ModuleLoader { 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// The options used in this compiler instance. 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IntrusiveRefCntPtr<CompilerInvocation> Invocation; 70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The diagnostics engine instance. 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics; 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// The target being compiled for. 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IntrusiveRefCntPtr<TargetInfo> Target; 76eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The file manager. 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IntrusiveRefCntPtr<FileManager> FileMgr; 79eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The source manager. 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IntrusiveRefCntPtr<SourceManager> SourceMgr; 82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// The preprocessor. 84eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IntrusiveRefCntPtr<Preprocessor> PP; 85eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 86eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The AST context. 87eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IntrusiveRefCntPtr<ASTContext> Context; 88eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 89eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The AST consumer. 90eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch OwningPtr<ASTConsumer> Consumer; 91eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 92eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The code completion consumer. 93eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch OwningPtr<CodeCompleteConsumer> CompletionConsumer; 94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief The semantic analysis object. 96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch OwningPtr<Sema> TheSema; 97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief The frontend timer 99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch OwningPtr<llvm::Timer> FrontendTimer; 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief Non-owning reference to the ASTReader, if one exists. 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ASTReader *ModuleManager; 103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief The set of top-level modules that has already been loaded, 105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// along with the module map 106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch llvm::DenseMap<const IdentifierInfo *, Module *> KnownModules; 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief The location of the module-import keyword for the last module 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// import. 110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch SourceLocation LastModuleImportLoc; 111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief The result of the last module import. 113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ModuleLoadResult LastModuleImportResult; 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Whether we should (re)build the global module index once we 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// have finished with this translation unit. 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool BuildGlobalModuleIndex; 119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 120eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief One or more modules failed to build. 121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch bool ModuleBuildFailed; 122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \brief Holds information about the output file. 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// If TempFilename is not empty we must rename it to Filename at the end. 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// TempFilename may be empty and Filename non empty if creating the temporary 127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// failed. 128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch struct OutputFile { 129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string Filename; 130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string TempFilename; 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) raw_ostream *OS; 132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch OutputFile(const std::string &filename, const std::string &tempFilename, 134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch raw_ostream *os) 135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch : Filename(filename), TempFilename(tempFilename), OS(os) { } 136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch }; 137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The list of active output files. 139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::list<OutputFile> OutputFiles; 140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CompilerInstance(const CompilerInstance &) LLVM_DELETED_FUNCTION; 142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void operator=(const CompilerInstance &) LLVM_DELETED_FUNCTION; 143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochpublic: 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CompilerInstance(); 145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ~CompilerInstance(); 146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// @name High-Level Operations 148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// { 1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 150eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// ExecuteAction - Execute the provided action against the compiler's 151eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// CompilerInvocation object. 152eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// This function makes the following assumptions: 154eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 155eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// - The invocation options should be initialized. This function does not 156eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// handle the '-help' or '-version' options, clients should handle those 157eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// directly. 158eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 159eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// - The diagnostics engine should have already been created by the client. 160eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// - No other CompilerInstance state should have been initialized (this is 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// an unchecked error). 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// 164eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// - Clients should have initialized any LLVM target features that may be 165eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// required. 166eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 167eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// - Clients should eventually call llvm_shutdown() upon the completion of 168eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// this routine to ensure that any managed objects are properly destroyed. 169eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 170eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// Note that this routine may write output to 'stderr'. 171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// 172eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \param Act - The action to execute. 173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// \return - True on success. 174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // 175ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // FIXME: This function should take the stream to write any debugging / 176eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // verbose output to as an argument. 177eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // 178eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // FIXME: Eliminate the llvm_shutdown requirement, that should either be part 179eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // of the context or else not CompilerInstance specific. 180eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch bool ExecuteAction(FrontendAction &Act); 181eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 182eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// } 183eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// @name Compiler Invocation and Options 184eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// { 185eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 186eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch bool hasInvocation() const { return Invocation != 0; } 187eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 188eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CompilerInvocation &getInvocation() { 189eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch assert(Invocation && "Compiler instance has no invocation!"); 190eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return *Invocation; 1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 192eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 193 /// setInvocation - Replace the current invocation. 194 void setInvocation(CompilerInvocation *Value); 195 196 /// \brief Indicates whether we should (re)build the global module index. 197 bool shouldBuildGlobalModuleIndex() const; 198 199 /// \brief Set the flag indicating whether we should (re)build the global 200 /// module index. 201 void setBuildGlobalModuleIndex(bool Build) { 202 BuildGlobalModuleIndex = Build; 203 } 204 205 /// } 206 /// @name Forwarding Methods 207 /// { 208 209 AnalyzerOptionsRef getAnalyzerOpts() { 210 return Invocation->getAnalyzerOpts(); 211 } 212 213 CodeGenOptions &getCodeGenOpts() { 214 return Invocation->getCodeGenOpts(); 215 } 216 const CodeGenOptions &getCodeGenOpts() const { 217 return Invocation->getCodeGenOpts(); 218 } 219 220 DependencyOutputOptions &getDependencyOutputOpts() { 221 return Invocation->getDependencyOutputOpts(); 222 } 223 const DependencyOutputOptions &getDependencyOutputOpts() const { 224 return Invocation->getDependencyOutputOpts(); 225 } 226 227 DiagnosticOptions &getDiagnosticOpts() { 228 return Invocation->getDiagnosticOpts(); 229 } 230 const DiagnosticOptions &getDiagnosticOpts() const { 231 return Invocation->getDiagnosticOpts(); 232 } 233 234 const FileSystemOptions &getFileSystemOpts() const { 235 return Invocation->getFileSystemOpts(); 236 } 237 238 FrontendOptions &getFrontendOpts() { 239 return Invocation->getFrontendOpts(); 240 } 241 const FrontendOptions &getFrontendOpts() const { 242 return Invocation->getFrontendOpts(); 243 } 244 245 HeaderSearchOptions &getHeaderSearchOpts() { 246 return Invocation->getHeaderSearchOpts(); 247 } 248 const HeaderSearchOptions &getHeaderSearchOpts() const { 249 return Invocation->getHeaderSearchOpts(); 250 } 251 252 LangOptions &getLangOpts() { 253 return *Invocation->getLangOpts(); 254 } 255 const LangOptions &getLangOpts() const { 256 return *Invocation->getLangOpts(); 257 } 258 259 PreprocessorOptions &getPreprocessorOpts() { 260 return Invocation->getPreprocessorOpts(); 261 } 262 const PreprocessorOptions &getPreprocessorOpts() const { 263 return Invocation->getPreprocessorOpts(); 264 } 265 266 PreprocessorOutputOptions &getPreprocessorOutputOpts() { 267 return Invocation->getPreprocessorOutputOpts(); 268 } 269 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const { 270 return Invocation->getPreprocessorOutputOpts(); 271 } 272 273 TargetOptions &getTargetOpts() { 274 return Invocation->getTargetOpts(); 275 } 276 const TargetOptions &getTargetOpts() const { 277 return Invocation->getTargetOpts(); 278 } 279 280 /// } 281 /// @name Diagnostics Engine 282 /// { 283 284 bool hasDiagnostics() const { return Diagnostics != 0; } 285 286 /// Get the current diagnostics engine. 287 DiagnosticsEngine &getDiagnostics() const { 288 assert(Diagnostics && "Compiler instance has no diagnostics!"); 289 return *Diagnostics; 290 } 291 292 /// setDiagnostics - Replace the current diagnostics engine. 293 void setDiagnostics(DiagnosticsEngine *Value); 294 295 DiagnosticConsumer &getDiagnosticClient() const { 296 assert(Diagnostics && Diagnostics->getClient() && 297 "Compiler instance has no diagnostic client!"); 298 return *Diagnostics->getClient(); 299 } 300 301 /// } 302 /// @name Target Info 303 /// { 304 305 bool hasTarget() const { return Target != 0; } 306 307 TargetInfo &getTarget() const { 308 assert(Target && "Compiler instance has no target!"); 309 return *Target; 310 } 311 312 /// Replace the current diagnostics engine. 313 void setTarget(TargetInfo *Value); 314 315 /// } 316 /// @name File Manager 317 /// { 318 319 bool hasFileManager() const { return FileMgr != 0; } 320 321 /// Return the current file manager to the caller. 322 FileManager &getFileManager() const { 323 assert(FileMgr && "Compiler instance has no file manager!"); 324 return *FileMgr; 325 } 326 327 void resetAndLeakFileManager() { 328 FileMgr.resetWithoutRelease(); 329 } 330 331 /// setFileManager - Replace the current file manager. 332 void setFileManager(FileManager *Value); 333 334 /// } 335 /// @name Source Manager 336 /// { 337 338 bool hasSourceManager() const { return SourceMgr != 0; } 339 340 /// Return the current source manager. 341 SourceManager &getSourceManager() const { 342 assert(SourceMgr && "Compiler instance has no source manager!"); 343 return *SourceMgr; 344 } 345 346 void resetAndLeakSourceManager() { 347 SourceMgr.resetWithoutRelease(); 348 } 349 350 /// setSourceManager - Replace the current source manager. 351 void setSourceManager(SourceManager *Value); 352 353 /// } 354 /// @name Preprocessor 355 /// { 356 357 bool hasPreprocessor() const { return PP != 0; } 358 359 /// Return the current preprocessor. 360 Preprocessor &getPreprocessor() const { 361 assert(PP && "Compiler instance has no preprocessor!"); 362 return *PP; 363 } 364 365 void resetAndLeakPreprocessor() { 366 PP.resetWithoutRelease(); 367 } 368 369 /// Replace the current preprocessor. 370 void setPreprocessor(Preprocessor *Value); 371 372 /// } 373 /// @name ASTContext 374 /// { 375 376 bool hasASTContext() const { return Context != 0; } 377 378 ASTContext &getASTContext() const { 379 assert(Context && "Compiler instance has no AST context!"); 380 return *Context; 381 } 382 383 void resetAndLeakASTContext() { 384 Context.resetWithoutRelease(); 385 } 386 387 /// setASTContext - Replace the current AST context. 388 void setASTContext(ASTContext *Value); 389 390 /// \brief Replace the current Sema; the compiler instance takes ownership 391 /// of S. 392 void setSema(Sema *S); 393 394 /// } 395 /// @name ASTConsumer 396 /// { 397 398 bool hasASTConsumer() const { return Consumer != 0; } 399 400 ASTConsumer &getASTConsumer() const { 401 assert(Consumer && "Compiler instance has no AST consumer!"); 402 return *Consumer; 403 } 404 405 /// takeASTConsumer - Remove the current AST consumer and give ownership to 406 /// the caller. 407 ASTConsumer *takeASTConsumer() { return Consumer.take(); } 408 409 /// setASTConsumer - Replace the current AST consumer; the compiler instance 410 /// takes ownership of \p Value. 411 void setASTConsumer(ASTConsumer *Value); 412 413 /// } 414 /// @name Semantic analysis 415 /// { 416 bool hasSema() const { return TheSema != 0; } 417 418 Sema &getSema() const { 419 assert(TheSema && "Compiler instance has no Sema object!"); 420 return *TheSema; 421 } 422 423 Sema *takeSema() { return TheSema.take(); } 424 425 /// } 426 /// @name Module Management 427 /// { 428 429 ASTReader *getModuleManager() const { return ModuleManager; } 430 void setModuleManager(ASTReader *Reader) { ModuleManager = Reader; } 431 432 /// } 433 /// @name Code Completion 434 /// { 435 436 bool hasCodeCompletionConsumer() const { return CompletionConsumer != 0; } 437 438 CodeCompleteConsumer &getCodeCompletionConsumer() const { 439 assert(CompletionConsumer && 440 "Compiler instance has no code completion consumer!"); 441 return *CompletionConsumer; 442 } 443 444 /// takeCodeCompletionConsumer - Remove the current code completion consumer 445 /// and give ownership to the caller. 446 CodeCompleteConsumer *takeCodeCompletionConsumer() { 447 return CompletionConsumer.take(); 448 } 449 450 /// setCodeCompletionConsumer - Replace the current code completion consumer; 451 /// the compiler instance takes ownership of \p Value. 452 void setCodeCompletionConsumer(CodeCompleteConsumer *Value); 453 454 /// } 455 /// @name Frontend timer 456 /// { 457 458 bool hasFrontendTimer() const { return FrontendTimer != 0; } 459 460 llvm::Timer &getFrontendTimer() const { 461 assert(FrontendTimer && "Compiler instance has no frontend timer!"); 462 return *FrontendTimer; 463 } 464 465 /// } 466 /// @name Output Files 467 /// { 468 469 /// addOutputFile - Add an output file onto the list of tracked output files. 470 /// 471 /// \param OutFile - The output file info. 472 void addOutputFile(const OutputFile &OutFile); 473 474 /// clearOutputFiles - Clear the output file list, destroying the contained 475 /// output streams. 476 /// 477 /// \param EraseFiles - If true, attempt to erase the files from disk. 478 void clearOutputFiles(bool EraseFiles); 479 480 /// } 481 /// @name Construction Utility Methods 482 /// { 483 484 /// Create the diagnostics engine using the invocation's diagnostic options 485 /// and replace any existing one with it. 486 /// 487 /// Note that this routine also replaces the diagnostic client, 488 /// allocating one if one is not provided. 489 /// 490 /// \param Client If non-NULL, a diagnostic client that will be 491 /// attached to (and, then, owned by) the DiagnosticsEngine inside this AST 492 /// unit. 493 /// 494 /// \param ShouldOwnClient If Client is non-NULL, specifies whether 495 /// the diagnostic object should take ownership of the client. 496 /// 497 /// \param ShouldCloneClient If Client is non-NULL, specifies whether that 498 /// client should be cloned. 499 void createDiagnostics(DiagnosticConsumer *Client = 0, 500 bool ShouldOwnClient = true, 501 bool ShouldCloneClient = true); 502 503 /// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter. 504 /// 505 /// If no diagnostic client is provided, this creates a 506 /// DiagnosticConsumer that is owned by the returned diagnostic 507 /// object, if using directly the caller is responsible for 508 /// releasing the returned DiagnosticsEngine's client eventually. 509 /// 510 /// \param Opts - The diagnostic options; note that the created text 511 /// diagnostic object contains a reference to these options. 512 /// 513 /// \param Client If non-NULL, a diagnostic client that will be 514 /// attached to (and, then, owned by) the returned DiagnosticsEngine 515 /// object. 516 /// 517 /// \param CodeGenOpts If non-NULL, the code gen options in use, which may be 518 /// used by some diagnostics printers (for logging purposes only). 519 /// 520 /// \return The new object on success, or null on failure. 521 static IntrusiveRefCntPtr<DiagnosticsEngine> 522 createDiagnostics(DiagnosticOptions *Opts, 523 DiagnosticConsumer *Client = 0, 524 bool ShouldOwnClient = true, 525 bool ShouldCloneClient = true, 526 const CodeGenOptions *CodeGenOpts = 0); 527 528 /// Create the file manager and replace any existing one with it. 529 void createFileManager(); 530 531 /// Create the source manager and replace any existing one with it. 532 void createSourceManager(FileManager &FileMgr); 533 534 /// Create the preprocessor, using the invocation, file, and source managers, 535 /// and replace any existing one with it. 536 void createPreprocessor(); 537 538 /// Create the AST context. 539 void createASTContext(); 540 541 /// Create an external AST source to read a PCH file and attach it to the AST 542 /// context. 543 void createPCHExternalASTSource(StringRef Path, 544 bool DisablePCHValidation, 545 bool AllowPCHWithCompilerErrors, 546 void *DeserializationListener); 547 548 /// Create an external AST source to read a PCH file. 549 /// 550 /// \return - The new object on success, or null on failure. 551 static ExternalASTSource * 552 createPCHExternalASTSource(StringRef Path, const std::string &Sysroot, 553 bool DisablePCHValidation, 554 bool AllowPCHWithCompilerErrors, 555 Preprocessor &PP, ASTContext &Context, 556 void *DeserializationListener, bool Preamble, 557 bool UseGlobalModuleIndex); 558 559 /// Create a code completion consumer using the invocation; note that this 560 /// will cause the source manager to truncate the input source file at the 561 /// completion point. 562 void createCodeCompletionConsumer(); 563 564 /// Create a code completion consumer to print code completion results, at 565 /// \p Filename, \p Line, and \p Column, to the given output stream \p OS. 566 static CodeCompleteConsumer * 567 createCodeCompletionConsumer(Preprocessor &PP, const std::string &Filename, 568 unsigned Line, unsigned Column, 569 const CodeCompleteOptions &Opts, 570 raw_ostream &OS); 571 572 /// \brief Create the Sema object to be used for parsing. 573 void createSema(TranslationUnitKind TUKind, 574 CodeCompleteConsumer *CompletionConsumer); 575 576 /// Create the frontend timer and replace any existing one with it. 577 void createFrontendTimer(); 578 579 /// Create the default output file (from the invocation's options) and add it 580 /// to the list of tracked output files. 581 /// 582 /// The files created by this function always use temporary files to write to 583 /// their result (that is, the data is written to a temporary file which will 584 /// atomically replace the target output on success). 585 /// 586 /// \return - Null on error. 587 llvm::raw_fd_ostream * 588 createDefaultOutputFile(bool Binary = true, StringRef BaseInput = "", 589 StringRef Extension = ""); 590 591 /// Create a new output file and add it to the list of tracked output files, 592 /// optionally deriving the output path name. 593 /// 594 /// \return - Null on error. 595 llvm::raw_fd_ostream * 596 createOutputFile(StringRef OutputPath, 597 bool Binary = true, bool RemoveFileOnSignal = true, 598 StringRef BaseInput = "", 599 StringRef Extension = "", 600 bool UseTemporary = false, 601 bool CreateMissingDirectories = false); 602 603 /// Create a new output file, optionally deriving the output path name. 604 /// 605 /// If \p OutputPath is empty, then createOutputFile will derive an output 606 /// path location as \p BaseInput, with any suffix removed, and \p Extension 607 /// appended. If \p OutputPath is not stdout and \p UseTemporary 608 /// is true, createOutputFile will create a new temporary file that must be 609 /// renamed to \p OutputPath in the end. 610 /// 611 /// \param OutputPath - If given, the path to the output file. 612 /// \param Error [out] - On failure, the error message. 613 /// \param BaseInput - If \p OutputPath is empty, the input path name to use 614 /// for deriving the output path. 615 /// \param Extension - The extension to use for derived output names. 616 /// \param Binary - The mode to open the file in. 617 /// \param RemoveFileOnSignal - Whether the file should be registered with 618 /// llvm::sys::RemoveFileOnSignal. Note that this is not safe for 619 /// multithreaded use, as the underlying signal mechanism is not reentrant 620 /// \param UseTemporary - Create a new temporary file that must be renamed to 621 /// OutputPath in the end. 622 /// \param CreateMissingDirectories - When \p UseTemporary is true, create 623 /// missing directories in the output path. 624 /// \param ResultPathName [out] - If given, the result path name will be 625 /// stored here on success. 626 /// \param TempPathName [out] - If given, the temporary file path name 627 /// will be stored here on success. 628 static llvm::raw_fd_ostream * 629 createOutputFile(StringRef OutputPath, std::string &Error, 630 bool Binary = true, bool RemoveFileOnSignal = true, 631 StringRef BaseInput = "", 632 StringRef Extension = "", 633 bool UseTemporary = false, 634 bool CreateMissingDirectories = false, 635 std::string *ResultPathName = 0, 636 std::string *TempPathName = 0); 637 638 /// } 639 /// @name Initialization Utility Methods 640 /// { 641 642 /// InitializeSourceManager - Initialize the source manager to set InputFile 643 /// as the main file. 644 /// 645 /// \return True on success. 646 bool InitializeSourceManager(const FrontendInputFile &Input); 647 648 /// InitializeSourceManager - Initialize the source manager to set InputFile 649 /// as the main file. 650 /// 651 /// \return True on success. 652 static bool InitializeSourceManager(const FrontendInputFile &Input, 653 DiagnosticsEngine &Diags, 654 FileManager &FileMgr, 655 SourceManager &SourceMgr, 656 const FrontendOptions &Opts); 657 658 /// } 659 660 virtual ModuleLoadResult loadModule(SourceLocation ImportLoc, 661 ModuleIdPath Path, 662 Module::NameVisibilityKind Visibility, 663 bool IsInclusionDirective); 664 665 virtual void makeModuleVisible(Module *Mod, 666 Module::NameVisibilityKind Visibility, 667 SourceLocation ImportLoc, 668 bool Complain); 669 670}; 671 672} // end namespace clang 673 674#endif 675