Skip to content

Commit c0b65a2

Browse files
committed
[clang][Interp] Diagnose casts from void pointers
1 parent cc158d4 commit c0b65a2

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,11 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
325325

326326
assert(isPtrType(*FromT));
327327
assert(isPtrType(*ToT));
328-
if (FromT == ToT)
328+
if (FromT == ToT) {
329+
if (SubExpr->getType()->isVoidPointerType())
330+
return this->visit(SubExpr) && this->emitVoidPtrCast(CE);
329331
return this->delegate(SubExpr);
332+
}
330333

331334
if (!this->visit(SubExpr))
332335
return false;

clang/lib/AST/Interp/Interp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,13 @@ static inline bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC,
19801980
return true;
19811981
}
19821982

1983+
static inline bool VoidPtrCast(InterpState &S, CodePtr OpPC) {
1984+
const SourceInfo &E = S.Current->getSource(OpPC);
1985+
S.CCEDiag(E, diag::note_constexpr_invalid_cast)
1986+
<< 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
1987+
return true;
1988+
}
1989+
19831990
//===----------------------------------------------------------------------===//
19841991
// Zero, Nullptr
19851992
//===----------------------------------------------------------------------===//

clang/lib/AST/Interp/Opcodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ def CastPointerIntegralAPS : Opcode {
665665
let HasGroup = 0;
666666
let Args = [ArgUint32];
667667
}
668+
def VoidPtrCast : Opcode;
668669

669670
def DecayPtr : Opcode {
670671
let Types = [PtrTypeClass, PtrTypeClass];

clang/test/AST/Interp/c.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,10 @@ _Static_assert((&a - 100) != 0, ""); // pedantic-ref-warning {{is a GNU extensio
6666
// pedantic-ref-note {{-100 of non-array}} \
6767
// pedantic-expected-note {{-100 of non-array}}
6868
/// extern variable of a composite type.
69-
/// FIXME: The 'this conversion is not allowed' note is missing in the new interpreter.
7069
extern struct Test50S Test50;
7170
_Static_assert(&Test50 != (void*)0, ""); // all-warning {{always true}} \
72-
// pedantic-ref-warning {{is a GNU extension}} \
73-
// pedantic-ref-note {{this conversion is not allowed in a constant expression}} \
74-
// pedantic-expected-warning {{is a GNU extension}}
71+
// pedantic-warning {{is a GNU extension}} \
72+
// pedantic-note {{this conversion is not allowed in a constant expression}}
7573

7674
struct y {int x,y;};
7775
int a2[(intptr_t)&((struct y*)0)->y]; // all-warning {{folded to constant array}}

clang/test/Sema/constexpr-void-cast.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// RUN: %clang_cc1 -x c -fsyntax-only %s -verify=c -std=c11
22
// RUN: %clang_cc1 -x c -fsyntax-only %s -pedantic -verify=c-pedantic -std=c11
3+
// RUN: %clang_cc1 -x c -fsyntax-only %s -verify=c -std=c11 -fexperimental-new-constant-interpreter
4+
// RUN: %clang_cc1 -x c -fsyntax-only %s -pedantic -verify=c-pedantic -std=c11 -fexperimental-new-constant-interpreter
35
//
46
// RUN: %clang_cc1 -x c++ -fsyntax-only %s -verify=cxx
57
// RUN: %clang_cc1 -x c++ -fsyntax-only %s -pedantic -verify=cxx-pedantic
8+
// RUN: %clang_cc1 -x c++ -fsyntax-only %s -verify=cxx -fexperimental-new-constant-interpreter
9+
// RUN: %clang_cc1 -x c++ -fsyntax-only %s -pedantic -verify=cxx-pedantic -fexperimental-new-constant-interpreter
610

711
// c-no-diagnostics
812
// cxx-no-diagnostics

0 commit comments

Comments
 (0)