blob: 66bec7c4aef1cf852b2a57f104ce2db2506ae9d6 [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 Dunbard3598a62010-06-07 23:23:06 +000043 bool IsAST = InputKind == IK_AST;
Daniel Dunbar4ee24092009-11-14 10:42:35 +000044 if (IsAST) {
45 assert(!usesPreprocessorOnly() &&
46 "Attempt to pass AST file to preprocessor only action!");
47 assert(hasASTSupport() && "This action does not have AST support!");
48
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
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 =
Chris Lattnera0a270c2010-04-05 22:42:27 +0000140 llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000141 CI.getSourceManager().createMainFileIDForMemBuffer(SB);
142 } else {
143 if (!CI.InitializeSourceManager(getCurrentFile()))
144 return;
145 }
146
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000147 if (CI.hasFrontendTimer()) {
148 llvm::TimeRegion Timer(CI.getFrontendTimer());
149 ExecuteAction();
150 }
151 else ExecuteAction();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000152}
153
154void FrontendAction::EndSourceFile() {
155 CompilerInstance &CI = getCompilerInstance();
156
157 // Finalize the action.
158 EndSourceFileAction();
159
160 // Release the consumer and the AST, in that order since the consumer may
161 // perform actions in its destructor which require the context.
162 //
163 // FIXME: There is more per-file stuff we could just drop here?
164 if (CI.getFrontendOpts().DisableFree) {
165 CI.takeASTConsumer();
166 if (!isCurrentFileAST())
167 CI.takeASTContext();
168 } else {
169 CI.setASTConsumer(0);
170 if (!isCurrentFileAST())
171 CI.setASTContext(0);
172 }
173
Daniel Dunbardbd82092010-03-23 05:09:10 +0000174 // Inform the preprocessor we are done.
175 if (CI.hasPreprocessor())
176 CI.getPreprocessor().EndSourceFile();
177
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000178 if (CI.getFrontendOpts().ShowStats) {
179 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
180 CI.getPreprocessor().PrintStats();
181 CI.getPreprocessor().getIdentifierTable().PrintStats();
182 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
183 CI.getSourceManager().PrintStats();
184 llvm::errs() << "\n";
185 }
186
187 // Cleanup the output streams, and erase the output files if we encountered
188 // an error.
Kovarththanan Rajaratname51dd7b2010-03-06 12:07:48 +0000189 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().getNumErrors());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000190
191 // Inform the diagnostic client we are done with this source file.
192 CI.getDiagnosticClient().EndSourceFile();
193
194 if (isCurrentFileAST()) {
195 CI.takeASTContext();
196 CI.takePreprocessor();
197 CI.takeSourceManager();
198 CI.takeFileManager();
199 }
200
201 setCompilerInstance(0);
202 setCurrentFile("");
203}
204
205//===----------------------------------------------------------------------===//
206// Utility Actions
207//===----------------------------------------------------------------------===//
208
209void ASTFrontendAction::ExecuteAction() {
210 CompilerInstance &CI = getCompilerInstance();
211
212 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
213 // here so the source manager would be initialized.
214 if (hasCodeCompletionSupport() &&
215 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
216 CI.createCodeCompletionConsumer();
217
218 // Use a code completion consumer?
219 CodeCompleteConsumer *CompletionConsumer = 0;
220 if (CI.hasCodeCompletionConsumer())
221 CompletionConsumer = &CI.getCodeCompletionConsumer();
222
223 ParseAST(CI.getPreprocessor(), &CI.getASTConsumer(), CI.getASTContext(),
224 CI.getFrontendOpts().ShowStats,
225 usesCompleteTranslationUnit(), CompletionConsumer);
226}
227
228ASTConsumer *
229PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
230 llvm::StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000231 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000232}