aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Frontend/CompilerInstance.h
blob: c503d7a09fc78655e44efd342c00364bc3ac049a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//===-- CompilerInstance.h - Clang Compiler Instance ------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
#define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_

#include "clang/Frontend/CompilerInvocation.h"
#include "llvm/ADT/OwningPtr.h"

namespace llvm {
class LLVMContext;
}

namespace clang {
class Diagnostic;
class DiagnosticClient;
class TargetInfo;

/// CompilerInstance - Helper class for managing a single instance of the Clang
/// compiler.
///
/// The CompilerInstance serves two purposes:
///  (1) It manages the various objects which are necessary to run the compiler,
///      for example the preprocessor, the target information, and the AST
///      context.
///  (2) It provides utility routines for constructing and manipulating the
///      common Clang objects.
///
/// The compiler instance generally owns the instance of all the objects that it
/// manages. However, clients can still share objects by manually setting the
/// object and retaking ownership prior to destroying the CompilerInstance.
///
/// The compiler instance is intended to simplify clients, but not to lock them
/// in to the compiler instance for everything. When possible, utility functions
/// come in two forms; a short form that reuses the CompilerInstance objects,
/// and a long form that takes explicit instances of any required objects.
class CompilerInstance {
  /// The LLVM context used for this instance.
  llvm::LLVMContext *LLVMContext;
  bool OwnsLLVMContext;

  /// The options used in this compiler instance.
  CompilerInvocation Invocation;

  /// The diagnostics engine instance.
  llvm::OwningPtr<Diagnostic> Diagnostics;

  /// The diagnostics client instance.
  llvm::OwningPtr<DiagnosticClient> DiagClient;

  /// The target being compiled for.
  llvm::OwningPtr<TargetInfo> Target;

public:
  /// Create a new compiler instance with the given LLVM context, optionally
  /// taking ownership of it.
  CompilerInstance(llvm::LLVMContext *_LLVMContext = 0,
                   bool _OwnsLLVMContext = true);
  ~CompilerInstance();

  /// @name LLVM Context
  /// {

  llvm::LLVMContext &getLLVMContext() { return *LLVMContext; }

  /// setLLVMContext - Replace the current LLVM context and take ownership of
  /// \arg Value.
  void setLLVMContext(llvm::LLVMContext *Value, bool TakeOwnership = true) {
    LLVMContext = Value;
    OwnsLLVMContext = TakeOwnership;
  }

  /// }
  /// @name Compiler Invocation and Options
  /// {

  CompilerInvocation &getInvocation() { return Invocation; }
  const CompilerInvocation &getInvocation() const { return Invocation; }
  void setInvocation(const CompilerInvocation &Value) { Invocation = Value; }

  /// }
  /// @name Forwarding Methods
  /// {

  AnalyzerOptions &getAnalyzerOpts() {
    return Invocation.getAnalyzerOpts();
  }
  const AnalyzerOptions &getAnalyzerOpts() const {
    return Invocation.getAnalyzerOpts();
  }

  CodeGenOptions &getCodeGenOpts() {
    return Invocation.getCodeGenOpts();
  }
  const CodeGenOptions &getCodeGenOpts() const {
    return Invocation.getCodeGenOpts();
  }

  DependencyOutputOptions &getDependencyOutputOpts() {
    return Invocation.getDependencyOutputOpts();
  }
  const DependencyOutputOptions &getDependencyOutputOpts() const {
    return Invocation.getDependencyOutputOpts();
  }

  DiagnosticOptions &getDiagnosticOpts() {
    return Invocation.getDiagnosticOpts();
  }
  const DiagnosticOptions &getDiagnosticOpts() const {
    return Invocation.getDiagnosticOpts();
  }

  FrontendOptions &getFrontendOpts() {
    return Invocation.getFrontendOpts();
  }
  const FrontendOptions &getFrontendOpts() const {
    return Invocation.getFrontendOpts();
  }

  HeaderSearchOptions &getHeaderSearchOpts() {
    return Invocation.getHeaderSearchOpts();
  }
  const HeaderSearchOptions &getHeaderSearchOpts() const {
    return Invocation.getHeaderSearchOpts();
  }

  LangOptions &getLangOpts() {
    return Invocation.getLangOpts();
  }
  const LangOptions &getLangOpts() const {
    return Invocation.getLangOpts();
  }

  PreprocessorOptions &getPreprocessorOpts() {
    return Invocation.getPreprocessorOpts();
  }
  const PreprocessorOptions &getPreprocessorOpts() const {
    return Invocation.getPreprocessorOpts();
  }

  PreprocessorOutputOptions &getPreprocessorOutputOpts() {
    return Invocation.getPreprocessorOutputOpts();
  }
  const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
    return Invocation.getPreprocessorOutputOpts();
  }

  /// }
  /// @name Diagnostics Engine
  /// {

  Diagnostic &getDiagnostics() const { return *Diagnostics; }

  /// takeDiagnostics - Remove the current diagnostics engine and give ownership
  /// to the caller.
  Diagnostic *takeDiagnostics() { return Diagnostics.take(); }

  /// setDiagnostics - Replace the current diagnostics engine; the compiler
  /// instance takes ownership of \arg Value.
  void setDiagnostics(Diagnostic *Value) { Diagnostics.reset(Value); }

  DiagnosticClient &getDiagnosticClient() const { return *DiagClient; }

  /// takeDiagnosticClient - Remove the current diagnostics client and give
  /// ownership to the caller.
  DiagnosticClient *takeDiagnosticClient() { return DiagClient.take(); }

  /// setDiagnosticClient - Replace the current diagnostics client; the compiler
  /// instance takes ownership of \arg Value.
  void setDiagnosticClient(DiagnosticClient *Value) {
    DiagClient.reset(Value);
  }

  /// }
  /// @name Target Info
  /// {

  TargetInfo &getTarget() const { return *Target; }

  /// takeTarget - Remove the current diagnostics engine and give ownership
  /// to the caller.
  TargetInfo *takeTarget() { return Target.take(); }

  /// setTarget - Replace the current diagnostics engine; the compiler
  /// instance takes ownership of \arg Value.
  void setTarget(TargetInfo *Value) { Target.reset(Value); }

  /// }
};

} // end namespace clang

#endif