Skip to content

Commit 283851d

Browse files
committed
[clang][bytecode] Refactor backtrace param printing
Instead of `classify()`ing the parameter types here, just use the saved `PrimType` in the `ParamDescriptor`. This also fixed an oddity with `interp::Context`: `Pointer::toRValue()` takes an `interp::Context`, but some call sites passed an `ASTContext` instead, which worked because of the implicit constructor. Make it explicit.
1 parent bb088f7 commit 283851d

4 files changed

Lines changed: 24 additions & 24 deletions

File tree

clang/lib/AST/ByteCode/Context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class EvalIDScope;
4747
class Context final {
4848
public:
4949
/// Initialises the constexpr VM.
50-
Context(ASTContext &Ctx);
50+
explicit Context(ASTContext &Ctx);
5151

5252
/// Cleans up the constexpr VM.
5353
~Context();

clang/lib/AST/ByteCode/EvalEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ bool EvalEmitter::emitRetValue(SourceInfo Info) {
298298
return false;
299299

300300
if (std::optional<APValue> APV =
301-
Ptr.toRValue(S.getASTContext(), EvalResult.getSourceType())) {
301+
Ptr.toRValue(Ctx, EvalResult.getSourceType())) {
302302
EvalResult.takeValue(std::move(*APV));
303303
return true;
304304
}

clang/lib/AST/ByteCode/Function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Function final {
148148
}
149149

150150
/// Returns a parameter descriptor.
151-
ParamDescriptor getParamDescriptor(unsigned Index) const {
151+
const ParamDescriptor &getParamDescriptor(unsigned Index) const {
152152
return ParamDescriptors[Index];
153153
}
154154

clang/lib/AST/ByteCode/InterpFrame.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ void InterpFrame::destroy(unsigned Idx) {
121121
}
122122

123123
template <typename T>
124-
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &ASTCtx,
124+
static void print(llvm::raw_ostream &OS, const T &V, const Context &Ctx,
125125
QualType Ty) {
126126
if constexpr (std::is_same_v<Pointer, T>) {
127127
if (Ty->isPointerOrReferenceType())
128-
V.toAPValue(ASTCtx).printPretty(OS, ASTCtx, Ty);
128+
V.toAPValue(Ctx.getASTContext()).printPretty(OS, Ctx.getASTContext(), Ty);
129129
else {
130-
if (std::optional<APValue> RValue = V.toRValue(ASTCtx, Ty))
131-
RValue->printPretty(OS, ASTCtx, Ty);
130+
if (std::optional<APValue> RValue = V.toRValue(Ctx, Ty))
131+
RValue->printPretty(OS, Ctx.getASTContext(), Ty);
132132
else
133133
OS << "...";
134134
}
135135
} else {
136-
V.toAPValue(ASTCtx).printPretty(OS, ASTCtx, Ty);
136+
V.toAPValue(Ctx.getASTContext()).printPretty(OS, Ctx.getASTContext(), Ty);
137137
}
138138
}
139139

@@ -159,9 +159,10 @@ void InterpFrame::describe(llvm::raw_ostream &OS) const {
159159
if (shouldSkipInBacktrace(Func))
160160
return;
161161

162+
const ASTContext &ASTCtx = S.getASTContext();
162163
const Expr *CallExpr = Caller->getExpr(getRetPC());
163164
const FunctionDecl *F = getCallee();
164-
auto PrintingPolicy = S.getASTContext().getPrintingPolicy();
165+
auto PrintingPolicy = ASTCtx.getPrintingPolicy();
165166
PrintingPolicy.SuppressLambdaBody = true;
166167

167168
bool IsMemberCall = false;
@@ -180,39 +181,38 @@ void InterpFrame::describe(llvm::raw_ostream &OS) const {
180181
if (Object->getType()->isPointerType())
181182
OS << "->";
182183
else
183-
OS << ".";
184+
OS << '.';
184185
} else if (const auto *OCE =
185186
dyn_cast_if_present<CXXOperatorCallExpr>(CallExpr)) {
186187
OCE->getArg(0)->printPretty(OS, /*Helper=*/nullptr,
187188
PrintingPolicy,
188189
/*Indentation=*/0);
189-
OS << ".";
190+
OS << '.';
190191
} else if (const auto *M = dyn_cast<CXXMethodDecl>(F)) {
191-
print(OS, getThis(), S.getASTContext(),
192-
S.getASTContext().getLValueReferenceType(
193-
S.getASTContext().getCanonicalTagType(M->getParent())));
194-
OS << ".";
192+
print(OS, getThis(), S.getContext(),
193+
ASTCtx.getLValueReferenceType(
194+
ASTCtx.getCanonicalTagType(M->getParent())));
195+
OS << '.';
195196
}
196197
}
197198

198-
F->getNameForDiagnostic(OS, PrintingPolicy,
199-
/*Qualified=*/false);
199+
F->getNameForDiagnostic(OS, PrintingPolicy, /*Qualified=*/false);
200200
OS << '(';
201201
unsigned Off = 0;
202-
202+
unsigned ParamIndex = 0;
203203
Off += Func->hasRVO() ? primSize(PT_Ptr) : 0;
204204
Off += Func->hasThisPointer() ? primSize(PT_Ptr) : 0;
205205
llvm::ListSeparator Comma;
206206
for (const ParmVarDecl *Param :
207207
F->parameters().slice(ExplicitInstanceParam)) {
208208
OS << Comma;
209-
QualType Ty = Param->getType();
210-
PrimType PrimTy = S.Ctx.classify(Ty).value_or(PT_Ptr);
211-
212-
TYPE_SWITCH(PrimTy, print(OS, stackRef<T>(Off), S.getASTContext(), Ty));
213-
Off += align(primSize(PrimTy));
209+
PrimType PrimT = Func->getParamDescriptor(ParamIndex).T;
210+
TYPE_SWITCH(PrimT,
211+
print(OS, stackRef<T>(Off), S.getContext(), Param->getType()));
212+
Off += align(primSize(PrimT));
213+
++ParamIndex;
214214
}
215-
OS << ")";
215+
OS << ')';
216216
}
217217

218218
SourceRange InterpFrame::getCallRange() const {

0 commit comments

Comments
 (0)