CompilerInstance.h revision 0fbb3d9a9cdd2201848be9eb017c54cd78538122
1//===-- CompilerInstance.h - Clang Compiler Instance ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ 11#define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_ 12 13#include "clang/Frontend/CompilerInvocation.h" 14#include "llvm/ADT/OwningPtr.h" 15#include <cassert> 16 17namespace llvm { 18class LLVMContext; 19} 20 21namespace clang { 22class Diagnostic; 23class DiagnosticClient; 24class Preprocessor; 25class FileManager; 26class SourceManager; 27class TargetInfo; 28 29/// CompilerInstance - Helper class for managing a single instance of the Clang 30/// compiler. 31/// 32/// The CompilerInstance serves two purposes: 33/// (1) It manages the various objects which are necessary to run the compiler, 34/// for example the preprocessor, the target information, and the AST 35/// context. 36/// (2) It provides utility routines for constructing and manipulating the 37/// common Clang objects. 38/// 39/// The compiler instance generally owns the instance of all the objects that it 40/// manages. However, clients can still share objects by manually setting the 41/// object and retaking ownership prior to destroying the CompilerInstance. 42/// 43/// The compiler instance is intended to simplify clients, but not to lock them 44/// in to the compiler instance for everything. When possible, utility functions 45/// come in two forms; a short form that reuses the CompilerInstance objects, 46/// and a long form that takes explicit instances of any required objects. 47class CompilerInstance { 48 /// The LLVM context used for this instance. 49 llvm::LLVMContext *LLVMContext; 50 bool OwnsLLVMContext; 51 52 /// The options used in this compiler instance. 53 CompilerInvocation Invocation; 54 55 /// The diagnostics engine instance. 56 llvm::OwningPtr<Diagnostic> Diagnostics; 57 58 /// The diagnostics client instance. 59 llvm::OwningPtr<DiagnosticClient> DiagClient; 60 61 /// The target being compiled for. 62 llvm::OwningPtr<TargetInfo> Target; 63 64 /// The file manager. 65 llvm::OwningPtr<FileManager> FileMgr; 66 67 /// The source manager. 68 llvm::OwningPtr<SourceManager> SourceMgr; 69 70 /// The preprocessor. 71 llvm::OwningPtr<Preprocessor> PP; 72 73public: 74 /// Create a new compiler instance with the given LLVM context, optionally 75 /// taking ownership of it. 76 CompilerInstance(llvm::LLVMContext *_LLVMContext = 0, 77 bool _OwnsLLVMContext = true); 78 ~CompilerInstance(); 79 80 /// @name LLVM Context 81 /// { 82 83 llvm::LLVMContext &getLLVMContext() { 84 assert(LLVMContext && "Compiler instance has no LLVM context!"); 85 return *LLVMContext; 86 } 87 88 /// setLLVMContext - Replace the current LLVM context and take ownership of 89 /// \arg Value. 90 void setLLVMContext(llvm::LLVMContext *Value, bool TakeOwnership = true) { 91 LLVMContext = Value; 92 OwnsLLVMContext = TakeOwnership; 93 } 94 95 /// } 96 /// @name Compiler Invocation and Options 97 /// { 98 99 CompilerInvocation &getInvocation() { return Invocation; } 100 const CompilerInvocation &getInvocation() const { return Invocation; } 101 void setInvocation(const CompilerInvocation &Value) { Invocation = Value; } 102 103 /// } 104 /// @name Forwarding Methods 105 /// { 106 107 AnalyzerOptions &getAnalyzerOpts() { 108 return Invocation.getAnalyzerOpts(); 109 } 110 const AnalyzerOptions &getAnalyzerOpts() const { 111 return Invocation.getAnalyzerOpts(); 112 } 113 114 CodeGenOptions &getCodeGenOpts() { 115 return Invocation.getCodeGenOpts(); 116 } 117 const CodeGenOptions &getCodeGenOpts() const { 118 return Invocation.getCodeGenOpts(); 119 } 120 121 DependencyOutputOptions &getDependencyOutputOpts() { 122 return Invocation.getDependencyOutputOpts(); 123 } 124 const DependencyOutputOptions &getDependencyOutputOpts() const { 125 return Invocation.getDependencyOutputOpts(); 126 } 127 128 DiagnosticOptions &getDiagnosticOpts() { 129 return Invocation.getDiagnosticOpts(); 130 } 131 const DiagnosticOptions &getDiagnosticOpts() const { 132 return Invocation.getDiagnosticOpts(); 133 } 134 135 FrontendOptions &getFrontendOpts() { 136 return Invocation.getFrontendOpts(); 137 } 138 const FrontendOptions &getFrontendOpts() const { 139 return Invocation.getFrontendOpts(); 140 } 141 142 HeaderSearchOptions &getHeaderSearchOpts() { 143 return Invocation.getHeaderSearchOpts(); 144 } 145 const HeaderSearchOptions &getHeaderSearchOpts() const { 146 return Invocation.getHeaderSearchOpts(); 147 } 148 149 LangOptions &getLangOpts() { 150 return Invocation.getLangOpts(); 151 } 152 const LangOptions &getLangOpts() const { 153 return Invocation.getLangOpts(); 154 } 155 156 PreprocessorOptions &getPreprocessorOpts() { 157 return Invocation.getPreprocessorOpts(); 158 } 159 const PreprocessorOptions &getPreprocessorOpts() const { 160 return Invocation.getPreprocessorOpts(); 161 } 162 163 PreprocessorOutputOptions &getPreprocessorOutputOpts() { 164 return Invocation.getPreprocessorOutputOpts(); 165 } 166 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const { 167 return Invocation.getPreprocessorOutputOpts(); 168 } 169 170 /// } 171 /// @name Diagnostics Engine 172 /// { 173 174 Diagnostic &getDiagnostics() const { 175 assert(Diagnostics && "Compiler instance has no diagnostics!"); 176 return *Diagnostics; 177 } 178 179 /// takeDiagnostics - Remove the current diagnostics engine and give ownership 180 /// to the caller. 181 Diagnostic *takeDiagnostics() { return Diagnostics.take(); } 182 183 /// setDiagnostics - Replace the current diagnostics engine; the compiler 184 /// instance takes ownership of \arg Value. 185 void setDiagnostics(Diagnostic *Value) { Diagnostics.reset(Value); } 186 187 DiagnosticClient &getDiagnosticClient() const { return *DiagClient; } 188 189 /// takeDiagnosticClient - Remove the current diagnostics client and give 190 /// ownership to the caller. 191 DiagnosticClient *takeDiagnosticClient() { return DiagClient.take(); } 192 193 /// setDiagnosticClient - Replace the current diagnostics client; the compiler 194 /// instance takes ownership of \arg Value. 195 void setDiagnosticClient(DiagnosticClient *Value) { 196 DiagClient.reset(Value); 197 } 198 199 /// } 200 /// @name Target Info 201 /// { 202 203 TargetInfo &getTarget() const { 204 assert(Target && "Compiler instance has no target!"); 205 return *Target; 206 } 207 208 /// takeTarget - Remove the current diagnostics engine and give ownership 209 /// to the caller. 210 TargetInfo *takeTarget() { return Target.take(); } 211 212 /// setTarget - Replace the current diagnostics engine; the compiler 213 /// instance takes ownership of \arg Value. 214 void setTarget(TargetInfo *Value) { Target.reset(Value); } 215 216 /// } 217 /// @name File Manager 218 /// { 219 220 FileManager &getFileManager() const { 221 assert(FileMgr && "Compiler instance has no file manager!"); 222 return *FileMgr; 223 } 224 225 /// takeFileManager - Remove the current file manager and give ownership to 226 /// the caller. 227 FileManager *takeFileManager() { return FileMgr.take(); } 228 229 /// setFileManager - Replace the current file manager; the compiler instance 230 /// takes ownership of \arg Value. 231 void setFileManager(FileManager *Value) { FileMgr.reset(Value); } 232 233 /// } 234 /// @name Source Manager 235 /// { 236 237 SourceManager &getSourceManager() const { 238 assert(SourceMgr && "Compiler instance has no source manager!"); 239 return *SourceMgr; 240 } 241 242 /// takeSourceManager - Remove the current source manager and give ownership 243 /// to the caller. 244 SourceManager *takeSourceManager() { return SourceMgr.take(); } 245 246 /// setSourceManager - Replace the current source manager; the compiler 247 /// instance takes ownership of \arg Value. 248 void setSourceManager(SourceManager *Value) { SourceMgr.reset(Value); } 249 250 /// } 251 /// @name Preprocessor 252 /// { 253 254 Preprocessor &getPreprocessor() const { 255 assert(PP && "Compiler instance has no preprocessor!"); 256 return *PP; 257 } 258 259 /// takePreprocessor - Remove the current preprocessor and give ownership to 260 /// the caller. 261 Preprocessor *takePreprocessor() { return PP.take(); } 262 263 /// setPreprocessor - Replace the current preprocessor; the compiler instance 264 /// takes ownership of \arg Value. 265 void setPreprocessor(Preprocessor *Value) { PP.reset(Value); } 266 267 /// } 268 /// @name Construction Utility Methods 269 /// { 270 271 /// Create the diagnostics engine using the invocation's diagnostic options 272 /// and replace any existing one with it. 273 /// 274 /// Note that this routine also replaces the diagnostic client. 275 void createDiagnostics(int Argc, char **Argv); 276 277 /// Create a Diagnostic object with a the TextDiagnosticPrinter. 278 /// 279 /// The \arg Argc and \arg Argv arguments are used only for logging purposes, 280 /// when the diagnostic options indicate that the compiler should output 281 /// logging information. 282 /// 283 /// \return The new object on success, or null on failure. 284 static Diagnostic *createDiagnostics(const DiagnosticOptions &Opts, 285 int Argc, char **Argv); 286 287 /// Create the file manager and replace any existing one with it. 288 void createFileManager(); 289 290 /// Create the source manager and replace any existing one with it. 291 void createSourceManager(); 292 293 /// Create the preprocessor, using the invocation, file, and source managers, 294 /// and replace any existing one with it. 295 void createPreprocessor(); 296 297 /// Create a Preprocessor object. 298 /// 299 /// Note that this also creates a new HeaderSearch object which will be owned 300 /// by the resulting Preprocessor. 301 /// 302 /// \return The new object on success, or null on failure. 303 static Preprocessor *createPreprocessor(Diagnostic &, const LangOptions &, 304 const PreprocessorOptions &, 305 const HeaderSearchOptions &, 306 const DependencyOutputOptions &, 307 const TargetInfo &, 308 SourceManager &, FileManager &); 309 310 /// } 311}; 312 313} // end namespace clang 314 315#endif 316