Skip to content

Commit

Permalink
[clang][Interp] Allow adding an offset to a function pointer
Browse files Browse the repository at this point in the history
Pretty sure this isn't doing anything, but it fixes a test and
is generally the right thing to do.
Fixing the behavior will come later.
  • Loading branch information
tbaederr committed Feb 21, 2024
1 parent 3ee8c93 commit ffcdf47
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
11 changes: 5 additions & 6 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,12 +1403,11 @@ bool ByteCodeExprGen<Emitter>::VisitPointerCompoundAssignOperator(

if (!LT || !RT)
return false;
assert(*LT == PT_Ptr);

if (!visit(LHS))
return false;

if (!this->emitLoadPtr(LHS))
if (!this->emitLoad(*LT, LHS))
return false;

if (!visit(RHS))
Expand Down Expand Up @@ -2828,7 +2827,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
if (!this->visit(SubExpr))
return false;

if (T == PT_Ptr) {
if (T == PT_Ptr || T == PT_FnPtr) {
if (!this->emitIncPtr(E))
return false;

Expand All @@ -2846,7 +2845,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
if (!this->visit(SubExpr))
return false;

if (T == PT_Ptr) {
if (T == PT_Ptr || T == PT_FnPtr) {
if (!this->emitDecPtr(E))
return false;

Expand All @@ -2864,7 +2863,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
if (!this->visit(SubExpr))
return false;

if (T == PT_Ptr) {
if (T == PT_Ptr || T == PT_FnPtr) {
if (!this->emitLoadPtr(E))
return false;
if (!this->emitConstUint8(1, E))
Expand Down Expand Up @@ -2903,7 +2902,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
if (!this->visit(SubExpr))
return false;

if (T == PT_Ptr) {
if (T == PT_Ptr || T == PT_FnPtr) {
if (!this->emitLoadPtr(E))
return false;
if (!this->emitConstUint8(1, E))
Expand Down
32 changes: 32 additions & 0 deletions clang/test/AST/Interp/pointer-addition.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %clang_cc1 %s -fsyntax-only -verify=gnu,expected -pedantic -Wextra -std=c11 -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 %s -fsyntax-only -triple i686-unknown-unknown -verify=gnu,expected -pedantic -Wextra -std=c11 -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 %s -fsyntax-only -triple x86_64-unknown-unknown -verify=gnu,expected -pedantic -Wextra -std=c11 -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wextra -Wno-gnu -std=c11 -fexperimental-new-constant-interpreter

typedef __INTPTR_TYPE__ intptr_t;
typedef struct S S; // expected-note 4 {{forward declaration of 'struct S'}}
extern _Atomic(S*) e;
void a(S* b, void* c) {
void (*fp)(int) = 0;
b++; // expected-error {{arithmetic on a pointer to an incomplete type}}
b += 1; // expected-error {{arithmetic on a pointer to an incomplete type}}
c++; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
c += 1; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
c--; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
c -= 1; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
(void) c[1]; // gnu-warning {{subscript of a pointer to void is a GNU extension}}
b = 1+b; // expected-error {{arithmetic on a pointer to an incomplete type}}
/* The next couple tests are only pedantic warnings in gcc */
void (*d)(S*,void*) = a;
d += 1; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
d++; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
d--; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
d -= 1; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
(void)(1 + d); // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
e++; // expected-error {{arithmetic on a pointer to an incomplete type}}
intptr_t i = (intptr_t)b;
char *f = (char*)0 + i; // gnu-warning {{arithmetic on a null pointer treated as a cast from integer to pointer is a GNU extension}}
// Cases that don't match the GNU inttoptr idiom get a different warning.
f = (char*)0 - i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
int *g = (int*)0 + i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
}

0 comments on commit ffcdf47

Please sign in to comment.