-
Notifications
You must be signed in to change notification settings - Fork 10
/
Parser.cpp
369 lines (327 loc) · 10.8 KB
/
Parser.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// Parser is used to invoke the clang libraries to perform actual parsing of
// the input received in the Console.
//
// Part of ccons, the interactive console for the C programming language.
//
// Copyright (c) 2009 Alexei Svitkine. This file is distributed under the
// terms of MIT Open Source License. See file LICENSE for details.
//
#include "Parser.h"
#include <iostream>
#include <stack>
#include <algorithm>
#include <llvm/Config/config.h>
#include <clang/AST/AST.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Basic/FileSystemOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Frontend/FrontendOptions.h>
#include <clang/Frontend/Utils.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/Lex/LexDiagnostic.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Parse/ParseAST.h>
#include <clang/Sema/SemaDiagnostic.h>
#include "Diagnostics.h"
#include "SrcGen.h"
using std::string;
namespace ccons {
//
// ParseOperation
//
ParseOperation::ParseOperation(const clang::LangOptions& options,
clang::TargetOptions* targetOptions,
clang::DiagnosticsEngine *diag,
clang::PPCallbacks *callbacks) :
_langOpts(options),
_hsOptions(new clang::HeaderSearchOptions),
_ppOptions(new clang::PreprocessorOptions),
_fsOpts(new clang::FileSystemOptions),
_fm(new clang::FileManager(*_fsOpts)),
_sm(new clang::SourceManager(*diag, *_fm))
{
_target.reset(clang::TargetInfo::CreateTargetInfo(*diag, new clang::TargetOptions(*targetOptions)));
_hs.reset(new clang::HeaderSearch(_hsOptions, *_fm, *diag, options, &*_target));
ApplyHeaderSearchOptions(*_hs, *_hsOptions, options, llvm::Triple(targetOptions->Triple));
_pp.reset(new clang::Preprocessor(_ppOptions, *diag, _langOpts, &*_target, *_sm, *_hs, *this));
_pp->addPPCallbacks(callbacks);
clang::FrontendOptions frontendOptions;
InitializePreprocessor(*_pp, *_ppOptions, *_hsOptions, frontendOptions);
_ast.reset(new clang::ASTContext(_langOpts,
*_sm,
&*_target,
_pp->getIdentifierTable(),
_pp->getSelectorTable(),
_pp->getBuiltinInfo(),
0));
}
ParseOperation::~ParseOperation()
{
}
clang::ASTContext * ParseOperation::getASTContext() const
{
return _ast.get();
}
clang::Preprocessor * ParseOperation::getPreprocessor() const
{
return _pp.get();
}
clang::SourceManager * ParseOperation::getSourceManager() const
{
return _sm.get();
}
clang::TargetInfo * ParseOperation::getTargetInfo() const
{
return _target.get();
}
clang::ModuleLoadResult ParseOperation::loadModule(clang::SourceLocation ImportLoc,
clang::ModuleIdPath Path,
clang::Module::NameVisibilityKind Visibility,
bool IsInclusionDirective)
{
return clang::ModuleLoadResult();
}
void ParseOperation::makeModuleVisible(clang::Module *Mod,
clang::Module::NameVisibilityKind Visibility,
clang::SourceLocation ImportLoc,
bool Complain) {
}
//
// Parser
//
Parser::Parser(const clang::LangOptions& options,
clang::TargetOptions* targetOptions) :
_options(options),
_targetOptions(targetOptions)
{
}
Parser::~Parser()
{
releaseAccumulatedParseOperations();
}
void Parser::releaseAccumulatedParseOperations()
{
for (std::vector<ParseOperation*>::iterator I = _ops.begin(), E = _ops.end();
I != E; ++I) {
delete *I;
}
_ops.clear();
}
ParseOperation * Parser::getLastParseOperation() const
{
return _ops.empty() ? NULL : _ops.back();
}
Parser::InputType Parser::analyzeInput(const string& contextSource,
const string& buffer,
int& indentLevel,
std::vector<clang::FunctionDecl*> *fds)
{
if (buffer.length() > 1 && buffer[buffer.length() - 2] == '\\') {
indentLevel = 1;
return Incomplete;
}
NullDiagnosticProvider ndp;
llvm::OwningPtr<ParseOperation>
parseOp(new ParseOperation(_options, _targetOptions, ndp.getDiagnosticsEngine()));
llvm::MemoryBuffer *memBuf =
createMemoryBuffer(buffer, "", parseOp->getSourceManager());
clang::Token LastTok;
LastTok.startToken();
bool TokWasDo = false;
int stackSize =
analyzeTokens(*parseOp->getPreprocessor(), memBuf, LastTok, indentLevel, TokWasDo);
if (stackSize < 0)
return TopLevel;
// TokWasDo is used for do { ... } while (...); loops
if (LastTok.is(clang::tok::semi) ||
(!stackSize && LastTok.is(clang::tok::unknown)) ||
(LastTok.is(clang::tok::r_brace) && !TokWasDo)) {
if (stackSize > 0) return Incomplete;
NullDiagnosticProvider ndp;
clang::DiagnosticsEngine& engine = *ndp.getDiagnosticsEngine();
// Setting this ensures "foo();" is not a valid top-level declaration.
engine.setDiagnosticMapping(clang::diag::ext_missing_type_specifier,
clang::diag::MAP_ERROR, clang::SourceLocation());
engine.setSuppressSystemWarnings(true);
string src = contextSource + buffer;
struct : public clang::ASTConsumer {
bool hadIncludedDecls;
unsigned pos;
unsigned maxPos;
clang::SourceManager *sm;
std::vector<clang::FunctionDecl*> fds;
bool HandleTopLevelDecl(clang::DeclGroupRef D) {
for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
if (clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I)) {
clang::SourceLocation Loc = FD->getTypeSpecStartLoc();
if (!Loc.isValid())
continue;
if (sm->isFromMainFile(Loc)) {
unsigned offset = sm->getFileOffset(sm->getExpansionLoc(Loc));
if (offset >= pos) {
fds.push_back(FD);
}
} else {
while (!sm->isFromMainFile(Loc)) {
const clang::SrcMgr::SLocEntry& Entry =
sm->getSLocEntry(sm->getFileID(sm->getSpellingLoc(Loc)));
if (!Entry.isFile())
break;
Loc = Entry.getFile().getIncludeLoc();
}
unsigned offset = sm->getFileOffset(Loc);
if (offset >= pos) {
hadIncludedDecls = true;
}
}
}
}
return true;
}
} consumer;
ParseOperation *parseOp = createParseOperation(&engine);
consumer.hadIncludedDecls = false;
consumer.pos = contextSource.length();
consumer.maxPos = consumer.pos + buffer.length();
consumer.sm = parseOp->getSourceManager();
parse(src, parseOp, &consumer);
ProxyDiagnosticConsumer *pdc = ndp.getProxyDiagnosticConsumer();
if (pdc->hadError(clang::diag::err_unterminated_block_comment))
return Incomplete;
if (!pdc->hadErrors() && (!consumer.fds.empty() || consumer.hadIncludedDecls)) {
if (!consumer.fds.empty())
fds->swap(consumer.fds);
return TopLevel;
}
return Stmt;
}
return Incomplete;
}
int Parser::analyzeTokens(clang::Preprocessor& PP,
const llvm::MemoryBuffer *MemBuf,
clang::Token& LastTok,
int& IndentLevel,
bool& TokWasDo)
{
int result;
std::stack<std::pair<clang::Token, clang::Token> > S; // Tok, PrevTok
IndentLevel = 0;
PP.EnterMainSourceFile();
clang::Token Tok;
PP.Lex(Tok);
while (Tok.isNot(clang::tok::eof)) {
if (Tok.is(clang::tok::l_square)) {
S.push(std::make_pair(Tok, LastTok)); // [
} else if (Tok.is(clang::tok::l_paren)) {
S.push(std::make_pair(Tok, LastTok)); // (
} else if (Tok.is(clang::tok::l_brace)) {
S.push(std::make_pair(Tok, LastTok)); // {
IndentLevel++;
} else if (Tok.is(clang::tok::r_square)) {
if (S.empty() || S.top().first.isNot(clang::tok::l_square)) {
std::cout << "Unmatched [\n";
return -1;
}
TokWasDo = false;
S.pop();
} else if (Tok.is(clang::tok::r_paren)) {
if (S.empty() || S.top().first.isNot(clang::tok::l_paren)) {
std::cout << "Unmatched (\n";
return -1;
}
TokWasDo = false;
S.pop();
} else if (Tok.is(clang::tok::r_brace)) {
if (S.empty() || S.top().first.isNot(clang::tok::l_brace)) {
std::cout << "Unmatched {\n";
return -1;
}
TokWasDo = S.top().second.is(clang::tok::kw_do);
S.pop();
IndentLevel--;
}
LastTok = Tok;
PP.Lex(Tok);
}
result = S.size();
// TODO: We need to properly account for indent-level for blocks that do not
// have braces... such as:
//
// if (X)
// Y;
//
// TODO: Do-while without braces doesn't work, e.g.:
//
// do
// foo();
// while (bar());
//
// Both of the above could be solved by some kind of rewriter-pass that would
// insert implicit braces (or simply a more involved analysis).
// Also try to match preprocessor conditionals...
if (result == 0) {
clang::Lexer Lexer(PP.getSourceManager().getMainFileID(),
MemBuf,
PP.getSourceManager(),
_options);
Lexer.LexFromRawLexer(Tok);
while (Tok.isNot(clang::tok::eof)) {
if (Tok.is(clang::tok::hash)) {
Lexer.LexFromRawLexer(Tok);
if (clang::IdentifierInfo *II = PP.LookUpIdentifierInfo(Tok)) {
switch (II->getPPKeywordID()) {
case clang::tok::pp_if:
case clang::tok::pp_ifdef:
case clang::tok::pp_ifndef:
result++;
break;
case clang::tok::pp_endif:
if (result == 0)
return -1; // Nesting error.
result--;
break;
default:
break;
}
}
}
Lexer.LexFromRawLexer(Tok);
}
}
return result;
}
ParseOperation * Parser::createParseOperation(clang::DiagnosticsEngine *engine,
clang::PPCallbacks *callbacks)
{
return new ParseOperation(_options, _targetOptions, engine, callbacks);
}
void Parser::parse(const string& src,
ParseOperation *parseOp,
clang::ASTConsumer *consumer)
{
_ops.push_back(parseOp);
createMemoryBuffer(src, "", parseOp->getSourceManager());
clang::ParseAST(*parseOp->getPreprocessor(), consumer,
*parseOp->getASTContext());
}
void Parser::parse(const string& src,
clang::DiagnosticsEngine *engine,
clang::ASTConsumer *consumer)
{
parse(src, createParseOperation(engine), consumer);
}
llvm::MemoryBuffer * Parser::createMemoryBuffer(const string& src,
const char *name,
clang::SourceManager *sm)
{
llvm::MemoryBuffer *mb =
llvm::MemoryBuffer::getMemBufferCopy(src, name);
assert(mb && "Error creating MemoryBuffer!");
sm->createMainFileIDForMemBuffer(mb);
assert(!sm->getMainFileID().isInvalid() && "Error creating MainFileID!");
return mb;
}
} // namespace ccons