blob: 95d417f63347dda8d0195cf0e3ba6842ae08afe4 [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
Axel Naumann7d0c4cc2010-10-11 09:13:46 +0000116void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv) {
Douglas Gregor28019772010-04-05 23:52:57 +0000117 Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000118}
119
Douglas Gregor28019772010-04-05 23:52:57 +0000120llvm::IntrusiveRefCntPtr<Diagnostic>
121CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
Axel Naumann7d0c4cc2010-10-11 09:13:46 +0000122 int Argc, const char* const *Argv) {
Douglas Gregor28019772010-04-05 23:52:57 +0000123 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic());
Daniel Dunbar221c7212009-11-14 07:53:24 +0000124
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000125 // Create the diagnostic client for reporting errors or for
126 // implementing -verify.
Douglas Gregord93256e2010-01-28 06:00:51 +0000127 llvm::OwningPtr<DiagnosticClient> DiagClient;
Douglas Gregord3ab63e2010-10-11 22:02:06 +0000128 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
Daniel Dunbarf79dced2009-11-14 03:24:39 +0000129
130 // Chain in -verify checker, if requested.
131 if (Opts.VerifyDiagnostics)
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000132 Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000133
134 if (!Opts.DumpBuildInformation.empty())
Kovarththanan Rajaratnam3d67b1e2010-03-17 09:24:48 +0000135 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000136
137 // Configure our handling of diagnostics.
Kovarththanan Rajaratnam5bf932b2010-03-17 09:36:02 +0000138 ProcessWarningOptions(*Diags, Opts);
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000139
Douglas Gregor28019772010-04-05 23:52:57 +0000140 return Diags;
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000141}
142
143// File Manager
144
Daniel Dunbar16b74492009-11-13 04:12:06 +0000145void CompilerInstance::createFileManager() {
146 FileMgr.reset(new FileManager());
147}
148
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000149// Source Manager
150
Daniel Dunbar16b74492009-11-13 04:12:06 +0000151void CompilerInstance::createSourceManager() {
Douglas Gregorf715ca12010-03-16 00:06:06 +0000152 SourceMgr.reset(new SourceManager(getDiagnostics()));
Daniel Dunbar16b74492009-11-13 04:12:06 +0000153}
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000154
Daniel Dunbar0fbb3d92009-11-13 05:52:34 +0000155// Preprocessor
156
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000157void CompilerInstance::createPreprocessor() {
158 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
159 getPreprocessorOpts(), getHeaderSearchOpts(),
160 getDependencyOutputOpts(), getTarget(),
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000161 getFrontendOpts(), getSourceManager(),
162 getFileManager()));
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000163}
164
165Preprocessor *
166CompilerInstance::createPreprocessor(Diagnostic &Diags,
167 const LangOptions &LangInfo,
168 const PreprocessorOptions &PPOpts,
169 const HeaderSearchOptions &HSOpts,
170 const DependencyOutputOptions &DepOpts,
171 const TargetInfo &Target,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000172 const FrontendOptions &FEOpts,
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000173 SourceManager &SourceMgr,
174 FileManager &FileMgr) {
175 // Create a PTH manager if we are using some form of a token cache.
176 PTHManager *PTHMgr = 0;
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000177 if (!PPOpts.TokenCache.empty())
178 PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000179
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000180 // Create the Preprocessor.
181 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
182 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
183 SourceMgr, *HeaderInfo, PTHMgr,
184 /*OwnsHeaderSearch=*/true);
185
186 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
187 // That argument is used as the IdentifierInfoLookup argument to
188 // IdentifierTable's ctor.
189 if (PTHMgr) {
190 PTHMgr->setPreprocessor(PP);
191 PP->setPTHManager(PTHMgr);
192 }
193
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000194 if (PPOpts.DetailedRecord)
195 PP->createPreprocessingRecord();
196
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000197 InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
Daniel Dunbar22dacfa2009-11-13 05:52:11 +0000198
199 // Handle generating dependencies, if requested.
200 if (!DepOpts.OutputFile.empty())
201 AttachDependencyFileGen(*PP, DepOpts);
202
203 return PP;
204}
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000205
206// ASTContext
207
208void CompilerInstance::createASTContext() {
209 Preprocessor &PP = getPreprocessor();
210 Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
211 getTarget(), PP.getIdentifierTable(),
212 PP.getSelectorTable(), PP.getBuiltinInfo(),
Daniel Dunbar5eb81002009-11-13 08:20:47 +0000213 /*size_reserve=*/ 0));
214}
Daniel Dunbar0f800392009-11-13 08:21:10 +0000215
216// ExternalASTSource
217
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000218void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000219 bool DisablePCHValidation,
220 void *DeserializationListener){
Daniel Dunbar0f800392009-11-13 08:21:10 +0000221 llvm::OwningPtr<ExternalASTSource> Source;
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000222 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
Daniel Dunbar0f800392009-11-13 08:21:10 +0000223 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000224 DisablePCHValidation,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000225 getPreprocessor(), getASTContext(),
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000226 DeserializationListener,
227 Preamble));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000228 getASTContext().setExternalSource(Source);
229}
230
231ExternalASTSource *
232CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
233 const std::string &Sysroot,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000234 bool DisablePCHValidation,
Daniel Dunbar0f800392009-11-13 08:21:10 +0000235 Preprocessor &PP,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000236 ASTContext &Context,
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000237 void *DeserializationListener,
238 bool Preamble) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000239 llvm::OwningPtr<ASTReader> Reader;
240 Reader.reset(new ASTReader(PP, &Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000241 Sysroot.empty() ? 0 : Sysroot.c_str(),
242 DisablePCHValidation));
Daniel Dunbar0f800392009-11-13 08:21:10 +0000243
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000244 Reader->setDeserializationListener(
Sebastian Redl571db7f2010-08-18 23:56:56 +0000245 static_cast<ASTDeserializationListener *>(DeserializationListener));
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +0000246 switch (Reader->ReadAST(Path,
247 Preamble ? ASTReader::Preamble : ASTReader::PCH)) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000248 case ASTReader::Success:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000249 // Set the predefines buffer as suggested by the PCH reader. Typically, the
250 // predefines buffer will be empty.
251 PP.setPredefines(Reader->getSuggestedPredefines());
252 return Reader.take();
253
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000254 case ASTReader::Failure:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000255 // Unrecoverable failure: don't even try to process the input file.
256 break;
257
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000258 case ASTReader::IgnorePCH:
Daniel Dunbar0f800392009-11-13 08:21:10 +0000259 // No suitable PCH file could be found. Return an error.
260 break;
261 }
262
263 return 0;
264}
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000265
266// Code Completion
267
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000268static bool EnableCodeCompletion(Preprocessor &PP,
269 const std::string &Filename,
270 unsigned Line,
271 unsigned Column) {
272 // Tell the source manager to chop off the given file at a specific
273 // line and column.
274 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
275 if (!Entry) {
276 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
277 << Filename;
278 return true;
279 }
280
281 // Truncate the named file at the given line/column.
282 PP.SetCodeCompletionPoint(Entry, Line, Column);
283 return false;
284}
285
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000286void CompilerInstance::createCodeCompletionConsumer() {
287 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000288 if (!CompletionConsumer) {
289 CompletionConsumer.reset(
290 createCodeCompletionConsumer(getPreprocessor(),
291 Loc.FileName, Loc.Line, Loc.Column,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000292 getFrontendOpts().ShowMacrosInCodeCompletion,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000293 getFrontendOpts().ShowCodePatternsInCodeCompletion,
Douglas Gregor8071e422010-08-15 06:18:01 +0000294 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000295 llvm::outs()));
296 if (!CompletionConsumer)
297 return;
298 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
299 Loc.Line, Loc.Column)) {
300 CompletionConsumer.reset();
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000301 return;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000302 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000303
304 if (CompletionConsumer->isOutputBinary() &&
305 llvm::sys::Program::ChangeStdoutToBinary()) {
306 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
307 CompletionConsumer.reset();
308 }
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000309}
310
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000311void CompilerInstance::createFrontendTimer() {
312 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
313}
314
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000315CodeCompleteConsumer *
316CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
317 const std::string &Filename,
318 unsigned Line,
319 unsigned Column,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000320 bool ShowMacros,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000321 bool ShowCodePatterns,
Douglas Gregor8071e422010-08-15 06:18:01 +0000322 bool ShowGlobals,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000323 llvm::raw_ostream &OS) {
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000324 if (EnableCodeCompletion(PP, Filename, Line, Column))
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000325 return 0;
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000326
327 // Set up the creation routine for code-completion.
Douglas Gregora9f4f622010-10-11 22:12:15 +0000328 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
Douglas Gregor8071e422010-08-15 06:18:01 +0000329 ShowGlobals, OS);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000330}
Daniel Dunbara9204832009-11-13 10:37:48 +0000331
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000332void CompilerInstance::createSema(bool CompleteTranslationUnit,
333 CodeCompleteConsumer *CompletionConsumer) {
334 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
335 CompleteTranslationUnit, CompletionConsumer));
336}
337
Daniel Dunbara9204832009-11-13 10:37:48 +0000338// Output Files
339
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000340void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
341 assert(OutFile.OS && "Attempt to add empty stream to output list!");
342 OutputFiles.push_back(OutFile);
Daniel Dunbara9204832009-11-13 10:37:48 +0000343}
344
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000345void CompilerInstance::clearOutputFiles(bool EraseFiles) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000346 for (std::list<OutputFile>::iterator
Daniel Dunbara9204832009-11-13 10:37:48 +0000347 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000348 delete it->OS;
349 if (!it->TempFilename.empty()) {
350 llvm::sys::Path TempPath(it->TempFilename);
351 if (EraseFiles)
352 TempPath.eraseFromDisk();
353 else {
354 std::string Error;
355 if (TempPath.renamePathOnDisk(llvm::sys::Path(it->Filename), &Error)) {
356 getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
357 << it->TempFilename << it->Filename << Error;
358 TempPath.eraseFromDisk();
359 }
360 }
361 } else if (!it->Filename.empty() && EraseFiles)
362 llvm::sys::Path(it->Filename).eraseFromDisk();
363
Daniel Dunbara9204832009-11-13 10:37:48 +0000364 }
365 OutputFiles.clear();
366}
367
Daniel Dunbarf482d592009-11-13 18:32:08 +0000368llvm::raw_fd_ostream *
369CompilerInstance::createDefaultOutputFile(bool Binary,
370 llvm::StringRef InFile,
371 llvm::StringRef Extension) {
372 return createOutputFile(getFrontendOpts().OutputFile, Binary,
373 InFile, Extension);
374}
375
376llvm::raw_fd_ostream *
377CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
378 bool Binary,
379 llvm::StringRef InFile,
380 llvm::StringRef Extension) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000381 std::string Error, OutputPathName, TempPathName;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000382 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
383 InFile, Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000384 &OutputPathName,
385 &TempPathName);
Daniel Dunbarf482d592009-11-13 18:32:08 +0000386 if (!OS) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000387 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
388 << OutputPath << Error;
389 return 0;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000390 }
391
392 // Add the output file -- but don't try to remove "-", since this means we are
393 // using stdin.
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000394 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
395 TempPathName, OS));
Daniel Dunbarf482d592009-11-13 18:32:08 +0000396
397 return OS;
398}
399
400llvm::raw_fd_ostream *
401CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
402 std::string &Error,
403 bool Binary,
404 llvm::StringRef InFile,
405 llvm::StringRef Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000406 std::string *ResultPathName,
407 std::string *TempPathName) {
408 std::string OutFile, TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000409 if (!OutputPath.empty()) {
410 OutFile = OutputPath;
411 } else if (InFile == "-") {
412 OutFile = "-";
413 } else if (!Extension.empty()) {
414 llvm::sys::Path Path(InFile);
415 Path.eraseSuffix();
416 Path.appendSuffix(Extension);
417 OutFile = Path.str();
418 } else {
419 OutFile = "-";
420 }
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000421
422 if (OutFile != "-") {
423 llvm::sys::Path OutPath(OutFile);
424 // Only create the temporary if we can actually write to OutPath, otherwise
425 // we want to fail early.
426 if (!OutPath.exists() ||
427 (OutPath.isRegularFile() && OutPath.canWrite())) {
428 // Create a temporary file.
429 llvm::sys::Path TempPath(OutFile);
430 if (!TempPath.createTemporaryFileOnDisk())
431 TempFile = TempPath.str();
432 }
433 }
434
435 std::string OSFile = OutFile;
436 if (!TempFile.empty())
437 OSFile = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000438
Daniel Dunbarfc971022009-11-20 22:32:38 +0000439 llvm::OwningPtr<llvm::raw_fd_ostream> OS(
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000440 new llvm::raw_fd_ostream(OSFile.c_str(), Error,
Daniel Dunbarfc971022009-11-20 22:32:38 +0000441 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
442 if (!Error.empty())
Daniel Dunbarf482d592009-11-13 18:32:08 +0000443 return 0;
444
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000445 // Make sure the out stream file gets removed if we crash.
446 llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
447
Daniel Dunbarf482d592009-11-13 18:32:08 +0000448 if (ResultPathName)
449 *ResultPathName = OutFile;
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000450 if (TempPathName)
451 *TempPathName = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000452
Daniel Dunbarfc971022009-11-20 22:32:38 +0000453 return OS.take();
Daniel Dunbarf482d592009-11-13 18:32:08 +0000454}
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000455
456// Initialization Utilities
457
458bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
459 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
460 getSourceManager(), getFrontendOpts());
461}
462
463bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
464 Diagnostic &Diags,
465 FileManager &FileMgr,
466 SourceManager &SourceMgr,
467 const FrontendOptions &Opts) {
468 // Figure out where to get and map in the main file.
Daniel Dunbar27585952010-03-19 19:44:04 +0000469 if (InputFile != "-") {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000470 const FileEntry *File = FileMgr.getFile(InputFile);
Dan Gohman694137c2010-10-26 21:13:51 +0000471 if (!File) {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000472 Diags.Report(diag::err_fe_error_reading) << InputFile;
473 return false;
474 }
Dan Gohman694137c2010-10-26 21:13:51 +0000475 SourceMgr.createMainFileID(File);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000476 } else {
477 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Dan Gohman694137c2010-10-26 21:13:51 +0000478 if (!SB) {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000479 Diags.Report(diag::err_fe_error_reading_stdin);
480 return false;
481 }
Dan Gohman90d90812010-10-26 23:21:25 +0000482 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
483 SB->getBufferSize(), 0);
484 SourceMgr.createMainFileID(File);
485 SourceMgr.overrideFileContents(File, SB);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000486 }
487
Dan Gohman694137c2010-10-26 21:13:51 +0000488 assert(!SourceMgr.getMainFileID().isInvalid() &&
489 "Couldn't establish MainFileID!");
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000490 return true;
491}
Daniel Dunbar0397af22010-01-13 00:48:06 +0000492
493// High-Level Operations
494
495bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
496 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
497 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
498 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
499
500 // FIXME: Take this as an argument, once all the APIs we used have moved to
501 // taking it as an input instead of hard-coding llvm::errs.
502 llvm::raw_ostream &OS = llvm::errs();
503
504 // Create the target instance.
505 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
506 if (!hasTarget())
507 return false;
508
509 // Inform the target of the language options.
510 //
511 // FIXME: We shouldn't need to do this, the target should be immutable once
512 // created. This complexity should be lifted elsewhere.
513 getTarget().setForcedLangOptions(getLangOpts());
514
515 // Validate/process some options.
516 if (getHeaderSearchOpts().Verbose)
517 OS << "clang -cc1 version " CLANG_VERSION_STRING
518 << " based upon " << PACKAGE_STRING
519 << " hosted on " << llvm::sys::getHostTriple() << "\n";
520
521 if (getFrontendOpts().ShowTimers)
522 createFrontendTimer();
523
Douglas Gregor95dd5582010-03-30 17:33:59 +0000524 if (getFrontendOpts().ShowStats)
525 llvm::EnableStatistics();
526
Daniel Dunbar0397af22010-01-13 00:48:06 +0000527 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
528 const std::string &InFile = getFrontendOpts().Inputs[i].second;
529
Daniel Dunbar20560482010-06-07 23:23:50 +0000530 // Reset the ID tables if we are reusing the SourceManager.
531 if (hasSourceManager())
532 getSourceManager().clearIDTables();
Daniel Dunbar0397af22010-01-13 00:48:06 +0000533
Daniel Dunbard3598a62010-06-07 23:23:06 +0000534 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000535 Act.Execute();
536 Act.EndSourceFile();
537 }
538 }
539
Chris Lattner53eee7b2010-04-07 18:47:42 +0000540 if (getDiagnosticOpts().ShowCarets) {
541 unsigned NumWarnings = getDiagnostics().getNumWarnings();
Douglas Gregor1864f2e2010-04-14 22:19:45 +0000542 unsigned NumErrors = getDiagnostics().getNumErrors() -
543 getDiagnostics().getNumErrorsSuppressed();
Chris Lattner53eee7b2010-04-07 18:47:42 +0000544
545 if (NumWarnings)
546 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
547 if (NumWarnings && NumErrors)
548 OS << " and ";
549 if (NumErrors)
550 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
551 if (NumWarnings || NumErrors)
552 OS << " generated.\n";
553 }
Daniel Dunbar0397af22010-01-13 00:48:06 +0000554
Daniel Dunbar20560482010-06-07 23:23:50 +0000555 if (getFrontendOpts().ShowStats && hasFileManager()) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000556 getFileManager().PrintStats();
557 OS << "\n";
558 }
559
560 // Return the appropriate status when verifying diagnostics.
561 //
562 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
563 // this.
564 if (getDiagnosticOpts().VerifyDiagnostics)
565 return !static_cast<VerifyDiagnosticsClient&>(
566 getDiagnosticClient()).HadErrors();
567
568 return !getDiagnostics().getNumErrors();
569}
570
571