Author: Timm Bäder Date: 2024-06-10T10:18:57+02:00 New Revision: c0b65a2491be44f0b162d55563c305c514c01d90
URL: https://github.com/llvm/llvm-project/commit/c0b65a2491be44f0b162d55563c305c514c01d90 DIFF: https://github.com/llvm/llvm-project/commit/c0b65a2491be44f0b162d55563c305c514c01d90.diff LOG: [clang][Interp] Diagnose casts from void pointers Added: Modified: clang/lib/AST/Interp/ByteCodeExprGen.cpp clang/lib/AST/Interp/Interp.h clang/lib/AST/Interp/Opcodes.td clang/test/AST/Interp/c.c clang/test/Sema/constexpr-void-cast.c Removed: ################################################################################ diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index ac245e7a0f24c..6654a27c92168 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -325,8 +325,11 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) { assert(isPtrType(*FromT)); assert(isPtrType(*ToT)); - if (FromT == ToT) + if (FromT == ToT) { + if (SubExpr->getType()->isVoidPointerType()) + return this->visit(SubExpr) && this->emitVoidPtrCast(CE); return this->delegate(SubExpr); + } if (!this->visit(SubExpr)) return false; diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h index 4bb9e7f0d22ea..0ad710c5ec1af 100644 --- a/clang/lib/AST/Interp/Interp.h +++ b/clang/lib/AST/Interp/Interp.h @@ -1980,6 +1980,13 @@ static inline bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, return true; } +static inline bool VoidPtrCast(InterpState &S, CodePtr OpPC) { + const SourceInfo &E = S.Current->getSource(OpPC); + S.CCEDiag(E, diag::note_constexpr_invalid_cast) + << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC); + return true; +} + //===----------------------------------------------------------------------===// // Zero, Nullptr //===----------------------------------------------------------------------===// diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td index a5ac8206104c8..ac5426c87c212 100644 --- a/clang/lib/AST/Interp/Opcodes.td +++ b/clang/lib/AST/Interp/Opcodes.td @@ -665,6 +665,7 @@ def CastPointerIntegralAPS : Opcode { let HasGroup = 0; let Args = [ArgUint32]; } +def VoidPtrCast : Opcode; def DecayPtr : Opcode { let Types = [PtrTypeClass, PtrTypeClass]; diff --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c index f4c7bf16f2f95..1cc450e48def0 100644 --- a/clang/test/AST/Interp/c.c +++ b/clang/test/AST/Interp/c.c @@ -66,12 +66,10 @@ _Static_assert((&a - 100) != 0, ""); // pedantic-ref-warning {{is a GNU extensio // pedantic-ref-note {{-100 of non-array}} \ // pedantic-expected-note {{-100 of non-array}} /// extern variable of a composite type. -/// FIXME: The 'this conversion is not allowed' note is missing in the new interpreter. extern struct Test50S Test50; _Static_assert(&Test50 != (void*)0, ""); // all-warning {{always true}} \ - // pedantic-ref-warning {{is a GNU extension}} \ - // pedantic-ref-note {{this conversion is not allowed in a constant expression}} \ - // pedantic-expected-warning {{is a GNU extension}} + // pedantic-warning {{is a GNU extension}} \ + // pedantic-note {{this conversion is not allowed in a constant expression}} struct y {int x,y;}; int a2[(intptr_t)&((struct y*)0)->y]; // all-warning {{folded to constant array}} diff --git a/clang/test/Sema/constexpr-void-cast.c b/clang/test/Sema/constexpr-void-cast.c index 91e4027f67fe3..2ffc59f509b4b 100644 --- a/clang/test/Sema/constexpr-void-cast.c +++ b/clang/test/Sema/constexpr-void-cast.c @@ -1,8 +1,12 @@ // RUN: %clang_cc1 -x c -fsyntax-only %s -verify=c -std=c11 // RUN: %clang_cc1 -x c -fsyntax-only %s -pedantic -verify=c-pedantic -std=c11 +// RUN: %clang_cc1 -x c -fsyntax-only %s -verify=c -std=c11 -fexperimental-new-constant-interpreter +// RUN: %clang_cc1 -x c -fsyntax-only %s -pedantic -verify=c-pedantic -std=c11 -fexperimental-new-constant-interpreter // // RUN: %clang_cc1 -x c++ -fsyntax-only %s -verify=cxx // RUN: %clang_cc1 -x c++ -fsyntax-only %s -pedantic -verify=cxx-pedantic +// RUN: %clang_cc1 -x c++ -fsyntax-only %s -verify=cxx -fexperimental-new-constant-interpreter +// RUN: %clang_cc1 -x c++ -fsyntax-only %s -pedantic -verify=cxx-pedantic -fexperimental-new-constant-interpreter // c-no-diagnostics // cxx-no-diagnostics _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits