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