Skip to content

Commit dcaab6d

Browse files
authored
[clang][bytecode] Add source info to jump ops (#188003)
The attached test case otherwise results in a function with one jump op but no source info at all.
1 parent 249b086 commit dcaab6d

File tree

7 files changed

+56
-45
lines changed

7 files changed

+56
-45
lines changed

clang/lib/AST/ByteCode/ByteCodeEmitter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,16 @@ bool ByteCodeEmitter::emitOp(Opcode Op, const Tys &...Args, SourceInfo SI) {
221221
return Success;
222222
}
223223

224-
bool ByteCodeEmitter::jumpTrue(const LabelTy &Label) {
225-
return emitJt(getOffset(Label), SourceInfo{});
224+
bool ByteCodeEmitter::jumpTrue(const LabelTy &Label, SourceInfo SI) {
225+
return emitJt(getOffset(Label), SI);
226226
}
227227

228-
bool ByteCodeEmitter::jumpFalse(const LabelTy &Label) {
229-
return emitJf(getOffset(Label), SourceInfo{});
228+
bool ByteCodeEmitter::jumpFalse(const LabelTy &Label, SourceInfo SI) {
229+
return emitJf(getOffset(Label), SI);
230230
}
231231

232-
bool ByteCodeEmitter::jump(const LabelTy &Label) {
233-
return emitJmp(getOffset(Label), SourceInfo{});
232+
bool ByteCodeEmitter::jump(const LabelTy &Label, SourceInfo SI) {
233+
return emitJmp(getOffset(Label), SI);
234234
}
235235

236236
bool ByteCodeEmitter::fallthrough(const LabelTy &Label) {

clang/lib/AST/ByteCode/ByteCodeEmitter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class ByteCodeEmitter {
5252
virtual bool emitBool(bool V, const Expr *E) = 0;
5353

5454
/// Emits jumps.
55-
bool jumpTrue(const LabelTy &Label);
56-
bool jumpFalse(const LabelTy &Label);
57-
bool jump(const LabelTy &Label);
55+
bool jumpTrue(const LabelTy &Label, SourceInfo SI);
56+
bool jumpFalse(const LabelTy &Label, SourceInfo SI);
57+
bool jump(const LabelTy &Label, SourceInfo SI);
5858
bool fallthrough(const LabelTy &Label);
5959
/// Speculative execution.
6060
bool speculate(const CallExpr *E, const LabelTy &EndLabel);

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,12 +1344,12 @@ bool Compiler<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
13441344

13451345
if (!this->visitBool(LHS))
13461346
return false;
1347-
if (!this->jumpTrue(LabelTrue))
1347+
if (!this->jumpTrue(LabelTrue, E))
13481348
return false;
13491349

13501350
if (!this->visitBool(RHS))
13511351
return false;
1352-
if (!this->jump(LabelEnd))
1352+
if (!this->jump(LabelEnd, E))
13531353
return false;
13541354

13551355
this->emitLabel(LabelTrue);
@@ -1366,12 +1366,12 @@ bool Compiler<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
13661366

13671367
if (!this->visitBool(LHS))
13681368
return false;
1369-
if (!this->jumpFalse(LabelFalse))
1369+
if (!this->jumpFalse(LabelFalse, E))
13701370
return false;
13711371

13721372
if (!this->visitBool(RHS))
13731373
return false;
1374-
if (!this->jump(LabelEnd))
1374+
if (!this->jump(LabelEnd, E))
13751375
return false;
13761376

13771377
this->emitLabel(LabelFalse);
@@ -2836,12 +2836,12 @@ bool Compiler<Emitter>::VisitAbstractConditionalOperator(
28362836
return false;
28372837
}
28382838

2839-
if (!this->jumpFalse(LabelFalse))
2839+
if (!this->jumpFalse(LabelFalse, E))
28402840
return false;
28412841
if (!this->delegate(TrueExpr))
28422842
return false;
28432843

2844-
if (!this->jump(LabelEnd))
2844+
if (!this->jump(LabelEnd, E))
28452845
return false;
28462846
this->emitLabel(LabelFalse);
28472847
if (!this->delegate(FalseExpr))
@@ -4031,7 +4031,7 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
40314031
return false;
40324032
if (!this->emitEQPtr(E))
40334033
return false;
4034-
if (!this->jumpTrue(EndLabel))
4034+
if (!this->jumpTrue(EndLabel, E))
40354035
return false;
40364036
}
40374037

@@ -4052,7 +4052,7 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
40524052
return false;
40534053
if (!this->emitLT(SizeT, E))
40544054
return false;
4055-
if (!this->jumpFalse(EndLabel))
4055+
if (!this->jumpFalse(EndLabel, E))
40564056
return false;
40574057

40584058
// Pointer to the allocated array is already on the stack.
@@ -4101,7 +4101,7 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
41014101
if (!this->emitIncPop(SizeT, false, E))
41024102
return false;
41034103

4104-
if (!this->jump(StartLabel))
4104+
if (!this->jump(StartLabel, E))
41054105
return false;
41064106

41074107
this->fallthrough(EndLabel);
@@ -6080,19 +6080,19 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
60806080
if (const Stmt *Else = IS->getElse()) {
60816081
LabelTy LabelElse = this->getLabel();
60826082
LabelTy LabelEnd = this->getLabel();
6083-
if (!this->jumpFalse(LabelElse))
6083+
if (!this->jumpFalse(LabelElse, IS))
60846084
return false;
60856085
if (!visitChildStmt(IS->getThen()))
60866086
return false;
6087-
if (!this->jump(LabelEnd))
6087+
if (!this->jump(LabelEnd, IS))
60886088
return false;
60896089
this->emitLabel(LabelElse);
60906090
if (!visitChildStmt(Else))
60916091
return false;
60926092
this->emitLabel(LabelEnd);
60936093
} else {
60946094
LabelTy LabelEnd = this->getLabel();
6095-
if (!this->jumpFalse(LabelEnd))
6095+
if (!this->jumpFalse(LabelEnd, IS))
60966096
return false;
60976097
if (!visitChildStmt(IS->getThen()))
60986098
return false;
@@ -6132,7 +6132,7 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
61326132
if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
61336133
return false;
61346134

6135-
if (!this->jumpFalse(EndLabel))
6135+
if (!this->jumpFalse(EndLabel, S))
61366136
return false;
61376137

61386138
if (!this->visitStmt(Body))
@@ -6142,7 +6142,7 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
61426142
return false;
61436143
// } End of loop body.
61446144

6145-
if (!this->jump(CondLabel))
6145+
if (!this->jump(CondLabel, S))
61466146
return false;
61476147
this->fallthrough(EndLabel);
61486148
this->emitLabel(EndLabel);
@@ -6175,7 +6175,7 @@ template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
61756175
if (!CondScope.destroyLocals())
61766176
return false;
61776177
}
6178-
if (!this->jumpTrue(StartLabel))
6178+
if (!this->jumpTrue(StartLabel, S))
61796179
return false;
61806180

61816181
this->fallthrough(EndLabel);
@@ -6213,7 +6213,7 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
62136213
if (Cond) {
62146214
if (!this->visitBool(Cond))
62156215
return false;
6216-
if (!this->jumpFalse(EndLabel))
6216+
if (!this->jumpFalse(EndLabel, S))
62176217
return false;
62186218
}
62196219
if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
@@ -6229,7 +6229,7 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
62296229

62306230
if (!CondScope.destroyLocals())
62316231
return false;
6232-
if (!this->jump(CondLabel))
6232+
if (!this->jump(CondLabel, S))
62336233
return false;
62346234
// } End of loop body.
62356235

@@ -6270,7 +6270,7 @@ bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
62706270
this->emitLabel(CondLabel);
62716271
if (!this->visitBool(Cond))
62726272
return false;
6273-
if (!this->jumpFalse(EndLabel))
6273+
if (!this->jumpFalse(EndLabel, S))
62746274
return false;
62756275

62766276
if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
@@ -6287,7 +6287,7 @@ bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
62876287
return false;
62886288
}
62896289

6290-
if (!this->jump(CondLabel))
6290+
if (!this->jump(CondLabel, S))
62916291
return false;
62926292

62936293
this->fallthrough(EndLabel);
@@ -6332,7 +6332,7 @@ bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) {
63326332
return false;
63336333
}
63346334

6335-
return this->jump(*TargetLabel);
6335+
return this->jump(*TargetLabel, S);
63366336
}
63376337

63386338
template <class Emitter>
@@ -6369,7 +6369,7 @@ bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) {
63696369
return false;
63706370
}
63716371

6372-
return this->jump(*TargetLabel);
6372+
return this->jump(*TargetLabel, S);
63736373
}
63746374

63756375
template <class Emitter>
@@ -6424,7 +6424,7 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
64246424
PrimType LT = this->classifyPrim(Low->getType());
64256425
if (!this->emitGE(LT, S))
64266426
return false;
6427-
if (!this->jumpFalse(EndOfRangeCheck))
6427+
if (!this->jumpFalse(EndOfRangeCheck, S))
64286428
return false;
64296429

64306430
if (!this->emitGetLocal(CondT, CondVar, CS))
@@ -6434,7 +6434,7 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
64346434
PrimType HT = this->classifyPrim(High->getType());
64356435
if (!this->emitLE(HT, S))
64366436
return false;
6437-
if (!this->jumpTrue(CaseLabels[CS]))
6437+
if (!this->jumpTrue(CaseLabels[CS], S))
64386438
return false;
64396439
this->emitLabel(EndOfRangeCheck);
64406440
continue;
@@ -6454,7 +6454,7 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
64546454
// Compare and jump to the case label.
64556455
if (!this->emitEQ(ValueT, S))
64566456
return false;
6457-
if (!this->jumpTrue(CaseLabels[CS]))
6457+
if (!this->jumpTrue(CaseLabels[CS], S))
64586458
return false;
64596459
} else {
64606460
assert(!DefaultLabel);
@@ -6465,10 +6465,10 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
64656465
// If none of the conditions above were true, fall through to the default
64666466
// statement or jump after the switch statement.
64676467
if (DefaultLabel) {
6468-
if (!this->jump(*DefaultLabel))
6468+
if (!this->jump(*DefaultLabel, S))
64696469
return false;
64706470
} else {
6471-
if (!this->jump(EndLabel))
6471+
if (!this->jump(EndLabel, S))
64726472
return false;
64736473
}
64746474

@@ -7701,7 +7701,7 @@ bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
77017701

77027702
// We now have the bool value of E[0] on the stack.
77037703
LabelTy LabelTrue = this->getLabel();
7704-
if (!this->jumpTrue(LabelTrue))
7704+
if (!this->jumpTrue(LabelTrue, E))
77057705
return false;
77067706

77077707
if (!this->emitArrayElemPop(ElemT, 1, E))
@@ -7715,7 +7715,7 @@ bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
77157715
}
77167716
// Leave the boolean value of E[1] on the stack.
77177717
LabelTy EndLabel = this->getLabel();
7718-
this->jump(EndLabel);
7718+
this->jump(EndLabel, E);
77197719

77207720
this->emitLabel(LabelTrue);
77217721
if (!this->emitPopPtr(E))

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
597597
typename Emitter::LabelTy EndLabel = this->Ctx->getLabel();
598598
if (!this->Ctx->emitGetLocalEnabled(Local.Offset, E))
599599
return false;
600-
if (!this->Ctx->jumpFalse(EndLabel))
600+
if (!this->Ctx->jumpFalse(EndLabel, E))
601601
return false;
602602

603603
if (!this->Ctx->emitGetPtrLocal(Local.Offset, E))

clang/lib/AST/ByteCode/EvalEmitter.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,29 @@ Scope::Local EvalEmitter::createLocal(Descriptor *D) {
125125
return {Off, D};
126126
}
127127

128-
bool EvalEmitter::jumpTrue(const LabelTy &Label) {
128+
bool EvalEmitter::jumpTrue(const LabelTy &Label, SourceInfo SI) {
129129
if (isActive()) {
130+
CurrentSource = SI;
130131
if (S.Stk.pop<bool>())
131132
ActiveLabel = Label;
132133
}
133134
return true;
134135
}
135136

136-
bool EvalEmitter::jumpFalse(const LabelTy &Label) {
137+
bool EvalEmitter::jumpFalse(const LabelTy &Label, SourceInfo SI) {
137138
if (isActive()) {
139+
CurrentSource = SI;
138140
if (!S.Stk.pop<bool>())
139141
ActiveLabel = Label;
140142
}
141143
return true;
142144
}
143145

144-
bool EvalEmitter::jump(const LabelTy &Label) {
145-
if (isActive())
146+
bool EvalEmitter::jump(const LabelTy &Label, SourceInfo SI) {
147+
if (isActive()) {
148+
CurrentSource = SI;
146149
CurrentLabel = ActiveLabel = Label;
150+
}
147151
return true;
148152
}
149153

clang/lib/AST/ByteCode/EvalEmitter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class EvalEmitter : public SourceMapper {
6868
virtual bool emitBool(bool V, const Expr *E) = 0;
6969

7070
/// Emits jumps.
71-
bool jumpTrue(const LabelTy &Label);
72-
bool jumpFalse(const LabelTy &Label);
73-
bool jump(const LabelTy &Label);
71+
bool jumpTrue(const LabelTy &Label, SourceInfo SI);
72+
bool jumpFalse(const LabelTy &Label, SourceInfo SI);
73+
bool jump(const LabelTy &Label, SourceInfo SI);
7474
bool fallthrough(const LabelTy &Label);
7575
/// Speculative execution.
7676
bool speculate(const CallExpr *E, const LabelTy &EndLabel);

clang/test/AST/ByteCode/constexpr-steps.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ constexpr int foo() { // expected-error {{never produces a constant expression}}
88
static_assert (foo() == 0, ""); // expected-error {{not an integral constant expression}} \
99
// expected-note {{in call to}}
1010

11+
constexpr void addr() { // expected-error {{never produces a constant expression}}
12+
for (;;) // expected-note 2{{constexpr evaluation hit maximum step limit}}
13+
;
14+
}
15+
static_assert((addr(), 1) == 1); // expected-error {{not an integral constant expression}} \
16+
// expected-note {{in call to}}
17+

0 commit comments

Comments
 (0)