Skip to content

Commit 159f253

Browse files
authored
[clang][bytecode] Diagnose invalid declrefs differently if we've... (#113140)
... tried their initializer already. In that case, diagnose the non-const initializer instead of the reference to a non-constexpr variable later. This is used in a lot of openmp tests.
1 parent f1ba894 commit 159f253

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6097,7 +6097,8 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
60976097

60986098
if (VD->evaluateValue())
60996099
return revisit(VD);
6100-
return this->emitInvalidDeclRef(cast<DeclRefExpr>(E), E);
6100+
return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
6101+
/*InitializerFailed=*/true, E);
61016102
}
61026103
}
61036104
} else {
@@ -6123,7 +6124,7 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
61236124
}
61246125

61256126
if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
6126-
return this->emitInvalidDeclRef(DRE, E);
6127+
return this->emitInvalidDeclRef(DRE, /*InitializerFailed=*/false, E);
61276128
return false;
61286129
}
61296130

clang/lib/AST/ByteCode/Interp.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,9 +2818,18 @@ inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
28182818
return false;
28192819
}
28202820

2821-
inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC,
2822-
const DeclRefExpr *DR) {
2821+
inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR,
2822+
bool InitializerFailed) {
28232823
assert(DR);
2824+
2825+
if (InitializerFailed) {
2826+
const SourceInfo &Loc = S.Current->getSource(OpPC);
2827+
const auto *VD = cast<VarDecl>(DR->getDecl());
2828+
S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
2829+
S.Note(VD->getLocation(), diag::note_declared_at);
2830+
return false;
2831+
}
2832+
28242833
return CheckDeclRef(S, OpPC, DR);
28252834
}
28262835

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def InvalidCast : Opcode {
769769
}
770770

771771
def InvalidDeclRef : Opcode {
772-
let Args = [ArgDeclRef];
772+
let Args = [ArgDeclRef, ArgBool];
773773
}
774774

775775
def SizelessVectorElementSize : Opcode;

clang/test/AST/ByteCode/openmp.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both -fopenmp %s
2+
// RUN: %clang_cc1 -verify=ref,both -fopenmp %s
3+
4+
int test1() {
5+
int i;
6+
int &j = i; // both-note {{declared here}}
7+
float *f;
8+
// both-note@+2 {{initializer of 'j' is not a constant expression}}
9+
// both-error@+1 {{integral constant expression}}
10+
#pragma omp for simd aligned(f:j)
11+
for (int i = 0; i < 10; ++i);
12+
}
13+

0 commit comments

Comments
 (0)