xref: /llvm-project/clang/lib/Frontend/CompilerInstance.cpp (revision 9ba8fb1e48baba536621d544da1299df83847548)
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       if (EraseFiles) {
366         bool existed;
367         llvm::sys::fs::remove(it->TempFilename, existed);
368       } else {
369         llvm::SmallString<128> NewOutFile(it->Filename);
370 
371         // If '-working-directory' was passed, the output filename should be
372         // relative to that.
373         FileMgr->FixupRelativePath(NewOutFile);
374         if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
375                                                         NewOutFile.str())) {
376           getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
377             << it->TempFilename << it->Filename << ec.message();
378 
379           bool existed;
380           llvm::sys::fs::remove(it->TempFilename, existed);
381         }
382       }
383     } else if (!it->Filename.empty() && EraseFiles)
384       llvm::sys::Path(it->Filename).eraseFromDisk();
385 
386   }
387   OutputFiles.clear();
388 }
389 
390 llvm::raw_fd_ostream *
391 CompilerInstance::createDefaultOutputFile(bool Binary,
392                                           llvm::StringRef InFile,
393                                           llvm::StringRef Extension) {
394   return createOutputFile(getFrontendOpts().OutputFile, Binary,
395                           /*RemoveFileOnSignal=*/true, InFile, Extension);
396 }
397 
398 llvm::raw_fd_ostream *
399 CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
400                                    bool Binary, bool RemoveFileOnSignal,
401                                    llvm::StringRef InFile,
402                                    llvm::StringRef Extension) {
403   std::string Error, OutputPathName, TempPathName;
404   llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
405                                               RemoveFileOnSignal,
406                                               InFile, Extension,
407                                               &OutputPathName,
408                                               &TempPathName);
409   if (!OS) {
410     getDiagnostics().Report(diag::err_fe_unable_to_open_output)
411       << OutputPath << Error;
412     return 0;
413   }
414 
415   // Add the output file -- but don't try to remove "-", since this means we are
416   // using stdin.
417   addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
418                 TempPathName, OS));
419 
420   return OS;
421 }
422 
423 llvm::raw_fd_ostream *
424 CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
425                                    std::string &Error,
426                                    bool Binary,
427                                    bool RemoveFileOnSignal,
428                                    llvm::StringRef InFile,
429                                    llvm::StringRef Extension,
430                                    std::string *ResultPathName,
431                                    std::string *TempPathName) {
432   std::string OutFile, TempFile;
433   if (!OutputPath.empty()) {
434     OutFile = OutputPath;
435   } else if (InFile == "-") {
436     OutFile = "-";
437   } else if (!Extension.empty()) {
438     llvm::sys::Path Path(InFile);
439     Path.eraseSuffix();
440     Path.appendSuffix(Extension);
441     OutFile = Path.str();
442   } else {
443     OutFile = "-";
444   }
445 
446   if (OutFile != "-") {
447     llvm::sys::Path OutPath(OutFile);
448     // Only create the temporary if we can actually write to OutPath, otherwise
449     // we want to fail early.
450     bool Exists;
451     if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) ||
452         (OutPath.isRegularFile() && OutPath.canWrite())) {
453       // Create a temporary file.
454       llvm::sys::Path TempPath(OutFile);
455       if (!TempPath.createTemporaryFileOnDisk())
456         TempFile = TempPath.str();
457     }
458   }
459 
460   std::string OSFile = OutFile;
461   if (!TempFile.empty())
462     OSFile = TempFile;
463 
464   llvm::OwningPtr<llvm::raw_fd_ostream> OS(
465     new llvm::raw_fd_ostream(OSFile.c_str(), Error,
466                              (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
467   if (!Error.empty())
468     return 0;
469 
470   // Make sure the out stream file gets removed if we crash.
471   if (RemoveFileOnSignal)
472     llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
473 
474   if (ResultPathName)
475     *ResultPathName = OutFile;
476   if (TempPathName)
477     *TempPathName = TempFile;
478 
479   return OS.take();
480 }
481 
482 // Initialization Utilities
483 
484 bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) {
485   return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
486                                  getSourceManager(), getFrontendOpts());
487 }
488 
489 bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile,
490                                                Diagnostic &Diags,
491                                                FileManager &FileMgr,
492                                                SourceManager &SourceMgr,
493                                                const FrontendOptions &Opts) {
494   // Figure out where to get and map in the main file, unless it's already
495   // been created (e.g., by a precompiled preamble).
496   if (!SourceMgr.getMainFileID().isInvalid()) {
497     // Do nothing: the main file has already been set.
498   } else if (InputFile != "-") {
499     const FileEntry *File = FileMgr.getFile(InputFile);
500     if (!File) {
501       Diags.Report(diag::err_fe_error_reading) << InputFile;
502       return false;
503     }
504     SourceMgr.createMainFileID(File);
505   } else {
506     llvm::OwningPtr<llvm::MemoryBuffer> SB;
507     if (llvm::MemoryBuffer::getSTDIN(SB)) {
508       // FIXME: Give ec.message() in this diag.
509       Diags.Report(diag::err_fe_error_reading_stdin);
510       return false;
511     }
512     const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
513                                                    SB->getBufferSize(), 0);
514     SourceMgr.createMainFileID(File);
515     SourceMgr.overrideFileContents(File, SB.take());
516   }
517 
518   assert(!SourceMgr.getMainFileID().isInvalid() &&
519          "Couldn't establish MainFileID!");
520   return true;
521 }
522 
523 // High-Level Operations
524 
525 bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
526   assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
527   assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
528   assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
529 
530   // FIXME: Take this as an argument, once all the APIs we used have moved to
531   // taking it as an input instead of hard-coding llvm::errs.
532   llvm::raw_ostream &OS = llvm::errs();
533 
534   // Create the target instance.
535   setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
536   if (!hasTarget())
537     return false;
538 
539   // Inform the target of the language options.
540   //
541   // FIXME: We shouldn't need to do this, the target should be immutable once
542   // created. This complexity should be lifted elsewhere.
543   getTarget().setForcedLangOptions(getLangOpts());
544 
545   // Validate/process some options.
546   if (getHeaderSearchOpts().Verbose)
547     OS << "clang -cc1 version " CLANG_VERSION_STRING
548        << " based upon " << PACKAGE_STRING
549        << " hosted on " << llvm::sys::getHostTriple() << "\n";
550 
551   if (getFrontendOpts().ShowTimers)
552     createFrontendTimer();
553 
554   if (getFrontendOpts().ShowStats)
555     llvm::EnableStatistics();
556 
557   for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
558     const std::string &InFile = getFrontendOpts().Inputs[i].second;
559 
560     // Reset the ID tables if we are reusing the SourceManager.
561     if (hasSourceManager())
562       getSourceManager().clearIDTables();
563 
564     if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
565       Act.Execute();
566       Act.EndSourceFile();
567     }
568   }
569 
570   if (getDiagnosticOpts().ShowCarets) {
571     // We can have multiple diagnostics sharing one diagnostic client.
572     // Get the total number of warnings/errors from the client.
573     unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
574     unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
575 
576     if (NumWarnings)
577       OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
578     if (NumWarnings && NumErrors)
579       OS << " and ";
580     if (NumErrors)
581       OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
582     if (NumWarnings || NumErrors)
583       OS << " generated.\n";
584   }
585 
586   if (getFrontendOpts().ShowStats && hasFileManager()) {
587     getFileManager().PrintStats();
588     OS << "\n";
589   }
590 
591   return !getDiagnostics().getClient()->getNumErrors();
592 }
593 
594 
595