xref: /llvm-project/clang/lib/Frontend/CompilerInstance.cpp (revision fe908a805b51d77d4fc5332aa96feb04497eb66b)
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 = Value;
51 }
52 
53 void CompilerInstance::setDiagnostics(Diagnostic *Value) {
54   Diagnostics = Value;
55 }
56 
57 void CompilerInstance::setTarget(TargetInfo *Value) {
58   Target = Value;
59 }
60 
61 void CompilerInstance::setFileManager(FileManager *Value) {
62   FileMgr = Value;
63 }
64 
65 void CompilerInstance::setSourceManager(SourceManager *Value) {
66   SourceMgr = Value;
67 }
68 
69 void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
70 
71 void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
72 
73 void CompilerInstance::setSema(Sema *S) {
74   TheSema.reset(S);
75 }
76 
77 void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
78   Consumer.reset(Value);
79 }
80 
81 void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
82   CompletionConsumer.reset(Value);
83 }
84 
85 // Diagnostics
86 static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
87                               unsigned argc, const char* const *argv,
88                               Diagnostic &Diags) {
89   std::string ErrorInfo;
90   llvm::OwningPtr<llvm::raw_ostream> OS(
91     new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
92   if (!ErrorInfo.empty()) {
93     Diags.Report(diag::err_fe_unable_to_open_logfile)
94                  << DiagOpts.DumpBuildInformation << ErrorInfo;
95     return;
96   }
97 
98   (*OS) << "clang -cc1 command line arguments: ";
99   for (unsigned i = 0; i != argc; ++i)
100     (*OS) << argv[i] << ' ';
101   (*OS) << '\n';
102 
103   // Chain in a diagnostic client which will log the diagnostics.
104   DiagnosticClient *Logger =
105     new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
106   Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
107 }
108 
109 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
110                                          DiagnosticClient *Client) {
111   Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client);
112 }
113 
114 llvm::IntrusiveRefCntPtr<Diagnostic>
115 CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
116                                     int Argc, const char* const *Argv,
117                                     DiagnosticClient *Client) {
118   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
119   llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID));
120 
121   // Create the diagnostic client for reporting errors or for
122   // implementing -verify.
123   if (Client)
124     Diags->setClient(Client);
125   else
126     Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
127 
128   // Chain in -verify checker, if requested.
129   if (Opts.VerifyDiagnostics)
130     Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
131 
132   if (!Opts.DumpBuildInformation.empty())
133     SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
134 
135   // Configure our handling of diagnostics.
136   ProcessWarningOptions(*Diags, Opts);
137 
138   return Diags;
139 }
140 
141 // File Manager
142 
143 void CompilerInstance::createFileManager() {
144   FileMgr = new FileManager(getFileSystemOpts());
145 }
146 
147 // Source Manager
148 
149 void CompilerInstance::createSourceManager(FileManager &FileMgr) {
150   SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
151 }
152 
153 // Preprocessor
154 
155 void CompilerInstance::createPreprocessor() {
156   PP = createPreprocessor(getDiagnostics(), getLangOpts(),
157                           getPreprocessorOpts(), getHeaderSearchOpts(),
158                           getDependencyOutputOpts(), getTarget(),
159                           getFrontendOpts(), getSourceManager(),
160                           getFileManager());
161 }
162 
163 Preprocessor *
164 CompilerInstance::createPreprocessor(Diagnostic &Diags,
165                                      const LangOptions &LangInfo,
166                                      const PreprocessorOptions &PPOpts,
167                                      const HeaderSearchOptions &HSOpts,
168                                      const DependencyOutputOptions &DepOpts,
169                                      const TargetInfo &Target,
170                                      const FrontendOptions &FEOpts,
171                                      SourceManager &SourceMgr,
172                                      FileManager &FileMgr) {
173   // Create a PTH manager if we are using some form of a token cache.
174   PTHManager *PTHMgr = 0;
175   if (!PPOpts.TokenCache.empty())
176     PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags);
177 
178   // Create the Preprocessor.
179   HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr);
180   Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target,
181                                       SourceMgr, *HeaderInfo, PTHMgr,
182                                       /*OwnsHeaderSearch=*/true);
183 
184   // Note that this is different then passing PTHMgr to Preprocessor's ctor.
185   // That argument is used as the IdentifierInfoLookup argument to
186   // IdentifierTable's ctor.
187   if (PTHMgr) {
188     PTHMgr->setPreprocessor(PP);
189     PP->setPTHManager(PTHMgr);
190   }
191 
192   if (PPOpts.DetailedRecord)
193     PP->createPreprocessingRecord();
194 
195   InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts);
196 
197   // Handle generating dependencies, if requested.
198   if (!DepOpts.OutputFile.empty())
199     AttachDependencyFileGen(*PP, DepOpts);
200 
201   // Handle generating header include information, if requested.
202   if (DepOpts.ShowHeaderIncludes)
203     AttachHeaderIncludeGen(*PP);
204   if (!DepOpts.HeaderIncludeOutputFile.empty()) {
205     llvm::StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
206     if (OutputPath == "-")
207       OutputPath = "";
208     AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
209                            /*ShowDepth=*/false);
210   }
211 
212   return PP;
213 }
214 
215 // ASTContext
216 
217 void CompilerInstance::createASTContext() {
218   Preprocessor &PP = getPreprocessor();
219   Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
220                            getTarget(), PP.getIdentifierTable(),
221                            PP.getSelectorTable(), PP.getBuiltinInfo(),
222                            /*size_reserve=*/ 0);
223 }
224 
225 // ExternalASTSource
226 
227 void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
228                                                   bool DisablePCHValidation,
229                                                   bool DisableStatCache,
230                                                  void *DeserializationListener){
231   llvm::OwningPtr<ExternalASTSource> Source;
232   bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
233   Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
234                                           DisablePCHValidation,
235                                           DisableStatCache,
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                                              bool DisableStatCache,
247                                              Preprocessor &PP,
248                                              ASTContext &Context,
249                                              void *DeserializationListener,
250                                              bool Preamble) {
251   llvm::OwningPtr<ASTReader> Reader;
252   Reader.reset(new ASTReader(PP, &Context,
253                              Sysroot.empty() ? 0 : Sysroot.c_str(),
254                              DisablePCHValidation, DisableStatCache));
255 
256   Reader->setDeserializationListener(
257             static_cast<ASTDeserializationListener *>(DeserializationListener));
258   switch (Reader->ReadAST(Path,
259                           Preamble ? ASTReader::Preamble : ASTReader::PCH)) {
260   case ASTReader::Success:
261     // Set the predefines buffer as suggested by the PCH reader. Typically, the
262     // predefines buffer will be empty.
263     PP.setPredefines(Reader->getSuggestedPredefines());
264     return Reader.take();
265 
266   case ASTReader::Failure:
267     // Unrecoverable failure: don't even try to process the input file.
268     break;
269 
270   case ASTReader::IgnorePCH:
271     // No suitable PCH file could be found. Return an error.
272     break;
273   }
274 
275   return 0;
276 }
277 
278 // Code Completion
279 
280 static bool EnableCodeCompletion(Preprocessor &PP,
281                                  const std::string &Filename,
282                                  unsigned Line,
283                                  unsigned Column) {
284   // Tell the source manager to chop off the given file at a specific
285   // line and column.
286   const FileEntry *Entry = PP.getFileManager().getFile(Filename);
287   if (!Entry) {
288     PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
289       << Filename;
290     return true;
291   }
292 
293   // Truncate the named file at the given line/column.
294   PP.SetCodeCompletionPoint(Entry, Line, Column);
295   return false;
296 }
297 
298 void CompilerInstance::createCodeCompletionConsumer() {
299   const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
300   if (!CompletionConsumer) {
301     CompletionConsumer.reset(
302       createCodeCompletionConsumer(getPreprocessor(),
303                                    Loc.FileName, Loc.Line, Loc.Column,
304                                    getFrontendOpts().ShowMacrosInCodeCompletion,
305                              getFrontendOpts().ShowCodePatternsInCodeCompletion,
306                            getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
307                                    llvm::outs()));
308     if (!CompletionConsumer)
309       return;
310   } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
311                                   Loc.Line, Loc.Column)) {
312     CompletionConsumer.reset();
313     return;
314   }
315 
316   if (CompletionConsumer->isOutputBinary() &&
317       llvm::sys::Program::ChangeStdoutToBinary()) {
318     getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
319     CompletionConsumer.reset();
320   }
321 }
322 
323 void CompilerInstance::createFrontendTimer() {
324   FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
325 }
326 
327 CodeCompleteConsumer *
328 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
329                                                const std::string &Filename,
330                                                unsigned Line,
331                                                unsigned Column,
332                                                bool ShowMacros,
333                                                bool ShowCodePatterns,
334                                                bool ShowGlobals,
335                                                llvm::raw_ostream &OS) {
336   if (EnableCodeCompletion(PP, Filename, Line, Column))
337     return 0;
338 
339   // Set up the creation routine for code-completion.
340   return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
341                                           ShowGlobals, OS);
342 }
343 
344 void CompilerInstance::createSema(bool CompleteTranslationUnit,
345                                   CodeCompleteConsumer *CompletionConsumer) {
346   TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
347                          CompleteTranslationUnit, CompletionConsumer));
348 }
349 
350 // Output Files
351 
352 void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
353   assert(OutFile.OS && "Attempt to add empty stream to output list!");
354   OutputFiles.push_back(OutFile);
355 }
356 
357 void CompilerInstance::clearOutputFiles(bool EraseFiles) {
358   for (std::list<OutputFile>::iterator
359          it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
360     delete it->OS;
361     if (!it->TempFilename.empty()) {
362       if (EraseFiles) {
363         bool existed;
364         llvm::sys::fs::remove(it->TempFilename, existed);
365       } else {
366         llvm::SmallString<128> NewOutFile(it->Filename);
367 
368         // If '-working-directory' was passed, the output filename should be
369         // relative to that.
370         FileMgr->FixupRelativePath(NewOutFile);
371         if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
372                                                         NewOutFile.str())) {
373           getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
374             << it->TempFilename << it->Filename << ec.message();
375 
376           bool existed;
377           llvm::sys::fs::remove(it->TempFilename, existed);
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