blob: 76773e40d38970e1efd4e2851c444a4149492df0 [file] [log] [blame]
Daniel Dunbar2a79e162009-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 Dunbar12ce6942009-11-14 02:47:17 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbar5eb81002009-11-13 08:20:47 +000012#include "clang/AST/ASTContext.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000013#include "clang/Basic/Diagnostic.h"
Daniel Dunbar16b74492009-11-13 04:12:06 +000014#include "clang/Basic/FileManager.h"
15#include "clang/Basic/SourceManager.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000016#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0397af22010-01-13 00:48:06 +000017#include "clang/Basic/Version.h"
Daniel Dunbar22dacfa2009-11-13 05:52:11 +000018#include "clang/Lex/HeaderSearch.h"
19#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/PTHManager.h"
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000021#include "clang/Frontend/ChainedDiagnosticClient.h"
Daniel Dunbar0397af22010-01-13 00:48:06 +000022#include "clang/Frontend/FrontendAction.h"
Daniel Dunbar0f800392009-11-13 08:21:10 +000023#include "clang/Frontend/PCHReader.h"
Daniel Dunbarc2f484f2009-11-13 09:36:05 +000024#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000025#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Dunbarf79dced2009-11-14 03:24:39 +000026#include "clang/Frontend/VerifyDiagnosticsClient.h"
Daniel Dunbar22dacfa2009-11-13 05:52:11 +000027#include "clang/Frontend/Utils.h"
Daniel Dunbarc2f484f2009-11-13 09:36:05 +000028#include "clang/Sema/CodeCompleteConsumer.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000029#include "llvm/LLVMContext.h"
Daniel Dunbarccb6cb62009-11-14 07:53:04 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregor95dd5582010-03-30 17:33:59 +000032#include "llvm/ADT/Statistic.h"
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +000033#include "llvm/Support/Timer.h"
Daniel Dunbar0397af22010-01-13 00:48:06 +000034#include "llvm/System/Host.h"
Daniel Dunbara9204832009-11-13 10:37:48 +000035#include "llvm/System/Path.h"
Douglas Gregor2b4074f2009-12-01 05:55:20 +000036#include "llvm/System/Program.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000037using namespace clang;
38
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +000039CompilerInstance::CompilerInstance()
Sebastian Redlffaab3e2010-07-30 00:29:29 +000040 : Invocation(new CompilerInvocation()) {
Daniel Dunbar6228ca02010-01-30 21:47:07 +000041}
Daniel Dunbar2a79e162009-11-13 03:51:44 +000042
43CompilerInstance::~CompilerInstance() {
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +000044}
45
46void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) {
47 LLVMContext.reset(Value);
Daniel Dunbar2a79e162009-11-13 03:51:44 +000048}
Daniel Dunbar16b74492009-11-13 04:12:06 +000049
Daniel Dunbar6228ca02010-01-30 21:47:07 +000050void CompilerInstance::setInvocation(CompilerInvocation *Value) {
51 Invocation.reset(Value);
52}
53
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000054void CompilerInstance::setDiagnostics(Diagnostic *Value) {
Douglas Gregor28019772010-04-05 23:52:57 +000055 Diagnostics = Value;
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000056}
57
58void CompilerInstance::setDiagnosticClient(DiagnosticClient *Value) {
59 DiagClient.reset(Value);
60}
61
62void CompilerInstance::setTarget(TargetInfo *Value) {
63 Target.reset(Value);
64}
65
66void CompilerInstance::setFileManager(FileManager *Value) {
67 FileMgr.reset(Value);
68}
69
70void CompilerInstance::setSourceManager(SourceManager *Value) {
71 SourceMgr.reset(Value);
72}
73
74void CompilerInstance::setPreprocessor(Preprocessor *Value) {
75 PP.reset(Value);
76}
77
78void CompilerInstance::setASTContext(ASTContext *Value) {
79 Context.reset(Value);
80}
81
Daniel Dunbar12ce6942009-11-14 02:47:17 +000082void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
83 Consumer.reset(Value);
84}
85
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000086void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
87 CompletionConsumer.reset(Value);
88}
89
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000090// Diagnostics
Douglas Gregord93256e2010-01-28 06:00:51 +000091namespace {
92 class BinaryDiagnosticSerializer : public DiagnosticClient {
93 llvm::raw_ostream &OS;
94 SourceManager *SourceMgr;
95 public:
96 explicit BinaryDiagnosticSerializer(llvm::raw_ostream &OS)
97 : OS(OS), SourceMgr(0) { }
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +000098
Douglas Gregord93256e2010-01-28 06:00:51 +000099 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
100 const DiagnosticInfo &Info);
101 };
102}
103
104void BinaryDiagnosticSerializer::HandleDiagnostic(Diagnostic::Level DiagLevel,
105 const DiagnosticInfo &Info) {
Douglas Gregora88084b2010-02-18 18:08:43 +0000106 StoredDiagnostic(DiagLevel, Info).Serialize(OS);
Douglas Gregord93256e2010-01-28 06:00:51 +0000107}
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000108
109static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
110 unsigned argc, char **argv,
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000111 Diagnostic &Diags) {
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000112 std::string ErrorInfo;
Kovarththanan Rajaratnam69247132010-03-17 09:47:30 +0000113 llvm::OwningPtr<llvm::raw_ostream> OS(
114 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000115 if (!ErrorInfo.empty()) {
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000116 Diags.Report(diag::err_fe_unable_to_open_logfile)
117 << DiagOpts.DumpBuildInformation << ErrorInfo;
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000118 return;
119 }
120
Daniel Dunbardd63b282009-12-11 23:04:35 +0000121 (*OS) << "clang -cc1 command line arguments: ";
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000122 for (unsigned i = 0; i != argc; ++i)
123 (*OS) << argv[i] << ' ';
124 (*OS) << '\n';
125
126 // Chain in a diagnostic client which will log the diagnostics.
127 DiagnosticClient *Logger =
Kovarththanan Rajaratnam69247132010-03-17 09:47:30 +0000128 new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000129 Diags.setClient(new ChainedDiagnosticClient(Diags.getClient(), Logger));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000130}
131
132void CompilerInstance::createDiagnostics(int Argc, char **Argv) {
Douglas Gregor28019772010-04-05 23:52:57 +0000133 Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000134
135 if (Diagnostics)
136 DiagClient.reset(Diagnostics->getClient());
137}
138
Douglas Gregor28019772010-04-05 23:52:57 +0000139llvm::IntrusiveRefCntPtr<Diagnostic>
140CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
141 int Argc, char **Argv) {
142 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000143
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000144 // Create the diagnostic client for reporting errors or for
145 // implementing -verify.
Douglas Gregord93256e2010-01-28 06:00:51 +0000146 llvm::OwningPtr<DiagnosticClient> DiagClient;
147 if (Opts.BinaryOutput) {
148 if (llvm::sys::Program::ChangeStderrToBinary()) {
149 // We weren't able to set standard error to binary, which is a
150 // bit of a problem. So, just create a text diagnostic printer
151 // to complain about this problem, and pretend that the user
152 // didn't try to use binary output.
153 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts));
154 Diags->setClient(DiagClient.take());
155 Diags->Report(diag::err_fe_stderr_binary);
Douglas Gregor28019772010-04-05 23:52:57 +0000156 return Diags;
Douglas Gregord93256e2010-01-28 06:00:51 +0000157 } else {
158 DiagClient.reset(new BinaryDiagnosticSerializer(llvm::errs()));
159 }
160 } else {
161 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts));
162 }
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000163
164 // Chain in -verify checker, if requested.
165 if (Opts.VerifyDiagnostics)
Daniel Dunbar221c7212009-11-14 07:53:24 +0000166 DiagClient.reset(new VerifyDiagnosticsClient(*Diags, DiagClient.take()));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000167
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000168 Diags->setClient(DiagClient.take());
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000169 if (!Opts.DumpBuildInformation.empty())
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000170 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000171
172 // Configure our handling of diagnostics.
Kovarththanan Rajaratnam5bf932b2010-03-17 09:36:02 +0000173 ProcessWarningOptions(*Diags, Opts);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000174
Douglas Gregor28019772010-04-05 23:52:57 +0000175 return Diags;
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000176}
177
178// File Manager
179
Daniel Dunbar16b74492009-11-13 04:12:06 +0000180void CompilerInstance::createFileManager() {
181 FileMgr.reset(new FileManager());
182}
183
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000184// Source Manager
185
Daniel Dunbar16b74492009-11-13 04:12:06 +0000186void CompilerInstance::createSourceManager() {
Douglas Gregorf715ca12010-03-16 00:06:06 +0000187 SourceMgr.reset(new SourceManager(getDiagnostics()));
Daniel Dunbar16b74492009-11-13 04:12:06 +0000188}
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000189
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000190// Preprocessor
191
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000192void CompilerInstance::createPreprocessor() {
193 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
194 getPreprocessorOpts(), getHeaderSearchOpts(),
195 getDependencyOutputOpts(), getTarget(),
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000196 getFrontendOpts(), getSourceManager(),
197 getFileManager()));
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000198}
199
200Preprocessor *
201CompilerInstance::createPreprocessor(Diagnostic &Diags,
202 const LangOptions &LangInfo,
203 const PreprocessorOptions &PPOpts,
204 const HeaderSearchOptions &HSOpts,
205 const DependencyOutputOptions &DepOpts,
206 const TargetInfo &Target,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000207 const FrontendOptions &FEOpts,
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000208 SourceManager &SourceMgr,
209 FileManager &FileMgr) {
210 // Create a PTH manager if we are using some form of a token cache.
211 PTHManager *PTHMgr = 0;
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000212 if (!PPOpts.TokenCache.empty())
213 PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000214
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000215 // Create the Preprocessor.
216 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
217 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
218 SourceMgr, *HeaderInfo, PTHMgr,
219 /*OwnsHeaderSearch=*/true);
220
221 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
222 // That argument is used as the IdentifierInfoLookup argument to
223 // IdentifierTable's ctor.
224 if (PTHMgr) {
225 PTHMgr->setPreprocessor(PP);
226 PP->setPTHManager(PTHMgr);
227 }
228
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000229 if (PPOpts.DetailedRecord)
230 PP->createPreprocessingRecord();
231
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000232 InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000233
234 // Handle generating dependencies, if requested.
235 if (!DepOpts.OutputFile.empty())
236 AttachDependencyFileGen(*PP, DepOpts);
237
238 return PP;
239}
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000240
241// ASTContext
242
243void CompilerInstance::createASTContext() {
244 Preprocessor &PP = getPreprocessor();
245 Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
246 getTarget(), PP.getIdentifierTable(),
247 PP.getSelectorTable(), PP.getBuiltinInfo(),
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000248 /*size_reserve=*/ 0));
249}
Daniel Dunbar0f800392009-11-13 08:21:10 +0000250
251// ExternalASTSource
252
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000253void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000254 bool DisablePCHValidation,
255 void *DeserializationListener){
Daniel Dunbar0f800392009-11-13 08:21:10 +0000256 llvm::OwningPtr<ExternalASTSource> Source;
257 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000258 DisablePCHValidation,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000259 getPreprocessor(), getASTContext(),
260 DeserializationListener));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000261 getASTContext().setExternalSource(Source);
262}
263
264ExternalASTSource *
265CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
266 const std::string &Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000267 bool DisablePCHValidation,
Daniel Dunbar0f800392009-11-13 08:21:10 +0000268 Preprocessor &PP,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000269 ASTContext &Context,
270 void *DeserializationListener) {
Daniel Dunbar0f800392009-11-13 08:21:10 +0000271 llvm::OwningPtr<PCHReader> Reader;
272 Reader.reset(new PCHReader(PP, &Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000273 Sysroot.empty() ? 0 : Sysroot.c_str(),
274 DisablePCHValidation));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000275
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000276 Reader->setDeserializationListener(
277 static_cast<PCHDeserializationListener *>(DeserializationListener));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000278 switch (Reader->ReadPCH(Path)) {
279 case PCHReader::Success:
280 // Set the predefines buffer as suggested by the PCH reader. Typically, the
281 // predefines buffer will be empty.
282 PP.setPredefines(Reader->getSuggestedPredefines());
283 return Reader.take();
284
285 case PCHReader::Failure:
286 // Unrecoverable failure: don't even try to process the input file.
287 break;
288
289 case PCHReader::IgnorePCH:
290 // No suitable PCH file could be found. Return an error.
291 break;
292 }
293
294 return 0;
295}
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000296
297// Code Completion
298
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000299static bool EnableCodeCompletion(Preprocessor &PP,
300 const std::string &Filename,
301 unsigned Line,
302 unsigned Column) {
303 // Tell the source manager to chop off the given file at a specific
304 // line and column.
305 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
306 if (!Entry) {
307 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
308 << Filename;
309 return true;
310 }
311
312 // Truncate the named file at the given line/column.
313 PP.SetCodeCompletionPoint(Entry, Line, Column);
314 return false;
315}
316
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000317void CompilerInstance::createCodeCompletionConsumer() {
318 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000319 if (!CompletionConsumer) {
320 CompletionConsumer.reset(
321 createCodeCompletionConsumer(getPreprocessor(),
322 Loc.FileName, Loc.Line, Loc.Column,
323 getFrontendOpts().DebugCodeCompletionPrinter,
324 getFrontendOpts().ShowMacrosInCodeCompletion,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000325 getFrontendOpts().ShowCodePatternsInCodeCompletion,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000326 llvm::outs()));
327 if (!CompletionConsumer)
328 return;
329 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
330 Loc.Line, Loc.Column)) {
331 CompletionConsumer.reset();
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000332 return;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000333 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000334
335 if (CompletionConsumer->isOutputBinary() &&
336 llvm::sys::Program::ChangeStdoutToBinary()) {
337 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
338 CompletionConsumer.reset();
339 }
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000340}
341
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000342void CompilerInstance::createFrontendTimer() {
343 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
344}
345
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000346CodeCompleteConsumer *
347CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
348 const std::string &Filename,
349 unsigned Line,
350 unsigned Column,
351 bool UseDebugPrinter,
352 bool ShowMacros,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000353 bool ShowCodePatterns,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000354 llvm::raw_ostream &OS) {
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000355 if (EnableCodeCompletion(PP, Filename, Line, Column))
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000356 return 0;
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000357
358 // Set up the creation routine for code-completion.
359 if (UseDebugPrinter)
Douglas Gregord8e8a582010-05-25 21:41:55 +0000360 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, OS);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000361 else
Douglas Gregord8e8a582010-05-25 21:41:55 +0000362 return new CIndexCodeCompleteConsumer(ShowMacros, ShowCodePatterns, OS);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000363}
Daniel Dunbara9204832009-11-13 10:37:48 +0000364
365// Output Files
366
367void CompilerInstance::addOutputFile(llvm::StringRef Path,
368 llvm::raw_ostream *OS) {
369 assert(OS && "Attempt to add empty stream to output list!");
370 OutputFiles.push_back(std::make_pair(Path, OS));
371}
372
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000373void CompilerInstance::clearOutputFiles(bool EraseFiles) {
Daniel Dunbara9204832009-11-13 10:37:48 +0000374 for (std::list< std::pair<std::string, llvm::raw_ostream*> >::iterator
375 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
376 delete it->second;
377 if (EraseFiles && !it->first.empty())
378 llvm::sys::Path(it->first).eraseFromDisk();
379 }
380 OutputFiles.clear();
381}
382
Daniel Dunbarf482d592009-11-13 18:32:08 +0000383llvm::raw_fd_ostream *
384CompilerInstance::createDefaultOutputFile(bool Binary,
385 llvm::StringRef InFile,
386 llvm::StringRef Extension) {
387 return createOutputFile(getFrontendOpts().OutputFile, Binary,
388 InFile, Extension);
389}
390
391llvm::raw_fd_ostream *
392CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
393 bool Binary,
394 llvm::StringRef InFile,
395 llvm::StringRef Extension) {
396 std::string Error, OutputPathName;
397 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
398 InFile, Extension,
399 &OutputPathName);
400 if (!OS) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000401 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
402 << OutputPath << Error;
403 return 0;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000404 }
405
406 // Add the output file -- but don't try to remove "-", since this means we are
407 // using stdin.
408 addOutputFile((OutputPathName != "-") ? OutputPathName : "", OS);
409
410 return OS;
411}
412
413llvm::raw_fd_ostream *
414CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
415 std::string &Error,
416 bool Binary,
417 llvm::StringRef InFile,
418 llvm::StringRef Extension,
419 std::string *ResultPathName) {
420 std::string OutFile;
421 if (!OutputPath.empty()) {
422 OutFile = OutputPath;
423 } else if (InFile == "-") {
424 OutFile = "-";
425 } else if (!Extension.empty()) {
426 llvm::sys::Path Path(InFile);
427 Path.eraseSuffix();
428 Path.appendSuffix(Extension);
429 OutFile = Path.str();
430 } else {
431 OutFile = "-";
432 }
433
Daniel Dunbarfc971022009-11-20 22:32:38 +0000434 llvm::OwningPtr<llvm::raw_fd_ostream> OS(
Daniel Dunbarf482d592009-11-13 18:32:08 +0000435 new llvm::raw_fd_ostream(OutFile.c_str(), Error,
Daniel Dunbarfc971022009-11-20 22:32:38 +0000436 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
437 if (!Error.empty())
Daniel Dunbarf482d592009-11-13 18:32:08 +0000438 return 0;
439
440 if (ResultPathName)
441 *ResultPathName = OutFile;
442
Daniel Dunbarfc971022009-11-20 22:32:38 +0000443 return OS.take();
Daniel Dunbarf482d592009-11-13 18:32:08 +0000444}
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000445
446// Initialization Utilities
447
448bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
449 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
450 getSourceManager(), getFrontendOpts());
451}
452
453bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
454 Diagnostic &Diags,
455 FileManager &FileMgr,
456 SourceManager &SourceMgr,
457 const FrontendOptions &Opts) {
458 // Figure out where to get and map in the main file.
Daniel Dunbar27585952010-03-19 19:44:04 +0000459 if (InputFile != "-") {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000460 const FileEntry *File = FileMgr.getFile(InputFile);
461 if (File) SourceMgr.createMainFileID(File, SourceLocation());
462 if (SourceMgr.getMainFileID().isInvalid()) {
463 Diags.Report(diag::err_fe_error_reading) << InputFile;
464 return false;
465 }
466 } else {
467 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Dan Gohman6f118972010-05-27 17:33:40 +0000468 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000469 if (SourceMgr.getMainFileID().isInvalid()) {
470 Diags.Report(diag::err_fe_error_reading_stdin);
471 return false;
472 }
473 }
474
475 return true;
476}
Daniel Dunbar0397af22010-01-13 00:48:06 +0000477
478// High-Level Operations
479
480bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
481 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
482 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
483 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
484
485 // FIXME: Take this as an argument, once all the APIs we used have moved to
486 // taking it as an input instead of hard-coding llvm::errs.
487 llvm::raw_ostream &OS = llvm::errs();
488
489 // Create the target instance.
490 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
491 if (!hasTarget())
492 return false;
493
494 // Inform the target of the language options.
495 //
496 // FIXME: We shouldn't need to do this, the target should be immutable once
497 // created. This complexity should be lifted elsewhere.
498 getTarget().setForcedLangOptions(getLangOpts());
499
500 // Validate/process some options.
501 if (getHeaderSearchOpts().Verbose)
502 OS << "clang -cc1 version " CLANG_VERSION_STRING
503 << " based upon " << PACKAGE_STRING
504 << " hosted on " << llvm::sys::getHostTriple() << "\n";
505
506 if (getFrontendOpts().ShowTimers)
507 createFrontendTimer();
508
Douglas Gregor95dd5582010-03-30 17:33:59 +0000509 if (getFrontendOpts().ShowStats)
510 llvm::EnableStatistics();
511
Daniel Dunbar0397af22010-01-13 00:48:06 +0000512 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
513 const std::string &InFile = getFrontendOpts().Inputs[i].second;
514
Daniel Dunbar20560482010-06-07 23:23:50 +0000515 // Reset the ID tables if we are reusing the SourceManager.
516 if (hasSourceManager())
517 getSourceManager().clearIDTables();
Daniel Dunbar0397af22010-01-13 00:48:06 +0000518
Daniel Dunbard3598a62010-06-07 23:23:06 +0000519 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000520 Act.Execute();
521 Act.EndSourceFile();
522 }
523 }
524
Chris Lattner53eee7b2010-04-07 18:47:42 +0000525 if (getDiagnosticOpts().ShowCarets) {
526 unsigned NumWarnings = getDiagnostics().getNumWarnings();
Douglas Gregor1864f2e2010-04-14 22:19:45 +0000527 unsigned NumErrors = getDiagnostics().getNumErrors() -
528 getDiagnostics().getNumErrorsSuppressed();
Chris Lattner53eee7b2010-04-07 18:47:42 +0000529
530 if (NumWarnings)
531 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
532 if (NumWarnings && NumErrors)
533 OS << " and ";
534 if (NumErrors)
535 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
536 if (NumWarnings || NumErrors)
537 OS << " generated.\n";
538 }
Daniel Dunbar0397af22010-01-13 00:48:06 +0000539
Daniel Dunbar20560482010-06-07 23:23:50 +0000540 if (getFrontendOpts().ShowStats && hasFileManager()) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000541 getFileManager().PrintStats();
542 OS << "\n";
543 }
544
545 // Return the appropriate status when verifying diagnostics.
546 //
547 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
548 // this.
549 if (getDiagnosticOpts().VerifyDiagnostics)
550 return !static_cast<VerifyDiagnosticsClient&>(
551 getDiagnosticClient()).HadErrors();
552
553 return !getDiagnostics().getNumErrors();
554}
555
556