xref: /llvm-project/clang/lib/Frontend/CompilerInstance.cpp (revision 89c31490d8896dbb6a9794de28a874a84268f541)
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 namespace {
94   class BinaryDiagnosticSerializer : public DiagnosticClient {
95     llvm::raw_ostream &OS;
96     SourceManager *SourceMgr;
97   public:
98     explicit BinaryDiagnosticSerializer(llvm::raw_ostream &OS)
99       : OS(OS), SourceMgr(0) { }
100 
101     virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
102                                   const DiagnosticInfo &Info);
103   };
104 }
105 
106 void BinaryDiagnosticSerializer::HandleDiagnostic(Diagnostic::Level DiagLevel,
107                                                   const DiagnosticInfo &Info) {
108   StoredDiagnostic(DiagLevel, Info).Serialize(OS);
109 }
110 
111 static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
112                               unsigned argc, const char* const *argv,
113                               Diagnostic &Diags) {
114   std::string ErrorInfo;
115   llvm::OwningPtr<llvm::raw_ostream> OS(
116     new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
117   if (!ErrorInfo.empty()) {
118     Diags.Report(diag::err_fe_unable_to_open_logfile)
119                  << DiagOpts.DumpBuildInformation << ErrorInfo;
120     return;
121   }
122 
123   (*OS) << "clang -cc1 command line arguments: ";
124   for (unsigned i = 0; i != argc; ++i)
125     (*OS) << argv[i] << ' ';
126   (*OS) << '\n';
127 
128   // Chain in a diagnostic client which will log the diagnostics.
129   DiagnosticClient *Logger =
130     new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
131   Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
132 }
133 
134 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv) {
135   Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv);
136 }
137 
138 llvm::IntrusiveRefCntPtr<Diagnostic>
139 CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
140                                     int Argc, const char* const *Argv) {
141   llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic());
142 
143   // Create the diagnostic client for reporting errors or for
144   // implementing -verify.
145   llvm::OwningPtr<DiagnosticClient> DiagClient;
146   if (Opts.BinaryOutput) {
147     if (llvm::sys::Program::ChangeStderrToBinary()) {
148       // We weren't able to set standard error to binary, which is a
149       // bit of a problem. So, just create a text diagnostic printer
150       // to complain about this problem, and pretend that the user
151       // didn't try to use binary output.
152       Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
153       Diags->Report(diag::err_fe_stderr_binary);
154       return Diags;
155     } else {
156       Diags->setClient(new BinaryDiagnosticSerializer(llvm::errs()));
157     }
158   } else {
159     Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
160   }
161 
162   // Chain in -verify checker, if requested.
163   if (Opts.VerifyDiagnostics)
164     Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
165 
166   if (!Opts.DumpBuildInformation.empty())
167     SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
168 
169   // Configure our handling of diagnostics.
170   ProcessWarningOptions(*Diags, Opts);
171 
172   return Diags;
173 }
174 
175 // File Manager
176 
177 void CompilerInstance::createFileManager() {
178   FileMgr.reset(new FileManager());
179 }
180 
181 // Source Manager
182 
183 void CompilerInstance::createSourceManager() {
184   SourceMgr.reset(new SourceManager(getDiagnostics()));
185 }
186 
187 // Preprocessor
188 
189 void CompilerInstance::createPreprocessor() {
190   PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
191                               getPreprocessorOpts(), getHeaderSearchOpts(),
192                               getDependencyOutputOpts(), getTarget(),
193                               getFrontendOpts(), getSourceManager(),
194                               getFileManager()));
195 }
196 
197 Preprocessor *
198 CompilerInstance::createPreprocessor(Diagnostic &Diags,
199                                      const LangOptions &LangInfo,
200                                      const PreprocessorOptions &PPOpts,
201                                      const HeaderSearchOptions &HSOpts,
202                                      const DependencyOutputOptions &DepOpts,
203                                      const TargetInfo &Target,
204                                      const FrontendOptions &FEOpts,
205                                      SourceManager &SourceMgr,
206                                      FileManager &FileMgr) {
207   // Create a PTH manager if we are using some form of a token cache.
208   PTHManager *PTHMgr = 0;
209   if (!PPOpts.TokenCache.empty())
210     PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
211 
212   // Create the Preprocessor.
213   HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
214   Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
215                                       SourceMgr, *HeaderInfo, PTHMgr,
216                                       /*OwnsHeaderSearch=*/true);
217 
218   // Note that this is different then passing PTHMgr to Preprocessor's ctor.
219   // That argument is used as the IdentifierInfoLookup argument to
220   // IdentifierTable's ctor.
221   if (PTHMgr) {
222     PTHMgr->setPreprocessor(PP);
223     PP->setPTHManager(PTHMgr);
224   }
225 
226   if (PPOpts.DetailedRecord)
227     PP->createPreprocessingRecord();
228 
229   InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
230 
231   // Handle generating dependencies, if requested.
232   if (!DepOpts.OutputFile.empty())
233     AttachDependencyFileGen(*PP, DepOpts);
234 
235   return PP;
236 }
237 
238 // ASTContext
239 
240 void CompilerInstance::createASTContext() {
241   Preprocessor &PP = getPreprocessor();
242   Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(),
243                                getTarget(), PP.getIdentifierTable(),
244                                PP.getSelectorTable(), PP.getBuiltinInfo(),
245                                /*size_reserve=*/ 0));
246 }
247 
248 // ExternalASTSource
249 
250 void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
251                                                   bool DisablePCHValidation,
252                                                  void *DeserializationListener){
253   llvm::OwningPtr<ExternalASTSource> Source;
254   bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
255   Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
256                                           DisablePCHValidation,
257                                           getPreprocessor(), getASTContext(),
258                                           DeserializationListener,
259                                           Preamble));
260   getASTContext().setExternalSource(Source);
261 }
262 
263 ExternalASTSource *
264 CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
265                                              const std::string &Sysroot,
266                                              bool DisablePCHValidation,
267                                              Preprocessor &PP,
268                                              ASTContext &Context,
269                                              void *DeserializationListener,
270                                              bool Preamble) {
271   llvm::OwningPtr<ASTReader> Reader;
272   Reader.reset(new ASTReader(PP, &Context,
273                              Sysroot.empty() ? 0 : Sysroot.c_str(),
274                              DisablePCHValidation));
275 
276   Reader->setDeserializationListener(
277             static_cast<ASTDeserializationListener *>(DeserializationListener));
278   switch (Reader->ReadAST(Path,
279                           Preamble ? ASTReader::Preamble : ASTReader::PCH)) {
280   case ASTReader::Success:
281     // Set the predefines buffer as suggested by the PCH reader. Typically, the
282     // predefines buffer will be empty.
283     PP.setPredefines(Reader->getSuggestedPredefines());
284     return Reader.take();
285 
286   case ASTReader::Failure:
287     // Unrecoverable failure: don't even try to process the input file.
288     break;
289 
290   case ASTReader::IgnorePCH:
291     // No suitable PCH file could be found. Return an error.
292     break;
293   }
294 
295   return 0;
296 }
297 
298 // Code Completion
299 
300 static bool EnableCodeCompletion(Preprocessor &PP,
301                                  const std::string &Filename,
302                                  unsigned Line,
303                                  unsigned Column) {
304   // Tell the source manager to chop off the given file at a specific
305   // line and column.
306   const FileEntry *Entry = PP.getFileManager().getFile(Filename);
307   if (!Entry) {
308     PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
309       << Filename;
310     return true;
311   }
312 
313   // Truncate the named file at the given line/column.
314   PP.SetCodeCompletionPoint(Entry, Line, Column);
315   return false;
316 }
317 
318 void CompilerInstance::createCodeCompletionConsumer() {
319   const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
320   if (!CompletionConsumer) {
321     CompletionConsumer.reset(
322       createCodeCompletionConsumer(getPreprocessor(),
323                                    Loc.FileName, Loc.Line, Loc.Column,
324                                    getFrontendOpts().DebugCodeCompletionPrinter,
325                                    getFrontendOpts().ShowMacrosInCodeCompletion,
326                              getFrontendOpts().ShowCodePatternsInCodeCompletion,
327                            getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
328                                    llvm::outs()));
329     if (!CompletionConsumer)
330       return;
331   } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
332                                   Loc.Line, Loc.Column)) {
333     CompletionConsumer.reset();
334     return;
335   }
336 
337   if (CompletionConsumer->isOutputBinary() &&
338       llvm::sys::Program::ChangeStdoutToBinary()) {
339     getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
340     CompletionConsumer.reset();
341   }
342 }
343 
344 void CompilerInstance::createFrontendTimer() {
345   FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
346 }
347 
348 CodeCompleteConsumer *
349 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
350                                                const std::string &Filename,
351                                                unsigned Line,
352                                                unsigned Column,
353                                                bool UseDebugPrinter,
354                                                bool ShowMacros,
355                                                bool ShowCodePatterns,
356                                                bool ShowGlobals,
357                                                llvm::raw_ostream &OS) {
358   if (EnableCodeCompletion(PP, Filename, Line, Column))
359     return 0;
360 
361   // Set up the creation routine for code-completion.
362   if (UseDebugPrinter)
363     return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
364                                             ShowGlobals, OS);
365   else
366     return new CIndexCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
367                                           ShowGlobals, OS);
368 }
369 
370 void CompilerInstance::createSema(bool CompleteTranslationUnit,
371                                   CodeCompleteConsumer *CompletionConsumer) {
372   TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
373                          CompleteTranslationUnit, CompletionConsumer));
374 }
375 
376 // Output Files
377 
378 void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
379   assert(OutFile.OS && "Attempt to add empty stream to output list!");
380   OutputFiles.push_back(OutFile);
381 }
382 
383 void CompilerInstance::clearOutputFiles(bool EraseFiles) {
384   for (std::list<OutputFile>::iterator
385          it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
386     delete it->OS;
387     if (!it->TempFilename.empty()) {
388       llvm::sys::Path TempPath(it->TempFilename);
389       if (EraseFiles)
390         TempPath.eraseFromDisk();
391       else {
392         std::string Error;
393         if (TempPath.renamePathOnDisk(llvm::sys::Path(it->Filename), &Error)) {
394           getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
395             << it->TempFilename << it->Filename << Error;
396           TempPath.eraseFromDisk();
397         }
398       }
399     } else if (!it->Filename.empty() && EraseFiles)
400       llvm::sys::Path(it->Filename).eraseFromDisk();
401 
402   }
403   OutputFiles.clear();
404 }
405 
406 llvm::raw_fd_ostream *
407 CompilerInstance::createDefaultOutputFile(bool Binary,
408                                           llvm::StringRef InFile,
409                                           llvm::StringRef Extension) {
410   return createOutputFile(getFrontendOpts().OutputFile, Binary,
411                           InFile, Extension);
412 }
413 
414 llvm::raw_fd_ostream *
415 CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
416                                    bool Binary,
417                                    llvm::StringRef InFile,
418                                    llvm::StringRef Extension) {
419   std::string Error, OutputPathName, TempPathName;
420   llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
421                                               InFile, Extension,
422                                               &OutputPathName,
423                                               &TempPathName);
424   if (!OS) {
425     getDiagnostics().Report(diag::err_fe_unable_to_open_output)
426       << OutputPath << Error;
427     return 0;
428   }
429 
430   // Add the output file -- but don't try to remove "-", since this means we are
431   // using stdin.
432   addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
433                 TempPathName, OS));
434 
435   return OS;
436 }
437 
438 llvm::raw_fd_ostream *
439 CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
440                                    std::string &Error,
441                                    bool Binary,
442                                    llvm::StringRef InFile,
443                                    llvm::StringRef Extension,
444                                    std::string *ResultPathName,
445                                    std::string *TempPathName) {
446   std::string OutFile, TempFile;
447   if (!OutputPath.empty()) {
448     OutFile = OutputPath;
449   } else if (InFile == "-") {
450     OutFile = "-";
451   } else if (!Extension.empty()) {
452     llvm::sys::Path Path(InFile);
453     Path.eraseSuffix();
454     Path.appendSuffix(Extension);
455     OutFile = Path.str();
456   } else {
457     OutFile = "-";
458   }
459 
460   if (OutFile != "-") {
461     llvm::sys::Path OutPath(OutFile);
462     // Only create the temporary if we can actually write to OutPath, otherwise
463     // we want to fail early.
464     if (!OutPath.exists() ||
465         (OutPath.isRegularFile() && OutPath.canWrite())) {
466       // Create a temporary file.
467       llvm::sys::Path TempPath(OutFile);
468       if (!TempPath.createTemporaryFileOnDisk())
469         TempFile = TempPath.str();
470     }
471   }
472 
473   std::string OSFile = OutFile;
474   if (!TempFile.empty())
475     OSFile = TempFile;
476 
477   llvm::OwningPtr<llvm::raw_fd_ostream> OS(
478     new llvm::raw_fd_ostream(OSFile.c_str(), Error,
479                              (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
480   if (!Error.empty())
481     return 0;
482 
483   // Make sure the out stream file gets removed if we crash.
484   llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
485 
486   if (ResultPathName)
487     *ResultPathName = OutFile;
488   if (TempPathName)
489     *TempPathName = TempFile;
490 
491   return OS.take();
492 }
493 
494 // Initialization Utilities
495 
496 bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
497   return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
498                                  getSourceManager(), getFrontendOpts());
499 }
500 
501 bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
502                                                Diagnostic &Diags,
503                                                FileManager &FileMgr,
504                                                SourceManager &SourceMgr,
505                                                const FrontendOptions &Opts) {
506   // Figure out where to get and map in the main file.
507   if (InputFile != "-") {
508     const FileEntry *File = FileMgr.getFile(InputFile);
509     if (File) SourceMgr.createMainFileID(File);
510     if (SourceMgr.getMainFileID().isInvalid()) {
511       Diags.Report(diag::err_fe_error_reading) << InputFile;
512       return false;
513     }
514   } else {
515     llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
516     if (SB) SourceMgr.createMainFileIDForMemBuffer(SB);
517     if (SourceMgr.getMainFileID().isInvalid()) {
518       Diags.Report(diag::err_fe_error_reading_stdin);
519       return false;
520     }
521   }
522 
523   return true;
524 }
525 
526 // High-Level Operations
527 
528 bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
529   assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
530   assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
531   assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
532 
533   // FIXME: Take this as an argument, once all the APIs we used have moved to
534   // taking it as an input instead of hard-coding llvm::errs.
535   llvm::raw_ostream &OS = llvm::errs();
536 
537   // Create the target instance.
538   setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
539   if (!hasTarget())
540     return false;
541 
542   // Inform the target of the language options.
543   //
544   // FIXME: We shouldn't need to do this, the target should be immutable once
545   // created. This complexity should be lifted elsewhere.
546   getTarget().setForcedLangOptions(getLangOpts());
547 
548   // Validate/process some options.
549   if (getHeaderSearchOpts().Verbose)
550     OS << "clang -cc1 version " CLANG_VERSION_STRING
551        << " based upon " << PACKAGE_STRING
552        << " hosted on " << llvm::sys::getHostTriple() << "\n";
553 
554   if (getFrontendOpts().ShowTimers)
555     createFrontendTimer();
556 
557   if (getFrontendOpts().ShowStats)
558     llvm::EnableStatistics();
559 
560   for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
561     const std::string &InFile = getFrontendOpts().Inputs[i].second;
562 
563     // Reset the ID tables if we are reusing the SourceManager.
564     if (hasSourceManager())
565       getSourceManager().clearIDTables();
566 
567     if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
568       Act.Execute();
569       Act.EndSourceFile();
570     }
571   }
572 
573   if (getDiagnosticOpts().ShowCarets) {
574     unsigned NumWarnings = getDiagnostics().getNumWarnings();
575     unsigned NumErrors = getDiagnostics().getNumErrors() -
576                                getDiagnostics().getNumErrorsSuppressed();
577 
578     if (NumWarnings)
579       OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
580     if (NumWarnings && NumErrors)
581       OS << " and ";
582     if (NumErrors)
583       OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
584     if (NumWarnings || NumErrors)
585       OS << " generated.\n";
586   }
587 
588   if (getFrontendOpts().ShowStats && hasFileManager()) {
589     getFileManager().PrintStats();
590     OS << "\n";
591   }
592 
593   // Return the appropriate status when verifying diagnostics.
594   //
595   // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
596   // this.
597   if (getDiagnosticOpts().VerifyDiagnostics)
598     return !static_cast<VerifyDiagnosticsClient&>(
599       getDiagnosticClient()).HadErrors();
600 
601   return !getDiagnostics().getNumErrors();
602 }
603 
604 
605