xref: /llvm-project/clang/lib/Frontend/CompilerInstance.cpp (revision 4e0f15a4a247abfcc53563815ce799d2ad45c786)
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/Basic/Diagnostic.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Basic/Version.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "clang/Lex/PTHManager.h"
22 #include "clang/Frontend/ChainedDiagnosticClient.h"
23 #include "clang/Frontend/FrontendAction.h"
24 #include "clang/Frontend/FrontendDiagnostic.h"
25 #include "clang/Frontend/TextDiagnosticPrinter.h"
26 #include "clang/Frontend/VerifyDiagnosticsClient.h"
27 #include "clang/Frontend/Utils.h"
28 #include "clang/Serialization/ASTReader.h"
29 #include "clang/Sema/CodeCompleteConsumer.h"
30 #include "llvm/LLVMContext.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Support/Timer.h"
35 #include "llvm/System/Host.h"
36 #include "llvm/System/Path.h"
37 #include "llvm/System/Program.h"
38 #include "llvm/System/Signals.h"
39 using namespace clang;
40 
41 CompilerInstance::CompilerInstance()
42   : Invocation(new CompilerInvocation()) {
43 }
44 
45 CompilerInstance::~CompilerInstance() {
46 }
47 
48 void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) {
49   LLVMContext.reset(Value);
50 }
51 
52 void CompilerInstance::setInvocation(CompilerInvocation *Value) {
53   Invocation.reset(Value);
54 }
55 
56 void CompilerInstance::setDiagnostics(Diagnostic *Value) {
57   Diagnostics = Value;
58 }
59 
60 void CompilerInstance::setTarget(TargetInfo *Value) {
61   Target.reset(Value);
62 }
63 
64 void CompilerInstance::setFileManager(FileManager *Value) {
65   FileMgr.reset(Value);
66 }
67 
68 void CompilerInstance::setSourceManager(SourceManager *Value) {
69   SourceMgr.reset(Value);
70 }
71 
72 void CompilerInstance::setPreprocessor(Preprocessor *Value) {
73   PP.reset(Value);
74 }
75 
76 void CompilerInstance::setASTContext(ASTContext *Value) {
77   Context.reset(Value);
78 }
79 
80 void CompilerInstance::setSema(Sema *S) {
81   TheSema.reset(S);
82 }
83 
84 void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
85   Consumer.reset(Value);
86 }
87 
88 void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
89   CompletionConsumer.reset(Value);
90 }
91 
92 // Diagnostics
93 static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
94                               unsigned argc, const char* const *argv,
95                               Diagnostic &Diags) {
96   std::string ErrorInfo;
97   llvm::OwningPtr<llvm::raw_ostream> OS(
98     new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
99   if (!ErrorInfo.empty()) {
100     Diags.Report(diag::err_fe_unable_to_open_logfile)
101                  << DiagOpts.DumpBuildInformation << ErrorInfo;
102     return;
103   }
104 
105   (*OS) << "clang -cc1 command line arguments: ";
106   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 =
112     new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
113   Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
114 }
115 
116 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv) {
117   Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv);
118 }
119 
120 llvm::IntrusiveRefCntPtr<Diagnostic>
121 CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
122                                     int Argc, const char* const *Argv) {
123   llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic());
124 
125   // Create the diagnostic client for reporting errors or for
126   // implementing -verify.
127   llvm::OwningPtr<DiagnosticClient> DiagClient;
128   Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
129 
130   // Chain in -verify checker, if requested.
131   if (Opts.VerifyDiagnostics)
132     Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
133 
134   if (!Opts.DumpBuildInformation.empty())
135     SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
136 
137   // Configure our handling of diagnostics.
138   ProcessWarningOptions(*Diags, Opts);
139 
140   return Diags;
141 }
142 
143 // File Manager
144 
145 void CompilerInstance::createFileManager() {
146   FileMgr.reset(new FileManager());
147 }
148 
149 // Source Manager
150 
151 void CompilerInstance::createSourceManager() {
152   SourceMgr.reset(new SourceManager(getDiagnostics()));
153 }
154 
155 // Preprocessor
156 
157 void CompilerInstance::createPreprocessor() {
158   PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
159                               getPreprocessorOpts(), getHeaderSearchOpts(),
160                               getDependencyOutputOpts(), getTarget(),
161                               getFrontendOpts(), getSourceManager(),
162                               getFileManager()));
163 }
164 
165 Preprocessor *
166 CompilerInstance::createPreprocessor(Diagnostic &Diags,
167                                      const LangOptions &LangInfo,
168                                      const PreprocessorOptions &PPOpts,
169                                      const HeaderSearchOptions &HSOpts,
170                                      const DependencyOutputOptions &DepOpts,
171                                      const TargetInfo &Target,
172                                      const FrontendOptions &FEOpts,
173                                      SourceManager &SourceMgr,
174                                      FileManager &FileMgr) {
175   // Create a PTH manager if we are using some form of a token cache.
176   PTHManager *PTHMgr = 0;
177   if (!PPOpts.TokenCache.empty())
178     PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
179 
180   // 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 
194   if (PPOpts.DetailedRecord)
195     PP->createPreprocessingRecord();
196 
197   InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
198 
199   // Handle generating dependencies, if requested.
200   if (!DepOpts.OutputFile.empty())
201     AttachDependencyFileGen(*PP, DepOpts);
202 
203   return PP;
204 }
205 
206 // ASTContext
207 
208 void CompilerInstance::createASTContext() {
209   Preprocessor &PP = getPreprocessor();
210   Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
211                                getTarget(), PP.getIdentifierTable(),
212                                PP.getSelectorTable(), PP.getBuiltinInfo(),
213                                /*size_reserve=*/ 0));
214 }
215 
216 // ExternalASTSource
217 
218 void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
219                                                   bool DisablePCHValidation,
220                                                  void *DeserializationListener){
221   llvm::OwningPtr<ExternalASTSource> Source;
222   bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
223   Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
224                                           DisablePCHValidation,
225                                           getPreprocessor(), getASTContext(),
226                                           DeserializationListener,
227                                           Preamble));
228   getASTContext().setExternalSource(Source);
229 }
230 
231 ExternalASTSource *
232 CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
233                                              const std::string &Sysroot,
234                                              bool DisablePCHValidation,
235                                              Preprocessor &PP,
236                                              ASTContext &Context,
237                                              void *DeserializationListener,
238                                              bool Preamble) {
239   llvm::OwningPtr<ASTReader> Reader;
240   Reader.reset(new ASTReader(PP, &Context,
241                              Sysroot.empty() ? 0 : Sysroot.c_str(),
242                              DisablePCHValidation));
243 
244   Reader->setDeserializationListener(
245             static_cast<ASTDeserializationListener *>(DeserializationListener));
246   switch (Reader->ReadAST(Path,
247                           Preamble ? ASTReader::Preamble : ASTReader::PCH)) {
248   case ASTReader::Success:
249     // 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 
254   case ASTReader::Failure:
255     // Unrecoverable failure: don't even try to process the input file.
256     break;
257 
258   case ASTReader::IgnorePCH:
259     // No suitable PCH file could be found. Return an error.
260     break;
261   }
262 
263   return 0;
264 }
265 
266 // Code Completion
267 
268 static 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 
286 void CompilerInstance::createCodeCompletionConsumer() {
287   const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
288   if (!CompletionConsumer) {
289     CompletionConsumer.reset(
290       createCodeCompletionConsumer(getPreprocessor(),
291                                    Loc.FileName, Loc.Line, Loc.Column,
292                                    getFrontendOpts().DebugCodeCompletionPrinter,
293                                    getFrontendOpts().ShowMacrosInCodeCompletion,
294                              getFrontendOpts().ShowCodePatternsInCodeCompletion,
295                            getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
296                                    llvm::outs()));
297     if (!CompletionConsumer)
298       return;
299   } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
300                                   Loc.Line, Loc.Column)) {
301     CompletionConsumer.reset();
302     return;
303   }
304 
305   if (CompletionConsumer->isOutputBinary() &&
306       llvm::sys::Program::ChangeStdoutToBinary()) {
307     getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
308     CompletionConsumer.reset();
309   }
310 }
311 
312 void CompilerInstance::createFrontendTimer() {
313   FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
314 }
315 
316 CodeCompleteConsumer *
317 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
318                                                const std::string &Filename,
319                                                unsigned Line,
320                                                unsigned Column,
321                                                bool UseDebugPrinter,
322                                                bool ShowMacros,
323                                                bool ShowCodePatterns,
324                                                bool ShowGlobals,
325                                                llvm::raw_ostream &OS) {
326   if (EnableCodeCompletion(PP, Filename, Line, Column))
327     return 0;
328 
329   // Set up the creation routine for code-completion.
330   if (UseDebugPrinter)
331     return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
332                                             ShowGlobals, OS);
333   else
334     return new CIndexCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
335                                           ShowGlobals, OS);
336 }
337 
338 void CompilerInstance::createSema(bool CompleteTranslationUnit,
339                                   CodeCompleteConsumer *CompletionConsumer) {
340   TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
341                          CompleteTranslationUnit, CompletionConsumer));
342 }
343 
344 // Output Files
345 
346 void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
347   assert(OutFile.OS && "Attempt to add empty stream to output list!");
348   OutputFiles.push_back(OutFile);
349 }
350 
351 void CompilerInstance::clearOutputFiles(bool EraseFiles) {
352   for (std::list<OutputFile>::iterator
353          it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
354     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 
370   }
371   OutputFiles.clear();
372 }
373 
374 llvm::raw_fd_ostream *
375 CompilerInstance::createDefaultOutputFile(bool Binary,
376                                           llvm::StringRef InFile,
377                                           llvm::StringRef Extension) {
378   return createOutputFile(getFrontendOpts().OutputFile, Binary,
379                           InFile, Extension);
380 }
381 
382 llvm::raw_fd_ostream *
383 CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
384                                    bool Binary,
385                                    llvm::StringRef InFile,
386                                    llvm::StringRef Extension) {
387   std::string Error, OutputPathName, TempPathName;
388   llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
389                                               InFile, Extension,
390                                               &OutputPathName,
391                                               &TempPathName);
392   if (!OS) {
393     getDiagnostics().Report(diag::err_fe_unable_to_open_output)
394       << OutputPath << Error;
395     return 0;
396   }
397 
398   // Add the output file -- but don't try to remove "-", since this means we are
399   // using stdin.
400   addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
401                 TempPathName, OS));
402 
403   return OS;
404 }
405 
406 llvm::raw_fd_ostream *
407 CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
408                                    std::string &Error,
409                                    bool Binary,
410                                    llvm::StringRef InFile,
411                                    llvm::StringRef Extension,
412                                    std::string *ResultPathName,
413                                    std::string *TempPathName) {
414   std::string OutFile, TempFile;
415   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   }
427 
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;
444 
445   llvm::OwningPtr<llvm::raw_fd_ostream> OS(
446     new llvm::raw_fd_ostream(OSFile.c_str(), Error,
447                              (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
448   if (!Error.empty())
449     return 0;
450 
451   // Make sure the out stream file gets removed if we crash.
452   llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
453 
454   if (ResultPathName)
455     *ResultPathName = OutFile;
456   if (TempPathName)
457     *TempPathName = TempFile;
458 
459   return OS.take();
460 }
461 
462 // Initialization Utilities
463 
464 bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
465   return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
466                                  getSourceManager(), getFrontendOpts());
467 }
468 
469 bool 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.
475   if (InputFile != "-") {
476     const FileEntry *File = FileMgr.getFile(InputFile);
477     if (File) SourceMgr.createMainFileID(File);
478     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();
484     if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
485     if (SourceMgr.getMainFileID().isInvalid()) {
486       Diags.Report(diag::err_fe_error_reading_stdin);
487       return false;
488     }
489   }
490 
491   return true;
492 }
493 
494 // High-Level Operations
495 
496 bool 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 
525   if (getFrontendOpts().ShowStats)
526     llvm::EnableStatistics();
527 
528   for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
529     const std::string &InFile = getFrontendOpts().Inputs[i].second;
530 
531     // Reset the ID tables if we are reusing the SourceManager.
532     if (hasSourceManager())
533       getSourceManager().clearIDTables();
534 
535     if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
536       Act.Execute();
537       Act.EndSourceFile();
538     }
539   }
540 
541   if (getDiagnosticOpts().ShowCarets) {
542     unsigned NumWarnings = getDiagnostics().getNumWarnings();
543     unsigned NumErrors = getDiagnostics().getNumErrors() -
544                                getDiagnostics().getNumErrorsSuppressed();
545 
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   }
555 
556   if (getFrontendOpts().ShowStats && hasFileManager()) {
557     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 
573