blob: 7f3eb21bb8c46a22adde64f555b186cefc14a313 [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"
Douglas Gregorf18d0d82010-08-12 23:31:19 +000011#include "clang/Sema/Sema.h"
Daniel Dunbar12ce6942009-11-14 02:47:17 +000012#include "clang/AST/ASTConsumer.h"
Daniel Dunbar5eb81002009-11-13 08:20:47 +000013#include "clang/AST/ASTContext.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000014#include "clang/Basic/Diagnostic.h"
Daniel Dunbar16b74492009-11-13 04:12:06 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000017#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0397af22010-01-13 00:48:06 +000018#include "clang/Basic/Version.h"
Daniel Dunbar22dacfa2009-11-13 05:52:11 +000019#include "clang/Lex/HeaderSearch.h"
20#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/PTHManager.h"
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000022#include "clang/Frontend/ChainedDiagnosticClient.h"
Daniel Dunbar0397af22010-01-13 00:48:06 +000023#include "clang/Frontend/FrontendAction.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"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Daniel Dunbarc2f484f2009-11-13 09:36:05 +000029#include "clang/Sema/CodeCompleteConsumer.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000030#include "llvm/LLVMContext.h"
Daniel Dunbarccb6cb62009-11-14 07:53:04 +000031#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregor95dd5582010-03-30 17:33:59 +000033#include "llvm/ADT/Statistic.h"
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +000034#include "llvm/Support/Timer.h"
Daniel Dunbar0397af22010-01-13 00:48:06 +000035#include "llvm/System/Host.h"
Daniel Dunbara9204832009-11-13 10:37:48 +000036#include "llvm/System/Path.h"
Douglas Gregor2b4074f2009-12-01 05:55:20 +000037#include "llvm/System/Program.h"
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +000038#include "llvm/System/Signals.h"
Daniel Dunbar2a79e162009-11-13 03:51:44 +000039using namespace clang;
40
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +000041CompilerInstance::CompilerInstance()
Sebastian Redlffaab3e2010-07-30 00:29:29 +000042 : Invocation(new CompilerInvocation()) {
Daniel Dunbar6228ca02010-01-30 21:47:07 +000043}
Daniel Dunbar2a79e162009-11-13 03:51:44 +000044
45CompilerInstance::~CompilerInstance() {
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +000046}
47
48void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) {
49 LLVMContext.reset(Value);
Daniel Dunbar2a79e162009-11-13 03:51:44 +000050}
Daniel Dunbar16b74492009-11-13 04:12:06 +000051
Daniel Dunbar6228ca02010-01-30 21:47:07 +000052void CompilerInstance::setInvocation(CompilerInvocation *Value) {
53 Invocation.reset(Value);
54}
55
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000056void CompilerInstance::setDiagnostics(Diagnostic *Value) {
Douglas Gregor28019772010-04-05 23:52:57 +000057 Diagnostics = Value;
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000058}
59
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000060void CompilerInstance::setTarget(TargetInfo *Value) {
61 Target.reset(Value);
62}
63
64void CompilerInstance::setFileManager(FileManager *Value) {
65 FileMgr.reset(Value);
66}
67
68void CompilerInstance::setSourceManager(SourceManager *Value) {
69 SourceMgr.reset(Value);
70}
71
72void CompilerInstance::setPreprocessor(Preprocessor *Value) {
73 PP.reset(Value);
74}
75
76void CompilerInstance::setASTContext(ASTContext *Value) {
77 Context.reset(Value);
78}
79
Douglas Gregorf18d0d82010-08-12 23:31:19 +000080void CompilerInstance::setSema(Sema *S) {
81 TheSema.reset(S);
82}
83
Daniel Dunbar12ce6942009-11-14 02:47:17 +000084void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
85 Consumer.reset(Value);
86}
87
Daniel Dunbar8a9f5692009-11-14 01:20:40 +000088void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
89 CompletionConsumer.reset(Value);
90}
91
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000092// Diagnostics
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000093static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
Axel Naumann7d0c4cc2010-10-11 09:13:46 +000094 unsigned argc, const char* const *argv,
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +000095 Diagnostic &Diags) {
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000096 std::string ErrorInfo;
Kovarththanan Rajaratnam69247132010-03-17 09:47:30 +000097 llvm::OwningPtr<llvm::raw_ostream> OS(
98 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +000099 if (!ErrorInfo.empty()) {
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000100 Diags.Report(diag::err_fe_unable_to_open_logfile)
101 << DiagOpts.DumpBuildInformation << ErrorInfo;
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000102 return;
103 }
104
Daniel Dunbardd63b282009-12-11 23:04:35 +0000105 (*OS) << "clang -cc1 command line arguments: ";
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000106 for (unsigned i = 0; i != argc; ++i)
107 (*OS) << argv[i] << ' ';
108 (*OS) << '\n';
109
110 // Chain in a diagnostic client which will log the diagnostics.
111 DiagnosticClient *Logger =
Kovarththanan Rajaratnam69247132010-03-17 09:47:30 +0000112 new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000113 Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000114}
115
Douglas Gregore47be3e2010-11-11 00:39:14 +0000116void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
117 DiagnosticClient *Client) {
118 Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000119}
120
Douglas Gregor28019772010-04-05 23:52:57 +0000121llvm::IntrusiveRefCntPtr<Diagnostic>
122CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
Douglas Gregore47be3e2010-11-11 00:39:14 +0000123 int Argc, const char* const *Argv,
124 DiagnosticClient *Client) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000125 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
126 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID));
Daniel Dunbar221c7212009-11-14 07:53:24 +0000127
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000128 // Create the diagnostic client for reporting errors or for
129 // implementing -verify.
Douglas Gregore47be3e2010-11-11 00:39:14 +0000130 if (Client)
131 Diags->setClient(Client);
132 else
133 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000134
135 // Chain in -verify checker, if requested.
136 if (Opts.VerifyDiagnostics)
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000137 Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000138
139 if (!Opts.DumpBuildInformation.empty())
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000140 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000141
142 // Configure our handling of diagnostics.
Kovarththanan Rajaratnam5bf932b2010-03-17 09:36:02 +0000143 ProcessWarningOptions(*Diags, Opts);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000144
Douglas Gregor28019772010-04-05 23:52:57 +0000145 return Diags;
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000146}
147
148// File Manager
149
Daniel Dunbar16b74492009-11-13 04:12:06 +0000150void CompilerInstance::createFileManager() {
Chris Lattner7ad97ff2010-11-23 07:51:02 +0000151 FileMgr.reset(new FileManager(getFileSystemOpts()));
Daniel Dunbar16b74492009-11-13 04:12:06 +0000152}
153
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000154// Source Manager
155
Chris Lattner39b49bc2010-11-23 08:35:12 +0000156void CompilerInstance::createSourceManager(FileManager &FileMgr) {
157 SourceMgr.reset(new SourceManager(getDiagnostics(), FileMgr));
Daniel Dunbar16b74492009-11-13 04:12:06 +0000158}
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000159
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000160// Preprocessor
161
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000162void CompilerInstance::createPreprocessor() {
163 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
164 getPreprocessorOpts(), getHeaderSearchOpts(),
165 getDependencyOutputOpts(), getTarget(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000166 getFrontendOpts(), getSourceManager(),
167 getFileManager()));
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000168}
169
170Preprocessor *
171CompilerInstance::createPreprocessor(Diagnostic &Diags,
172 const LangOptions &LangInfo,
173 const PreprocessorOptions &PPOpts,
174 const HeaderSearchOptions &HSOpts,
175 const DependencyOutputOptions &DepOpts,
176 const TargetInfo &Target,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000177 const FrontendOptions &FEOpts,
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000178 SourceManager &SourceMgr,
179 FileManager &FileMgr) {
180 // Create a PTH manager if we are using some form of a token cache.
181 PTHManager *PTHMgr = 0;
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000182 if (!PPOpts.TokenCache.empty())
Chris Lattner681c74a2010-11-23 09:01:31 +0000183 PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000184
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000185 // Create the Preprocessor.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000186 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000187 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
188 SourceMgr, *HeaderInfo, PTHMgr,
189 /*OwnsHeaderSearch=*/true);
190
191 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
192 // That argument is used as the IdentifierInfoLookup argument to
193 // IdentifierTable's ctor.
194 if (PTHMgr) {
195 PTHMgr->setPreprocessor(PP);
196 PP->setPTHManager(PTHMgr);
197 }
198
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000199 if (PPOpts.DetailedRecord)
200 PP->createPreprocessingRecord();
201
Chris Lattner39b49bc2010-11-23 08:35:12 +0000202 InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000203
204 // Handle generating dependencies, if requested.
205 if (!DepOpts.OutputFile.empty())
206 AttachDependencyFileGen(*PP, DepOpts);
207
208 return PP;
209}
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000210
211// ASTContext
212
213void CompilerInstance::createASTContext() {
214 Preprocessor &PP = getPreprocessor();
215 Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
216 getTarget(), PP.getIdentifierTable(),
217 PP.getSelectorTable(), PP.getBuiltinInfo(),
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000218 /*size_reserve=*/ 0));
219}
Daniel Dunbar0f800392009-11-13 08:21:10 +0000220
221// ExternalASTSource
222
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000223void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000224 bool DisablePCHValidation,
225 void *DeserializationListener){
Daniel Dunbar0f800392009-11-13 08:21:10 +0000226 llvm::OwningPtr<ExternalASTSource> Source;
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000227 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
Daniel Dunbar0f800392009-11-13 08:21:10 +0000228 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000229 DisablePCHValidation,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000230 getPreprocessor(), getASTContext(),
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000231 DeserializationListener,
232 Preamble));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000233 getASTContext().setExternalSource(Source);
234}
235
236ExternalASTSource *
237CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
238 const std::string &Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000239 bool DisablePCHValidation,
Daniel Dunbar0f800392009-11-13 08:21:10 +0000240 Preprocessor &PP,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000241 ASTContext &Context,
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000242 void *DeserializationListener,
243 bool Preamble) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000244 llvm::OwningPtr<ASTReader> Reader;
245 Reader.reset(new ASTReader(PP, &Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000246 Sysroot.empty() ? 0 : Sysroot.c_str(),
247 DisablePCHValidation));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000248
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000249 Reader->setDeserializationListener(
Sebastian Redl571db7f2010-08-18 23:56:56 +0000250 static_cast<ASTDeserializationListener *>(DeserializationListener));
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000251 switch (Reader->ReadAST(Path,
252 Preamble ? ASTReader::Preamble : ASTReader::PCH)) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000253 case ASTReader::Success:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000254 // Set the predefines buffer as suggested by the PCH reader. Typically, the
255 // predefines buffer will be empty.
256 PP.setPredefines(Reader->getSuggestedPredefines());
257 return Reader.take();
258
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000259 case ASTReader::Failure:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000260 // Unrecoverable failure: don't even try to process the input file.
261 break;
262
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000263 case ASTReader::IgnorePCH:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000264 // No suitable PCH file could be found. Return an error.
265 break;
266 }
267
268 return 0;
269}
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000270
271// Code Completion
272
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000273static bool EnableCodeCompletion(Preprocessor &PP,
274 const std::string &Filename,
275 unsigned Line,
276 unsigned Column) {
277 // Tell the source manager to chop off the given file at a specific
278 // line and column.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000279 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000280 if (!Entry) {
281 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
282 << Filename;
283 return true;
284 }
285
286 // Truncate the named file at the given line/column.
287 PP.SetCodeCompletionPoint(Entry, Line, Column);
288 return false;
289}
290
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000291void CompilerInstance::createCodeCompletionConsumer() {
292 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000293 if (!CompletionConsumer) {
294 CompletionConsumer.reset(
295 createCodeCompletionConsumer(getPreprocessor(),
296 Loc.FileName, Loc.Line, Loc.Column,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000297 getFrontendOpts().ShowMacrosInCodeCompletion,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000298 getFrontendOpts().ShowCodePatternsInCodeCompletion,
Douglas Gregor8071e422010-08-15 06:18:01 +0000299 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000300 llvm::outs()));
301 if (!CompletionConsumer)
302 return;
303 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
304 Loc.Line, Loc.Column)) {
305 CompletionConsumer.reset();
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000306 return;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000307 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000308
309 if (CompletionConsumer->isOutputBinary() &&
310 llvm::sys::Program::ChangeStdoutToBinary()) {
311 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
312 CompletionConsumer.reset();
313 }
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000314}
315
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000316void CompilerInstance::createFrontendTimer() {
317 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
318}
319
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000320CodeCompleteConsumer *
321CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
322 const std::string &Filename,
323 unsigned Line,
324 unsigned Column,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000325 bool ShowMacros,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000326 bool ShowCodePatterns,
Douglas Gregor8071e422010-08-15 06:18:01 +0000327 bool ShowGlobals,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000328 llvm::raw_ostream &OS) {
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000329 if (EnableCodeCompletion(PP, Filename, Line, Column))
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000330 return 0;
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000331
332 // Set up the creation routine for code-completion.
Douglas Gregora9f4f622010-10-11 22:12:15 +0000333 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
Douglas Gregor8071e422010-08-15 06:18:01 +0000334 ShowGlobals, OS);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000335}
Daniel Dunbara9204832009-11-13 10:37:48 +0000336
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000337void CompilerInstance::createSema(bool CompleteTranslationUnit,
338 CodeCompleteConsumer *CompletionConsumer) {
339 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
340 CompleteTranslationUnit, CompletionConsumer));
341}
342
Daniel Dunbara9204832009-11-13 10:37:48 +0000343// Output Files
344
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000345void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
346 assert(OutFile.OS && "Attempt to add empty stream to output list!");
347 OutputFiles.push_back(OutFile);
Daniel Dunbara9204832009-11-13 10:37:48 +0000348}
349
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000350void CompilerInstance::clearOutputFiles(bool EraseFiles) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000351 for (std::list<OutputFile>::iterator
Daniel Dunbara9204832009-11-13 10:37:48 +0000352 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000353 delete it->OS;
354 if (!it->TempFilename.empty()) {
355 llvm::sys::Path TempPath(it->TempFilename);
356 if (EraseFiles)
357 TempPath.eraseFromDisk();
358 else {
359 std::string Error;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000360 llvm::sys::Path NewOutFile(it->Filename);
361 // If '-working-directory' was passed, the output filename should be
362 // relative to that.
363 FileManager::FixupRelativePath(NewOutFile, getFileSystemOpts());
364 if (TempPath.renamePathOnDisk(NewOutFile, &Error)) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000365 getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
366 << it->TempFilename << it->Filename << Error;
367 TempPath.eraseFromDisk();
368 }
369 }
370 } else if (!it->Filename.empty() && EraseFiles)
371 llvm::sys::Path(it->Filename).eraseFromDisk();
372
Daniel Dunbara9204832009-11-13 10:37:48 +0000373 }
374 OutputFiles.clear();
375}
376
Daniel Dunbarf482d592009-11-13 18:32:08 +0000377llvm::raw_fd_ostream *
378CompilerInstance::createDefaultOutputFile(bool Binary,
379 llvm::StringRef InFile,
380 llvm::StringRef Extension) {
381 return createOutputFile(getFrontendOpts().OutputFile, Binary,
382 InFile, Extension);
383}
384
385llvm::raw_fd_ostream *
386CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
387 bool Binary,
388 llvm::StringRef InFile,
389 llvm::StringRef Extension) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000390 std::string Error, OutputPathName, TempPathName;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000391 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
392 InFile, Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000393 &OutputPathName,
394 &TempPathName);
Daniel Dunbarf482d592009-11-13 18:32:08 +0000395 if (!OS) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000396 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
397 << OutputPath << Error;
398 return 0;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000399 }
400
401 // Add the output file -- but don't try to remove "-", since this means we are
402 // using stdin.
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000403 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
404 TempPathName, OS));
Daniel Dunbarf482d592009-11-13 18:32:08 +0000405
406 return OS;
407}
408
409llvm::raw_fd_ostream *
410CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
411 std::string &Error,
412 bool Binary,
413 llvm::StringRef InFile,
414 llvm::StringRef Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000415 std::string *ResultPathName,
416 std::string *TempPathName) {
417 std::string OutFile, TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000418 if (!OutputPath.empty()) {
419 OutFile = OutputPath;
420 } else if (InFile == "-") {
421 OutFile = "-";
422 } else if (!Extension.empty()) {
423 llvm::sys::Path Path(InFile);
424 Path.eraseSuffix();
425 Path.appendSuffix(Extension);
426 OutFile = Path.str();
427 } else {
428 OutFile = "-";
429 }
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000430
431 if (OutFile != "-") {
432 llvm::sys::Path OutPath(OutFile);
433 // Only create the temporary if we can actually write to OutPath, otherwise
434 // we want to fail early.
435 if (!OutPath.exists() ||
436 (OutPath.isRegularFile() && OutPath.canWrite())) {
437 // Create a temporary file.
438 llvm::sys::Path TempPath(OutFile);
439 if (!TempPath.createTemporaryFileOnDisk())
440 TempFile = TempPath.str();
441 }
442 }
443
444 std::string OSFile = OutFile;
445 if (!TempFile.empty())
446 OSFile = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000447
Daniel Dunbarfc971022009-11-20 22:32:38 +0000448 llvm::OwningPtr<llvm::raw_fd_ostream> OS(
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000449 new llvm::raw_fd_ostream(OSFile.c_str(), Error,
Daniel Dunbarfc971022009-11-20 22:32:38 +0000450 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
451 if (!Error.empty())
Daniel Dunbarf482d592009-11-13 18:32:08 +0000452 return 0;
453
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000454 // Make sure the out stream file gets removed if we crash.
455 llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
456
Daniel Dunbarf482d592009-11-13 18:32:08 +0000457 if (ResultPathName)
458 *ResultPathName = OutFile;
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000459 if (TempPathName)
460 *TempPathName = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000461
Daniel Dunbarfc971022009-11-20 22:32:38 +0000462 return OS.take();
Daniel Dunbarf482d592009-11-13 18:32:08 +0000463}
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000464
465// Initialization Utilities
466
467bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
468 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
469 getSourceManager(), getFrontendOpts());
470}
471
472bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
473 Diagnostic &Diags,
474 FileManager &FileMgr,
475 SourceManager &SourceMgr,
476 const FrontendOptions &Opts) {
477 // Figure out where to get and map in the main file.
Daniel Dunbar27585952010-03-19 19:44:04 +0000478 if (InputFile != "-") {
Chris Lattner39b49bc2010-11-23 08:35:12 +0000479 const FileEntry *File = FileMgr.getFile(InputFile);
Dan Gohman694137c2010-10-26 21:13:51 +0000480 if (!File) {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000481 Diags.Report(diag::err_fe_error_reading) << InputFile;
482 return false;
483 }
Dan Gohman694137c2010-10-26 21:13:51 +0000484 SourceMgr.createMainFileID(File);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000485 } else {
486 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Dan Gohman694137c2010-10-26 21:13:51 +0000487 if (!SB) {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000488 Diags.Report(diag::err_fe_error_reading_stdin);
489 return false;
490 }
Dan Gohman90d90812010-10-26 23:21:25 +0000491 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000492 SB->getBufferSize(), 0);
Dan Gohman90d90812010-10-26 23:21:25 +0000493 SourceMgr.createMainFileID(File);
494 SourceMgr.overrideFileContents(File, SB);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000495 }
496
Dan Gohman694137c2010-10-26 21:13:51 +0000497 assert(!SourceMgr.getMainFileID().isInvalid() &&
498 "Couldn't establish MainFileID!");
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000499 return true;
500}
Daniel Dunbar0397af22010-01-13 00:48:06 +0000501
502// High-Level Operations
503
504bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
505 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
506 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
507 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
508
509 // FIXME: Take this as an argument, once all the APIs we used have moved to
510 // taking it as an input instead of hard-coding llvm::errs.
511 llvm::raw_ostream &OS = llvm::errs();
512
513 // Create the target instance.
514 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
515 if (!hasTarget())
516 return false;
517
518 // Inform the target of the language options.
519 //
520 // FIXME: We shouldn't need to do this, the target should be immutable once
521 // created. This complexity should be lifted elsewhere.
522 getTarget().setForcedLangOptions(getLangOpts());
523
524 // Validate/process some options.
525 if (getHeaderSearchOpts().Verbose)
526 OS << "clang -cc1 version " CLANG_VERSION_STRING
527 << " based upon " << PACKAGE_STRING
528 << " hosted on " << llvm::sys::getHostTriple() << "\n";
529
530 if (getFrontendOpts().ShowTimers)
531 createFrontendTimer();
532
Douglas Gregor95dd5582010-03-30 17:33:59 +0000533 if (getFrontendOpts().ShowStats)
534 llvm::EnableStatistics();
535
Daniel Dunbar0397af22010-01-13 00:48:06 +0000536 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
537 const std::string &InFile = getFrontendOpts().Inputs[i].second;
538
Daniel Dunbar20560482010-06-07 23:23:50 +0000539 // Reset the ID tables if we are reusing the SourceManager.
540 if (hasSourceManager())
541 getSourceManager().clearIDTables();
Daniel Dunbar0397af22010-01-13 00:48:06 +0000542
Daniel Dunbard3598a62010-06-07 23:23:06 +0000543 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000544 Act.Execute();
545 Act.EndSourceFile();
546 }
547 }
548
Chris Lattner53eee7b2010-04-07 18:47:42 +0000549 if (getDiagnosticOpts().ShowCarets) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000550 // We can have multiple diagnostics sharing one diagnostic client.
551 // Get the total number of warnings/errors from the client.
552 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
553 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
Chris Lattner53eee7b2010-04-07 18:47:42 +0000554
555 if (NumWarnings)
556 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
557 if (NumWarnings && NumErrors)
558 OS << " and ";
559 if (NumErrors)
560 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
561 if (NumWarnings || NumErrors)
562 OS << " generated.\n";
563 }
Daniel Dunbar0397af22010-01-13 00:48:06 +0000564
Daniel Dunbar20560482010-06-07 23:23:50 +0000565 if (getFrontendOpts().ShowStats && hasFileManager()) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000566 getFileManager().PrintStats();
567 OS << "\n";
568 }
569
Argyrios Kyrtzidisab41b972010-11-18 21:13:57 +0000570 return !getDiagnostics().getClient()->getNumErrors();
Daniel Dunbar0397af22010-01-13 00:48:06 +0000571}
572
573