blob: 92137a6dc597651ca983836e38492fb12ae6bf61 [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
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +000024FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +000025
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,
Daniel Dunbard3598a62010-06-07 23:23:06 +000035 InputKind InputKind) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +000036 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.
Daniel Dunbar20560482010-06-07 23:23:50 +000043 if (InputKind == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +000044 assert(!usesPreprocessorOnly() &&
45 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +000046 assert(hasASTFileSupport() &&
47 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +000048
Douglas Gregor28019772010-04-05 23:52:57 +000049 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
Daniel Dunbar4ee24092009-11-14 10:42:35 +000050 std::string Error;
Douglas Gregor28019772010-04-05 23:52:57 +000051 ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, Diags);
Daniel Dunbar5262fda2009-12-03 01:45:44 +000052 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +000053 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +000054
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
Daniel Dunbar20560482010-06-07 23:23:50 +000076 // Setup the file and source managers, if needed, and the preprocessor.
77 if (!CI.hasFileManager())
78 CI.createFileManager();
79 if (!CI.hasSourceManager())
80 CI.createSourceManager();
81 CI.createPreprocessor();
82
Daniel Dunbar4ee24092009-11-14 10:42:35 +000083 // Inform the diagnostic client we are processing a source file.
84 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
85 &CI.getPreprocessor());
86
87 // Initialize the action.
88 if (!BeginSourceFileAction(CI, Filename))
89 goto failure;
90
91 /// Create the AST context and consumer unless this is a preprocessor only
92 /// action.
93 if (!usesPreprocessorOnly()) {
94 CI.createASTContext();
95 CI.setASTConsumer(CreateASTConsumer(CI, Filename));
96 if (!CI.hasASTConsumer())
97 goto failure;
98
99 /// Use PCH?
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000100 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000101 assert(hasPCHSupport() && "This action does not have PCH support!");
102 CI.createPCHExternalASTSource(
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000103 CI.getPreprocessorOpts().ImplicitPCHInclude);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000104 if (!CI.getASTContext().getExternalSource())
105 goto failure;
106 }
107 }
108
109 // Initialize builtin info as long as we aren't using an external AST
110 // source.
111 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
112 Preprocessor &PP = CI.getPreprocessor();
113 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
114 PP.getLangOptions().NoBuiltin);
115 }
116
117 return true;
118
119 // If we failed, reset state since the client will not end up calling the
120 // matching EndSourceFile().
121 failure:
122 if (isCurrentFileAST()) {
123 CI.takeASTContext();
124 CI.takePreprocessor();
125 CI.takeSourceManager();
126 CI.takeFileManager();
127 }
128
129 CI.getDiagnosticClient().EndSourceFile();
130 setCurrentFile("");
131 setCompilerInstance(0);
132 return false;
133}
134
135void FrontendAction::Execute() {
136 CompilerInstance &CI = getCompilerInstance();
137
138 // Initialize the main file entry. This needs to be delayed until after PCH
139 // has loaded.
140 if (isCurrentFileAST()) {
141 // Set the main file ID to an empty file.
142 //
143 // FIXME: We probably shouldn't need this, but for now this is the
144 // simplest way to reuse the logic in ParseAST.
145 const char *EmptyStr = "";
146 llvm::MemoryBuffer *SB =
Chris Lattnera0a270c2010-04-05 22:42:27 +0000147 llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000148 CI.getSourceManager().createMainFileIDForMemBuffer(SB);
149 } else {
150 if (!CI.InitializeSourceManager(getCurrentFile()))
151 return;
152 }
153
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000154 if (CI.hasFrontendTimer()) {
155 llvm::TimeRegion Timer(CI.getFrontendTimer());
156 ExecuteAction();
157 }
158 else ExecuteAction();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000159}
160
161void FrontendAction::EndSourceFile() {
162 CompilerInstance &CI = getCompilerInstance();
163
164 // Finalize the action.
165 EndSourceFileAction();
166
167 // Release the consumer and the AST, in that order since the consumer may
168 // perform actions in its destructor which require the context.
169 //
170 // FIXME: There is more per-file stuff we could just drop here?
171 if (CI.getFrontendOpts().DisableFree) {
172 CI.takeASTConsumer();
173 if (!isCurrentFileAST())
174 CI.takeASTContext();
175 } else {
176 CI.setASTConsumer(0);
177 if (!isCurrentFileAST())
178 CI.setASTContext(0);
179 }
180
Daniel Dunbardbd82092010-03-23 05:09:10 +0000181 // Inform the preprocessor we are done.
182 if (CI.hasPreprocessor())
183 CI.getPreprocessor().EndSourceFile();
184
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000185 if (CI.getFrontendOpts().ShowStats) {
186 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
187 CI.getPreprocessor().PrintStats();
188 CI.getPreprocessor().getIdentifierTable().PrintStats();
189 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
190 CI.getSourceManager().PrintStats();
191 llvm::errs() << "\n";
192 }
193
194 // Cleanup the output streams, and erase the output files if we encountered
195 // an error.
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000196 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().getNumErrors());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000197
198 // Inform the diagnostic client we are done with this source file.
199 CI.getDiagnosticClient().EndSourceFile();
200
201 if (isCurrentFileAST()) {
202 CI.takeASTContext();
203 CI.takePreprocessor();
204 CI.takeSourceManager();
205 CI.takeFileManager();
206 }
207
208 setCompilerInstance(0);
209 setCurrentFile("");
210}
211
212//===----------------------------------------------------------------------===//
213// Utility Actions
214//===----------------------------------------------------------------------===//
215
216void ASTFrontendAction::ExecuteAction() {
217 CompilerInstance &CI = getCompilerInstance();
218
219 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
220 // here so the source manager would be initialized.
221 if (hasCodeCompletionSupport() &&
222 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
223 CI.createCodeCompletionConsumer();
224
225 // Use a code completion consumer?
226 CodeCompleteConsumer *CompletionConsumer = 0;
227 if (CI.hasCodeCompletionConsumer())
228 CompletionConsumer = &CI.getCodeCompletionConsumer();
229
230 ParseAST(CI.getPreprocessor(), &CI.getASTConsumer(), CI.getASTContext(),
231 CI.getFrontendOpts().ShowStats,
232 usesCompleteTranslationUnit(), CompletionConsumer);
233}
234
235ASTConsumer *
236PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
237 llvm::StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000238 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000239}