blob: edd19d7a539bf77ac2fdf31ed4aa7aa2ecd4146a [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"
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +000032#include "llvm/Support/Timer.h"
Daniel Dunbar0397af22010-01-13 00:48:06 +000033#include "llvm/System/Host.h"
Daniel Dunbara9204832009-11-13 10:37:48 +000034#include "llvm/System/Path.h"
Douglas Gregor2b4074f2009-12-01 05:55:20 +000035#include "llvm/System/Program.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000036using namespace clang;
37
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +000038CompilerInstance::CompilerInstance()
39 : Invocation(new CompilerInvocation()) {
Daniel Dunbar6228ca02010-01-30 21:47:07 +000040}
Daniel Dunbar2a79e162009-11-13 03:51:44 +000041
42CompilerInstance::~CompilerInstance() {
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +000043}
44
45void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) {
46 LLVMContext.reset(Value);
Daniel Dunbar2a79e162009-11-13 03:51:44 +000047}
Daniel Dunbar16b74492009-11-13 04:12:06 +000048
Daniel Dunbar6228ca02010-01-30 21:47:07 +000049void CompilerInstance::setInvocation(CompilerInvocation *Value) {
50 Invocation.reset(Value);
51}
52
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000053void CompilerInstance::setDiagnostics(Diagnostic *Value) {
54 Diagnostics.reset(Value);
55}
56
57void CompilerInstance::setDiagnosticClient(DiagnosticClient *Value) {
58 DiagClient.reset(Value);
59}
60
61void CompilerInstance::setTarget(TargetInfo *Value) {
62 Target.reset(Value);
63}
64
65void CompilerInstance::setFileManager(FileManager *Value) {
66 FileMgr.reset(Value);
67}
68
69void CompilerInstance::setSourceManager(SourceManager *Value) {
70 SourceMgr.reset(Value);
71}
72
73void CompilerInstance::setPreprocessor(Preprocessor *Value) {
74 PP.reset(Value);
75}
76
77void CompilerInstance::setASTContext(ASTContext *Value) {
78 Context.reset(Value);
79}
80
Daniel Dunbar12ce6942009-11-14 02:47:17 +000081void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
82 Consumer.reset(Value);
83}
84
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000085void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
86 CompletionConsumer.reset(Value);
87}
88
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000089// Diagnostics
Douglas Gregord93256e2010-01-28 06:00:51 +000090namespace {
91 class BinaryDiagnosticSerializer : public DiagnosticClient {
92 llvm::raw_ostream &OS;
93 SourceManager *SourceMgr;
94 public:
95 explicit BinaryDiagnosticSerializer(llvm::raw_ostream &OS)
96 : OS(OS), SourceMgr(0) { }
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +000097
Douglas Gregord93256e2010-01-28 06:00:51 +000098 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
99 const DiagnosticInfo &Info);
100 };
101}
102
103void BinaryDiagnosticSerializer::HandleDiagnostic(Diagnostic::Level DiagLevel,
104 const DiagnosticInfo &Info) {
Douglas Gregora88084b2010-02-18 18:08:43 +0000105 StoredDiagnostic(DiagLevel, Info).Serialize(OS);
Douglas Gregord93256e2010-01-28 06:00:51 +0000106}
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000107
108static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
109 unsigned argc, char **argv,
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000110 Diagnostic &Diags) {
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000111 std::string ErrorInfo;
112 llvm::raw_ostream *OS =
113 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo);
114 if (!ErrorInfo.empty()) {
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000115 Diags.Report(diag::err_fe_unable_to_open_logfile)
116 << DiagOpts.DumpBuildInformation << ErrorInfo;
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000117 delete OS;
118 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 =
128 new TextDiagnosticPrinter(*OS, 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) {
133 Diagnostics.reset(createDiagnostics(getDiagnosticOpts(), Argc, Argv));
134
135 if (Diagnostics)
136 DiagClient.reset(Diagnostics->getClient());
137}
138
139Diagnostic *CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
140 int Argc, char **Argv) {
Daniel Dunbar221c7212009-11-14 07:53:24 +0000141 llvm::OwningPtr<Diagnostic> Diags(new Diagnostic());
142
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000143 // Create the diagnostic client for reporting errors or for
144 // implementing -verify.
Douglas Gregord93256e2010-01-28 06:00:51 +0000145 llvm::OwningPtr<DiagnosticClient> DiagClient;
146 if (Opts.BinaryOutput) {
147 if (llvm::sys::Program::ChangeStderrToBinary()) {
148 // We weren't able to set standard error to binary, which is a
149 // bit of a problem. So, just create a text diagnostic printer
150 // to complain about this problem, and pretend that the user
151 // didn't try to use binary output.
152 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts));
153 Diags->setClient(DiagClient.take());
154 Diags->Report(diag::err_fe_stderr_binary);
155 return Diags.take();
156 } else {
157 DiagClient.reset(new BinaryDiagnosticSerializer(llvm::errs()));
158 }
159 } else {
160 DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts));
161 }
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000162
163 // Chain in -verify checker, if requested.
164 if (Opts.VerifyDiagnostics)
Daniel Dunbar221c7212009-11-14 07:53:24 +0000165 DiagClient.reset(new VerifyDiagnosticsClient(*Diags, DiagClient.take()));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000166
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000167 Diags->setClient(DiagClient.take());
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000168 if (!Opts.DumpBuildInformation.empty())
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000169 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000170
171 // Configure our handling of diagnostics.
Kovarththanan Rajaratnam5bf932b2010-03-17 09:36:02 +0000172 ProcessWarningOptions(*Diags, Opts);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000173
Daniel Dunbar221c7212009-11-14 07:53:24 +0000174 return Diags.take();
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000175}
176
177// File Manager
178
Daniel Dunbar16b74492009-11-13 04:12:06 +0000179void CompilerInstance::createFileManager() {
180 FileMgr.reset(new FileManager());
181}
182
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000183// Source Manager
184
Daniel Dunbar16b74492009-11-13 04:12:06 +0000185void CompilerInstance::createSourceManager() {
Douglas Gregorf715ca12010-03-16 00:06:06 +0000186 SourceMgr.reset(new SourceManager(getDiagnostics()));
Daniel Dunbar16b74492009-11-13 04:12:06 +0000187}
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000188
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000189// Preprocessor
190
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000191void CompilerInstance::createPreprocessor() {
192 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
193 getPreprocessorOpts(), getHeaderSearchOpts(),
194 getDependencyOutputOpts(), getTarget(),
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000195 getFrontendOpts(), getSourceManager(),
196 getFileManager()));
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000197}
198
199Preprocessor *
200CompilerInstance::createPreprocessor(Diagnostic &Diags,
201 const LangOptions &LangInfo,
202 const PreprocessorOptions &PPOpts,
203 const HeaderSearchOptions &HSOpts,
204 const DependencyOutputOptions &DepOpts,
205 const TargetInfo &Target,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000206 const FrontendOptions &FEOpts,
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000207 SourceManager &SourceMgr,
208 FileManager &FileMgr) {
209 // Create a PTH manager if we are using some form of a token cache.
210 PTHManager *PTHMgr = 0;
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000211 if (!PPOpts.TokenCache.empty())
212 PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000213
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000214 // Create the Preprocessor.
215 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
216 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
217 SourceMgr, *HeaderInfo, PTHMgr,
218 /*OwnsHeaderSearch=*/true);
219
220 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
221 // That argument is used as the IdentifierInfoLookup argument to
222 // IdentifierTable's ctor.
223 if (PTHMgr) {
224 PTHMgr->setPreprocessor(PP);
225 PP->setPTHManager(PTHMgr);
226 }
227
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000228 InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000229
230 // Handle generating dependencies, if requested.
231 if (!DepOpts.OutputFile.empty())
232 AttachDependencyFileGen(*PP, DepOpts);
233
234 return PP;
235}
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000236
237// ASTContext
238
239void CompilerInstance::createASTContext() {
240 Preprocessor &PP = getPreprocessor();
241 Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
242 getTarget(), PP.getIdentifierTable(),
243 PP.getSelectorTable(), PP.getBuiltinInfo(),
244 /*FreeMemory=*/ !getFrontendOpts().DisableFree,
245 /*size_reserve=*/ 0));
246}
Daniel Dunbar0f800392009-11-13 08:21:10 +0000247
248// ExternalASTSource
249
250void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path) {
251 llvm::OwningPtr<ExternalASTSource> Source;
252 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
253 getPreprocessor(), getASTContext()));
254 getASTContext().setExternalSource(Source);
255}
256
257ExternalASTSource *
258CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
259 const std::string &Sysroot,
260 Preprocessor &PP,
261 ASTContext &Context) {
262 llvm::OwningPtr<PCHReader> Reader;
263 Reader.reset(new PCHReader(PP, &Context,
264 Sysroot.empty() ? 0 : Sysroot.c_str()));
265
266 switch (Reader->ReadPCH(Path)) {
267 case PCHReader::Success:
268 // Set the predefines buffer as suggested by the PCH reader. Typically, the
269 // predefines buffer will be empty.
270 PP.setPredefines(Reader->getSuggestedPredefines());
271 return Reader.take();
272
273 case PCHReader::Failure:
274 // Unrecoverable failure: don't even try to process the input file.
275 break;
276
277 case PCHReader::IgnorePCH:
278 // No suitable PCH file could be found. Return an error.
279 break;
280 }
281
282 return 0;
283}
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000284
285// Code Completion
286
287void CompilerInstance::createCodeCompletionConsumer() {
288 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
289 CompletionConsumer.reset(
290 createCodeCompletionConsumer(getPreprocessor(),
291 Loc.FileName, Loc.Line, Loc.Column,
292 getFrontendOpts().DebugCodeCompletionPrinter,
293 getFrontendOpts().ShowMacrosInCodeCompletion,
294 llvm::outs()));
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000295 if (!CompletionConsumer)
296 return;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000297
298 if (CompletionConsumer->isOutputBinary() &&
299 llvm::sys::Program::ChangeStdoutToBinary()) {
300 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
301 CompletionConsumer.reset();
302 }
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000303}
304
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000305void CompilerInstance::createFrontendTimer() {
306 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
307}
308
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000309CodeCompleteConsumer *
310CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
311 const std::string &Filename,
312 unsigned Line,
313 unsigned Column,
314 bool UseDebugPrinter,
315 bool ShowMacros,
316 llvm::raw_ostream &OS) {
317 // Tell the source manager to chop off the given file at a specific
318 // line and column.
319 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
320 if (!Entry) {
321 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
322 << Filename;
323 return 0;
324 }
325
326 // Truncate the named file at the given line/column.
Douglas Gregor29684422009-12-02 06:49:09 +0000327 PP.SetCodeCompletionPoint(Entry, Line, Column);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000328
329 // Set up the creation routine for code-completion.
330 if (UseDebugPrinter)
331 return new PrintingCodeCompleteConsumer(ShowMacros, OS);
332 else
333 return new CIndexCodeCompleteConsumer(ShowMacros, OS);
334}
Daniel Dunbara9204832009-11-13 10:37:48 +0000335
336// Output Files
337
338void CompilerInstance::addOutputFile(llvm::StringRef Path,
339 llvm::raw_ostream *OS) {
340 assert(OS && "Attempt to add empty stream to output list!");
341 OutputFiles.push_back(std::make_pair(Path, OS));
342}
343
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000344void CompilerInstance::clearOutputFiles(bool EraseFiles) {
Daniel Dunbara9204832009-11-13 10:37:48 +0000345 for (std::list< std::pair<std::string, llvm::raw_ostream*> >::iterator
346 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
347 delete it->second;
348 if (EraseFiles && !it->first.empty())
349 llvm::sys::Path(it->first).eraseFromDisk();
350 }
351 OutputFiles.clear();
352}
353
Daniel Dunbarf482d592009-11-13 18:32:08 +0000354llvm::raw_fd_ostream *
355CompilerInstance::createDefaultOutputFile(bool Binary,
356 llvm::StringRef InFile,
357 llvm::StringRef Extension) {
358 return createOutputFile(getFrontendOpts().OutputFile, Binary,
359 InFile, Extension);
360}
361
362llvm::raw_fd_ostream *
363CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
364 bool Binary,
365 llvm::StringRef InFile,
366 llvm::StringRef Extension) {
367 std::string Error, OutputPathName;
368 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
369 InFile, Extension,
370 &OutputPathName);
371 if (!OS) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000372 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
373 << OutputPath << Error;
374 return 0;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000375 }
376
377 // Add the output file -- but don't try to remove "-", since this means we are
378 // using stdin.
379 addOutputFile((OutputPathName != "-") ? OutputPathName : "", OS);
380
381 return OS;
382}
383
384llvm::raw_fd_ostream *
385CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
386 std::string &Error,
387 bool Binary,
388 llvm::StringRef InFile,
389 llvm::StringRef Extension,
390 std::string *ResultPathName) {
391 std::string OutFile;
392 if (!OutputPath.empty()) {
393 OutFile = OutputPath;
394 } else if (InFile == "-") {
395 OutFile = "-";
396 } else if (!Extension.empty()) {
397 llvm::sys::Path Path(InFile);
398 Path.eraseSuffix();
399 Path.appendSuffix(Extension);
400 OutFile = Path.str();
401 } else {
402 OutFile = "-";
403 }
404
Daniel Dunbarfc971022009-11-20 22:32:38 +0000405 llvm::OwningPtr<llvm::raw_fd_ostream> OS(
Daniel Dunbarf482d592009-11-13 18:32:08 +0000406 new llvm::raw_fd_ostream(OutFile.c_str(), Error,
Daniel Dunbarfc971022009-11-20 22:32:38 +0000407 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
408 if (!Error.empty())
Daniel Dunbarf482d592009-11-13 18:32:08 +0000409 return 0;
410
411 if (ResultPathName)
412 *ResultPathName = OutFile;
413
Daniel Dunbarfc971022009-11-20 22:32:38 +0000414 return OS.take();
Daniel Dunbarf482d592009-11-13 18:32:08 +0000415}
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000416
417// Initialization Utilities
418
419bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
420 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
421 getSourceManager(), getFrontendOpts());
422}
423
424bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
425 Diagnostic &Diags,
426 FileManager &FileMgr,
427 SourceManager &SourceMgr,
428 const FrontendOptions &Opts) {
429 // Figure out where to get and map in the main file.
430 if (Opts.EmptyInputOnly) {
431 const char *EmptyStr = "";
432 llvm::MemoryBuffer *SB =
433 llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<empty input>");
434 SourceMgr.createMainFileIDForMemBuffer(SB);
435 } else if (InputFile != "-") {
436 const FileEntry *File = FileMgr.getFile(InputFile);
437 if (File) SourceMgr.createMainFileID(File, SourceLocation());
438 if (SourceMgr.getMainFileID().isInvalid()) {
439 Diags.Report(diag::err_fe_error_reading) << InputFile;
440 return false;
441 }
442 } else {
443 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
444 SourceMgr.createMainFileIDForMemBuffer(SB);
445 if (SourceMgr.getMainFileID().isInvalid()) {
446 Diags.Report(diag::err_fe_error_reading_stdin);
447 return false;
448 }
449 }
450
451 return true;
452}
Daniel Dunbar0397af22010-01-13 00:48:06 +0000453
454// High-Level Operations
455
456bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
457 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
458 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
459 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
460
461 // FIXME: Take this as an argument, once all the APIs we used have moved to
462 // taking it as an input instead of hard-coding llvm::errs.
463 llvm::raw_ostream &OS = llvm::errs();
464
465 // Create the target instance.
466 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
467 if (!hasTarget())
468 return false;
469
470 // Inform the target of the language options.
471 //
472 // FIXME: We shouldn't need to do this, the target should be immutable once
473 // created. This complexity should be lifted elsewhere.
474 getTarget().setForcedLangOptions(getLangOpts());
475
476 // Validate/process some options.
477 if (getHeaderSearchOpts().Verbose)
478 OS << "clang -cc1 version " CLANG_VERSION_STRING
479 << " based upon " << PACKAGE_STRING
480 << " hosted on " << llvm::sys::getHostTriple() << "\n";
481
482 if (getFrontendOpts().ShowTimers)
483 createFrontendTimer();
484
485 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
486 const std::string &InFile = getFrontendOpts().Inputs[i].second;
487
488 // If we aren't using an AST file, setup the file and source managers and
489 // the preprocessor.
490 bool IsAST = getFrontendOpts().Inputs[i].first == FrontendOptions::IK_AST;
491 if (!IsAST) {
492 if (!i) {
493 // Create a file manager object to provide access to and cache the
494 // filesystem.
495 createFileManager();
496
497 // Create the source manager.
498 createSourceManager();
499 } else {
500 // Reset the ID tables if we are reusing the SourceManager.
501 getSourceManager().clearIDTables();
502 }
503
504 // Create the preprocessor.
505 createPreprocessor();
506 }
507
508 if (Act.BeginSourceFile(*this, InFile, IsAST)) {
509 Act.Execute();
510 Act.EndSourceFile();
511 }
512 }
513
514 if (getDiagnosticOpts().ShowCarets)
515 if (unsigned NumDiagnostics = getDiagnostics().getNumDiagnostics())
516 OS << NumDiagnostics << " diagnostic"
517 << (NumDiagnostics == 1 ? "" : "s")
518 << " generated.\n";
519
520 if (getFrontendOpts().ShowStats) {
521 getFileManager().PrintStats();
522 OS << "\n";
523 }
524
525 // Return the appropriate status when verifying diagnostics.
526 //
527 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
528 // this.
529 if (getDiagnosticOpts().VerifyDiagnostics)
530 return !static_cast<VerifyDiagnosticsClient&>(
531 getDiagnosticClient()).HadErrors();
532
533 return !getDiagnostics().getNumErrors();
534}
535
536