1 //===--- 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"
11 #include "clang/Sema/Sema.h"
12 #include "clang/AST/ASTConsumer.h"
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "clang/Basic/Version.h"
20 #include "clang/Lex/HeaderSearch.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Lex/PTHManager.h"
23 #include "clang/Frontend/ChainedDiagnosticConsumer.h"
24 #include "clang/Frontend/FrontendAction.h"
25 #include "clang/Frontend/FrontendActions.h"
26 #include "clang/Frontend/FrontendDiagnostic.h"
27 #include "clang/Frontend/LogDiagnosticPrinter.h"
28 #include "clang/Frontend/SerializedDiagnosticPrinter.h"
29 #include "clang/Frontend/TextDiagnosticPrinter.h"
30 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
31 #include "clang/Frontend/Utils.h"
32 #include "clang/Serialization/ASTReader.h"
33 #include "clang/Sema/CodeCompleteConsumer.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/Support/Timer.h"
39 #include "llvm/Support/Host.h"
40 #include "llvm/Support/LockFileManager.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/Program.h"
43 #include "llvm/Support/Signals.h"
44 #include "llvm/Support/system_error.h"
45 #include "llvm/Support/CrashRecoveryContext.h"
46 #include "llvm/Config/config.h"
47
48 using namespace clang;
49
CompilerInstance()50 CompilerInstance::CompilerInstance()
51 : Invocation(new CompilerInvocation()), ModuleManager(0) {
52 }
53
~CompilerInstance()54 CompilerInstance::~CompilerInstance() {
55 }
56
setInvocation(CompilerInvocation * Value)57 void CompilerInstance::setInvocation(CompilerInvocation *Value) {
58 Invocation = Value;
59 }
60
setDiagnostics(DiagnosticsEngine * Value)61 void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
62 Diagnostics = Value;
63 }
64
setTarget(TargetInfo * Value)65 void CompilerInstance::setTarget(TargetInfo *Value) {
66 Target = Value;
67 }
68
setFileManager(FileManager * Value)69 void CompilerInstance::setFileManager(FileManager *Value) {
70 FileMgr = Value;
71 }
72
setSourceManager(SourceManager * Value)73 void CompilerInstance::setSourceManager(SourceManager *Value) {
74 SourceMgr = Value;
75 }
76
setPreprocessor(Preprocessor * Value)77 void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
78
setASTContext(ASTContext * Value)79 void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
80
setSema(Sema * S)81 void CompilerInstance::setSema(Sema *S) {
82 TheSema.reset(S);
83 }
84
setASTConsumer(ASTConsumer * Value)85 void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
86 Consumer.reset(Value);
87 }
88
setCodeCompletionConsumer(CodeCompleteConsumer * Value)89 void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
90 CompletionConsumer.reset(Value);
91 getFrontendOpts().SkipFunctionBodies = Value != 0;
92 }
93
94 // Diagnostics
SetUpBuildDumpLog(const DiagnosticOptions & DiagOpts,unsigned argc,const char * const * argv,DiagnosticsEngine & Diags)95 static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
96 unsigned argc, const char* const *argv,
97 DiagnosticsEngine &Diags) {
98 std::string ErrorInfo;
99 OwningPtr<raw_ostream> OS(
100 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
101 if (!ErrorInfo.empty()) {
102 Diags.Report(diag::err_fe_unable_to_open_logfile)
103 << DiagOpts.DumpBuildInformation << ErrorInfo;
104 return;
105 }
106
107 (*OS) << "clang -cc1 command line arguments: ";
108 for (unsigned i = 0; i != argc; ++i)
109 (*OS) << argv[i] << ' ';
110 (*OS) << '\n';
111
112 // Chain in a diagnostic client which will log the diagnostics.
113 DiagnosticConsumer *Logger =
114 new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
115 Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
116 }
117
SetUpDiagnosticLog(const DiagnosticOptions & DiagOpts,const CodeGenOptions * CodeGenOpts,DiagnosticsEngine & Diags)118 static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
119 const CodeGenOptions *CodeGenOpts,
120 DiagnosticsEngine &Diags) {
121 std::string ErrorInfo;
122 bool OwnsStream = false;
123 raw_ostream *OS = &llvm::errs();
124 if (DiagOpts.DiagnosticLogFile != "-") {
125 // Create the output stream.
126 llvm::raw_fd_ostream *FileOS(
127 new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(),
128 ErrorInfo, llvm::raw_fd_ostream::F_Append));
129 if (!ErrorInfo.empty()) {
130 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
131 << DiagOpts.DumpBuildInformation << ErrorInfo;
132 } else {
133 FileOS->SetUnbuffered();
134 FileOS->SetUseAtomicWrites(true);
135 OS = FileOS;
136 OwnsStream = true;
137 }
138 }
139
140 // Chain in the diagnostic client which will log the diagnostics.
141 LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
142 OwnsStream);
143 if (CodeGenOpts)
144 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
145 Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
146 }
147
SetupSerializedDiagnostics(const DiagnosticOptions & DiagOpts,DiagnosticsEngine & Diags,StringRef OutputFile)148 static void SetupSerializedDiagnostics(const DiagnosticOptions &DiagOpts,
149 DiagnosticsEngine &Diags,
150 StringRef OutputFile) {
151 std::string ErrorInfo;
152 OwningPtr<llvm::raw_fd_ostream> OS;
153 OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo,
154 llvm::raw_fd_ostream::F_Binary));
155
156 if (!ErrorInfo.empty()) {
157 Diags.Report(diag::warn_fe_serialized_diag_failure)
158 << OutputFile << ErrorInfo;
159 return;
160 }
161
162 DiagnosticConsumer *SerializedConsumer =
163 clang::serialized_diags::create(OS.take(), DiagOpts);
164
165
166 Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(),
167 SerializedConsumer));
168 }
169
createDiagnostics(int Argc,const char * const * Argv,DiagnosticConsumer * Client,bool ShouldOwnClient,bool ShouldCloneClient)170 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
171 DiagnosticConsumer *Client,
172 bool ShouldOwnClient,
173 bool ShouldCloneClient) {
174 Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client,
175 ShouldOwnClient, ShouldCloneClient,
176 &getCodeGenOpts());
177 }
178
179 IntrusiveRefCntPtr<DiagnosticsEngine>
createDiagnostics(const DiagnosticOptions & Opts,int Argc,const char * const * Argv,DiagnosticConsumer * Client,bool ShouldOwnClient,bool ShouldCloneClient,const CodeGenOptions * CodeGenOpts)180 CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
181 int Argc, const char* const *Argv,
182 DiagnosticConsumer *Client,
183 bool ShouldOwnClient,
184 bool ShouldCloneClient,
185 const CodeGenOptions *CodeGenOpts) {
186 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
187 IntrusiveRefCntPtr<DiagnosticsEngine>
188 Diags(new DiagnosticsEngine(DiagID));
189
190 // Create the diagnostic client for reporting errors or for
191 // implementing -verify.
192 if (Client) {
193 if (ShouldCloneClient)
194 Diags->setClient(Client->clone(*Diags), ShouldOwnClient);
195 else
196 Diags->setClient(Client, ShouldOwnClient);
197 } else
198 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
199
200 // Chain in -verify checker, if requested.
201 if (Opts.VerifyDiagnostics)
202 Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
203
204 // Chain in -diagnostic-log-file dumper, if requested.
205 if (!Opts.DiagnosticLogFile.empty())
206 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
207
208 if (!Opts.DumpBuildInformation.empty())
209 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
210
211 if (!Opts.DiagnosticSerializationFile.empty())
212 SetupSerializedDiagnostics(Opts, *Diags,
213 Opts.DiagnosticSerializationFile);
214
215 // Configure our handling of diagnostics.
216 ProcessWarningOptions(*Diags, Opts);
217
218 return Diags;
219 }
220
221 // File Manager
222
createFileManager()223 void CompilerInstance::createFileManager() {
224 FileMgr = new FileManager(getFileSystemOpts());
225 }
226
227 // Source Manager
228
createSourceManager(FileManager & FileMgr)229 void CompilerInstance::createSourceManager(FileManager &FileMgr) {
230 SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
231 }
232
233 // Preprocessor
234
createPreprocessor()235 void CompilerInstance::createPreprocessor() {
236 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
237
238 // Create a PTH manager if we are using some form of a token cache.
239 PTHManager *PTHMgr = 0;
240 if (!PPOpts.TokenCache.empty())
241 PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
242
243 // Create the Preprocessor.
244 HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager(),
245 getDiagnostics(),
246 getLangOpts(),
247 &getTarget());
248 PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(),
249 getSourceManager(), *HeaderInfo, *this, PTHMgr,
250 /*OwnsHeaderSearch=*/true);
251
252 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
253 // That argument is used as the IdentifierInfoLookup argument to
254 // IdentifierTable's ctor.
255 if (PTHMgr) {
256 PTHMgr->setPreprocessor(&*PP);
257 PP->setPTHManager(PTHMgr);
258 }
259
260 if (PPOpts.DetailedRecord)
261 PP->createPreprocessingRecord(PPOpts.DetailedRecordConditionalDirectives);
262
263 InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
264
265 // Set up the module path, including the hash for the
266 // module-creation options.
267 SmallString<256> SpecificModuleCache(
268 getHeaderSearchOpts().ModuleCachePath);
269 if (!getHeaderSearchOpts().DisableModuleHash)
270 llvm::sys::path::append(SpecificModuleCache,
271 getInvocation().getModuleHash());
272 PP->getHeaderSearchInfo().setModuleCachePath(SpecificModuleCache);
273
274 // Handle generating dependencies, if requested.
275 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
276 if (!DepOpts.OutputFile.empty())
277 AttachDependencyFileGen(*PP, DepOpts);
278 if (!DepOpts.DOTOutputFile.empty())
279 AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
280 getHeaderSearchOpts().Sysroot);
281
282
283 // Handle generating header include information, if requested.
284 if (DepOpts.ShowHeaderIncludes)
285 AttachHeaderIncludeGen(*PP);
286 if (!DepOpts.HeaderIncludeOutputFile.empty()) {
287 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
288 if (OutputPath == "-")
289 OutputPath = "";
290 AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
291 /*ShowDepth=*/false);
292 }
293 }
294
295 // ASTContext
296
createASTContext()297 void CompilerInstance::createASTContext() {
298 Preprocessor &PP = getPreprocessor();
299 Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
300 &getTarget(), PP.getIdentifierTable(),
301 PP.getSelectorTable(), PP.getBuiltinInfo(),
302 /*size_reserve=*/ 0);
303 }
304
305 // ExternalASTSource
306
createPCHExternalASTSource(StringRef Path,bool DisablePCHValidation,bool DisableStatCache,bool AllowPCHWithCompilerErrors,void * DeserializationListener)307 void CompilerInstance::createPCHExternalASTSource(StringRef Path,
308 bool DisablePCHValidation,
309 bool DisableStatCache,
310 bool AllowPCHWithCompilerErrors,
311 void *DeserializationListener){
312 OwningPtr<ExternalASTSource> Source;
313 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
314 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
315 DisablePCHValidation,
316 DisableStatCache,
317 AllowPCHWithCompilerErrors,
318 getPreprocessor(), getASTContext(),
319 DeserializationListener,
320 Preamble));
321 ModuleManager = static_cast<ASTReader*>(Source.get());
322 getASTContext().setExternalSource(Source);
323 }
324
325 ExternalASTSource *
createPCHExternalASTSource(StringRef Path,const std::string & Sysroot,bool DisablePCHValidation,bool DisableStatCache,bool AllowPCHWithCompilerErrors,Preprocessor & PP,ASTContext & Context,void * DeserializationListener,bool Preamble)326 CompilerInstance::createPCHExternalASTSource(StringRef Path,
327 const std::string &Sysroot,
328 bool DisablePCHValidation,
329 bool DisableStatCache,
330 bool AllowPCHWithCompilerErrors,
331 Preprocessor &PP,
332 ASTContext &Context,
333 void *DeserializationListener,
334 bool Preamble) {
335 OwningPtr<ASTReader> Reader;
336 Reader.reset(new ASTReader(PP, Context,
337 Sysroot.empty() ? "" : Sysroot.c_str(),
338 DisablePCHValidation, DisableStatCache,
339 AllowPCHWithCompilerErrors));
340
341 Reader->setDeserializationListener(
342 static_cast<ASTDeserializationListener *>(DeserializationListener));
343 switch (Reader->ReadAST(Path,
344 Preamble ? serialization::MK_Preamble
345 : serialization::MK_PCH)) {
346 case ASTReader::Success:
347 // Set the predefines buffer as suggested by the PCH reader. Typically, the
348 // predefines buffer will be empty.
349 PP.setPredefines(Reader->getSuggestedPredefines());
350 return Reader.take();
351
352 case ASTReader::Failure:
353 // Unrecoverable failure: don't even try to process the input file.
354 break;
355
356 case ASTReader::IgnorePCH:
357 // No suitable PCH file could be found. Return an error.
358 break;
359 }
360
361 return 0;
362 }
363
364 // Code Completion
365
EnableCodeCompletion(Preprocessor & PP,const std::string & Filename,unsigned Line,unsigned Column)366 static bool EnableCodeCompletion(Preprocessor &PP,
367 const std::string &Filename,
368 unsigned Line,
369 unsigned Column) {
370 // Tell the source manager to chop off the given file at a specific
371 // line and column.
372 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
373 if (!Entry) {
374 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
375 << Filename;
376 return true;
377 }
378
379 // Truncate the named file at the given line/column.
380 PP.SetCodeCompletionPoint(Entry, Line, Column);
381 return false;
382 }
383
createCodeCompletionConsumer()384 void CompilerInstance::createCodeCompletionConsumer() {
385 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
386 if (!CompletionConsumer) {
387 setCodeCompletionConsumer(
388 createCodeCompletionConsumer(getPreprocessor(),
389 Loc.FileName, Loc.Line, Loc.Column,
390 getFrontendOpts().ShowMacrosInCodeCompletion,
391 getFrontendOpts().ShowCodePatternsInCodeCompletion,
392 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
393 llvm::outs()));
394 if (!CompletionConsumer)
395 return;
396 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
397 Loc.Line, Loc.Column)) {
398 setCodeCompletionConsumer(0);
399 return;
400 }
401
402 if (CompletionConsumer->isOutputBinary() &&
403 llvm::sys::Program::ChangeStdoutToBinary()) {
404 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
405 setCodeCompletionConsumer(0);
406 }
407 }
408
createFrontendTimer()409 void CompilerInstance::createFrontendTimer() {
410 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
411 }
412
413 CodeCompleteConsumer *
createCodeCompletionConsumer(Preprocessor & PP,const std::string & Filename,unsigned Line,unsigned Column,bool ShowMacros,bool ShowCodePatterns,bool ShowGlobals,raw_ostream & OS)414 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
415 const std::string &Filename,
416 unsigned Line,
417 unsigned Column,
418 bool ShowMacros,
419 bool ShowCodePatterns,
420 bool ShowGlobals,
421 raw_ostream &OS) {
422 if (EnableCodeCompletion(PP, Filename, Line, Column))
423 return 0;
424
425 // Set up the creation routine for code-completion.
426 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
427 ShowGlobals, OS);
428 }
429
createSema(TranslationUnitKind TUKind,CodeCompleteConsumer * CompletionConsumer)430 void CompilerInstance::createSema(TranslationUnitKind TUKind,
431 CodeCompleteConsumer *CompletionConsumer) {
432 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
433 TUKind, CompletionConsumer));
434 }
435
436 // Output Files
437
addOutputFile(const OutputFile & OutFile)438 void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
439 assert(OutFile.OS && "Attempt to add empty stream to output list!");
440 OutputFiles.push_back(OutFile);
441 }
442
clearOutputFiles(bool EraseFiles)443 void CompilerInstance::clearOutputFiles(bool EraseFiles) {
444 for (std::list<OutputFile>::iterator
445 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
446 delete it->OS;
447 if (!it->TempFilename.empty()) {
448 if (EraseFiles) {
449 bool existed;
450 llvm::sys::fs::remove(it->TempFilename, existed);
451 } else {
452 SmallString<128> NewOutFile(it->Filename);
453
454 // If '-working-directory' was passed, the output filename should be
455 // relative to that.
456 FileMgr->FixupRelativePath(NewOutFile);
457 if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
458 NewOutFile.str())) {
459 getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
460 << it->TempFilename << it->Filename << ec.message();
461
462 bool existed;
463 llvm::sys::fs::remove(it->TempFilename, existed);
464 }
465 }
466 } else if (!it->Filename.empty() && EraseFiles)
467 llvm::sys::Path(it->Filename).eraseFromDisk();
468
469 }
470 OutputFiles.clear();
471 }
472
473 llvm::raw_fd_ostream *
createDefaultOutputFile(bool Binary,StringRef InFile,StringRef Extension)474 CompilerInstance::createDefaultOutputFile(bool Binary,
475 StringRef InFile,
476 StringRef Extension) {
477 return createOutputFile(getFrontendOpts().OutputFile, Binary,
478 /*RemoveFileOnSignal=*/true, InFile, Extension,
479 /*UseTemporary=*/true);
480 }
481
482 llvm::raw_fd_ostream *
createOutputFile(StringRef OutputPath,bool Binary,bool RemoveFileOnSignal,StringRef InFile,StringRef Extension,bool UseTemporary,bool CreateMissingDirectories)483 CompilerInstance::createOutputFile(StringRef OutputPath,
484 bool Binary, bool RemoveFileOnSignal,
485 StringRef InFile,
486 StringRef Extension,
487 bool UseTemporary,
488 bool CreateMissingDirectories) {
489 std::string Error, OutputPathName, TempPathName;
490 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
491 RemoveFileOnSignal,
492 InFile, Extension,
493 UseTemporary,
494 CreateMissingDirectories,
495 &OutputPathName,
496 &TempPathName);
497 if (!OS) {
498 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
499 << OutputPath << Error;
500 return 0;
501 }
502
503 // Add the output file -- but don't try to remove "-", since this means we are
504 // using stdin.
505 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
506 TempPathName, OS));
507
508 return OS;
509 }
510
511 llvm::raw_fd_ostream *
createOutputFile(StringRef OutputPath,std::string & Error,bool Binary,bool RemoveFileOnSignal,StringRef InFile,StringRef Extension,bool UseTemporary,bool CreateMissingDirectories,std::string * ResultPathName,std::string * TempPathName)512 CompilerInstance::createOutputFile(StringRef OutputPath,
513 std::string &Error,
514 bool Binary,
515 bool RemoveFileOnSignal,
516 StringRef InFile,
517 StringRef Extension,
518 bool UseTemporary,
519 bool CreateMissingDirectories,
520 std::string *ResultPathName,
521 std::string *TempPathName) {
522 assert((!CreateMissingDirectories || UseTemporary) &&
523 "CreateMissingDirectories is only allowed when using temporary files");
524
525 std::string OutFile, TempFile;
526 if (!OutputPath.empty()) {
527 OutFile = OutputPath;
528 } else if (InFile == "-") {
529 OutFile = "-";
530 } else if (!Extension.empty()) {
531 llvm::sys::Path Path(InFile);
532 Path.eraseSuffix();
533 Path.appendSuffix(Extension);
534 OutFile = Path.str();
535 } else {
536 OutFile = "-";
537 }
538
539 OwningPtr<llvm::raw_fd_ostream> OS;
540 std::string OSFile;
541
542 if (UseTemporary && OutFile != "-") {
543 // Only create the temporary if the parent directory exists (or create
544 // missing directories is true) and we can actually write to OutPath,
545 // otherwise we want to fail early.
546 SmallString<256> AbsPath(OutputPath);
547 llvm::sys::fs::make_absolute(AbsPath);
548 llvm::sys::Path OutPath(AbsPath);
549 bool ParentExists = false;
550 if (llvm::sys::fs::exists(llvm::sys::path::parent_path(AbsPath.str()),
551 ParentExists))
552 ParentExists = false;
553 bool Exists;
554 if ((CreateMissingDirectories || ParentExists) &&
555 ((llvm::sys::fs::exists(AbsPath.str(), Exists) || !Exists) ||
556 (OutPath.isRegularFile() && OutPath.canWrite()))) {
557 // Create a temporary file.
558 SmallString<128> TempPath;
559 TempPath = OutFile;
560 TempPath += "-%%%%%%%%";
561 int fd;
562 if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
563 /*makeAbsolute=*/false) == llvm::errc::success) {
564 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
565 OSFile = TempFile = TempPath.str();
566 }
567 }
568 }
569
570 if (!OS) {
571 OSFile = OutFile;
572 OS.reset(
573 new llvm::raw_fd_ostream(OSFile.c_str(), Error,
574 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
575 if (!Error.empty())
576 return 0;
577 }
578
579 // Make sure the out stream file gets removed if we crash.
580 if (RemoveFileOnSignal)
581 llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
582
583 if (ResultPathName)
584 *ResultPathName = OutFile;
585 if (TempPathName)
586 *TempPathName = TempFile;
587
588 return OS.take();
589 }
590
591 // Initialization Utilities
592
InitializeSourceManager(StringRef InputFile,SrcMgr::CharacteristicKind Kind)593 bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
594 SrcMgr::CharacteristicKind Kind){
595 return InitializeSourceManager(InputFile, Kind, getDiagnostics(),
596 getFileManager(), getSourceManager(),
597 getFrontendOpts());
598 }
599
InitializeSourceManager(StringRef InputFile,SrcMgr::CharacteristicKind Kind,DiagnosticsEngine & Diags,FileManager & FileMgr,SourceManager & SourceMgr,const FrontendOptions & Opts)600 bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
601 SrcMgr::CharacteristicKind Kind,
602 DiagnosticsEngine &Diags,
603 FileManager &FileMgr,
604 SourceManager &SourceMgr,
605 const FrontendOptions &Opts) {
606 // Figure out where to get and map in the main file.
607 if (InputFile != "-") {
608 const FileEntry *File = FileMgr.getFile(InputFile);
609 if (!File) {
610 Diags.Report(diag::err_fe_error_reading) << InputFile;
611 return false;
612 }
613 SourceMgr.createMainFileID(File, Kind);
614 } else {
615 OwningPtr<llvm::MemoryBuffer> SB;
616 if (llvm::MemoryBuffer::getSTDIN(SB)) {
617 // FIXME: Give ec.message() in this diag.
618 Diags.Report(diag::err_fe_error_reading_stdin);
619 return false;
620 }
621 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
622 SB->getBufferSize(), 0);
623 SourceMgr.createMainFileID(File, Kind);
624 SourceMgr.overrideFileContents(File, SB.take());
625 }
626
627 assert(!SourceMgr.getMainFileID().isInvalid() &&
628 "Couldn't establish MainFileID!");
629 return true;
630 }
631
632 // High-Level Operations
633
ExecuteAction(FrontendAction & Act)634 bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
635 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
636 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
637 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
638
639 // FIXME: Take this as an argument, once all the APIs we used have moved to
640 // taking it as an input instead of hard-coding llvm::errs.
641 raw_ostream &OS = llvm::errs();
642
643 // Create the target instance.
644 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
645 if (!hasTarget())
646 return false;
647
648 // Inform the target of the language options.
649 //
650 // FIXME: We shouldn't need to do this, the target should be immutable once
651 // created. This complexity should be lifted elsewhere.
652 getTarget().setForcedLangOptions(getLangOpts());
653
654 // rewriter project will change target built-in bool type from its default.
655 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
656 getTarget().noSignedCharForObjCBool();
657
658 // Validate/process some options.
659 if (getHeaderSearchOpts().Verbose)
660 OS << "clang -cc1 version " CLANG_VERSION_STRING
661 << " based upon " << PACKAGE_STRING
662 << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
663
664 if (getFrontendOpts().ShowTimers)
665 createFrontendTimer();
666
667 if (getFrontendOpts().ShowStats)
668 llvm::EnableStatistics();
669
670 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
671 // Reset the ID tables if we are reusing the SourceManager.
672 if (hasSourceManager())
673 getSourceManager().clearIDTables();
674
675 if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) {
676 Act.Execute();
677 Act.EndSourceFile();
678 }
679 }
680
681 // Notify the diagnostic client that all files were processed.
682 getDiagnostics().getClient()->finish();
683
684 if (getDiagnosticOpts().ShowCarets) {
685 // We can have multiple diagnostics sharing one diagnostic client.
686 // Get the total number of warnings/errors from the client.
687 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
688 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
689
690 if (NumWarnings)
691 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
692 if (NumWarnings && NumErrors)
693 OS << " and ";
694 if (NumErrors)
695 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
696 if (NumWarnings || NumErrors)
697 OS << " generated.\n";
698 }
699
700 if (getFrontendOpts().ShowStats && hasFileManager()) {
701 getFileManager().PrintStats();
702 OS << "\n";
703 }
704
705 return !getDiagnostics().getClient()->getNumErrors();
706 }
707
708 /// \brief Determine the appropriate source input kind based on language
709 /// options.
getSourceInputKindFromOptions(const LangOptions & LangOpts)710 static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
711 if (LangOpts.OpenCL)
712 return IK_OpenCL;
713 if (LangOpts.CUDA)
714 return IK_CUDA;
715 if (LangOpts.ObjC1)
716 return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
717 return LangOpts.CPlusPlus? IK_CXX : IK_C;
718 }
719
720 namespace {
721 struct CompileModuleMapData {
722 CompilerInstance &Instance;
723 GenerateModuleAction &CreateModuleAction;
724 };
725 }
726
727 /// \brief Helper function that executes the module-generating action under
728 /// a crash recovery context.
doCompileMapModule(void * UserData)729 static void doCompileMapModule(void *UserData) {
730 CompileModuleMapData &Data
731 = *reinterpret_cast<CompileModuleMapData *>(UserData);
732 Data.Instance.ExecuteAction(Data.CreateModuleAction);
733 }
734
735 /// \brief Compile a module file for the given module, using the options
736 /// provided by the importing compiler instance.
compileModule(CompilerInstance & ImportingInstance,Module * Module,StringRef ModuleFileName)737 static void compileModule(CompilerInstance &ImportingInstance,
738 Module *Module,
739 StringRef ModuleFileName) {
740 llvm::LockFileManager Locked(ModuleFileName);
741 switch (Locked) {
742 case llvm::LockFileManager::LFS_Error:
743 return;
744
745 case llvm::LockFileManager::LFS_Owned:
746 // We're responsible for building the module ourselves. Do so below.
747 break;
748
749 case llvm::LockFileManager::LFS_Shared:
750 // Someone else is responsible for building the module. Wait for them to
751 // finish.
752 Locked.waitForUnlock();
753 break;
754 }
755
756 ModuleMap &ModMap
757 = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
758
759 // Construct a compiler invocation for creating this module.
760 IntrusiveRefCntPtr<CompilerInvocation> Invocation
761 (new CompilerInvocation(ImportingInstance.getInvocation()));
762
763 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
764
765 // For any options that aren't intended to affect how a module is built,
766 // reset them to their default values.
767 Invocation->getLangOpts()->resetNonModularOptions();
768 PPOpts.resetNonModularOptions();
769
770 // Note the name of the module we're building.
771 Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName();
772
773 // Note that this module is part of the module build path, so that we
774 // can detect cycles in the module graph.
775 PPOpts.ModuleBuildPath.push_back(Module->getTopLevelModuleName());
776
777 // If there is a module map file, build the module using the module map.
778 // Set up the inputs/outputs so that we build the module from its umbrella
779 // header.
780 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
781 FrontendOpts.OutputFile = ModuleFileName.str();
782 FrontendOpts.DisableFree = false;
783 FrontendOpts.Inputs.clear();
784 InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts());
785
786 // Get or create the module map that we'll use to build this module.
787 SmallString<128> TempModuleMapFileName;
788 if (const FileEntry *ModuleMapFile
789 = ModMap.getContainingModuleMapFile(Module)) {
790 // Use the module map where this module resides.
791 FrontendOpts.Inputs.push_back(FrontendInputFile(ModuleMapFile->getName(),
792 IK));
793 } else {
794 // Create a temporary module map file.
795 TempModuleMapFileName = Module->Name;
796 TempModuleMapFileName += "-%%%%%%%%.map";
797 int FD;
798 if (llvm::sys::fs::unique_file(TempModuleMapFileName.str(), FD,
799 TempModuleMapFileName,
800 /*makeAbsolute=*/true)
801 != llvm::errc::success) {
802 ImportingInstance.getDiagnostics().Report(diag::err_module_map_temp_file)
803 << TempModuleMapFileName;
804 return;
805 }
806 // Print the module map to this file.
807 llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
808 Module->print(OS);
809 FrontendOpts.Inputs.push_back(
810 FrontendInputFile(TempModuleMapFileName.str().str(), IK));
811 }
812
813 // Don't free the remapped file buffers; they are owned by our caller.
814 PPOpts.RetainRemappedFileBuffers = true;
815
816 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
817 assert(ImportingInstance.getInvocation().getModuleHash() ==
818 Invocation->getModuleHash() && "Module hash mismatch!");
819
820 // Construct a compiler instance that will be used to actually create the
821 // module.
822 CompilerInstance Instance;
823 Instance.setInvocation(&*Invocation);
824 Instance.createDiagnostics(/*argc=*/0, /*argv=*/0,
825 &ImportingInstance.getDiagnosticClient(),
826 /*ShouldOwnClient=*/true,
827 /*ShouldCloneClient=*/true);
828
829 // Construct a module-generating action.
830 GenerateModuleAction CreateModuleAction;
831
832 // Execute the action to actually build the module in-place. Use a separate
833 // thread so that we get a stack large enough.
834 const unsigned ThreadStackSize = 8 << 20;
835 llvm::CrashRecoveryContext CRC;
836 CompileModuleMapData Data = { Instance, CreateModuleAction };
837 CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize);
838
839 // Delete the temporary module map file.
840 // FIXME: Even though we're executing under crash protection, it would still
841 // be nice to do this with RemoveFileOnSignal when we can. However, that
842 // doesn't make sense for all clients, so clean this up manually.
843 if (!TempModuleMapFileName.empty())
844 llvm::sys::Path(TempModuleMapFileName).eraseFromDisk();
845 }
846
loadModule(SourceLocation ImportLoc,ModuleIdPath Path,Module::NameVisibilityKind Visibility,bool IsInclusionDirective)847 Module *CompilerInstance::loadModule(SourceLocation ImportLoc,
848 ModuleIdPath Path,
849 Module::NameVisibilityKind Visibility,
850 bool IsInclusionDirective) {
851 // If we've already handled this import, just return the cached result.
852 // This one-element cache is important to eliminate redundant diagnostics
853 // when both the preprocessor and parser see the same import declaration.
854 if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) {
855 // Make the named module visible.
856 if (LastModuleImportResult)
857 ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility);
858 return LastModuleImportResult;
859 }
860
861 // Determine what file we're searching from.
862 SourceManager &SourceMgr = getSourceManager();
863 SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc);
864 const FileEntry *CurFile
865 = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc));
866 if (!CurFile)
867 CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
868
869 StringRef ModuleName = Path[0].first->getName();
870 SourceLocation ModuleNameLoc = Path[0].second;
871
872 clang::Module *Module = 0;
873
874 // If we don't already have information on this module, load the module now.
875 llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
876 = KnownModules.find(Path[0].first);
877 if (Known != KnownModules.end()) {
878 // Retrieve the cached top-level module.
879 Module = Known->second;
880 } else if (ModuleName == getLangOpts().CurrentModule) {
881 // This is the module we're building.
882 Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName);
883 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
884 } else {
885 // Search for a module with the given name.
886 Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
887 std::string ModuleFileName;
888 if (Module)
889 ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module);
890 else
891 ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(ModuleName);
892
893 if (ModuleFileName.empty()) {
894 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
895 << ModuleName
896 << SourceRange(ImportLoc, ModuleNameLoc);
897 LastModuleImportLoc = ImportLoc;
898 LastModuleImportResult = 0;
899 return 0;
900 }
901
902 const FileEntry *ModuleFile
903 = getFileManager().getFile(ModuleFileName, /*OpenFile=*/false,
904 /*CacheFailure=*/false);
905 bool BuildingModule = false;
906 if (!ModuleFile && Module) {
907 // The module is not cached, but we have a module map from which we can
908 // build the module.
909
910 // Check whether there is a cycle in the module graph.
911 SmallVectorImpl<std::string> &ModuleBuildPath
912 = getPreprocessorOpts().ModuleBuildPath;
913 SmallVectorImpl<std::string>::iterator Pos
914 = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(), ModuleName);
915 if (Pos != ModuleBuildPath.end()) {
916 SmallString<256> CyclePath;
917 for (; Pos != ModuleBuildPath.end(); ++Pos) {
918 CyclePath += *Pos;
919 CyclePath += " -> ";
920 }
921 CyclePath += ModuleName;
922
923 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
924 << ModuleName << CyclePath;
925 return 0;
926 }
927
928 getDiagnostics().Report(ModuleNameLoc, diag::warn_module_build)
929 << ModuleName;
930 BuildingModule = true;
931 compileModule(*this, Module, ModuleFileName);
932 ModuleFile = FileMgr->getFile(ModuleFileName);
933 }
934
935 if (!ModuleFile) {
936 getDiagnostics().Report(ModuleNameLoc,
937 BuildingModule? diag::err_module_not_built
938 : diag::err_module_not_found)
939 << ModuleName
940 << SourceRange(ImportLoc, ModuleNameLoc);
941 return 0;
942 }
943
944 // If we don't already have an ASTReader, create one now.
945 if (!ModuleManager) {
946 if (!hasASTContext())
947 createASTContext();
948
949 std::string Sysroot = getHeaderSearchOpts().Sysroot;
950 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
951 ModuleManager = new ASTReader(getPreprocessor(), *Context,
952 Sysroot.empty() ? "" : Sysroot.c_str(),
953 PPOpts.DisablePCHValidation,
954 PPOpts.DisableStatCache);
955 if (hasASTConsumer()) {
956 ModuleManager->setDeserializationListener(
957 getASTConsumer().GetASTDeserializationListener());
958 getASTContext().setASTMutationListener(
959 getASTConsumer().GetASTMutationListener());
960 }
961 OwningPtr<ExternalASTSource> Source;
962 Source.reset(ModuleManager);
963 getASTContext().setExternalSource(Source);
964 if (hasSema())
965 ModuleManager->InitializeSema(getSema());
966 if (hasASTConsumer())
967 ModuleManager->StartTranslationUnit(&getASTConsumer());
968 }
969
970 // Try to load the module we found.
971 switch (ModuleManager->ReadAST(ModuleFile->getName(),
972 serialization::MK_Module)) {
973 case ASTReader::Success:
974 break;
975
976 case ASTReader::IgnorePCH:
977 // FIXME: The ASTReader will already have complained, but can we showhorn
978 // that diagnostic information into a more useful form?
979 KnownModules[Path[0].first] = 0;
980 return 0;
981
982 case ASTReader::Failure:
983 // Already complained, but note now that we failed.
984 KnownModules[Path[0].first] = 0;
985 return 0;
986 }
987
988 if (!Module) {
989 // If we loaded the module directly, without finding a module map first,
990 // we'll have loaded the module's information from the module itself.
991 Module = PP->getHeaderSearchInfo().getModuleMap()
992 .findModule((Path[0].first->getName()));
993 }
994
995 // Cache the result of this top-level module lookup for later.
996 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
997 }
998
999 // If we never found the module, fail.
1000 if (!Module)
1001 return 0;
1002
1003 // Verify that the rest of the module path actually corresponds to
1004 // a submodule.
1005 if (Path.size() > 1) {
1006 for (unsigned I = 1, N = Path.size(); I != N; ++I) {
1007 StringRef Name = Path[I].first->getName();
1008 clang::Module *Sub = Module->findSubmodule(Name);
1009
1010 if (!Sub) {
1011 // Attempt to perform typo correction to find a module name that works.
1012 llvm::SmallVector<StringRef, 2> Best;
1013 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
1014
1015 for (clang::Module::submodule_iterator J = Module->submodule_begin(),
1016 JEnd = Module->submodule_end();
1017 J != JEnd; ++J) {
1018 unsigned ED = Name.edit_distance((*J)->Name,
1019 /*AllowReplacements=*/true,
1020 BestEditDistance);
1021 if (ED <= BestEditDistance) {
1022 if (ED < BestEditDistance) {
1023 Best.clear();
1024 BestEditDistance = ED;
1025 }
1026
1027 Best.push_back((*J)->Name);
1028 }
1029 }
1030
1031 // If there was a clear winner, user it.
1032 if (Best.size() == 1) {
1033 getDiagnostics().Report(Path[I].second,
1034 diag::err_no_submodule_suggest)
1035 << Path[I].first << Module->getFullModuleName() << Best[0]
1036 << SourceRange(Path[0].second, Path[I-1].second)
1037 << FixItHint::CreateReplacement(SourceRange(Path[I].second),
1038 Best[0]);
1039
1040 Sub = Module->findSubmodule(Best[0]);
1041 }
1042 }
1043
1044 if (!Sub) {
1045 // No submodule by this name. Complain, and don't look for further
1046 // submodules.
1047 getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
1048 << Path[I].first << Module->getFullModuleName()
1049 << SourceRange(Path[0].second, Path[I-1].second);
1050 break;
1051 }
1052
1053 Module = Sub;
1054 }
1055 }
1056
1057 // Make the named module visible, if it's not already part of the module
1058 // we are parsing.
1059 if (ModuleName != getLangOpts().CurrentModule) {
1060 if (!Module->IsFromModuleFile) {
1061 // We have an umbrella header or directory that doesn't actually include
1062 // all of the headers within the directory it covers. Complain about
1063 // this missing submodule and recover by forgetting that we ever saw
1064 // this submodule.
1065 // FIXME: Should we detect this at module load time? It seems fairly
1066 // expensive (and rare).
1067 getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
1068 << Module->getFullModuleName()
1069 << SourceRange(Path.front().second, Path.back().second);
1070
1071 return 0;
1072 }
1073
1074 // Check whether this module is available.
1075 StringRef Feature;
1076 if (!Module->isAvailable(getLangOpts(), getTarget(), Feature)) {
1077 getDiagnostics().Report(ImportLoc, diag::err_module_unavailable)
1078 << Module->getFullModuleName()
1079 << Feature
1080 << SourceRange(Path.front().second, Path.back().second);
1081 LastModuleImportLoc = ImportLoc;
1082 LastModuleImportResult = 0;
1083 return 0;
1084 }
1085
1086 ModuleManager->makeModuleVisible(Module, Visibility);
1087 }
1088
1089 // If this module import was due to an inclusion directive, create an
1090 // implicit import declaration to capture it in the AST.
1091 if (IsInclusionDirective && hasASTContext()) {
1092 TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
1093 TU->addDecl(ImportDecl::CreateImplicit(getASTContext(), TU,
1094 ImportLoc, Module,
1095 Path.back().second));
1096 }
1097
1098 LastModuleImportLoc = ImportLoc;
1099 LastModuleImportResult = Module;
1100 return Module;
1101 }
1102