blob: 7cfcd1c41984d2359c60a4a13e74315c70f2289f [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) {
Douglas Gregor28019772010-04-05 23:52:57 +0000125 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000126
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000127 // Create the diagnostic client for reporting errors or for
128 // implementing -verify.
Douglas Gregore47be3e2010-11-11 00:39:14 +0000129 if (Client)
130 Diags->setClient(Client);
131 else
132 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000133
134 // Chain in -verify checker, if requested.
135 if (Opts.VerifyDiagnostics)
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000136 Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000137
138 if (!Opts.DumpBuildInformation.empty())
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000139 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000140
141 // Configure our handling of diagnostics.
Kovarththanan Rajaratnam5bf932b2010-03-17 09:36:02 +0000142 ProcessWarningOptions(*Diags, Opts);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000143
Douglas Gregor28019772010-04-05 23:52:57 +0000144 return Diags;
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000145}
146
147// File Manager
148
Daniel Dunbar16b74492009-11-13 04:12:06 +0000149void CompilerInstance::createFileManager() {
150 FileMgr.reset(new FileManager());
151}
152
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000153// Source Manager
154
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000155void CompilerInstance::createSourceManager(FileManager &FileMgr,
156 const FileSystemOptions &FSOpts) {
157 SourceMgr.reset(new SourceManager(getDiagnostics(), FileMgr, FSOpts));
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(),
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000166 getFrontendOpts(), getFileSystemOpts(),
167 getSourceManager(), 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,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000178 const FileSystemOptions &FSOpts,
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000179 SourceManager &SourceMgr,
180 FileManager &FileMgr) {
181 // Create a PTH manager if we are using some form of a token cache.
182 PTHManager *PTHMgr = 0;
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000183 if (!PPOpts.TokenCache.empty())
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000184 PTHMgr = PTHManager::Create(PPOpts.TokenCache, FileMgr, FSOpts, Diags);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000185
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000186 // Create the Preprocessor.
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000187 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr, FSOpts);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000188 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
189 SourceMgr, *HeaderInfo, PTHMgr,
190 /*OwnsHeaderSearch=*/true);
191
192 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
193 // That argument is used as the IdentifierInfoLookup argument to
194 // IdentifierTable's ctor.
195 if (PTHMgr) {
196 PTHMgr->setPreprocessor(PP);
197 PP->setPTHManager(PTHMgr);
198 }
199
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000200 if (PPOpts.DetailedRecord)
201 PP->createPreprocessingRecord();
202
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000203 InitializePreprocessor(*PP, FSOpts, PPOpts, HSOpts, FEOpts);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000204
205 // Handle generating dependencies, if requested.
206 if (!DepOpts.OutputFile.empty())
207 AttachDependencyFileGen(*PP, DepOpts);
208
209 return PP;
210}
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000211
212// ASTContext
213
214void CompilerInstance::createASTContext() {
215 Preprocessor &PP = getPreprocessor();
216 Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
217 getTarget(), PP.getIdentifierTable(),
218 PP.getSelectorTable(), PP.getBuiltinInfo(),
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000219 /*size_reserve=*/ 0));
220}
Daniel Dunbar0f800392009-11-13 08:21:10 +0000221
222// ExternalASTSource
223
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000224void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000225 bool DisablePCHValidation,
226 void *DeserializationListener){
Daniel Dunbar0f800392009-11-13 08:21:10 +0000227 llvm::OwningPtr<ExternalASTSource> Source;
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000228 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
Daniel Dunbar0f800392009-11-13 08:21:10 +0000229 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000230 DisablePCHValidation,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000231 getPreprocessor(), getASTContext(),
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000232 DeserializationListener,
233 Preamble));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000234 getASTContext().setExternalSource(Source);
235}
236
237ExternalASTSource *
238CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
239 const std::string &Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000240 bool DisablePCHValidation,
Daniel Dunbar0f800392009-11-13 08:21:10 +0000241 Preprocessor &PP,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000242 ASTContext &Context,
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000243 void *DeserializationListener,
244 bool Preamble) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000245 llvm::OwningPtr<ASTReader> Reader;
246 Reader.reset(new ASTReader(PP, &Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000247 Sysroot.empty() ? 0 : Sysroot.c_str(),
248 DisablePCHValidation));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000249
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000250 Reader->setDeserializationListener(
Sebastian Redl571db7f2010-08-18 23:56:56 +0000251 static_cast<ASTDeserializationListener *>(DeserializationListener));
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000252 switch (Reader->ReadAST(Path,
253 Preamble ? ASTReader::Preamble : ASTReader::PCH)) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000254 case ASTReader::Success:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000255 // Set the predefines buffer as suggested by the PCH reader. Typically, the
256 // predefines buffer will be empty.
257 PP.setPredefines(Reader->getSuggestedPredefines());
258 return Reader.take();
259
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000260 case ASTReader::Failure:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000261 // Unrecoverable failure: don't even try to process the input file.
262 break;
263
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000264 case ASTReader::IgnorePCH:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000265 // No suitable PCH file could be found. Return an error.
266 break;
267 }
268
269 return 0;
270}
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000271
272// Code Completion
273
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000274static bool EnableCodeCompletion(Preprocessor &PP,
275 const std::string &Filename,
276 unsigned Line,
277 unsigned Column) {
278 // Tell the source manager to chop off the given file at a specific
279 // line and column.
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000280 const FileEntry *Entry = PP.getFileManager().getFile(Filename,
281 PP.getFileSystemOpts());
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000282 if (!Entry) {
283 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
284 << Filename;
285 return true;
286 }
287
288 // Truncate the named file at the given line/column.
289 PP.SetCodeCompletionPoint(Entry, Line, Column);
290 return false;
291}
292
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000293void CompilerInstance::createCodeCompletionConsumer() {
294 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000295 if (!CompletionConsumer) {
296 CompletionConsumer.reset(
297 createCodeCompletionConsumer(getPreprocessor(),
298 Loc.FileName, Loc.Line, Loc.Column,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000299 getFrontendOpts().ShowMacrosInCodeCompletion,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000300 getFrontendOpts().ShowCodePatternsInCodeCompletion,
Douglas Gregor8071e422010-08-15 06:18:01 +0000301 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000302 llvm::outs()));
303 if (!CompletionConsumer)
304 return;
305 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
306 Loc.Line, Loc.Column)) {
307 CompletionConsumer.reset();
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000308 return;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000309 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000310
311 if (CompletionConsumer->isOutputBinary() &&
312 llvm::sys::Program::ChangeStdoutToBinary()) {
313 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
314 CompletionConsumer.reset();
315 }
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000316}
317
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000318void CompilerInstance::createFrontendTimer() {
319 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
320}
321
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000322CodeCompleteConsumer *
323CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
324 const std::string &Filename,
325 unsigned Line,
326 unsigned Column,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000327 bool ShowMacros,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000328 bool ShowCodePatterns,
Douglas Gregor8071e422010-08-15 06:18:01 +0000329 bool ShowGlobals,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000330 llvm::raw_ostream &OS) {
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000331 if (EnableCodeCompletion(PP, Filename, Line, Column))
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000332 return 0;
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000333
334 // Set up the creation routine for code-completion.
Douglas Gregora9f4f622010-10-11 22:12:15 +0000335 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
Douglas Gregor8071e422010-08-15 06:18:01 +0000336 ShowGlobals, OS);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000337}
Daniel Dunbara9204832009-11-13 10:37:48 +0000338
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000339void CompilerInstance::createSema(bool CompleteTranslationUnit,
340 CodeCompleteConsumer *CompletionConsumer) {
341 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
342 CompleteTranslationUnit, CompletionConsumer));
343}
344
Daniel Dunbara9204832009-11-13 10:37:48 +0000345// Output Files
346
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000347void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
348 assert(OutFile.OS && "Attempt to add empty stream to output list!");
349 OutputFiles.push_back(OutFile);
Daniel Dunbara9204832009-11-13 10:37:48 +0000350}
351
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000352void CompilerInstance::clearOutputFiles(bool EraseFiles) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000353 for (std::list<OutputFile>::iterator
Daniel Dunbara9204832009-11-13 10:37:48 +0000354 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000355 delete it->OS;
356 if (!it->TempFilename.empty()) {
357 llvm::sys::Path TempPath(it->TempFilename);
358 if (EraseFiles)
359 TempPath.eraseFromDisk();
360 else {
361 std::string Error;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000362 llvm::sys::Path NewOutFile(it->Filename);
363 // If '-working-directory' was passed, the output filename should be
364 // relative to that.
365 FileManager::FixupRelativePath(NewOutFile, getFileSystemOpts());
366 if (TempPath.renamePathOnDisk(NewOutFile, &Error)) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000367 getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
368 << it->TempFilename << it->Filename << Error;
369 TempPath.eraseFromDisk();
370 }
371 }
372 } else if (!it->Filename.empty() && EraseFiles)
373 llvm::sys::Path(it->Filename).eraseFromDisk();
374
Daniel Dunbara9204832009-11-13 10:37:48 +0000375 }
376 OutputFiles.clear();
377}
378
Daniel Dunbarf482d592009-11-13 18:32:08 +0000379llvm::raw_fd_ostream *
380CompilerInstance::createDefaultOutputFile(bool Binary,
381 llvm::StringRef InFile,
382 llvm::StringRef Extension) {
383 return createOutputFile(getFrontendOpts().OutputFile, Binary,
384 InFile, Extension);
385}
386
387llvm::raw_fd_ostream *
388CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
389 bool Binary,
390 llvm::StringRef InFile,
391 llvm::StringRef Extension) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000392 std::string Error, OutputPathName, TempPathName;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000393 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
394 InFile, Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000395 &OutputPathName,
396 &TempPathName);
Daniel Dunbarf482d592009-11-13 18:32:08 +0000397 if (!OS) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000398 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
399 << OutputPath << Error;
400 return 0;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000401 }
402
403 // Add the output file -- but don't try to remove "-", since this means we are
404 // using stdin.
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000405 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
406 TempPathName, OS));
Daniel Dunbarf482d592009-11-13 18:32:08 +0000407
408 return OS;
409}
410
411llvm::raw_fd_ostream *
412CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
413 std::string &Error,
414 bool Binary,
415 llvm::StringRef InFile,
416 llvm::StringRef Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000417 std::string *ResultPathName,
418 std::string *TempPathName) {
419 std::string OutFile, TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000420 if (!OutputPath.empty()) {
421 OutFile = OutputPath;
422 } else if (InFile == "-") {
423 OutFile = "-";
424 } else if (!Extension.empty()) {
425 llvm::sys::Path Path(InFile);
426 Path.eraseSuffix();
427 Path.appendSuffix(Extension);
428 OutFile = Path.str();
429 } else {
430 OutFile = "-";
431 }
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000432
433 if (OutFile != "-") {
434 llvm::sys::Path OutPath(OutFile);
435 // Only create the temporary if we can actually write to OutPath, otherwise
436 // we want to fail early.
437 if (!OutPath.exists() ||
438 (OutPath.isRegularFile() && OutPath.canWrite())) {
439 // Create a temporary file.
440 llvm::sys::Path TempPath(OutFile);
441 if (!TempPath.createTemporaryFileOnDisk())
442 TempFile = TempPath.str();
443 }
444 }
445
446 std::string OSFile = OutFile;
447 if (!TempFile.empty())
448 OSFile = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000449
Daniel Dunbarfc971022009-11-20 22:32:38 +0000450 llvm::OwningPtr<llvm::raw_fd_ostream> OS(
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000451 new llvm::raw_fd_ostream(OSFile.c_str(), Error,
Daniel Dunbarfc971022009-11-20 22:32:38 +0000452 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
453 if (!Error.empty())
Daniel Dunbarf482d592009-11-13 18:32:08 +0000454 return 0;
455
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000456 // Make sure the out stream file gets removed if we crash.
457 llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
458
Daniel Dunbarf482d592009-11-13 18:32:08 +0000459 if (ResultPathName)
460 *ResultPathName = OutFile;
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000461 if (TempPathName)
462 *TempPathName = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000463
Daniel Dunbarfc971022009-11-20 22:32:38 +0000464 return OS.take();
Daniel Dunbarf482d592009-11-13 18:32:08 +0000465}
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000466
467// Initialization Utilities
468
469bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
470 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000471 getFileSystemOpts(),
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000472 getSourceManager(), getFrontendOpts());
473}
474
475bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
476 Diagnostic &Diags,
477 FileManager &FileMgr,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000478 const FileSystemOptions &FSOpts,
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000479 SourceManager &SourceMgr,
480 const FrontendOptions &Opts) {
481 // Figure out where to get and map in the main file.
Daniel Dunbar27585952010-03-19 19:44:04 +0000482 if (InputFile != "-") {
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000483 const FileEntry *File = FileMgr.getFile(InputFile, FSOpts);
Dan Gohman694137c2010-10-26 21:13:51 +0000484 if (!File) {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000485 Diags.Report(diag::err_fe_error_reading) << InputFile;
486 return false;
487 }
Dan Gohman694137c2010-10-26 21:13:51 +0000488 SourceMgr.createMainFileID(File);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000489 } else {
490 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Dan Gohman694137c2010-10-26 21:13:51 +0000491 if (!SB) {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000492 Diags.Report(diag::err_fe_error_reading_stdin);
493 return false;
494 }
Dan Gohman90d90812010-10-26 23:21:25 +0000495 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000496 SB->getBufferSize(), 0,
497 FSOpts);
Dan Gohman90d90812010-10-26 23:21:25 +0000498 SourceMgr.createMainFileID(File);
499 SourceMgr.overrideFileContents(File, SB);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000500 }
501
Dan Gohman694137c2010-10-26 21:13:51 +0000502 assert(!SourceMgr.getMainFileID().isInvalid() &&
503 "Couldn't establish MainFileID!");
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000504 return true;
505}
Daniel Dunbar0397af22010-01-13 00:48:06 +0000506
507// High-Level Operations
508
509bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
510 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
511 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
512 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
513
514 // FIXME: Take this as an argument, once all the APIs we used have moved to
515 // taking it as an input instead of hard-coding llvm::errs.
516 llvm::raw_ostream &OS = llvm::errs();
517
518 // Create the target instance.
519 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
520 if (!hasTarget())
521 return false;
522
523 // Inform the target of the language options.
524 //
525 // FIXME: We shouldn't need to do this, the target should be immutable once
526 // created. This complexity should be lifted elsewhere.
527 getTarget().setForcedLangOptions(getLangOpts());
528
529 // Validate/process some options.
530 if (getHeaderSearchOpts().Verbose)
531 OS << "clang -cc1 version " CLANG_VERSION_STRING
532 << " based upon " << PACKAGE_STRING
533 << " hosted on " << llvm::sys::getHostTriple() << "\n";
534
535 if (getFrontendOpts().ShowTimers)
536 createFrontendTimer();
537
Douglas Gregor95dd5582010-03-30 17:33:59 +0000538 if (getFrontendOpts().ShowStats)
539 llvm::EnableStatistics();
540
Daniel Dunbar0397af22010-01-13 00:48:06 +0000541 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
542 const std::string &InFile = getFrontendOpts().Inputs[i].second;
543
Daniel Dunbar20560482010-06-07 23:23:50 +0000544 // Reset the ID tables if we are reusing the SourceManager.
545 if (hasSourceManager())
546 getSourceManager().clearIDTables();
Daniel Dunbar0397af22010-01-13 00:48:06 +0000547
Daniel Dunbard3598a62010-06-07 23:23:06 +0000548 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000549 Act.Execute();
550 Act.EndSourceFile();
551 }
552 }
553
Chris Lattner53eee7b2010-04-07 18:47:42 +0000554 if (getDiagnosticOpts().ShowCarets) {
555 unsigned NumWarnings = getDiagnostics().getNumWarnings();
Douglas Gregor1864f2e2010-04-14 22:19:45 +0000556 unsigned NumErrors = getDiagnostics().getNumErrors() -
557 getDiagnostics().getNumErrorsSuppressed();
Chris Lattner53eee7b2010-04-07 18:47:42 +0000558
559 if (NumWarnings)
560 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
561 if (NumWarnings && NumErrors)
562 OS << " and ";
563 if (NumErrors)
564 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
565 if (NumWarnings || NumErrors)
566 OS << " generated.\n";
567 }
Daniel Dunbar0397af22010-01-13 00:48:06 +0000568
Daniel Dunbar20560482010-06-07 23:23:50 +0000569 if (getFrontendOpts().ShowStats && hasFileManager()) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000570 getFileManager().PrintStats();
571 OS << "\n";
572 }
573
574 // Return the appropriate status when verifying diagnostics.
575 //
576 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
577 // this.
578 if (getDiagnosticOpts().VerifyDiagnostics)
579 return !static_cast<VerifyDiagnosticsClient&>(
580 getDiagnosticClient()).HadErrors();
581
582 return !getDiagnostics().getNumErrors();
583}
584
585