blob: ff63a0dab5f90799da57c8eb3c61e35c94c03f92 [file] [log] [blame]
Daniel Dunbar4ee24092009-11-14 10:42:35 +00001//===--- 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/ASTContext.h"
12#include "clang/Lex/HeaderSearch.h"
13#include "clang/Lex/Preprocessor.h"
14#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/FrontendDiagnostic.h"
17#include "clang/Sema/ParseAST.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/Timer.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/raw_ostream.h"
22using namespace clang;
23
24FrontendAction::FrontendAction() : Instance(0), CurrentTimer(0) {}
25
26FrontendAction::~FrontendAction() {}
27
28void FrontendAction::setCurrentFile(llvm::StringRef Value, ASTUnit *AST) {
29 CurrentFile = Value;
30 CurrentASTUnit.reset(AST);
31}
32
33bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
34 llvm::StringRef Filename,
35 bool IsAST) {
36 assert(!Instance && "Already processing a source file!");
37 assert(!Filename.empty() && "Unexpected empty filename!");
38 setCurrentFile(Filename);
39 setCompilerInstance(&CI);
40
41 // AST files follow a very different path, since they share objects via the
42 // AST unit.
43 if (IsAST) {
44 assert(!usesPreprocessorOnly() &&
45 "Attempt to pass AST file to preprocessor only action!");
46 assert(hasASTSupport() && "This action does not have AST support!");
47
48 std::string Error;
49 ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, &Error);
50 if (!AST) {
51 CI.getDiagnostics().Report(diag::err_fe_invalid_ast_file) << Error;
52 goto failure;
53 }
54
55 setCurrentFile(Filename, AST);
56
57 // Set the shared objects, these are reset when we finish processing the
58 // file, otherwise the CompilerInstance will happily destroy them.
59 CI.setFileManager(&AST->getFileManager());
60 CI.setSourceManager(&AST->getSourceManager());
61 CI.setPreprocessor(&AST->getPreprocessor());
62 CI.setASTContext(&AST->getASTContext());
63
64 // Initialize the action.
65 if (!BeginSourceFileAction(CI, Filename))
66 goto failure;
67
68 /// Create the AST consumer.
69 CI.setASTConsumer(CreateASTConsumer(CI, Filename));
70 if (!CI.hasASTConsumer())
71 goto failure;
72
73 return true;
74 }
75
76 // Inform the diagnostic client we are processing a source file.
77 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
78 &CI.getPreprocessor());
79
80 // Initialize the action.
81 if (!BeginSourceFileAction(CI, Filename))
82 goto failure;
83
84 /// Create the AST context and consumer unless this is a preprocessor only
85 /// action.
86 if (!usesPreprocessorOnly()) {
87 CI.createASTContext();
88 CI.setASTConsumer(CreateASTConsumer(CI, Filename));
89 if (!CI.hasASTConsumer())
90 goto failure;
91
92 /// Use PCH?
Daniel Dunbar049d3a02009-11-17 05:52:41 +000093 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +000094 assert(hasPCHSupport() && "This action does not have PCH support!");
95 CI.createPCHExternalASTSource(
Daniel Dunbar049d3a02009-11-17 05:52:41 +000096 CI.getPreprocessorOpts().ImplicitPCHInclude);
Daniel Dunbar4ee24092009-11-14 10:42:35 +000097 if (!CI.getASTContext().getExternalSource())
98 goto failure;
99 }
100 }
101
102 // Initialize builtin info as long as we aren't using an external AST
103 // source.
104 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
105 Preprocessor &PP = CI.getPreprocessor();
106 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
107 PP.getLangOptions().NoBuiltin);
108 }
109
110 return true;
111
112 // If we failed, reset state since the client will not end up calling the
113 // matching EndSourceFile().
114 failure:
115 if (isCurrentFileAST()) {
116 CI.takeASTContext();
117 CI.takePreprocessor();
118 CI.takeSourceManager();
119 CI.takeFileManager();
120 }
121
122 CI.getDiagnosticClient().EndSourceFile();
123 setCurrentFile("");
124 setCompilerInstance(0);
125 return false;
126}
127
128void FrontendAction::Execute() {
129 CompilerInstance &CI = getCompilerInstance();
130
131 // Initialize the main file entry. This needs to be delayed until after PCH
132 // has loaded.
133 if (isCurrentFileAST()) {
134 // Set the main file ID to an empty file.
135 //
136 // FIXME: We probably shouldn't need this, but for now this is the
137 // simplest way to reuse the logic in ParseAST.
138 const char *EmptyStr = "";
139 llvm::MemoryBuffer *SB =
140 llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<dummy input>");
141 CI.getSourceManager().createMainFileIDForMemBuffer(SB);
142 } else {
143 if (!CI.InitializeSourceManager(getCurrentFile()))
144 return;
145 }
146
147 llvm::TimeRegion Timer(CurrentTimer);
148 ExecuteAction();
149}
150
151void FrontendAction::EndSourceFile() {
152 CompilerInstance &CI = getCompilerInstance();
153
154 // Finalize the action.
155 EndSourceFileAction();
156
157 // Release the consumer and the AST, in that order since the consumer may
158 // perform actions in its destructor which require the context.
159 //
160 // FIXME: There is more per-file stuff we could just drop here?
161 if (CI.getFrontendOpts().DisableFree) {
162 CI.takeASTConsumer();
163 if (!isCurrentFileAST())
164 CI.takeASTContext();
165 } else {
166 CI.setASTConsumer(0);
167 if (!isCurrentFileAST())
168 CI.setASTContext(0);
169 }
170
171 if (CI.getFrontendOpts().ShowStats) {
172 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
173 CI.getPreprocessor().PrintStats();
174 CI.getPreprocessor().getIdentifierTable().PrintStats();
175 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
176 CI.getSourceManager().PrintStats();
177 llvm::errs() << "\n";
178 }
179
180 // Cleanup the output streams, and erase the output files if we encountered
181 // an error.
182 CI.ClearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().getNumErrors());
183
184 // Inform the diagnostic client we are done with this source file.
185 CI.getDiagnosticClient().EndSourceFile();
186
187 if (isCurrentFileAST()) {
188 CI.takeASTContext();
189 CI.takePreprocessor();
190 CI.takeSourceManager();
191 CI.takeFileManager();
192 }
193
194 setCompilerInstance(0);
195 setCurrentFile("");
196}
197
198//===----------------------------------------------------------------------===//
199// Utility Actions
200//===----------------------------------------------------------------------===//
201
202void ASTFrontendAction::ExecuteAction() {
203 CompilerInstance &CI = getCompilerInstance();
204
205 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
206 // here so the source manager would be initialized.
207 if (hasCodeCompletionSupport() &&
208 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
209 CI.createCodeCompletionConsumer();
210
211 // Use a code completion consumer?
212 CodeCompleteConsumer *CompletionConsumer = 0;
213 if (CI.hasCodeCompletionConsumer())
214 CompletionConsumer = &CI.getCodeCompletionConsumer();
215
216 ParseAST(CI.getPreprocessor(), &CI.getASTConsumer(), CI.getASTContext(),
217 CI.getFrontendOpts().ShowStats,
218 usesCompleteTranslationUnit(), CompletionConsumer);
219}
220
221ASTConsumer *
222PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
223 llvm::StringRef InFile) {
224 llvm::llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
225}