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