FrontendAction.cpp revision 212904
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/Lex/HeaderSearch.h"
14#include "clang/Lex/Preprocessor.h"
15#include "clang/Frontend/ASTUnit.h"
16#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Parse/ParseAST.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/Timer.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
25FrontendAction::FrontendAction() : Instance(0) {}
26
27FrontendAction::~FrontendAction() {}
28
29void FrontendAction::setCurrentFile(llvm::StringRef Value, InputKind Kind,
30                                    ASTUnit *AST) {
31  CurrentFile = Value;
32  CurrentFileKind = Kind;
33  CurrentASTUnit.reset(AST);
34}
35
36bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
37                                     llvm::StringRef Filename,
38                                     InputKind InputKind) {
39  assert(!Instance && "Already processing a source file!");
40  assert(!Filename.empty() && "Unexpected empty filename!");
41  setCurrentFile(Filename, InputKind);
42  setCompilerInstance(&CI);
43
44  // AST files follow a very different path, since they share objects via the
45  // AST unit.
46  if (InputKind == IK_AST) {
47    assert(!usesPreprocessorOnly() &&
48           "Attempt to pass AST file to preprocessor only action!");
49    assert(hasASTFileSupport() &&
50           "This action does not have AST file support!");
51
52    llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
53    std::string Error;
54    ASTUnit *AST = ASTUnit::LoadFromASTFile(Filename, Diags);
55    if (!AST)
56      goto failure;
57
58    setCurrentFile(Filename, InputKind, AST);
59
60    // Set the shared objects, these are reset when we finish processing the
61    // file, otherwise the CompilerInstance will happily destroy them.
62    CI.setFileManager(&AST->getFileManager());
63    CI.setSourceManager(&AST->getSourceManager());
64    CI.setPreprocessor(&AST->getPreprocessor());
65    CI.setASTContext(&AST->getASTContext());
66
67    // Initialize the action.
68    if (!BeginSourceFileAction(CI, Filename))
69      goto failure;
70
71    /// Create the AST consumer.
72    CI.setASTConsumer(CreateASTConsumer(CI, Filename));
73    if (!CI.hasASTConsumer())
74      goto failure;
75
76    return true;
77  }
78
79  // Set up the file and source managers, if needed.
80  if (!CI.hasFileManager())
81    CI.createFileManager();
82  if (!CI.hasSourceManager())
83    CI.createSourceManager();
84
85  // IR files bypass the rest of initialization.
86  if (InputKind == IK_LLVM_IR) {
87    assert(hasIRSupport() &&
88           "This action does not have IR file support!");
89
90    // Inform the diagnostic client we are processing a source file.
91    CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
92
93    // Initialize the action.
94    if (!BeginSourceFileAction(CI, Filename))
95      goto failure;
96
97    return true;
98  }
99
100  // Set up the preprocessor.
101  CI.createPreprocessor();
102
103  // Inform the diagnostic client we are processing a source file.
104  CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
105                                           &CI.getPreprocessor());
106
107  // Initialize the action.
108  if (!BeginSourceFileAction(CI, Filename))
109    goto failure;
110
111  /// Create the AST context and consumer unless this is a preprocessor only
112  /// action.
113  if (!usesPreprocessorOnly()) {
114    CI.createASTContext();
115
116    llvm::OwningPtr<ASTConsumer> Consumer(CreateASTConsumer(CI, Filename));
117
118    /// Use PCH?
119    if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
120      assert(hasPCHSupport() && "This action does not have PCH support!");
121      CI.createPCHExternalASTSource(
122                                CI.getPreprocessorOpts().ImplicitPCHInclude,
123                                CI.getPreprocessorOpts().DisablePCHValidation,
124                                CI.getInvocation().getFrontendOpts().ChainedPCH?
125                                 Consumer->GetASTDeserializationListener() : 0);
126      if (!CI.getASTContext().getExternalSource())
127        goto failure;
128    }
129
130    CI.setASTConsumer(Consumer.take());
131    if (!CI.hasASTConsumer())
132      goto failure;
133  }
134
135  // Initialize builtin info as long as we aren't using an external AST
136  // source.
137  if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
138    Preprocessor &PP = CI.getPreprocessor();
139    PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
140                                           PP.getLangOptions().NoBuiltin);
141  }
142
143  return true;
144
145  // If we failed, reset state since the client will not end up calling the
146  // matching EndSourceFile().
147  failure:
148  if (isCurrentFileAST()) {
149    CI.takeASTContext();
150    CI.takePreprocessor();
151    CI.takeSourceManager();
152    CI.takeFileManager();
153  }
154
155  CI.getDiagnosticClient().EndSourceFile();
156  setCurrentFile("", IK_None);
157  setCompilerInstance(0);
158  return false;
159}
160
161void FrontendAction::Execute() {
162  CompilerInstance &CI = getCompilerInstance();
163
164  // Initialize the main file entry. This needs to be delayed until after PCH
165  // has loaded.
166  if (isCurrentFileAST()) {
167    // Set the main file ID to an empty file.
168    //
169    // FIXME: We probably shouldn't need this, but for now this is the
170    // simplest way to reuse the logic in ParseAST.
171    const char *EmptyStr = "";
172    llvm::MemoryBuffer *SB =
173      llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>");
174    CI.getSourceManager().createMainFileIDForMemBuffer(SB);
175  } else {
176    if (!CI.InitializeSourceManager(getCurrentFile()))
177      return;
178  }
179
180  if (CI.hasFrontendTimer()) {
181    llvm::TimeRegion Timer(CI.getFrontendTimer());
182    ExecuteAction();
183  }
184  else ExecuteAction();
185}
186
187void FrontendAction::EndSourceFile() {
188  CompilerInstance &CI = getCompilerInstance();
189
190  // Finalize the action.
191  EndSourceFileAction();
192
193  // Release the consumer and the AST, in that order since the consumer may
194  // perform actions in its destructor which require the context.
195  //
196  // FIXME: There is more per-file stuff we could just drop here?
197  if (CI.getFrontendOpts().DisableFree) {
198    CI.takeASTConsumer();
199    if (!isCurrentFileAST()) {
200      CI.takeSema();
201      CI.takeASTContext();
202    }
203  } else {
204    if (!isCurrentFileAST()) {
205      CI.setSema(0);
206      CI.setASTContext(0);
207    }
208    CI.setASTConsumer(0);
209  }
210
211  // Inform the preprocessor we are done.
212  if (CI.hasPreprocessor())
213    CI.getPreprocessor().EndSourceFile();
214
215  if (CI.getFrontendOpts().ShowStats) {
216    llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
217    CI.getPreprocessor().PrintStats();
218    CI.getPreprocessor().getIdentifierTable().PrintStats();
219    CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
220    CI.getSourceManager().PrintStats();
221    llvm::errs() << "\n";
222  }
223
224  // Cleanup the output streams, and erase the output files if we encountered
225  // an error.
226  CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().getNumErrors());
227
228  // Inform the diagnostic client we are done with this source file.
229  CI.getDiagnosticClient().EndSourceFile();
230
231  if (isCurrentFileAST()) {
232    CI.takeSema();
233    CI.takeASTContext();
234    CI.takePreprocessor();
235    CI.takeSourceManager();
236    CI.takeFileManager();
237  }
238
239  setCompilerInstance(0);
240  setCurrentFile("", IK_None);
241}
242
243//===----------------------------------------------------------------------===//
244// Utility Actions
245//===----------------------------------------------------------------------===//
246
247void ASTFrontendAction::ExecuteAction() {
248  CompilerInstance &CI = getCompilerInstance();
249
250  // FIXME: Move the truncation aspect of this into Sema, we delayed this till
251  // here so the source manager would be initialized.
252  if (hasCodeCompletionSupport() &&
253      !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
254    CI.createCodeCompletionConsumer();
255
256  // Use a code completion consumer?
257  CodeCompleteConsumer *CompletionConsumer = 0;
258  if (CI.hasCodeCompletionConsumer())
259    CompletionConsumer = &CI.getCodeCompletionConsumer();
260
261  if (!CI.hasSema())
262    CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer);
263
264  ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats);
265}
266
267ASTConsumer *
268PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
269                                              llvm::StringRef InFile) {
270  llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
271}
272