-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
ASTHelpers.h
156 lines (129 loc) · 6.5 KB
/
ASTHelpers.h
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
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#ifndef INSIGHTS_AST_HELPERS_H
#define INSIGHTS_AST_HELPERS_H
//-----------------------------------------------------------------------------
#include <array>
#include <span>
#include <string_view>
#include <vector>
#include "clang/AST/ASTContext.h"
#include "clang/AST/ExprCXX.h"
#include "llvm/ADT/SmallVector.h"
#include "InsightsStrongTypes.h"
//-----------------------------------------------------------------------------
namespace clang::insights::asthelpers {
void ReplaceNode(Stmt* parent, Stmt* oldNode, Stmt* newNode);
using params_vector = std::vector<std::pair<std::string_view, QualType>>;
using params_store = std::vector<std::pair<std::string, QualType>>;
params_vector to_params_view(params_store& params);
QualType GetRecordDeclType(const CXXMethodDecl* md);
QualType GetRecordDeclType(const RecordDecl* rd);
DeclRefExpr* mkVarDeclRefExpr(std::string_view name, QualType type);
STRONG_BOOL(DoCast);
STRONG_BOOL(AsReference);
CallExpr* CallConstructor(QualType ctorType,
QualType lhsType,
const FieldDecl* fieldDecl,
ArrayRef<Expr*> callParams,
DoCast doCast = DoCast::No,
AsReference asReference = AsReference::No);
CallExpr* CallConstructor(QualType ctorType,
const VarDecl* fieldDecl,
ArrayRef<Expr*> callParams,
DoCast doCast = DoCast::No,
AsReference asReference = AsReference::No);
CXXBoolLiteralExpr* Bool(bool b);
CallExpr* CallDestructor(const VarDecl* fieldDecl);
CXXNewExpr* New(ArrayRef<Expr*> placementArgs, const Expr* expr, QualType t);
BinaryOperator* Mul(Expr* lhs, Expr* rhs);
BinaryOperator* And(VarDecl* lhs, Expr* rhs);
QualType Typedef(std::string_view name, QualType underlayingType);
SmallVector<Expr*, 5> ArgsToExprVector(const Expr* expr);
///! A helper type to have a container for ArrayRef
struct StmtsContainer
{
SmallVector<Stmt*, 64> mStmts{};
StmtsContainer() = default;
StmtsContainer(std::initializer_list<const Stmt*> stmts)
{
for(const auto& stmt : stmts) {
Add(stmt);
}
}
void clear() { mStmts.clear(); }
void Add(const Stmt* stmt)
{
if(stmt) {
mStmts.push_back(const_cast<Stmt*>(stmt));
}
}
void AddBodyStmts(Stmt* body);
operator ArrayRef<Stmt*>() { return mStmts; }
};
//-----------------------------------------------------------------------------
template<typename... Dcls>
DeclStmt* mkDeclStmt(Dcls... dcls)
{
std::array<Decl*, sizeof...(dcls)> decls{dcls...};
DeclStmt* _mkDeclStmt(std::span<Decl*> decls);
return _mkDeclStmt(decls);
}
Stmt* Comment(std::string_view comment);
VarDecl* Variable(std::string_view name, QualType type, DeclContext* dc = nullptr);
CXXRecordDecl* Struct(std::string_view name);
ReturnStmt* Return(Expr* stmt = nullptr);
ReturnStmt* Return(const ValueDecl* stmt);
CompoundStmt* mkCompoundStmt(ArrayRef<Stmt*> bodyStmts, SourceLocation beginLoc = {}, SourceLocation endLoc = {});
DeclRefExpr* mkDeclRefExpr(const ValueDecl* vd);
NullStmt* mkNullStmt();
FieldDecl* mkFieldDecl(DeclContext* dc, std::string_view name, QualType type);
ParenExpr* Paren(Expr*);
QualType ContantArrayTy(QualType t, int size);
InitListExpr* InitList(ArrayRef<Expr*> initExprs, QualType t);
ArraySubscriptExpr* ArraySubscript(const Expr* lhs, uint64_t index, QualType type);
MemberExpr* AccessMember(const Expr* expr, const ValueDecl* vd, bool isArrow = true);
CXXMemberCallExpr* CallMemberFun(Expr* memExpr, QualType retType);
ImplicitCastExpr* CastLToRValue(const VarDecl* vd);
FunctionDecl* Function(std::string_view name, QualType returnType, const params_vector& parameters);
ParmVarDecl* Parameter(const FunctionDecl* fd, std::string_view name, QualType type);
BinaryOperator* Assign(DeclRefExpr* declRef, ValueDecl* field, Expr* assignExpr);
BinaryOperator* Assign(MemberExpr* me, ValueDecl* field, Expr* assignExpr);
BinaryOperator* Assign(DeclRefExpr* declRef, Expr* assignExpr);
BinaryOperator* Assign(const VarDecl* var, Expr* assignExpr);
BinaryOperator* Assign(UnaryOperator* var, Expr* assignExpr);
BinaryOperator* Assign(Expr* var, Expr* assignExpr);
BinaryOperator* Equal(Expr* var, Expr* assignExpr);
BinaryOperator* Plus(Expr* var, Expr* assignExpr);
CXXReinterpretCastExpr* ReinterpretCast(QualType toType, const Expr* toExpr, bool makePointer = false);
CXXStaticCastExpr* StaticCast(QualType toType, const Expr* toExpr, bool makePointer = false);
CXXStaticCastExpr* CastToVoidFunPtr(std::string_view name);
CXXStaticCastExpr* Cast(const Expr* toExpr, QualType toType);
IntegerLiteral* Int32(uint64_t value);
IfStmt* If(const Expr* condition, ArrayRef<Stmt*> bodyStmts);
SwitchStmt* Switch(Expr* stmt);
CaseStmt* Case(int value, Stmt* stmt);
BreakStmt* Break();
LabelStmt* Label(std::string_view name);
GotoStmt* Goto(std::string_view labelName);
UnaryOperator* Not(const Expr* stmt);
UnaryOperator* Ref(const Expr* e);
UnaryOperator* Ref(const ValueDecl* d);
UnaryOperator* Dref(const Expr* stmt);
UnaryOperator* AddrOf(const Expr* stmt);
CallExpr* Call(const FunctionDecl* fd, ArrayRef<Expr*> params);
CallExpr* Call(MemberExpr* fd, ArrayRef<Expr*> params);
CallExpr* Call(std::string_view name, ArrayRef<Expr*> args);
CXXTryStmt* Try(const Stmt* tryBody, CXXCatchStmt* catchAllBody);
CXXCatchStmt* Catch(Stmt* body);
CXXCatchStmt* Catch(ArrayRef<Stmt*> body);
CXXThrowExpr* Throw(const Expr* expr = nullptr);
UnaryExprOrTypeTraitExpr* Sizeof(QualType toType);
QualType Ptr(QualType srcType);
CanQualType VoidTy();
} // namespace clang::insights::asthelpers
#endif /* INSIGHTS_AST_HELPERS_H */