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
|
//===--- SourceCodeBuilder.cpp ----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Tooling/Transformer/SourceCodeBuilders.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Transformer/SourceCode.h"
#include "llvm/ADT/Twine.h"
#include <string>
using namespace clang;
using namespace tooling;
const Expr *tooling::reallyIgnoreImplicit(const Expr &E) {
const Expr *Expr = E.IgnoreImplicit();
if (const auto *CE = dyn_cast<CXXConstructExpr>(Expr)) {
if (CE->getNumArgs() > 0 &&
CE->getArg(0)->getSourceRange() == Expr->getSourceRange())
return CE->getArg(0)->IgnoreImplicit();
}
return Expr;
}
bool tooling::mayEverNeedParens(const Expr &E) {
const Expr *Expr = reallyIgnoreImplicit(E);
// We always want parens around unary, binary, and ternary operators, because
// they are lower precedence.
if (isa<UnaryOperator>(Expr) || isa<BinaryOperator>(Expr) ||
isa<AbstractConditionalOperator>(Expr))
return true;
// We need parens around calls to all overloaded operators except: function
// calls, subscripts, and expressions that are already part of an (implicit)
// call to operator->. These latter are all in the same precedence level as
// dot/arrow and that level is left associative, so they don't need parens
// when appearing on the left.
if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(Expr))
return Op->getOperator() != OO_Call && Op->getOperator() != OO_Subscript &&
Op->getOperator() != OO_Arrow;
return false;
}
bool tooling::needParensAfterUnaryOperator(const Expr &E) {
const Expr *Expr = reallyIgnoreImplicit(E);
if (isa<BinaryOperator>(Expr) || isa<AbstractConditionalOperator>(Expr))
return true;
if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(Expr))
return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
Op->getOperator() != OO_Subscript;
return false;
}
bool tooling::isKnownPointerLikeType(QualType Ty, ASTContext &Context) {
using namespace ast_matchers;
const auto PointerLikeTy = type(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
"::std::unique_ptr", "::std::shared_ptr", "::std::weak_ptr",
"::std::optional", "::absl::optional", "::llvm::Optional",
"absl::StatusOr", "::llvm::Expected"))))));
return match(PointerLikeTy, Ty, Context).size() > 0;
}
std::optional<std::string> tooling::buildParens(const Expr &E,
const ASTContext &Context) {
StringRef Text = getText(E, Context);
if (Text.empty())
return std::nullopt;
if (mayEverNeedParens(E))
return ("(" + Text + ")").str();
return Text.str();
}
std::optional<std::string>
tooling::buildDereference(const Expr &E, const ASTContext &Context) {
if (const auto *Op = dyn_cast<UnaryOperator>(&E))
if (Op->getOpcode() == UO_AddrOf) {
// Strip leading '&'.
StringRef Text =
getText(*Op->getSubExpr()->IgnoreParenImpCasts(), Context);
if (Text.empty())
return std::nullopt;
return Text.str();
}
StringRef Text = getText(E, Context);
if (Text.empty())
return std::nullopt;
// Add leading '*'.
if (needParensAfterUnaryOperator(E))
return ("*(" + Text + ")").str();
return ("*" + Text).str();
}
std::optional<std::string> tooling::buildAddressOf(const Expr &E,
const ASTContext &Context) {
if (E.isImplicitCXXThis())
return std::string("this");
if (const auto *Op = dyn_cast<UnaryOperator>(&E))
if (Op->getOpcode() == UO_Deref) {
// Strip leading '*'.
StringRef Text =
getText(*Op->getSubExpr()->IgnoreParenImpCasts(), Context);
if (Text.empty())
return std::nullopt;
return Text.str();
}
// Add leading '&'.
StringRef Text = getText(E, Context);
if (Text.empty())
return std::nullopt;
if (needParensAfterUnaryOperator(E)) {
return ("&(" + Text + ")").str();
}
return ("&" + Text).str();
}
// Append the appropriate access operation (syntactically) to `E`, assuming `E`
// is a non-pointer value.
static std::optional<std::string>
buildAccessForValue(const Expr &E, const ASTContext &Context) {
if (const auto *Op = llvm::dyn_cast<UnaryOperator>(&E))
if (Op->getOpcode() == UO_Deref) {
// Strip leading '*', add following '->'.
const Expr *SubExpr = Op->getSubExpr()->IgnoreParenImpCasts();
StringRef DerefText = getText(*SubExpr, Context);
if (DerefText.empty())
return std::nullopt;
if (needParensBeforeDotOrArrow(*SubExpr))
return ("(" + DerefText + ")->").str();
return (DerefText + "->").str();
}
// Add following '.'.
StringRef Text = getText(E, Context);
if (Text.empty())
return std::nullopt;
if (needParensBeforeDotOrArrow(E)) {
return ("(" + Text + ").").str();
}
return (Text + ".").str();
}
// Append the appropriate access operation (syntactically) to `E`, assuming `E`
// is a pointer value.
static std::optional<std::string>
buildAccessForPointer(const Expr &E, const ASTContext &Context) {
if (const auto *Op = llvm::dyn_cast<UnaryOperator>(&E))
if (Op->getOpcode() == UO_AddrOf) {
// Strip leading '&', add following '.'.
const Expr *SubExpr = Op->getSubExpr()->IgnoreParenImpCasts();
StringRef DerefText = getText(*SubExpr, Context);
if (DerefText.empty())
return std::nullopt;
if (needParensBeforeDotOrArrow(*SubExpr))
return ("(" + DerefText + ").").str();
return (DerefText + ".").str();
}
// Add following '->'.
StringRef Text = getText(E, Context);
if (Text.empty())
return std::nullopt;
if (needParensBeforeDotOrArrow(E))
return ("(" + Text + ")->").str();
return (Text + "->").str();
}
std::optional<std::string> tooling::buildDot(const Expr &E,
const ASTContext &Context) {
return buildAccessForValue(E, Context);
}
std::optional<std::string> tooling::buildArrow(const Expr &E,
const ASTContext &Context) {
return buildAccessForPointer(E, Context);
}
// If `E` is an overloaded-operator call of kind `K` on an object `O`, returns
// `O`. Otherwise, returns `nullptr`.
static const Expr *maybeGetOperatorObjectArg(const Expr &E,
OverloadedOperatorKind K) {
if (const auto *OpCall = dyn_cast<clang::CXXOperatorCallExpr>(&E)) {
if (OpCall->getOperator() == K && OpCall->getNumArgs() == 1)
return OpCall->getArg(0);
}
return nullptr;
}
static bool treatLikePointer(QualType Ty, PLTClass C, ASTContext &Context) {
switch (C) {
case PLTClass::Value:
return false;
case PLTClass::Pointer:
return isKnownPointerLikeType(Ty, Context);
}
llvm_unreachable("Unknown PLTClass enum");
}
// FIXME: move over the other `maybe` functionality from Stencil. Should all be
// in one place.
std::optional<std::string> tooling::buildAccess(const Expr &RawExpression,
ASTContext &Context,
PLTClass Classification) {
if (RawExpression.isImplicitCXXThis())
// Return the empty string, because `std::nullopt` signifies some sort of
// failure.
return std::string();
const Expr *E = RawExpression.IgnoreImplicitAsWritten();
if (E->getType()->isAnyPointerType() ||
treatLikePointer(E->getType(), Classification, Context)) {
// Strip off operator-> calls. They can only occur inside an actual arrow
// member access, so we treat them as equivalent to an actual object
// expression.
if (const auto *Obj = maybeGetOperatorObjectArg(*E, clang::OO_Arrow))
E = Obj;
return buildAccessForPointer(*E, Context);
}
if (const auto *Obj = maybeGetOperatorObjectArg(*E, clang::OO_Star)) {
if (treatLikePointer(Obj->getType(), Classification, Context))
return buildAccessForPointer(*Obj, Context);
};
return buildAccessForValue(*E, Context);
}
|