FrontendAction.cpp revision a6b00fc97669aa25d89ae9f202b05dfadfd0e324
1//===--- FrontendAction.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/FrontendAction.h"
11#include "clang/AST/ASTConsumer.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/DeclGroup.h"
14#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/ChainedIncludesSource.h"
16#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Frontend/FrontendPluginRegistry.h"
19#include "clang/Frontend/LayoutOverrideSource.h"
20#include "clang/Frontend/MultiplexConsumer.h"
21#include "clang/Lex/HeaderSearch.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Parse/ParseAST.h"
24#include "clang/Serialization/ASTDeserializationListener.h"
25#include "clang/Serialization/ASTReader.h"
26#include "clang/Serialization/GlobalModuleIndex.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/Timer.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/system_error.h"
33using namespace clang;
34
35namespace {
36
37class DelegatingDeserializationListener : public ASTDeserializationListener {
38  ASTDeserializationListener *Previous;
39
40public:
41  explicit DelegatingDeserializationListener(
42                                           ASTDeserializationListener *Previous)
43    : Previous(Previous) { }
44
45  virtual void ReaderInitialized(ASTReader *Reader) {
46    if (Previous)
47      Previous->ReaderInitialized(Reader);
48  }
49  virtual void IdentifierRead(serialization::IdentID ID,
50                              IdentifierInfo *II) {
51    if (Previous)
52      Previous->IdentifierRead(ID, II);
53  }
54  virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {
55    if (Previous)
56      Previous->TypeRead(Idx, T);
57  }
58  virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
59    if (Previous)
60      Previous->DeclRead(ID, D);
61  }
62  virtual void SelectorRead(serialization::SelectorID ID, Selector Sel) {
63    if (Previous)
64      Previous->SelectorRead(ID, Sel);
65  }
66  virtual void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
67                                   MacroDefinition *MD) {
68    if (Previous)
69      Previous->MacroDefinitionRead(PPID, MD);
70  }
71};
72
73/// \brief Dumps deserialized declarations.
74class DeserializedDeclsDumper : public DelegatingDeserializationListener {
75public:
76  explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
77    : DelegatingDeserializationListener(Previous) { }
78
79  virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
80    llvm::outs() << "PCH DECL: " << D->getDeclKindName();
81    if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
82      llvm::outs() << " - " << *ND;
83    llvm::outs() << "\n";
84
85    DelegatingDeserializationListener::DeclRead(ID, D);
86  }
87};
88
89/// \brief Checks deserialized declarations and emits error if a name
90/// matches one given in command-line using -error-on-deserialized-decl.
91class DeserializedDeclsChecker : public DelegatingDeserializationListener {
92  ASTContext &Ctx;
93  std::set<std::string> NamesToCheck;
94
95public:
96  DeserializedDeclsChecker(ASTContext &Ctx,
97                           const std::set<std::string> &NamesToCheck,
98                           ASTDeserializationListener *Previous)
99    : DelegatingDeserializationListener(Previous),
100      Ctx(Ctx), NamesToCheck(NamesToCheck) { }
101
102  virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
103    if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
104      if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
105        unsigned DiagID
106          = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
107                                                 "%0 was deserialized");
108        Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
109            << ND->getNameAsString();
110      }
111
112    DelegatingDeserializationListener::DeclRead(ID, D);
113  }
114};
115
116} // end anonymous namespace
117
118FrontendAction::FrontendAction() : Instance(0) {}
119
120FrontendAction::~FrontendAction() {}
121
122void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
123                                     ASTUnit *AST) {
124  this->CurrentInput = CurrentInput;
125  CurrentASTUnit.reset(AST);
126}
127
128ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
129                                                      StringRef InFile) {
130  ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
131  if (!Consumer)
132    return 0;
133
134  if (CI.getFrontendOpts().AddPluginActions.size() == 0)
135    return Consumer;
136
137  // Make sure the non-plugin consumer is first, so that plugins can't
138  // modifiy the AST.
139  std::vector<ASTConsumer*> Consumers(1, Consumer);
140
141  for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
142       i != e; ++i) {
143    // This is O(|plugins| * |add_plugins|), but since both numbers are
144    // way below 50 in practice, that's ok.
145    for (FrontendPluginRegistry::iterator
146        it = FrontendPluginRegistry::begin(),
147        ie = FrontendPluginRegistry::end();
148        it != ie; ++it) {
149      if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
150        OwningPtr<PluginASTAction> P(it->instantiate());
151        FrontendAction* c = P.get();
152        if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
153          Consumers.push_back(c->CreateASTConsumer(CI, InFile));
154      }
155    }
156  }
157
158  return new MultiplexConsumer(Consumers);
159}
160
161
162bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
163                                     const FrontendInputFile &Input) {
164  assert(!Instance && "Already processing a source file!");
165  assert(!Input.isEmpty() && "Unexpected empty filename!");
166  setCurrentInput(Input);
167  setCompilerInstance(&CI);
168
169  StringRef InputFile = Input.getFile();
170  bool HasBegunSourceFile = false;
171  if (!BeginInvocation(CI))
172    goto failure;
173
174  // AST files follow a very different path, since they share objects via the
175  // AST unit.
176  if (Input.getKind() == IK_AST) {
177    assert(!usesPreprocessorOnly() &&
178           "Attempt to pass AST file to preprocessor only action!");
179    assert(hasASTFileSupport() &&
180           "This action does not have AST file support!");
181
182    IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
183    std::string Error;
184    ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
185                                            CI.getFileSystemOpts());
186    if (!AST)
187      goto failure;
188
189    setCurrentInput(Input, AST);
190
191    // Set the shared objects, these are reset when we finish processing the
192    // file, otherwise the CompilerInstance will happily destroy them.
193    CI.setFileManager(&AST->getFileManager());
194    CI.setSourceManager(&AST->getSourceManager());
195    CI.setPreprocessor(&AST->getPreprocessor());
196    CI.setASTContext(&AST->getASTContext());
197
198    // Initialize the action.
199    if (!BeginSourceFileAction(CI, InputFile))
200      goto failure;
201
202    // Create the AST consumer.
203    CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
204    if (!CI.hasASTConsumer())
205      goto failure;
206
207    return true;
208  }
209
210  // Set up the file and source managers, if needed.
211  if (!CI.hasFileManager())
212    CI.createFileManager();
213  if (!CI.hasSourceManager())
214    CI.createSourceManager(CI.getFileManager());
215
216  // IR files bypass the rest of initialization.
217  if (Input.getKind() == IK_LLVM_IR) {
218    assert(hasIRSupport() &&
219           "This action does not have IR file support!");
220
221    // Inform the diagnostic client we are processing a source file.
222    CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
223    HasBegunSourceFile = true;
224
225    // Initialize the action.
226    if (!BeginSourceFileAction(CI, InputFile))
227      goto failure;
228
229    return true;
230  }
231
232  // If the implicit PCH include is actually a directory, rather than
233  // a single file, search for a suitable PCH file in that directory.
234  if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
235    FileManager &FileMgr = CI.getFileManager();
236    PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
237    StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
238    if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
239      llvm::error_code EC;
240      SmallString<128> DirNative;
241      llvm::sys::path::native(PCHDir->getName(), DirNative);
242      bool Found = false;
243      for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
244           Dir != DirEnd && !EC; Dir.increment(EC)) {
245        // Check whether this is an acceptable AST file.
246        if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
247                                           CI.getLangOpts(),
248                                           CI.getTargetOpts(),
249                                           CI.getPreprocessorOpts())) {
250          for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) {
251            if (PPOpts.Includes[I] == PPOpts.ImplicitPCHInclude) {
252              PPOpts.Includes[I] = Dir->path();
253              PPOpts.ImplicitPCHInclude = Dir->path();
254              Found = true;
255              break;
256            }
257          }
258
259          assert(Found && "Implicit PCH include not in includes list?");
260          break;
261        }
262      }
263
264      if (!Found) {
265        CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
266        return true;
267      }
268    }
269  }
270
271  // Set up the preprocessor.
272  CI.createPreprocessor();
273
274  // Inform the diagnostic client we are processing a source file.
275  CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
276                                           &CI.getPreprocessor());
277  HasBegunSourceFile = true;
278
279  // Initialize the action.
280  if (!BeginSourceFileAction(CI, InputFile))
281    goto failure;
282
283  // Create the AST context and consumer unless this is a preprocessor only
284  // action.
285  if (!usesPreprocessorOnly()) {
286    CI.createASTContext();
287
288    OwningPtr<ASTConsumer> Consumer(
289                                   CreateWrappedASTConsumer(CI, InputFile));
290    if (!Consumer)
291      goto failure;
292
293    CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
294    CI.getPreprocessor().setPPMutationListener(
295      Consumer->GetPPMutationListener());
296
297    if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
298      // Convert headers to PCH and chain them.
299      OwningPtr<ExternalASTSource> source;
300      source.reset(ChainedIncludesSource::create(CI));
301      if (!source)
302        goto failure;
303      CI.getASTContext().setExternalSource(source);
304
305    } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
306      // Use PCH.
307      assert(hasPCHSupport() && "This action does not have PCH support!");
308      ASTDeserializationListener *DeserialListener =
309          Consumer->GetASTDeserializationListener();
310      if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
311        DeserialListener = new DeserializedDeclsDumper(DeserialListener);
312      if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
313        DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
314                         CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
315                                                        DeserialListener);
316      CI.createPCHExternalASTSource(
317                                CI.getPreprocessorOpts().ImplicitPCHInclude,
318                                CI.getPreprocessorOpts().DisablePCHValidation,
319                            CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
320                                DeserialListener);
321      if (!CI.getASTContext().getExternalSource())
322        goto failure;
323    }
324
325    CI.setASTConsumer(Consumer.take());
326    if (!CI.hasASTConsumer())
327      goto failure;
328  }
329
330  // Initialize built-in info as long as we aren't using an external AST
331  // source.
332  if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
333    Preprocessor &PP = CI.getPreprocessor();
334    PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
335                                           PP.getLangOpts());
336  }
337
338  // If there is a layout overrides file, attach an external AST source that
339  // provides the layouts from that file.
340  if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
341      CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
342    OwningPtr<ExternalASTSource>
343      Override(new LayoutOverrideSource(
344                     CI.getFrontendOpts().OverrideRecordLayoutsFile));
345    CI.getASTContext().setExternalSource(Override);
346  }
347
348  return true;
349
350  // If we failed, reset state since the client will not end up calling the
351  // matching EndSourceFile().
352  failure:
353  if (isCurrentFileAST()) {
354    CI.setASTContext(0);
355    CI.setPreprocessor(0);
356    CI.setSourceManager(0);
357    CI.setFileManager(0);
358  }
359
360  if (HasBegunSourceFile)
361    CI.getDiagnosticClient().EndSourceFile();
362  CI.clearOutputFiles(/*EraseFiles=*/true);
363  setCurrentInput(FrontendInputFile());
364  setCompilerInstance(0);
365  return false;
366}
367
368bool FrontendAction::Execute() {
369  CompilerInstance &CI = getCompilerInstance();
370
371  // Initialize the main file entry. This needs to be delayed until after PCH
372  // has loaded.
373  if (!isCurrentFileAST()) {
374    if (!CI.InitializeSourceManager(getCurrentInput()))
375      return false;
376  }
377
378  if (CI.hasFrontendTimer()) {
379    llvm::TimeRegion Timer(CI.getFrontendTimer());
380    ExecuteAction();
381  }
382  else ExecuteAction();
383
384  // If we are supposed to rebuild the global module index, do so now unless
385  // an error occurred.
386  if (CI.getBuildGlobalModuleIndex() && CI.hasFileManager() &&
387      CI.hasPreprocessor() &&
388      (!CI.hasDiagnostics() || !CI.getDiagnostics().hasErrorOccurred())) {
389    GlobalModuleIndex::writeIndex(
390      CI.getFileManager(),
391      CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
392  }
393
394  return true;
395}
396
397void FrontendAction::EndSourceFile() {
398  CompilerInstance &CI = getCompilerInstance();
399
400  // Inform the diagnostic client we are done with this source file.
401  CI.getDiagnosticClient().EndSourceFile();
402
403  // Finalize the action.
404  EndSourceFileAction();
405
406  // Release the consumer and the AST, in that order since the consumer may
407  // perform actions in its destructor which require the context.
408  //
409  // FIXME: There is more per-file stuff we could just drop here?
410  if (CI.getFrontendOpts().DisableFree) {
411    CI.takeASTConsumer();
412    if (!isCurrentFileAST()) {
413      CI.takeSema();
414      CI.resetAndLeakASTContext();
415    }
416  } else {
417    if (!isCurrentFileAST()) {
418      CI.setSema(0);
419      CI.setASTContext(0);
420    }
421    CI.setASTConsumer(0);
422  }
423
424  // Inform the preprocessor we are done.
425  if (CI.hasPreprocessor())
426    CI.getPreprocessor().EndSourceFile();
427
428  if (CI.getFrontendOpts().ShowStats) {
429    llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
430    CI.getPreprocessor().PrintStats();
431    CI.getPreprocessor().getIdentifierTable().PrintStats();
432    CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
433    CI.getSourceManager().PrintStats();
434    llvm::errs() << "\n";
435  }
436
437  // Cleanup the output streams, and erase the output files if we encountered
438  // an error.
439  CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
440
441  if (isCurrentFileAST()) {
442    CI.takeSema();
443    CI.resetAndLeakASTContext();
444    CI.resetAndLeakPreprocessor();
445    CI.resetAndLeakSourceManager();
446    CI.resetAndLeakFileManager();
447  }
448
449  setCompilerInstance(0);
450  setCurrentInput(FrontendInputFile());
451}
452
453//===----------------------------------------------------------------------===//
454// Utility Actions
455//===----------------------------------------------------------------------===//
456
457void ASTFrontendAction::ExecuteAction() {
458  CompilerInstance &CI = getCompilerInstance();
459
460  // FIXME: Move the truncation aspect of this into Sema, we delayed this till
461  // here so the source manager would be initialized.
462  if (hasCodeCompletionSupport() &&
463      !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
464    CI.createCodeCompletionConsumer();
465
466  // Use a code completion consumer?
467  CodeCompleteConsumer *CompletionConsumer = 0;
468  if (CI.hasCodeCompletionConsumer())
469    CompletionConsumer = &CI.getCodeCompletionConsumer();
470
471  if (!CI.hasSema())
472    CI.createSema(getTranslationUnitKind(), CompletionConsumer);
473
474  ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
475           CI.getFrontendOpts().SkipFunctionBodies);
476}
477
478void PluginASTAction::anchor() { }
479
480ASTConsumer *
481PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
482                                              StringRef InFile) {
483  llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
484}
485
486ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
487                                                      StringRef InFile) {
488  return WrappedAction->CreateASTConsumer(CI, InFile);
489}
490bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
491  return WrappedAction->BeginInvocation(CI);
492}
493bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
494                                                  StringRef Filename) {
495  WrappedAction->setCurrentInput(getCurrentInput());
496  WrappedAction->setCompilerInstance(&CI);
497  return WrappedAction->BeginSourceFileAction(CI, Filename);
498}
499void WrapperFrontendAction::ExecuteAction() {
500  WrappedAction->ExecuteAction();
501}
502void WrapperFrontendAction::EndSourceFileAction() {
503  WrappedAction->EndSourceFileAction();
504}
505
506bool WrapperFrontendAction::usesPreprocessorOnly() const {
507  return WrappedAction->usesPreprocessorOnly();
508}
509TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
510  return WrappedAction->getTranslationUnitKind();
511}
512bool WrapperFrontendAction::hasPCHSupport() const {
513  return WrappedAction->hasPCHSupport();
514}
515bool WrapperFrontendAction::hasASTFileSupport() const {
516  return WrappedAction->hasASTFileSupport();
517}
518bool WrapperFrontendAction::hasIRSupport() const {
519  return WrappedAction->hasIRSupport();
520}
521bool WrapperFrontendAction::hasCodeCompletionSupport() const {
522  return WrappedAction->hasCodeCompletionSupport();
523}
524
525WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
526  : WrappedAction(WrappedAction) {}
527
528