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
|
//===-- ASTResultSynthesizer.cpp --------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "stdlib.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclGroup.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Stmt.h"
#include "clang/Parse/Action.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/Scope.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include "lldb/Core/Log.h"
#include "lldb/Expression/ASTResultSynthesizer.h"
using namespace llvm;
using namespace clang;
using namespace lldb_private;
ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough) :
m_ast_context (NULL),
m_passthrough (passthrough),
m_passthrough_sema (NULL),
m_sema (NULL),
m_action (NULL)
{
if (!m_passthrough)
return;
m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
}
ASTResultSynthesizer::~ASTResultSynthesizer()
{
}
void
ASTResultSynthesizer::Initialize(ASTContext &Context)
{
m_ast_context = &Context;
if (m_passthrough)
m_passthrough->Initialize(Context);
}
void
ASTResultSynthesizer::TransformTopLevelDecl(Decl* D)
{
LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
if (linkage_spec_decl)
{
RecordDecl::decl_iterator decl_iterator;
for (decl_iterator = linkage_spec_decl->decls_begin();
decl_iterator != linkage_spec_decl->decls_end();
++decl_iterator)
{
TransformTopLevelDecl(*decl_iterator);
}
}
FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
if (m_ast_context &&
function_decl &&
!strcmp(function_decl->getNameAsCString(),
"___clang_expr"))
{
SynthesizeResult(function_decl);
}
}
void
ASTResultSynthesizer::HandleTopLevelDecl(DeclGroupRef D)
{
DeclGroupRef::iterator decl_iterator;
for (decl_iterator = D.begin();
decl_iterator != D.end();
++decl_iterator)
{
Decl *decl = *decl_iterator;
TransformTopLevelDecl(decl);
}
if (m_passthrough)
m_passthrough->HandleTopLevelDecl(D);
}
bool
ASTResultSynthesizer::SynthesizeResult (FunctionDecl *FunDecl)
{
ASTContext &Ctx(*m_ast_context);
Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
if (!m_sema)
return false;
FunctionDecl *function_decl = FunDecl;
if (!function_decl)
return false;
Stmt *function_body = function_decl->getBody();
CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
if (!compound_stmt)
return false;
if (compound_stmt->body_empty())
return false;
Stmt **last_stmt_ptr = compound_stmt->body_end() - 1;
Stmt *last_stmt = *last_stmt_ptr;
Expr *last_expr = dyn_cast<Expr>(last_stmt);
if (!last_expr)
// No auxiliary variable necessary; expression returns void
return true;
QualType expr_qual_type = last_expr->getType();
clang::Type *expr_type = expr_qual_type.getTypePtr();
if (!expr_type)
return false;
if (expr_type->isVoidType())
return true;
if (log)
{
std::string s = expr_qual_type.getAsString();
log->Printf("Last statement's type: %s", s.c_str());
}
IdentifierInfo &result_id = Ctx.Idents.get("___clang_expr_result");
clang::VarDecl *result_decl = VarDecl::Create(Ctx,
function_decl,
SourceLocation(),
&result_id,
expr_qual_type,
NULL,
VarDecl::Static,
VarDecl::Static);
if (!result_decl)
return false;
function_decl->addDecl(result_decl);
///////////////////////////////
// call AddInitializerToDecl
//
Parser::DeclPtrTy result_decl_ptr;
result_decl_ptr.set(result_decl);
m_action->AddInitializerToDecl(result_decl_ptr, Parser::ExprArg(*m_action, last_expr));
/////////////////////////////////
// call ConvertDeclToDeclGroup
//
Parser::DeclGroupPtrTy result_decl_group_ptr;
result_decl_group_ptr = m_action->ConvertDeclToDeclGroup(result_decl_ptr);
////////////////////////
// call ActOnDeclStmt
//
Parser::OwningStmtResult result_initialization_stmt_result(m_action->ActOnDeclStmt(result_decl_group_ptr,
SourceLocation(),
SourceLocation()));
////////////////////////////////////////////////
// replace the old statement with the new one
//
*last_stmt_ptr = reinterpret_cast<Stmt*>(result_initialization_stmt_result.take());
if (log)
{
std::string s;
raw_string_ostream os(s);
function_decl->print(os);
os.flush();
log->Printf("Transformed function AST:\n%s", s.c_str());
}
return true;
}
void
ASTResultSynthesizer::HandleTranslationUnit(ASTContext &Ctx)
{
if (m_passthrough)
m_passthrough->HandleTranslationUnit(Ctx);
}
void
ASTResultSynthesizer::HandleTagDeclDefinition(TagDecl *D)
{
if (m_passthrough)
m_passthrough->HandleTagDeclDefinition(D);
}
void
ASTResultSynthesizer::CompleteTentativeDefinition(VarDecl *D)
{
if (m_passthrough)
m_passthrough->CompleteTentativeDefinition(D);
}
void
ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired)
{
if (m_passthrough)
m_passthrough->HandleVTable(RD, DefinitionRequired);
}
void
ASTResultSynthesizer::PrintStats()
{
if (m_passthrough)
m_passthrough->PrintStats();
}
void
ASTResultSynthesizer::InitializeSema(Sema &S)
{
m_sema = &S;
m_action = reinterpret_cast<Action*>(m_sema);
if (m_passthrough_sema)
m_passthrough_sema->InitializeSema(S);
}
void
ASTResultSynthesizer::ForgetSema()
{
m_sema = NULL;
m_action = NULL;
if (m_passthrough_sema)
m_passthrough_sema->ForgetSema();
}
|