LLDB  mainline
ASTResultSynthesizer.cpp
Go to the documentation of this file.
1 //===-- ASTResultSynthesizer.cpp --------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ASTResultSynthesizer.h"
10 
12 
15 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/Log.h"
18 #include "stdlib.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclGroup.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/Stmt.h"
26 #include "clang/Parse/Parser.h"
27 #include "clang/Sema/SemaDiagnostic.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 using namespace llvm;
32 using namespace clang;
33 using namespace lldb_private;
34 
35 ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough,
36  bool top_level, Target &target)
37  : m_ast_context(NULL), m_passthrough(passthrough), m_passthrough_sema(NULL),
38  m_target(target), m_sema(NULL), m_top_level(top_level) {
39  if (!m_passthrough)
40  return;
41 
42  m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
43 }
44 
46 
47 void ASTResultSynthesizer::Initialize(ASTContext &Context) {
48  m_ast_context = &Context;
49 
50  if (m_passthrough)
51  m_passthrough->Initialize(Context);
52 }
53 
54 void ASTResultSynthesizer::TransformTopLevelDecl(Decl *D) {
56 
57  if (NamedDecl *named_decl = dyn_cast<NamedDecl>(D)) {
58  if (log && log->GetVerbose()) {
59  if (named_decl->getIdentifier())
60  log->Printf("TransformTopLevelDecl(%s)",
61  named_decl->getIdentifier()->getNameStart());
62  else if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D))
63  log->Printf("TransformTopLevelDecl(%s)",
64  method_decl->getSelector().getAsString().c_str());
65  else
66  log->Printf("TransformTopLevelDecl(<complex>)");
67  }
68 
69  if (m_top_level) {
70  RecordPersistentDecl(named_decl);
71  }
72  }
73 
74  if (LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D)) {
75  RecordDecl::decl_iterator decl_iterator;
76 
77  for (decl_iterator = linkage_spec_decl->decls_begin();
78  decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) {
79  TransformTopLevelDecl(*decl_iterator);
80  }
81  } else if (!m_top_level) {
82  if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D)) {
83  if (m_ast_context &&
84  !method_decl->getSelector().getAsString().compare("$__lldb_expr:")) {
85  RecordPersistentTypes(method_decl);
86  SynthesizeObjCMethodResult(method_decl);
87  }
88  } else if (FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D)) {
89  // When completing user input the body of the function may be a nullptr.
90  if (m_ast_context && function_decl->hasBody() &&
91  !function_decl->getNameInfo().getAsString().compare("$__lldb_expr")) {
92  RecordPersistentTypes(function_decl);
93  SynthesizeFunctionResult(function_decl);
94  }
95  }
96  }
97 }
98 
100  DeclGroupRef::iterator decl_iterator;
101 
102  for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) {
103  Decl *decl = *decl_iterator;
104 
105  TransformTopLevelDecl(decl);
106  }
107 
108  if (m_passthrough)
109  return m_passthrough->HandleTopLevelDecl(D);
110  return true;
111 }
112 
113 bool ASTResultSynthesizer::SynthesizeFunctionResult(FunctionDecl *FunDecl) {
115 
116  if (!m_sema)
117  return false;
118 
119  FunctionDecl *function_decl = FunDecl;
120 
121  if (!function_decl)
122  return false;
123 
124  if (log && log->GetVerbose()) {
125  std::string s;
126  raw_string_ostream os(s);
127 
128  function_decl->print(os);
129 
130  os.flush();
131 
132  log->Printf("Untransformed function AST:\n%s", s.c_str());
133  }
134 
135  Stmt *function_body = function_decl->getBody();
136  CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
137 
138  bool ret = SynthesizeBodyResult(compound_stmt, function_decl);
139 
140  if (log && log->GetVerbose()) {
141  std::string s;
142  raw_string_ostream os(s);
143 
144  function_decl->print(os);
145 
146  os.flush();
147 
148  log->Printf("Transformed function AST:\n%s", s.c_str());
149  }
150 
151  return ret;
152 }
153 
154 bool ASTResultSynthesizer::SynthesizeObjCMethodResult(
155  ObjCMethodDecl *MethodDecl) {
157 
158  if (!m_sema)
159  return false;
160 
161  if (!MethodDecl)
162  return false;
163 
164  if (log && log->GetVerbose()) {
165  std::string s;
166  raw_string_ostream os(s);
167 
168  MethodDecl->print(os);
169 
170  os.flush();
171 
172  log->Printf("Untransformed method AST:\n%s", s.c_str());
173  }
174 
175  Stmt *method_body = MethodDecl->getBody();
176 
177  if (!method_body)
178  return false;
179 
180  CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(method_body);
181 
182  bool ret = SynthesizeBodyResult(compound_stmt, MethodDecl);
183 
184  if (log && log->GetVerbose()) {
185  std::string s;
186  raw_string_ostream os(s);
187 
188  MethodDecl->print(os);
189 
190  os.flush();
191 
192  log->Printf("Transformed method AST:\n%s", s.c_str());
193  }
194 
195  return ret;
196 }
197 
198 bool ASTResultSynthesizer::SynthesizeBodyResult(CompoundStmt *Body,
199  DeclContext *DC) {
201 
202  ASTContext &Ctx(*m_ast_context);
203 
204  if (!Body)
205  return false;
206 
207  if (Body->body_empty())
208  return false;
209 
210  Stmt **last_stmt_ptr = Body->body_end() - 1;
211  Stmt *last_stmt = *last_stmt_ptr;
212 
213  while (dyn_cast<NullStmt>(last_stmt)) {
214  if (last_stmt_ptr != Body->body_begin()) {
215  last_stmt_ptr--;
216  last_stmt = *last_stmt_ptr;
217  } else {
218  return false;
219  }
220  }
221 
222  Expr *last_expr = dyn_cast<Expr>(last_stmt);
223 
224  if (!last_expr)
225  // No auxiliary variable necessary; expression returns void
226  return true;
227 
228  // In C++11, last_expr can be a LValueToRvalue implicit cast. Strip that off
229  // if that's the case.
230 
231  do {
232  ImplicitCastExpr *implicit_cast = dyn_cast<ImplicitCastExpr>(last_expr);
233 
234  if (!implicit_cast)
235  break;
236 
237  if (implicit_cast->getCastKind() != CK_LValueToRValue)
238  break;
239 
240  last_expr = implicit_cast->getSubExpr();
241  } while (0);
242 
243  // is_lvalue is used to record whether the expression returns an assignable
244  // Lvalue or an Rvalue. This is relevant because they are handled
245  // differently.
246  //
247  // For Lvalues
248  //
249  // - In AST result synthesis (here!) the expression E is transformed into an
250  // initialization
251  // T *$__lldb_expr_result_ptr = &E.
252  //
253  // - In structure allocation, a pointer-sized slot is allocated in the
254  // struct that is to be
255  // passed into the expression.
256  //
257  // - In IR transformations, reads and writes to $__lldb_expr_result_ptr are
258  // redirected at
259  // an entry in the struct ($__lldb_arg) passed into the expression.
260  // (Other persistent
261  // variables are treated similarly, having been materialized as
262  // references, but in those
263  // cases the value of the reference itself is never modified.)
264  //
265  // - During materialization, $0 (the result persistent variable) is ignored.
266  //
267  // - During dematerialization, $0 is marked up as a load address with value
268  // equal to the
269  // contents of the structure entry.
270  //
271  // For Rvalues
272  //
273  // - In AST result synthesis the expression E is transformed into an
274  // initialization
275  // static T $__lldb_expr_result = E.
276  //
277  // - In structure allocation, a pointer-sized slot is allocated in the
278  // struct that is to be
279  // passed into the expression.
280  //
281  // - In IR transformations, an instruction is inserted at the beginning of
282  // the function to
283  // dereference the pointer resident in the slot. Reads and writes to
284  // $__lldb_expr_result
285  // are redirected at that dereferenced version. Guard variables for the
286  // static variable
287  // are excised.
288  //
289  // - During materialization, $0 (the result persistent variable) is
290  // populated with the location
291  // of a newly-allocated area of memory.
292  //
293  // - During dematerialization, $0 is ignored.
294 
295  bool is_lvalue = last_expr->getValueKind() == VK_LValue &&
296  last_expr->getObjectKind() == OK_Ordinary;
297 
298  QualType expr_qual_type = last_expr->getType();
299  const clang::Type *expr_type = expr_qual_type.getTypePtr();
300 
301  if (!expr_type)
302  return false;
303 
304  if (expr_type->isVoidType())
305  return true;
306 
307  if (log) {
308  std::string s = expr_qual_type.getAsString();
309 
310  log->Printf("Last statement is an %s with type: %s",
311  (is_lvalue ? "lvalue" : "rvalue"), s.c_str());
312  }
313 
314  clang::VarDecl *result_decl = NULL;
315 
316  if (is_lvalue) {
317  IdentifierInfo *result_ptr_id;
318 
319  if (expr_type->isFunctionType())
320  result_ptr_id =
321  &Ctx.Idents.get("$__lldb_expr_result"); // functions actually should
322  // be treated like function
323  // pointers
324  else
325  result_ptr_id = &Ctx.Idents.get("$__lldb_expr_result_ptr");
326 
327  m_sema->RequireCompleteType(SourceLocation(), expr_qual_type,
328  clang::diag::err_incomplete_type);
329 
330  QualType ptr_qual_type;
331 
332  if (expr_qual_type->getAs<ObjCObjectType>() != NULL)
333  ptr_qual_type = Ctx.getObjCObjectPointerType(expr_qual_type);
334  else
335  ptr_qual_type = Ctx.getPointerType(expr_qual_type);
336 
337  result_decl =
338  VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(),
339  result_ptr_id, ptr_qual_type, NULL, SC_Static);
340 
341  if (!result_decl)
342  return false;
343 
344  ExprResult address_of_expr =
345  m_sema->CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, last_expr);
346  if (address_of_expr.get())
347  m_sema->AddInitializerToDecl(result_decl, address_of_expr.get(), true);
348  else
349  return false;
350  } else {
351  IdentifierInfo &result_id = Ctx.Idents.get("$__lldb_expr_result");
352 
353  result_decl = VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(),
354  &result_id, expr_qual_type, NULL, SC_Static);
355 
356  if (!result_decl)
357  return false;
358 
359  m_sema->AddInitializerToDecl(result_decl, last_expr, true);
360  }
361 
362  DC->addDecl(result_decl);
363 
364  ///////////////////////////////
365  // call AddInitializerToDecl
366  //
367 
368  // m_sema->AddInitializerToDecl(result_decl, last_expr);
369 
370  /////////////////////////////////
371  // call ConvertDeclToDeclGroup
372  //
373 
374  Sema::DeclGroupPtrTy result_decl_group_ptr;
375 
376  result_decl_group_ptr = m_sema->ConvertDeclToDeclGroup(result_decl);
377 
378  ////////////////////////
379  // call ActOnDeclStmt
380  //
381 
382  StmtResult result_initialization_stmt_result(m_sema->ActOnDeclStmt(
383  result_decl_group_ptr, SourceLocation(), SourceLocation()));
384 
385  ////////////////////////////////////////////////
386  // replace the old statement with the new one
387  //
388 
389  *last_stmt_ptr =
390  reinterpret_cast<Stmt *>(result_initialization_stmt_result.get());
391 
392  return true;
393 }
394 
396  if (m_passthrough)
397  m_passthrough->HandleTranslationUnit(Ctx);
398 }
399 
400 void ASTResultSynthesizer::RecordPersistentTypes(DeclContext *FunDeclCtx) {
401  typedef DeclContext::specific_decl_iterator<TypeDecl> TypeDeclIterator;
402 
403  for (TypeDeclIterator i = TypeDeclIterator(FunDeclCtx->decls_begin()),
404  e = TypeDeclIterator(FunDeclCtx->decls_end());
405  i != e; ++i) {
406  MaybeRecordPersistentType(*i);
407  }
408 }
409 
410 void ASTResultSynthesizer::MaybeRecordPersistentType(TypeDecl *D) {
411  if (!D->getIdentifier())
412  return;
413 
414  StringRef name = D->getName();
415 
416  if (name.size() == 0 || name[0] != '$')
417  return;
418 
420 
421  ConstString name_cs(name.str().c_str());
422 
423  if (log)
424  log->Printf("Recording persistent type %s\n", name_cs.GetCString());
425 
426  m_decls.push_back(D);
427 }
428 
429 void ASTResultSynthesizer::RecordPersistentDecl(NamedDecl *D) {
430  lldbassert(m_top_level);
431 
432  if (!D->getIdentifier())
433  return;
434 
435  StringRef name = D->getName();
436 
437  if (name.size() == 0)
438  return;
439 
441 
442  ConstString name_cs(name.str().c_str());
443 
444  if (log)
445  log->Printf("Recording persistent decl %s\n", name_cs.GetCString());
446 
447  m_decls.push_back(D);
448 }
449 
451  for (clang::NamedDecl *decl : m_decls) {
452  StringRef name = decl->getName();
453  ConstString name_cs(name.str().c_str());
454 
455  Decl *D_scratch = m_target.GetClangASTImporter()->DeportDecl(
456  m_target.GetScratchClangASTContext()->getASTContext(), m_ast_context,
457  decl);
458 
459  if (!D_scratch) {
461 
462  if (log) {
463  std::string s;
464  llvm::raw_string_ostream ss(s);
465  decl->dump(ss);
466  ss.flush();
467 
468  log->Printf("Couldn't commit persistent decl: %s\n", s.c_str());
469  }
470 
471  continue;
472  }
473 
474  if (NamedDecl *NamedDecl_scratch = dyn_cast<NamedDecl>(D_scratch))
475  llvm::cast<ClangPersistentVariables>(
478  ->RegisterPersistentDecl(name_cs, NamedDecl_scratch);
479  }
480 }
481 
483  if (m_passthrough)
484  m_passthrough->HandleTagDeclDefinition(D);
485 }
486 
488  if (m_passthrough)
489  m_passthrough->CompleteTentativeDefinition(D);
490 }
491 
492 void ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD) {
493  if (m_passthrough)
494  m_passthrough->HandleVTable(RD);
495 }
496 
498  if (m_passthrough)
499  m_passthrough->PrintStats();
500 }
501 
503  m_sema = &S;
504 
505  if (m_passthrough_sema)
506  m_passthrough_sema->InitializeSema(S);
507 }
508 
510  m_sema = NULL;
511 
512  if (m_passthrough_sema)
513  m_passthrough_sema->ForgetSema();
514 }
~ASTResultSynthesizer() override
Destructor.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
Definition: Debugger.h:71
#define lldbassert(x)
Definition: LLDBAssert.h:15
clang::ASTContext * getASTContext()
void ForgetSema() override
Reset the Sema to NULL now that transformations are done.
void CommitPersistentDecls()
The parse has succeeded, so record its persistent decls.
void HandleVTable(clang::CXXRecordDecl *RD) override
Passthrough stub.
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
void InitializeSema(clang::Sema &S) override
Set the Sema object to use when performing transforms, and pass it on.
void HandleTranslationUnit(clang::ASTContext &Ctx) override
Passthrough stub.
bool GetVerbose() const
Definition: Log.cpp:250
ClangASTContext * GetScratchClangASTContext(bool create_on_demand=true)
Definition: Target.cpp:2299
void CompleteTentativeDefinition(clang::VarDecl *D) override
Passthrough stub.
void Initialize(clang::ASTContext &Context) override
Link this consumer with a particular AST context.
void PrintStats() override
Passthrough stub.
bool HandleTopLevelDecl(clang::DeclGroupRef D) override
Examine a list of Decls to find the function $__lldb_expr and transform its code. ...
A uniqued constant string class.
Definition: ConstString.h:38
Non-standardized C, such as K&R.
#define LIBLLDB_LOG_EXPRESSIONS
Definition: Logging.h:22
void HandleTagDeclDefinition(clang::TagDecl *D) override
Passthrough stub.
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition: Target.cpp:2206
lldb::ClangASTImporterSP GetClangASTImporter()
Definition: Target.cpp:2308