-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAst.cpp
More file actions
510 lines (455 loc) · 16.5 KB
/
Ast.cpp
File metadata and controls
510 lines (455 loc) · 16.5 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include "Ast.h"
#include "TU.h"
#include "Rewrite.h"
#include "TypeDBEntry.h"
#include <iomanip>
#include <sstream>
using namespace clang_mutate;
using namespace clang;
const AstRef clang_mutate::NoAst;
const SourceOffset clang_mutate::BadOffset = 0xBAD0FF5E;
std::map<std::string, Ast::Field*> Ast::s_ast_fields;
TU & AstRef::tu() const
{ return *TUs[m_tu]; }
Ast * AstRef::operator->() const
{ return TUs[m_tu]->asts[m_index - 1]; }
Ast & AstRef::operator*() const
{ return *TUs[m_tu]->asts[m_index - 1]; }
std::string AstRef::to_string() const
{
std::ostringstream oss;
oss << m_tu << "." << m_index;
return oss.str();
}
template <typename T>
AstRef Ast::impl_create(T * clang_obj, Requirements & required)
{
TU & tu = *TUs[required.tu()];
AstRef ref = tu.nextAstRef();
AstRef parent = required.parent();
tu.asts.push_back(new Ast(clang_obj,
ref,
required.parent(),
required.syn_ctx(),
required.sourceRange(),
required.normalizedSourceRange(),
required.beginLoc(),
required.endLoc()));
ref->update_range_offsets(required.CI());
if (parent != NoAst)
parent->add_child(ref);
ref->setIncludes(required.includes());
ref->setMacros(required.macros());
ref->setTypes(required.types());
ref->setScopePosition(required.scopePos());
ref->setFreeVariables(required.variables());
ref->setFreeFunctions(required.functions());
ref->setReplacements(required.replacements());
ref->setIsFullStmt(Utils::is_full_stmt(clang_obj, parent, *required.CI()));
Stmt * parentStmt = parent == NoAst
? NULL
: parent->asStmt(*required.CI());
if (ref->isStmt()) {
bool has_compilation_data =
Utils::ShouldAssociateCompilationDataWithStmt(
ref->asStmt(*required.CI()),
parentStmt);
ref->setCanHaveCompilationData(has_compilation_data);
Stmt *stmt = ref->asStmt(*required.CI());
if (isa<Expr>(stmt)) {
Expr *expr = static_cast<Expr *>(stmt);
QualType qt = expr->getType();
ref->setExprType(hash_type(qt, required.CI(),
required.astContext()));
}
}
else if (ref->isFunctionDecl()) {
ref->setCanHaveCompilationData(true);
}
else {
ref->setCanHaveCompilationData(false);
}
if (ref->isDecl() && isa<FieldDecl>(ref->asDecl(*required.CI()))) {
ref->setFieldDeclProperties(required.astContext(),
required.CI());
}
return ref;
}
template AstRef Ast::impl_create<Stmt>(Stmt * stmt, Requirements & reqs);
template AstRef Ast::impl_create<Decl>(Decl * stmt, Requirements & reqs);
std::string Ast::srcFilename() const
{ return m_counter.tu().filename; }
Utils::Optional<AddressRange>
Ast::binaryAddressRange() const
{
TU & tu = m_counter.tu();
LineRange lineRange(m_begin_loc.getLine(),
m_end_loc.getLine());
Utils::Optional<BinaryData> binaryData =
tu.addrMap.getCompilationData(srcFilename(),
lineRange);
return binaryData?
Utils::Optional<AddressRange>(binaryData.value().first) :
Utils::Optional<AddressRange>();
}
Utils::Optional<Bytes>
Ast::bytes() const
{
TU & tu = m_counter.tu();
LineRange lineRange(m_begin_loc.getLine(),
m_end_loc.getLine());
Utils::Optional<BinaryData> binaryData =
tu.addrMap.getCompilationData(srcFilename(),
lineRange);
return binaryData?
Utils::Optional<Bytes>(binaryData.value().second) :
Utils::Optional<Bytes>();
}
Utils::Optional<Instructions>
Ast::llvm_ir() const
{
TU & tu = m_counter.tu();
LineRange lineRange(m_begin_loc.getLine(),
m_end_loc.getLine());
return tu.llvmInstrMap.getCompilationData(srcFilename(),
lineRange);
}
void Ast::setFieldDeclProperties(ASTContext * context,
CompilerInstance * ci)
{
FieldDecl *D = static_cast<FieldDecl *>(asDecl(*ci));
m_field_decl = true;
QualType Type = D->getType();
if (Type->isArrayType()) {
const ArrayType *AT = context->getAsArrayType(Type);
m_base_type = AT->getElementType().getAsString();
if (context->getAsConstantArrayType(Type))
m_array_length = context->getConstantArrayElementCount(
context->getAsConstantArrayType(Type));
}
else {
m_base_type = Type.getAsString();
}
if (D->isBitField()) {
m_bit_field = true;
m_bit_field_width = D->getBitWidthValue(*context);
}
}
std::pair<AstRef, AstRef> Ast::stmt_range() const
{
return std::make_pair(
counter(),
m_children.empty()
? counter()
: m_children.back()->stmt_range().second);
}
bool Ast::is_ancestor_of(AstRef ast) const
{
while (ast != NoAst) {
if (ast == counter())
return true;
ast = ast->parent();
}
return false;
}
// Register structs representing all of the AST fields.
//
#define FIELD_DEF(Name, T, Descr, Predicate, Body) \
struct Name##_field : public Ast::Field \
{ \
static std::string name() { return #Name; } \
virtual bool has_field(TU & tu, Ast & ast) \
{ return Predicate; } \
static T get(TU & tu, Ast & ast) Body \
virtual picojson::value to_json(TU & tu, Ast & ast) \
{ return ::to_json(get(tu, ast)); } \
virtual std::vector<std::string> purpose() \
{ return Utils::split(Descr, '\n'); } \
};
#include "FieldDefs.cxx"
std::map<std::string, Ast::Field*> & Ast::ast_fields()
{
if (s_ast_fields.empty()) {
#define FIELD_DEF(Name, T, Descr, Predicate, Body) \
s_ast_fields[Name##_field::name()] = new Name##_field();
#include "FieldDefs.cxx"
}
return s_ast_fields;
}
picojson::value Ast::toJSON(
const std::set<std::string> & keys, bool include_aux) const
{
TU & tu = m_counter.tu();
Ast & ast = *m_counter;
std::map<std::string, picojson::value> ans;
for (auto & field : ast_fields()) {
if (!keys.empty() && keys.find(field.first) == keys.end())
continue;
if (field.second->has_field(tu, ast)) {
ans[field.first] = field.second->to_json(tu, ast);
}
}
if (include_aux) {
picojson::value aux = to_json(m_aux);
assert(aux.is<picojson::object>());
for (auto & field : aux.get<picojson::object>()) {
ans[field.first] = field.second;
}
}
return to_json(ans);
}
bool Ast::has_bytes() const
{
return canHaveCompilationData()
&& binaryAddressRange();
}
bool Ast::has_llvm_ir() const
{
return canHaveCompilationData()
&& llvm_ir();
}
SourceLocation findEndOfToken(CompilerInstance * ci, SourceLocation loc,
AstRef parent)
{
SourceManager & sm = ci->getSourceManager();
SourceLocation end_of_token =
Lexer::getLocForEndOfToken(loc, 0, sm, ci->getLangOpts());
// This is tricky, due to macros.
// If the AST is within a macro expansion, then the above call may return
// an invalid location. In that case, we can use try to use the range of
// the parent. Often the top-level AST in a macro expansion will end at a
// location outside the macro (e.g. if there's a semicolon afteward), in
// which case all is well.
// But when that's not the case, we eventually reach a parent that isn't
// part of the macro expansion. Its range may extend too far, so we don't
// want to use that. Instead, call getFileLoc() to translate our macro
// location to a file location, and then get the end of the token. This
// should always give us a valid location.
if (end_of_token.isInvalid() && (parent == NoAst || !parent->inMacroExpansion()))
return Lexer::getLocForEndOfToken(sm.getFileLoc(loc),
0, sm, ci->getLangOpts());
else
return end_of_token;
}
void Ast::update_range_offsets(CompilerInstance * ci)
{
SourceManager & sm = ci->getSourceManager();
FileID mainFileID = sm.getMainFileID();
SourceRange sr = sm.getExpansionRange(m_range);
SourceRange nsr = sm.getExpansionRange(m_normalized_range);
std::pair<FileID, unsigned> decomp;
// Apparently clang reports different source ranges for global Var
// declarations when compared to local Var decls. This is supposed
// to correct for the difference.
int endShift = parent() == NoAst ? 0 : 1;
decomp = sm.getDecomposedExpansionLoc(sr.getBegin());
m_start_off
= decomp.first == mainFileID ? decomp.second
: parent() == NoAst ? BadOffset
: parent()->initial_offset();
decomp = sm.getDecomposedExpansionLoc(
findEndOfToken(ci, sr.getEnd(), parent()));
m_end_off
= decomp.first == mainFileID ? decomp.second - endShift
: parent() == NoAst ? BadOffset
: parent()->final_offset();
decomp = sm.getDecomposedExpansionLoc(nsr.getBegin());
m_norm_start_off
= decomp.first == mainFileID ? decomp.second
: parent() == NoAst ? BadOffset
: parent()->initial_normalized_offset();
decomp = sm.getDecomposedExpansionLoc(
findEndOfToken(ci, nsr.getEnd(), parent()));
m_norm_end_off
= decomp.first == mainFileID ? decomp.second - endShift
: parent() == NoAst ? BadOffset
: parent()->final_normalized_offset();
// Make any required adjustments to the AST offsets.
// If this is a Var and our most recent sibling is too, make sure that
// our ranges don't overlap. If they do, cut this one short.
if (className() == "Var") {
TU & tu = counter().tu();
AstRef prev = NoAst;
if (parent() == NoAst && counter().counter() >= 2) {
prev = tu.asts[counter().counter() - 2]->counter();
while (prev->parent() != NoAst)
prev = prev->parent();
if (prev->className() != "Var" || prev->isFullStmt())
prev = NoAst;
}
else if (parent() != NoAst &&
parent()->className() == "DeclStmt" &&
!parent()->children().empty())
{
prev = parent()->children().back();
}
if (prev != NoAst) {
prev->setSyntacticContext(SyntacticContext::ListElt());
setSyntacticContext(SyntacticContext::FinalListElt());
}
if (prev != NoAst &&
initial_offset() <= prev->final_normalized_offset())
{
// Scan over whitespace, and optionally a comma and some more
// whitespace.
// TODO: handle comments etc. This should use clang's parsing,
// but there will still be corner cases to handle
// (e.g. the , comes from an expanded macro)
SourceOffset offset = prev->final_normalized_offset() + 1;
StringRef buf = sm.getBufferData(sm.getMainFileID());
while (isspace(buf.data()[offset]) && offset < m_end_off) {
++offset;
}
if (buf.data()[offset] == ',') {
// If we do hit a comma, extend the previous Var's
// normalized range to include it.
prev->m_norm_end_off = offset;
++offset;
while (isspace(buf.data()[offset]) &&
offset < m_end_off &&
offset < (SourceOffset) buf.size())
{
++offset;
}
}
if (offset >= m_end_off)
offset = prev->final_normalized_offset() + 1;
m_norm_start_off = offset;
m_start_off = prev->inMacroExpansion() ?
m_start_off : offset;
}
}
if (className() == "ParmVar" &&
!parent()->children().empty() &&
parent()->children().back()->className() == "ParmVar")
{
// We are a parameter, but not the first one. Extend our sibling's
// normalized range forward until it reaches a comma.
AstRef prev = parent()->children().back();
prev->setSyntacticContext(SyntacticContext::ListElt());
setSyntacticContext(SyntacticContext::FinalListElt());
SourceOffset offset = prev->final_normalized_offset();
StringRef buf = sm.getBufferData(sm.getMainFileID());
while (buf.data()[offset] != ',' && offset < (SourceOffset) buf.size())
++offset;
prev->m_norm_end_off = offset;
}
}
void Ast::expand_to_child_ranges()
{
// In some cases, AST's source range will be smaller than children. This
// seems to be a bug in clang and will cause weird source text and other
// problems. Expand ranges so they are at least as large as child ranges.
for (AstRef c : children()) {
if (c->m_start_off < this->m_start_off)
this->m_start_off = c->m_start_off;
if (c->m_norm_start_off < this->m_norm_start_off)
this->m_norm_start_off = c->m_norm_start_off;
if (c->m_end_off > this->m_end_off)
this->m_end_off = c->m_end_off;
if (c->m_norm_end_off > this->m_norm_end_off)
this->m_norm_end_off = c->m_norm_end_off;
}
}
Ast::Ast(Stmt * _stmt,
AstRef _counter,
AstRef _parent,
SyntacticContext syn_ctx,
SourceRange r,
SourceRange nr,
PresumedLoc pBegin,
PresumedLoc pEnd)
: m_stmt(_stmt)
, m_decl(NULL)
, m_counter(_counter)
, m_parent(_parent)
, m_children()
, m_class(std::string(_stmt->getStmtClassName()))
, m_range(r)
, m_normalized_range(nr)
, m_begin_loc(pBegin)
, m_end_loc(pEnd)
, m_declares()
, m_guard(false)
, m_expr_type(0)
, m_scope_pos(NoNode)
, m_macros()
, m_free_vars()
, m_free_funs()
, m_opcode("")
, m_full_stmt(false)
, m_in_macro_expansion(false)
, m_syn_ctx(syn_ctx)
, m_can_have_compilation_data(false)
, m_replacements()
, m_field_decl(false)
, m_base_type()
, m_bit_field(false)
, m_bit_field_width(0)
, m_array_length(0)
, m_label_name()
, m_is_member_expr(false)
{
if (isa<BinaryOperator>(m_stmt)) {
m_opcode = static_cast<BinaryOperator*>(m_stmt)
->getOpcodeStr().str();
}
if (isa<UnaryOperator>(m_stmt)) {
m_opcode = static_cast<UnaryOperator*>(m_stmt)
->getOpcodeStr(static_cast<UnaryOperator*>(m_stmt)->getOpcode()).str();
}
if (isa<MemberExpr>(m_stmt)) {
m_label_name = static_cast<MemberExpr*>(m_stmt)
->getMemberDecl()->getNameAsString();
m_is_member_expr = true;
}
}
Ast::Ast(Decl * _decl,
AstRef _counter,
AstRef _parent,
SyntacticContext syn_ctx,
SourceRange r,
SourceRange nr,
PresumedLoc pBegin,
PresumedLoc pEnd)
: m_stmt(NULL)
, m_decl(_decl)
, m_counter(_counter)
, m_parent(_parent)
, m_children()
, m_class(std::string(_decl->getDeclKindName()))
, m_range(r)
, m_normalized_range(nr)
, m_begin_loc(pBegin)
, m_end_loc(pEnd)
, m_declares()
, m_guard(false)
, m_expr_type(0)
, m_scope_pos(NoNode)
, m_macros()
, m_free_vars()
, m_free_funs()
, m_opcode("")
, m_full_stmt(false)
, m_in_macro_expansion(false)
, m_syn_ctx(syn_ctx)
, m_can_have_compilation_data(false)
, m_replacements()
, m_field_decl(false)
, m_base_type()
, m_bit_field(false)
, m_bit_field_width(0)
, m_array_length(0)
, m_label_name()
, m_is_member_expr(false)
{
// clang seems to give us the wrong source ranges for FieldDecls.
// Work around this by setting the range to the normalized range, which
// will grab everything up to (but not including) the semicolon.
if (isa<FieldDecl>(_decl))
m_range = m_normalized_range;
//Get annotations from the declaration
for (const auto *I : _decl->specific_attrs<AnnotateAttr>()) {
m_annotations.push_back(I->getAnnotation());
}
}