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