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