LLDB mainline
ASTStructExtractor.cpp
Go to the documentation of this file.
1//===-- ASTStructExtractor.cpp --------------------------------------------===//
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
10
11#include "lldb/Utility/Log.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/Decl.h"
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/DeclGroup.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/RecordLayout.h"
18#include "clang/AST/Stmt.h"
19#include "clang/Parse/Parser.h"
20#include "clang/Sema/Sema.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/raw_ostream.h"
23#include <cstdlib>
24
25using namespace llvm;
26using namespace clang;
27using namespace lldb_private;
28
30 const char *struct_name,
31 ClangFunctionCaller &function)
32 : m_ast_context(nullptr), m_passthrough(passthrough),
33 m_passthrough_sema(nullptr), m_sema(nullptr), m_function(function),
34 m_struct_name(struct_name) {
35 if (!m_passthrough)
36 return;
37
38 m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
39}
40
42
43void ASTStructExtractor::Initialize(ASTContext &Context) {
44 m_ast_context = &Context;
45
46 if (m_passthrough)
47 m_passthrough->Initialize(Context);
48}
49
51 if (!F->hasBody())
52 return;
53
54 Stmt *body_stmt = F->getBody();
55 CompoundStmt *body_compound_stmt = dyn_cast<CompoundStmt>(body_stmt);
56
57 if (!body_compound_stmt)
58 return; // do we have to handle this?
59
60 RecordDecl *struct_decl = nullptr;
61
62 StringRef desired_name(m_struct_name);
63
64 for (CompoundStmt::const_body_iterator bi = body_compound_stmt->body_begin(),
65 be = body_compound_stmt->body_end();
66 bi != be; ++bi) {
67 Stmt *curr_stmt = *bi;
68 DeclStmt *curr_decl_stmt = dyn_cast<DeclStmt>(curr_stmt);
69 if (!curr_decl_stmt)
70 continue;
71 DeclGroupRef decl_group = curr_decl_stmt->getDeclGroup();
72 for (Decl *candidate_decl : decl_group) {
73 RecordDecl *candidate_record_decl = dyn_cast<RecordDecl>(candidate_decl);
74 if (!candidate_record_decl)
75 continue;
76 if (candidate_record_decl->getName() == desired_name) {
77 struct_decl = candidate_record_decl;
78 break;
79 }
80 }
81 if (struct_decl)
82 break;
83 }
84
85 if (!struct_decl)
86 return;
87
88 const ASTRecordLayout *struct_layout(
89 &m_ast_context->getASTRecordLayout(struct_decl));
90
91 if (!struct_layout)
92 return;
93
95 struct_layout->getSize()
96 .getQuantity(); // TODO Store m_struct_size as CharUnits
98 struct_layout->getFieldOffset(struct_layout->getFieldCount() - 1) / 8;
100 struct_layout->getDataSize().getQuantity() - m_function.m_return_offset;
101
102 for (unsigned field_index = 0, num_fields = struct_layout->getFieldCount();
103 field_index < num_fields; ++field_index) {
105 struct_layout->getFieldOffset(field_index) / 8);
106 }
107
109}
110
112 LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
113
114 if (linkage_spec_decl) {
115 RecordDecl::decl_iterator decl_iterator;
116
117 for (decl_iterator = linkage_spec_decl->decls_begin();
118 decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) {
119 ExtractFromTopLevelDecl(*decl_iterator);
120 }
121 }
122
123 FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
124
125 if (m_ast_context && function_decl &&
127 function_decl->getNameAsString())) {
128 ExtractFromFunctionDecl(function_decl);
129 }
130}
131
133 DeclGroupRef::iterator decl_iterator;
134
135 for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) {
136 Decl *decl = *decl_iterator;
137
139 }
140
141 if (m_passthrough)
142 return m_passthrough->HandleTopLevelDecl(D);
143 return true;
144}
145
147 if (m_passthrough)
148 m_passthrough->HandleTranslationUnit(Ctx);
149}
150
152 if (m_passthrough)
153 m_passthrough->HandleTagDeclDefinition(D);
154}
155
157 if (m_passthrough)
158 m_passthrough->CompleteTentativeDefinition(D);
159}
160
161void ASTStructExtractor::HandleVTable(CXXRecordDecl *RD) {
162 if (m_passthrough)
163 m_passthrough->HandleVTable(RD);
164}
165
167 if (m_passthrough)
168 m_passthrough->PrintStats();
169}
170
172 m_sema = &S;
173
175 m_passthrough_sema->InitializeSema(S);
176}
177
179 m_sema = nullptr;
180
182 m_passthrough_sema->ForgetSema();
183}
clang::SemaConsumer * m_passthrough_sema
The SemaConsumer down the chain, for passthrough.
void ForgetSema() override
Reset the Sema to NULL now that transformations are done.
void CompleteTentativeDefinition(clang::VarDecl *D) override
Passthrough stub.
void HandleTagDeclDefinition(clang::TagDecl *D) override
Passthrough stub.
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.
std::string m_struct_name
The name of the structure to extract.
clang::ASTConsumer * m_passthrough
The ASTConsumer down the chain, for passthrough.
ASTStructExtractor(clang::ASTConsumer *passthrough, const char *struct_name, ClangFunctionCaller &function)
Constructor.
clang::Sema * m_sema
The Sema to use.
clang::ASTContext * m_ast_context
The AST context to use for identifiers and types.
void ExtractFromFunctionDecl(clang::FunctionDecl *F)
Hunt the given FunctionDecl for the argument struct and place information about it into m_function.
~ASTStructExtractor() override
Destructor.
ClangFunctionCaller & m_function
The function to populate with information about the argument structure.
void HandleVTable(clang::CXXRecordDecl *RD) override
Passthrough stub.
void HandleTranslationUnit(clang::ASTContext &Ctx) override
Passthrough stub.
void ExtractFromTopLevelDecl(clang::Decl *D)
Hunt the given Decl for FunctionDecls named the same as the wrapper function name,...
void Initialize(clang::ASTContext &Context) override
Link this consumer with a particular AST context.
void InitializeSema(clang::Sema &S) override
Set the Sema object to use when performing transforms, and pass it on.
"lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can be called.
size_t m_struct_size
These values are populated by the ASTStructExtractor.
uint64_t m_return_offset
The offset of the result variable in the struct, in bytes.
bool m_struct_valid
True if the ASTStructExtractor has populated the variables below.
std::string m_wrapper_function_name
The name of the wrapper function.
uint64_t m_return_size
The size of the result variable, in bytes.
std::vector< uint64_t > m_member_offsets
The offset of each member in the struct, in bytes.
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: Debugger.h:53