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