CompilerInstance.h revision 81f5a1e699b2eefa4a5e50b5dfc06df600748f59
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/StringRef.h"
15#include "llvm/ADT/OwningPtr.h"
16#include <cassert>
17#include <list>
18#include <string>
19
20namespace llvm {
21class LLVMContext;
22class raw_ostream;
23class raw_fd_ostream;
24}
25
26namespace clang {
27class ASTContext;
28class ASTConsumer;
29class CodeCompleteConsumer;
30class Diagnostic;
31class DiagnosticClient;
32class ExternalASTSource;
33class FileManager;
34class Preprocessor;
35class Source;
36class SourceManager;
37class TargetInfo;
38
39/// CompilerInstance - Helper class for managing a single instance of the Clang
40/// compiler.
41///
42/// The CompilerInstance serves two purposes:
43///  (1) It manages the various objects which are necessary to run the compiler,
44///      for example the preprocessor, the target information, and the AST
45///      context.
46///  (2) It provides utility routines for constructing and manipulating the
47///      common Clang objects.
48///
49/// The compiler instance generally owns the instance of all the objects that it
50/// manages. However, clients can still share objects by manually setting the
51/// object and retaking ownership prior to destroying the CompilerInstance.
52///
53/// The compiler instance is intended to simplify clients, but not to lock them
54/// in to the compiler instance for everything. When possible, utility functions
55/// come in two forms; a short form that reuses the CompilerInstance objects,
56/// and a long form that takes explicit instances of any required objects.
57class CompilerInstance {
58  /// The LLVM context used for this instance.
59  llvm::LLVMContext *LLVMContext;
60  bool OwnsLLVMContext;
61
62  /// The options used in this compiler instance.
63  CompilerInvocation Invocation;
64
65  /// The diagnostics engine instance.
66  llvm::OwningPtr<Diagnostic> Diagnostics;
67
68  /// The diagnostics client instance.
69  llvm::OwningPtr<DiagnosticClient> DiagClient;
70
71  /// The target being compiled for.
72  llvm::OwningPtr<TargetInfo> Target;
73
74  /// The file manager.
75  llvm::OwningPtr<FileManager> FileMgr;
76
77  /// The source manager.
78  llvm::OwningPtr<SourceManager> SourceMgr;
79
80  /// The preprocessor.
81  llvm::OwningPtr<Preprocessor> PP;
82
83  /// The AST context.
84  llvm::OwningPtr<ASTContext> Context;
85
86  /// The AST consumer.
87  llvm::OwningPtr<ASTConsumer> Consumer;
88
89  /// The code completion consumer.
90  llvm::OwningPtr<CodeCompleteConsumer> CompletionConsumer;
91
92  /// The list of active output files.
93  std::list< std::pair<std::string, llvm::raw_ostream*> > OutputFiles;
94
95public:
96  /// Create a new compiler instance with the given LLVM context, optionally
97  /// taking ownership of it.
98  CompilerInstance(llvm::LLVMContext *_LLVMContext = 0,
99                   bool _OwnsLLVMContext = true);
100  ~CompilerInstance();
101
102  /// @name LLVM Context
103  /// {
104
105  bool hasLLVMContext() const { return LLVMContext != 0; }
106
107  llvm::LLVMContext &getLLVMContext() const {
108    assert(LLVMContext && "Compiler instance has no LLVM context!");
109    return *LLVMContext;
110  }
111
112  /// setLLVMContext - Replace the current LLVM context and take ownership of
113  /// \arg Value.
114  void setLLVMContext(llvm::LLVMContext *Value, bool TakeOwnership = true) {
115    LLVMContext = Value;
116    OwnsLLVMContext = TakeOwnership;
117  }
118
119  /// }
120  /// @name Compiler Invocation and Options
121  /// {
122
123  CompilerInvocation &getInvocation() { return Invocation; }
124  const CompilerInvocation &getInvocation() const { return Invocation; }
125  void setInvocation(const CompilerInvocation &Value) { Invocation = Value; }
126
127  /// }
128  /// @name Forwarding Methods
129  /// {
130
131  AnalyzerOptions &getAnalyzerOpts() {
132    return Invocation.getAnalyzerOpts();
133  }
134  const AnalyzerOptions &getAnalyzerOpts() const {
135    return Invocation.getAnalyzerOpts();
136  }
137
138  CodeGenOptions &getCodeGenOpts() {
139    return Invocation.getCodeGenOpts();
140  }
141  const CodeGenOptions &getCodeGenOpts() const {
142    return Invocation.getCodeGenOpts();
143  }
144
145  DependencyOutputOptions &getDependencyOutputOpts() {
146    return Invocation.getDependencyOutputOpts();
147  }
148  const DependencyOutputOptions &getDependencyOutputOpts() const {
149    return Invocation.getDependencyOutputOpts();
150  }
151
152  DiagnosticOptions &getDiagnosticOpts() {
153    return Invocation.getDiagnosticOpts();
154  }
155  const DiagnosticOptions &getDiagnosticOpts() const {
156    return Invocation.getDiagnosticOpts();
157  }
158
159  FrontendOptions &getFrontendOpts() {
160    return Invocation.getFrontendOpts();
161  }
162  const FrontendOptions &getFrontendOpts() const {
163    return Invocation.getFrontendOpts();
164  }
165
166  HeaderSearchOptions &getHeaderSearchOpts() {
167    return Invocation.getHeaderSearchOpts();
168  }
169  const HeaderSearchOptions &getHeaderSearchOpts() const {
170    return Invocation.getHeaderSearchOpts();
171  }
172
173  LangOptions &getLangOpts() {
174    return Invocation.getLangOpts();
175  }
176  const LangOptions &getLangOpts() const {
177    return Invocation.getLangOpts();
178  }
179
180  PreprocessorOptions &getPreprocessorOpts() {
181    return Invocation.getPreprocessorOpts();
182  }
183  const PreprocessorOptions &getPreprocessorOpts() const {
184    return Invocation.getPreprocessorOpts();
185  }
186
187  PreprocessorOutputOptions &getPreprocessorOutputOpts() {
188    return Invocation.getPreprocessorOutputOpts();
189  }
190  const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
191    return Invocation.getPreprocessorOutputOpts();
192  }
193
194  /// }
195  /// @name Diagnostics Engine
196  /// {
197
198  bool hasDiagnostics() const { return Diagnostics != 0; }
199
200  Diagnostic &getDiagnostics() const {
201    assert(Diagnostics && "Compiler instance has no diagnostics!");
202    return *Diagnostics;
203  }
204
205  /// takeDiagnostics - Remove the current diagnostics engine and give ownership
206  /// to the caller.
207  Diagnostic *takeDiagnostics() { return Diagnostics.take(); }
208
209  /// setDiagnostics - Replace the current diagnostics engine; the compiler
210  /// instance takes ownership of \arg Value.
211  void setDiagnostics(Diagnostic *Value);
212
213  DiagnosticClient &getDiagnosticClient() const {
214    assert(Target && "Compiler instance has no diagnostic client!");
215    return *DiagClient;
216  }
217
218  /// takeDiagnosticClient - Remove the current diagnostics client and give
219  /// ownership to the caller.
220  DiagnosticClient *takeDiagnosticClient() { return DiagClient.take(); }
221
222  /// setDiagnosticClient - Replace the current diagnostics client; the compiler
223  /// instance takes ownership of \arg Value.
224  void setDiagnosticClient(DiagnosticClient *Value);
225
226  /// }
227  /// @name Target Info
228  /// {
229
230  bool hasTarget() const { return Target != 0; }
231
232  TargetInfo &getTarget() const {
233    assert(Target && "Compiler instance has no target!");
234    return *Target;
235  }
236
237  /// takeTarget - Remove the current diagnostics engine and give ownership
238  /// to the caller.
239  TargetInfo *takeTarget() { return Target.take(); }
240
241  /// setTarget - Replace the current diagnostics engine; the compiler
242  /// instance takes ownership of \arg Value.
243  void setTarget(TargetInfo *Value);
244
245  /// }
246  /// @name File Manager
247  /// {
248
249  bool hasFileManager() const { return FileMgr != 0; }
250
251  FileManager &getFileManager() const {
252    assert(FileMgr && "Compiler instance has no file manager!");
253    return *FileMgr;
254  }
255
256  /// takeFileManager - Remove the current file manager and give ownership to
257  /// the caller.
258  FileManager *takeFileManager() { return FileMgr.take(); }
259
260  /// setFileManager - Replace the current file manager; the compiler instance
261  /// takes ownership of \arg Value.
262  void setFileManager(FileManager *Value);
263
264  /// }
265  /// @name Source Manager
266  /// {
267
268  bool hasSourceManager() const { return SourceMgr != 0; }
269
270  SourceManager &getSourceManager() const {
271    assert(SourceMgr && "Compiler instance has no source manager!");
272    return *SourceMgr;
273  }
274
275  /// takeSourceManager - Remove the current source manager and give ownership
276  /// to the caller.
277  SourceManager *takeSourceManager() { return SourceMgr.take(); }
278
279  /// setSourceManager - Replace the current source manager; the compiler
280  /// instance takes ownership of \arg Value.
281  void setSourceManager(SourceManager *Value);
282
283  /// }
284  /// @name Preprocessor
285  /// {
286
287  bool hasPreprocessor() const { return PP != 0; }
288
289  Preprocessor &getPreprocessor() const {
290    assert(PP && "Compiler instance has no preprocessor!");
291    return *PP;
292  }
293
294  /// takePreprocessor - Remove the current preprocessor and give ownership to
295  /// the caller.
296  Preprocessor *takePreprocessor() { return PP.take(); }
297
298  /// setPreprocessor - Replace the current preprocessor; the compiler instance
299  /// takes ownership of \arg Value.
300  void setPreprocessor(Preprocessor *Value);
301
302  /// }
303  /// @name ASTContext
304  /// {
305
306  bool hasASTContext() const { return Context != 0; }
307
308  ASTContext &getASTContext() const {
309    assert(Context && "Compiler instance has no AST context!");
310    return *Context;
311  }
312
313  /// takeASTContext - Remove the current AST context and give ownership to the
314  /// caller.
315  ASTContext *takeASTContext() { return Context.take(); }
316
317  /// setASTContext - Replace the current AST context; the compiler instance
318  /// takes ownership of \arg Value.
319  void setASTContext(ASTContext *Value);
320
321  /// }
322  /// @name ASTConsumer
323  /// {
324
325  bool hasASTConsumer() const { return Consumer != 0; }
326
327  ASTConsumer &getASTConsumer() const {
328    assert(Consumer && "Compiler instance has no AST consumer!");
329    return *Consumer;
330  }
331
332  /// takeASTConsumer - Remove the current AST consumer and give ownership to
333  /// the caller.
334  ASTConsumer *takeASTConsumer() { return Consumer.take(); }
335
336  /// setASTConsumer - Replace the current AST consumer; the compiler instance
337  /// takes ownership of \arg Value.
338  void setASTConsumer(ASTConsumer *Value);
339
340  /// }
341  /// @name Code Completion
342  /// {
343
344  bool hasCodeCompletionConsumer() const { return CompletionConsumer != 0; }
345
346  CodeCompleteConsumer &getCodeCompletionConsumer() const {
347    assert(CompletionConsumer &&
348           "Compiler instance has no code completion consumer!");
349    return *CompletionConsumer;
350  }
351
352  /// takeCodeCompletionConsumer - Remove the current code completion consumer
353  /// and give ownership to the caller.
354  CodeCompleteConsumer *takeCodeCompletionConsumer() {
355    return CompletionConsumer.take();
356  }
357
358  /// setCodeCompletionConsumer - Replace the current code completion consumer;
359  /// the compiler instance takes ownership of \arg Value.
360  void setCodeCompletionConsumer(CodeCompleteConsumer *Value);
361
362  /// }
363  /// @name Output Files
364  /// {
365
366  /// getOutputFileList - Get the list of (path, output stream) pairs of output
367  /// files; the path may be empty but the stream will always be non-null.
368  const std::list< std::pair<std::string,
369                             llvm::raw_ostream*> > &getOutputFileList() const;
370
371  /// addOutputFile - Add an output file onto the list of tracked output files.
372  ///
373  /// \param Path - The path to the output file, or empty.
374  /// \param OS - The output stream, which should be non-null.
375  void addOutputFile(llvm::StringRef Path, llvm::raw_ostream *OS);
376
377  /// ClearOutputFiles - Clear the output file list, destroying the contained
378  /// output streams.
379  ///
380  /// \param EraseFiles - If true, attempt to erase the files from disk.
381  void ClearOutputFiles(bool EraseFiles);
382
383  /// }
384  /// @name Construction Utility Methods
385  /// {
386
387  /// Create the diagnostics engine using the invocation's diagnostic options
388  /// and replace any existing one with it.
389  ///
390  /// Note that this routine also replaces the diagnostic client.
391  void createDiagnostics(int Argc, char **Argv);
392
393  /// Create a Diagnostic object with a the TextDiagnosticPrinter.
394  ///
395  /// The \arg Argc and \arg Argv arguments are used only for logging purposes,
396  /// when the diagnostic options indicate that the compiler should output
397  /// logging information.
398  ///
399  /// Note that this creates an unowned DiagnosticClient, if using directly the
400  /// caller is responsible for releaseing the returned Diagnostic's client
401  /// eventually.
402  ///
403  /// \return The new object on success, or null on failure.
404  static Diagnostic *createDiagnostics(const DiagnosticOptions &Opts,
405                                       int Argc, char **Argv);
406
407  /// Create the file manager and replace any existing one with it.
408  void createFileManager();
409
410  /// Create the source manager and replace any existing one with it.
411  void createSourceManager();
412
413  /// Create the preprocessor, using the invocation, file, and source managers,
414  /// and replace any existing one with it.
415  void createPreprocessor();
416
417  /// Create a Preprocessor object.
418  ///
419  /// Note that this also creates a new HeaderSearch object which will be owned
420  /// by the resulting Preprocessor.
421  ///
422  /// \return The new object on success, or null on failure.
423  static Preprocessor *createPreprocessor(Diagnostic &, const LangOptions &,
424                                          const PreprocessorOptions &,
425                                          const HeaderSearchOptions &,
426                                          const DependencyOutputOptions &,
427                                          const TargetInfo &,
428                                          SourceManager &, FileManager &);
429
430  /// Create the AST context.
431  void createASTContext();
432
433  /// Create an external AST source to read a PCH file and attach it to the AST
434  /// context.
435  void createPCHExternalASTSource(llvm::StringRef Path);
436
437  /// Create an external AST source to read a PCH file.
438  ///
439  /// \return - The new object on success, or null on failure.
440  static ExternalASTSource *
441  createPCHExternalASTSource(llvm::StringRef Path, const std::string &Sysroot,
442                             Preprocessor &PP, ASTContext &Context);
443
444  /// Create a code completion consumer using the invocation; note that this
445  /// will cause the source manager to truncate the input source file at the
446  /// completion point.
447  void createCodeCompletionConsumer();
448
449  /// Create a code completion consumer to print code completion results, at
450  /// \arg Filename, \arg Line, and \arg Column, to the given output stream \arg
451  /// OS.
452  static CodeCompleteConsumer *
453  createCodeCompletionConsumer(Preprocessor &PP, const std::string &Filename,
454                               unsigned Line, unsigned Column,
455                               bool UseDebugPrinter, bool ShowMacros,
456                               llvm::raw_ostream &OS);
457
458  /// Create the default output file (from the invocation's options) and add it
459  /// to the list of tracked output files.
460  llvm::raw_fd_ostream *
461  createDefaultOutputFile(bool Binary = true, llvm::StringRef BaseInput = "",
462                          llvm::StringRef Extension = "");
463
464  /// Create a new output file and add it to the list of tracked output files,
465  /// optionally deriving the output path name.
466  llvm::raw_fd_ostream *
467  createOutputFile(llvm::StringRef OutputPath, bool Binary = true,
468                   llvm::StringRef BaseInput = "",
469                   llvm::StringRef Extension = "");
470
471  /// Create a new output file, optionally deriving the output path name.
472  ///
473  /// If \arg OutputPath is empty, then createOutputFile will derive an output
474  /// path location as \arg BaseInput, with any suffix removed, and \arg
475  /// Extension appended.
476  ///
477  /// \param OutputPath - If given, the path to the output file.
478  /// \param Error [out] - On failure, the error message.
479  /// \param BaseInput - If \arg OutputPath is empty, the input path name to use
480  /// for deriving the output path.
481  /// \param Extension - The extension to use for derived output names.
482  /// \param Binary - The mode to open the file in.
483  /// \param ResultPathName [out] - If given, the result path name will be
484  /// stored here on success.
485  static llvm::raw_fd_ostream *
486  createOutputFile(llvm::StringRef OutputPath, std::string &Error,
487                   bool Binary = true, llvm::StringRef BaseInput = "",
488                   llvm::StringRef Extension = "",
489                   std::string *ResultPathName = 0);
490
491  /// }
492};
493
494} // end namespace clang
495
496#endif
497