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