blob: 44e5fd2215ca1227bf1c4e6581f702955892fb3e [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,
292 getFrontendOpts().DebugCodeCompletionPrinter,
293 getFrontendOpts().ShowMacrosInCodeCompletion,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000294 getFrontendOpts().ShowCodePatternsInCodeCompletion,
Douglas Gregor8071e422010-08-15 06:18:01 +0000295 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000296 llvm::outs()));
297 if (!CompletionConsumer)
298 return;
299 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
300 Loc.Line, Loc.Column)) {
301 CompletionConsumer.reset();
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000302 return;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000303 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000304
305 if (CompletionConsumer->isOutputBinary() &&
306 llvm::sys::Program::ChangeStdoutToBinary()) {
307 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
308 CompletionConsumer.reset();
309 }
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000310}
311
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000312void CompilerInstance::createFrontendTimer() {
313 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
314}
315
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000316CodeCompleteConsumer *
317CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
318 const std::string &Filename,
319 unsigned Line,
320 unsigned Column,
321 bool UseDebugPrinter,
322 bool ShowMacros,
Douglas Gregord8e8a582010-05-25 21:41:55 +0000323 bool ShowCodePatterns,
Douglas Gregor8071e422010-08-15 06:18:01 +0000324 bool ShowGlobals,
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000325 llvm::raw_ostream &OS) {
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000326 if (EnableCodeCompletion(PP, Filename, Line, Column))
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000327 return 0;
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000328
329 // Set up the creation routine for code-completion.
330 if (UseDebugPrinter)
Douglas Gregor8071e422010-08-15 06:18:01 +0000331 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
332 ShowGlobals, OS);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000333 else
Douglas Gregor8071e422010-08-15 06:18:01 +0000334 return new CIndexCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
335 ShowGlobals, OS);
Daniel Dunbarc2f484f2009-11-13 09:36:05 +0000336}
Daniel Dunbara9204832009-11-13 10:37:48 +0000337
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000338void CompilerInstance::createSema(bool CompleteTranslationUnit,
339 CodeCompleteConsumer *CompletionConsumer) {
340 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
341 CompleteTranslationUnit, CompletionConsumer));
342}
343
Daniel Dunbara9204832009-11-13 10:37:48 +0000344// Output Files
345
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000346void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
347 assert(OutFile.OS && "Attempt to add empty stream to output list!");
348 OutputFiles.push_back(OutFile);
Daniel Dunbara9204832009-11-13 10:37:48 +0000349}
350
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000351void CompilerInstance::clearOutputFiles(bool EraseFiles) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000352 for (std::list<OutputFile>::iterator
Daniel Dunbara9204832009-11-13 10:37:48 +0000353 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000354 delete it->OS;
355 if (!it->TempFilename.empty()) {
356 llvm::sys::Path TempPath(it->TempFilename);
357 if (EraseFiles)
358 TempPath.eraseFromDisk();
359 else {
360 std::string Error;
361 if (TempPath.renamePathOnDisk(llvm::sys::Path(it->Filename), &Error)) {
362 getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
363 << it->TempFilename << it->Filename << Error;
364 TempPath.eraseFromDisk();
365 }
366 }
367 } else if (!it->Filename.empty() && EraseFiles)
368 llvm::sys::Path(it->Filename).eraseFromDisk();
369
Daniel Dunbara9204832009-11-13 10:37:48 +0000370 }
371 OutputFiles.clear();
372}
373
Daniel Dunbarf482d592009-11-13 18:32:08 +0000374llvm::raw_fd_ostream *
375CompilerInstance::createDefaultOutputFile(bool Binary,
376 llvm::StringRef InFile,
377 llvm::StringRef Extension) {
378 return createOutputFile(getFrontendOpts().OutputFile, Binary,
379 InFile, Extension);
380}
381
382llvm::raw_fd_ostream *
383CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
384 bool Binary,
385 llvm::StringRef InFile,
386 llvm::StringRef Extension) {
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000387 std::string Error, OutputPathName, TempPathName;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000388 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
389 InFile, Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000390 &OutputPathName,
391 &TempPathName);
Daniel Dunbarf482d592009-11-13 18:32:08 +0000392 if (!OS) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000393 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
394 << OutputPath << Error;
395 return 0;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000396 }
397
398 // Add the output file -- but don't try to remove "-", since this means we are
399 // using stdin.
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000400 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
401 TempPathName, OS));
Daniel Dunbarf482d592009-11-13 18:32:08 +0000402
403 return OS;
404}
405
406llvm::raw_fd_ostream *
407CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
408 std::string &Error,
409 bool Binary,
410 llvm::StringRef InFile,
411 llvm::StringRef Extension,
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000412 std::string *ResultPathName,
413 std::string *TempPathName) {
414 std::string OutFile, TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000415 if (!OutputPath.empty()) {
416 OutFile = OutputPath;
417 } else if (InFile == "-") {
418 OutFile = "-";
419 } else if (!Extension.empty()) {
420 llvm::sys::Path Path(InFile);
421 Path.eraseSuffix();
422 Path.appendSuffix(Extension);
423 OutFile = Path.str();
424 } else {
425 OutFile = "-";
426 }
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000427
428 if (OutFile != "-") {
429 llvm::sys::Path OutPath(OutFile);
430 // Only create the temporary if we can actually write to OutPath, otherwise
431 // we want to fail early.
432 if (!OutPath.exists() ||
433 (OutPath.isRegularFile() && OutPath.canWrite())) {
434 // Create a temporary file.
435 llvm::sys::Path TempPath(OutFile);
436 if (!TempPath.createTemporaryFileOnDisk())
437 TempFile = TempPath.str();
438 }
439 }
440
441 std::string OSFile = OutFile;
442 if (!TempFile.empty())
443 OSFile = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000444
Daniel Dunbarfc971022009-11-20 22:32:38 +0000445 llvm::OwningPtr<llvm::raw_fd_ostream> OS(
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000446 new llvm::raw_fd_ostream(OSFile.c_str(), Error,
Daniel Dunbarfc971022009-11-20 22:32:38 +0000447 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
448 if (!Error.empty())
Daniel Dunbarf482d592009-11-13 18:32:08 +0000449 return 0;
450
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000451 // Make sure the out stream file gets removed if we crash.
452 llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
453
Daniel Dunbarf482d592009-11-13 18:32:08 +0000454 if (ResultPathName)
455 *ResultPathName = OutFile;
Argyrios Kyrtzidisdc245722010-09-17 17:38:48 +0000456 if (TempPathName)
457 *TempPathName = TempFile;
Daniel Dunbarf482d592009-11-13 18:32:08 +0000458
Daniel Dunbarfc971022009-11-20 22:32:38 +0000459 return OS.take();
Daniel Dunbarf482d592009-11-13 18:32:08 +0000460}
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000461
462// Initialization Utilities
463
464bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
465 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
466 getSourceManager(), getFrontendOpts());
467}
468
469bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
470 Diagnostic &Diags,
471 FileManager &FileMgr,
472 SourceManager &SourceMgr,
473 const FrontendOptions &Opts) {
474 // Figure out where to get and map in the main file.
Daniel Dunbar27585952010-03-19 19:44:04 +0000475 if (InputFile != "-") {
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000476 const FileEntry *File = FileMgr.getFile(InputFile);
Dan Gohmanf155dfa2010-08-27 15:44:11 +0000477 if (File) SourceMgr.createMainFileID(File);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000478 if (SourceMgr.getMainFileID().isInvalid()) {
479 Diags.Report(diag::err_fe_error_reading) << InputFile;
480 return false;
481 }
482 } else {
483 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
Dan Gohman6f118972010-05-27 17:33:40 +0000484 if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
Daniel Dunbarccb6cb62009-11-14 07:53:04 +0000485 if (SourceMgr.getMainFileID().isInvalid()) {
486 Diags.Report(diag::err_fe_error_reading_stdin);
487 return false;
488 }
489 }
490
491 return true;
492}
Daniel Dunbar0397af22010-01-13 00:48:06 +0000493
494// High-Level Operations
495
496bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
497 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
498 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
499 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
500
501 // FIXME: Take this as an argument, once all the APIs we used have moved to
502 // taking it as an input instead of hard-coding llvm::errs.
503 llvm::raw_ostream &OS = llvm::errs();
504
505 // Create the target instance.
506 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
507 if (!hasTarget())
508 return false;
509
510 // Inform the target of the language options.
511 //
512 // FIXME: We shouldn't need to do this, the target should be immutable once
513 // created. This complexity should be lifted elsewhere.
514 getTarget().setForcedLangOptions(getLangOpts());
515
516 // Validate/process some options.
517 if (getHeaderSearchOpts().Verbose)
518 OS << "clang -cc1 version " CLANG_VERSION_STRING
519 << " based upon " << PACKAGE_STRING
520 << " hosted on " << llvm::sys::getHostTriple() << "\n";
521
522 if (getFrontendOpts().ShowTimers)
523 createFrontendTimer();
524
Douglas Gregor95dd5582010-03-30 17:33:59 +0000525 if (getFrontendOpts().ShowStats)
526 llvm::EnableStatistics();
527
Daniel Dunbar0397af22010-01-13 00:48:06 +0000528 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
529 const std::string &InFile = getFrontendOpts().Inputs[i].second;
530
Daniel Dunbar20560482010-06-07 23:23:50 +0000531 // Reset the ID tables if we are reusing the SourceManager.
532 if (hasSourceManager())
533 getSourceManager().clearIDTables();
Daniel Dunbar0397af22010-01-13 00:48:06 +0000534
Daniel Dunbard3598a62010-06-07 23:23:06 +0000535 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000536 Act.Execute();
537 Act.EndSourceFile();
538 }
539 }
540
Chris Lattner53eee7b2010-04-07 18:47:42 +0000541 if (getDiagnosticOpts().ShowCarets) {
542 unsigned NumWarnings = getDiagnostics().getNumWarnings();
Douglas Gregor1864f2e2010-04-14 22:19:45 +0000543 unsigned NumErrors = getDiagnostics().getNumErrors() -
544 getDiagnostics().getNumErrorsSuppressed();
Chris Lattner53eee7b2010-04-07 18:47:42 +0000545
546 if (NumWarnings)
547 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
548 if (NumWarnings && NumErrors)
549 OS << " and ";
550 if (NumErrors)
551 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
552 if (NumWarnings || NumErrors)
553 OS << " generated.\n";
554 }
Daniel Dunbar0397af22010-01-13 00:48:06 +0000555
Daniel Dunbar20560482010-06-07 23:23:50 +0000556 if (getFrontendOpts().ShowStats && hasFileManager()) {
Daniel Dunbar0397af22010-01-13 00:48:06 +0000557 getFileManager().PrintStats();
558 OS << "\n";
559 }
560
561 // Return the appropriate status when verifying diagnostics.
562 //
563 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
564 // this.
565 if (getDiagnosticOpts().VerifyDiagnostics)
566 return !static_cast<VerifyDiagnosticsClient&>(
567 getDiagnosticClient()).HadErrors();
568
569 return !getDiagnostics().getNumErrors();
570}
571
572