blob: 1cb5993faef09d3d7f784f8bfe42256284360fa0 [file] [log] [blame]
Daniel Dunbar636404a2009-11-13 03:51:44 +00001//===--- CompilerInstance.cpp ---------------------------------------------===//
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#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar56d9c292009-11-14 02:47:17 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbardf3e30c2009-11-13 08:20:47 +000012#include "clang/AST/ASTContext.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000013#include "clang/Basic/Diagnostic.h"
Daniel Dunbar546a6762009-11-13 04:12:06 +000014#include "clang/Basic/FileManager.h"
15#include "clang/Basic/SourceManager.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000016#include "clang/Basic/TargetInfo.h"
Daniel Dunbaraaa148f2009-11-13 05:52:11 +000017#include "clang/Lex/HeaderSearch.h"
18#include "clang/Lex/Preprocessor.h"
19#include "clang/Lex/PTHManager.h"
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000020#include "clang/Frontend/ChainedDiagnosticClient.h"
Daniel Dunbar599313e2009-11-13 08:21:10 +000021#include "clang/Frontend/PCHReader.h"
Daniel Dunbarf7093b52009-11-13 09:36:05 +000022#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000023#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Dunbar50ec0da2009-11-14 03:24:39 +000024#include "clang/Frontend/VerifyDiagnosticsClient.h"
Daniel Dunbaraaa148f2009-11-13 05:52:11 +000025#include "clang/Frontend/Utils.h"
Daniel Dunbarf7093b52009-11-13 09:36:05 +000026#include "clang/Sema/CodeCompleteConsumer.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000027#include "llvm/LLVMContext.h"
Daniel Dunbar409e8902009-11-14 07:53:04 +000028#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000029#include "llvm/Support/raw_ostream.h"
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +000030#include "llvm/Support/Timer.h"
Daniel Dunbar566eeb22009-11-13 10:37:48 +000031#include "llvm/System/Path.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000032using namespace clang;
33
34CompilerInstance::CompilerInstance(llvm::LLVMContext *_LLVMContext,
35 bool _OwnsLLVMContext)
36 : LLVMContext(_LLVMContext),
37 OwnsLLVMContext(_OwnsLLVMContext) {
Daniel Dunbarf7093b52009-11-13 09:36:05 +000038 }
Daniel Dunbar636404a2009-11-13 03:51:44 +000039
40CompilerInstance::~CompilerInstance() {
41 if (OwnsLLVMContext)
42 delete LLVMContext;
43}
Daniel Dunbar546a6762009-11-13 04:12:06 +000044
Daniel Dunbare01dc862009-11-14 01:20:40 +000045void CompilerInstance::setDiagnostics(Diagnostic *Value) {
46 Diagnostics.reset(Value);
47}
48
49void CompilerInstance::setDiagnosticClient(DiagnosticClient *Value) {
50 DiagClient.reset(Value);
51}
52
53void CompilerInstance::setTarget(TargetInfo *Value) {
54 Target.reset(Value);
55}
56
57void CompilerInstance::setFileManager(FileManager *Value) {
58 FileMgr.reset(Value);
59}
60
61void CompilerInstance::setSourceManager(SourceManager *Value) {
62 SourceMgr.reset(Value);
63}
64
65void CompilerInstance::setPreprocessor(Preprocessor *Value) {
66 PP.reset(Value);
67}
68
69void CompilerInstance::setASTContext(ASTContext *Value) {
70 Context.reset(Value);
71}
72
Daniel Dunbar56d9c292009-11-14 02:47:17 +000073void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
74 Consumer.reset(Value);
75}
76
Daniel Dunbare01dc862009-11-14 01:20:40 +000077void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
78 CompletionConsumer.reset(Value);
79}
80
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000081// Diagnostics
82
83static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
84 unsigned argc, char **argv,
85 llvm::OwningPtr<DiagnosticClient> &DiagClient) {
86 std::string ErrorInfo;
87 llvm::raw_ostream *OS =
88 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo);
89 if (!ErrorInfo.empty()) {
90 // FIXME: Do not fail like this.
91 llvm::errs() << "error opening -dump-build-information file '"
92 << DiagOpts.DumpBuildInformation << "', option ignored!\n";
93 delete OS;
94 return;
95 }
96
97 (*OS) << "clang-cc command line arguments: ";
98 for (unsigned i = 0; i != argc; ++i)
99 (*OS) << argv[i] << ' ';
100 (*OS) << '\n';
101
102 // Chain in a diagnostic client which will log the diagnostics.
103 DiagnosticClient *Logger =
104 new TextDiagnosticPrinter(*OS, DiagOpts, /*OwnsOutputStream=*/true);
105 DiagClient.reset(new ChainedDiagnosticClient(DiagClient.take(), Logger));
106}
107
108void CompilerInstance::createDiagnostics(int Argc, char **Argv) {
109 Diagnostics.reset(createDiagnostics(getDiagnosticOpts(), Argc, Argv));
110
111 if (Diagnostics)
112 DiagClient.reset(Diagnostics->getClient());
113}
114
115Diagnostic *CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
116 int Argc, char **Argv) {
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000117 llvm::OwningPtr<Diagnostic> Diags(new Diagnostic());
118
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000119 // Create the diagnostic client for reporting errors or for
120 // implementing -verify.
Daniel Dunbar50ec0da2009-11-14 03:24:39 +0000121 llvm::OwningPtr<DiagnosticClient> DiagClient(
122 new TextDiagnosticPrinter(llvm::errs(), Opts));
123
124 // Chain in -verify checker, if requested.
125 if (Opts.VerifyDiagnostics)
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000126 DiagClient.reset(new VerifyDiagnosticsClient(*Diags, DiagClient.take()));
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000127
128 if (!Opts.DumpBuildInformation.empty())
129 SetUpBuildDumpLog(Opts, Argc, Argv, DiagClient);
130
131 // Configure our handling of diagnostics.
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000132 Diags->setClient(DiagClient.take());
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000133 if (ProcessWarningOptions(*Diags, Opts))
134 return 0;
135
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000136 return Diags.take();
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000137}
138
139// File Manager
140
Daniel Dunbar546a6762009-11-13 04:12:06 +0000141void CompilerInstance::createFileManager() {
142 FileMgr.reset(new FileManager());
143}
144
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000145// Source Manager
146
Daniel Dunbar546a6762009-11-13 04:12:06 +0000147void CompilerInstance::createSourceManager() {
148 SourceMgr.reset(new SourceManager());
149}
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000150
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000151// Preprocessor
152
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000153void CompilerInstance::createPreprocessor() {
154 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
155 getPreprocessorOpts(), getHeaderSearchOpts(),
156 getDependencyOutputOpts(), getTarget(),
157 getSourceManager(), getFileManager()));
158}
159
160Preprocessor *
161CompilerInstance::createPreprocessor(Diagnostic &Diags,
162 const LangOptions &LangInfo,
163 const PreprocessorOptions &PPOpts,
164 const HeaderSearchOptions &HSOpts,
165 const DependencyOutputOptions &DepOpts,
166 const TargetInfo &Target,
167 SourceManager &SourceMgr,
168 FileManager &FileMgr) {
169 // Create a PTH manager if we are using some form of a token cache.
170 PTHManager *PTHMgr = 0;
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000171 if (!PPOpts.TokenCache.empty())
172 PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000173
174 // FIXME: Don't fail like this.
175 if (Diags.hasErrorOccurred())
176 exit(1);
177
178 // Create the Preprocessor.
179 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
180 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
181 SourceMgr, *HeaderInfo, PTHMgr,
182 /*OwnsHeaderSearch=*/true);
183
184 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
185 // That argument is used as the IdentifierInfoLookup argument to
186 // IdentifierTable's ctor.
187 if (PTHMgr) {
188 PTHMgr->setPreprocessor(PP);
189 PP->setPTHManager(PTHMgr);
190 }
191
192 InitializePreprocessor(*PP, PPOpts, HSOpts);
193
194 // Handle generating dependencies, if requested.
195 if (!DepOpts.OutputFile.empty())
196 AttachDependencyFileGen(*PP, DepOpts);
197
198 return PP;
199}
Daniel Dunbardf3e30c2009-11-13 08:20:47 +0000200
201// ASTContext
202
203void CompilerInstance::createASTContext() {
204 Preprocessor &PP = getPreprocessor();
205 Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
206 getTarget(), PP.getIdentifierTable(),
207 PP.getSelectorTable(), PP.getBuiltinInfo(),
208 /*FreeMemory=*/ !getFrontendOpts().DisableFree,
209 /*size_reserve=*/ 0));
210}
Daniel Dunbar599313e2009-11-13 08:21:10 +0000211
212// ExternalASTSource
213
214void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path) {
215 llvm::OwningPtr<ExternalASTSource> Source;
216 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
217 getPreprocessor(), getASTContext()));
218 getASTContext().setExternalSource(Source);
219}
220
221ExternalASTSource *
222CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
223 const std::string &Sysroot,
224 Preprocessor &PP,
225 ASTContext &Context) {
226 llvm::OwningPtr<PCHReader> Reader;
227 Reader.reset(new PCHReader(PP, &Context,
228 Sysroot.empty() ? 0 : Sysroot.c_str()));
229
230 switch (Reader->ReadPCH(Path)) {
231 case PCHReader::Success:
232 // Set the predefines buffer as suggested by the PCH reader. Typically, the
233 // predefines buffer will be empty.
234 PP.setPredefines(Reader->getSuggestedPredefines());
235 return Reader.take();
236
237 case PCHReader::Failure:
238 // Unrecoverable failure: don't even try to process the input file.
239 break;
240
241 case PCHReader::IgnorePCH:
242 // No suitable PCH file could be found. Return an error.
243 break;
244 }
245
246 return 0;
247}
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000248
249// Code Completion
250
251void CompilerInstance::createCodeCompletionConsumer() {
252 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
253 CompletionConsumer.reset(
254 createCodeCompletionConsumer(getPreprocessor(),
255 Loc.FileName, Loc.Line, Loc.Column,
256 getFrontendOpts().DebugCodeCompletionPrinter,
257 getFrontendOpts().ShowMacrosInCodeCompletion,
258 llvm::outs()));
259}
260
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000261void CompilerInstance::createFrontendTimer() {
262 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
263}
264
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000265CodeCompleteConsumer *
266CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
267 const std::string &Filename,
268 unsigned Line,
269 unsigned Column,
270 bool UseDebugPrinter,
271 bool ShowMacros,
272 llvm::raw_ostream &OS) {
273 // Tell the source manager to chop off the given file at a specific
274 // line and column.
275 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
276 if (!Entry) {
277 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
278 << Filename;
279 return 0;
280 }
281
282 // Truncate the named file at the given line/column.
283 PP.getSourceManager().truncateFileAt(Entry, Line, Column);
284
285 // Set up the creation routine for code-completion.
286 if (UseDebugPrinter)
287 return new PrintingCodeCompleteConsumer(ShowMacros, OS);
288 else
289 return new CIndexCodeCompleteConsumer(ShowMacros, OS);
290}
Daniel Dunbar566eeb22009-11-13 10:37:48 +0000291
292// Output Files
293
294void CompilerInstance::addOutputFile(llvm::StringRef Path,
295 llvm::raw_ostream *OS) {
296 assert(OS && "Attempt to add empty stream to output list!");
297 OutputFiles.push_back(std::make_pair(Path, OS));
298}
299
300void CompilerInstance::ClearOutputFiles(bool EraseFiles) {
301 for (std::list< std::pair<std::string, llvm::raw_ostream*> >::iterator
302 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
303 delete it->second;
304 if (EraseFiles && !it->first.empty())
305 llvm::sys::Path(it->first).eraseFromDisk();
306 }
307 OutputFiles.clear();
308}
309
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000310llvm::raw_fd_ostream *
311CompilerInstance::createDefaultOutputFile(bool Binary,
312 llvm::StringRef InFile,
313 llvm::StringRef Extension) {
314 return createOutputFile(getFrontendOpts().OutputFile, Binary,
315 InFile, Extension);
316}
317
318llvm::raw_fd_ostream *
319CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
320 bool Binary,
321 llvm::StringRef InFile,
322 llvm::StringRef Extension) {
323 std::string Error, OutputPathName;
324 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
325 InFile, Extension,
326 &OutputPathName);
327 if (!OS) {
328 // FIXME: Don't fail this way.
Daniel Dunbar2eaef182009-11-20 22:32:38 +0000329 llvm::errs() << "error: " << Error << "\n";
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000330 ::exit(1);
331 }
332
333 // Add the output file -- but don't try to remove "-", since this means we are
334 // using stdin.
335 addOutputFile((OutputPathName != "-") ? OutputPathName : "", OS);
336
337 return OS;
338}
339
340llvm::raw_fd_ostream *
341CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
342 std::string &Error,
343 bool Binary,
344 llvm::StringRef InFile,
345 llvm::StringRef Extension,
346 std::string *ResultPathName) {
347 std::string OutFile;
348 if (!OutputPath.empty()) {
349 OutFile = OutputPath;
350 } else if (InFile == "-") {
351 OutFile = "-";
352 } else if (!Extension.empty()) {
353 llvm::sys::Path Path(InFile);
354 Path.eraseSuffix();
355 Path.appendSuffix(Extension);
356 OutFile = Path.str();
357 } else {
358 OutFile = "-";
359 }
360
Daniel Dunbar2eaef182009-11-20 22:32:38 +0000361 llvm::OwningPtr<llvm::raw_fd_ostream> OS(
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000362 new llvm::raw_fd_ostream(OutFile.c_str(), Error,
Daniel Dunbar2eaef182009-11-20 22:32:38 +0000363 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
364 if (!Error.empty())
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000365 return 0;
366
367 if (ResultPathName)
368 *ResultPathName = OutFile;
369
Daniel Dunbar2eaef182009-11-20 22:32:38 +0000370 return OS.take();
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000371}
Daniel Dunbar409e8902009-11-14 07:53:04 +0000372
373// Initialization Utilities
374
375bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
376 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
377 getSourceManager(), getFrontendOpts());
378}
379
380bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
381 Diagnostic &Diags,
382 FileManager &FileMgr,
383 SourceManager &SourceMgr,
384 const FrontendOptions &Opts) {
385 // Figure out where to get and map in the main file.
386 if (Opts.EmptyInputOnly) {
387 const char *EmptyStr = "";
388 llvm::MemoryBuffer *SB =
389 llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<empty input>");
390 SourceMgr.createMainFileIDForMemBuffer(SB);
391 } else if (InputFile != "-") {
392 const FileEntry *File = FileMgr.getFile(InputFile);
393 if (File) SourceMgr.createMainFileID(File, SourceLocation());
394 if (SourceMgr.getMainFileID().isInvalid()) {
395 Diags.Report(diag::err_fe_error_reading) << InputFile;
396 return false;
397 }
398 } else {
399 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
400 SourceMgr.createMainFileIDForMemBuffer(SB);
401 if (SourceMgr.getMainFileID().isInvalid()) {
402 Diags.Report(diag::err_fe_error_reading_stdin);
403 return false;
404 }
405 }
406
407 return true;
408}