Skip to content

Commit ffcdf47

Browse files
committed
[clang][Interp] Allow adding an offset to a function pointer
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.
1 parent 3ee8c93 commit ffcdf47

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,12 +1403,11 @@ bool ByteCodeExprGen<Emitter>::VisitPointerCompoundAssignOperator(
14031403

14041404
if (!LT || !RT)
14051405
return false;
1406-
assert(*LT == PT_Ptr);
14071406

14081407
if (!visit(LHS))
14091408
return false;
14101409

1411-
if (!this->emitLoadPtr(LHS))
1410+
if (!this->emitLoad(*LT, LHS))
14121411
return false;
14131412

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

2831-
if (T == PT_Ptr) {
2830+
if (T == PT_Ptr || T == PT_FnPtr) {
28322831
if (!this->emitIncPtr(E))
28332832
return false;
28342833

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

2849-
if (T == PT_Ptr) {
2848+
if (T == PT_Ptr || T == PT_FnPtr) {
28502849
if (!this->emitDecPtr(E))
28512850
return false;
28522851

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

2867-
if (T == PT_Ptr) {
2866+
if (T == PT_Ptr || T == PT_FnPtr) {
28682867
if (!this->emitLoadPtr(E))
28692868
return false;
28702869
if (!this->emitConstUint8(1, E))
@@ -2903,7 +2902,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
29032902
if (!this->visit(SubExpr))
29042903
return false;
29052904

2906-
if (T == PT_Ptr) {
2905+
if (T == PT_Ptr || T == PT_FnPtr) {
29072906
if (!this->emitLoadPtr(E))
29082907
return false;
29092908
if (!this->emitConstUint8(1, E))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_cc1 %s -fsyntax-only -verify=gnu,expected -pedantic -Wextra -std=c11 -fexperimental-new-constant-interpreter
2+
// RUN: %clang_cc1 %s -fsyntax-only -triple i686-unknown-unknown -verify=gnu,expected -pedantic -Wextra -std=c11 -fexperimental-new-constant-interpreter
3+
// RUN: %clang_cc1 %s -fsyntax-only -triple x86_64-unknown-unknown -verify=gnu,expected -pedantic -Wextra -std=c11 -fexperimental-new-constant-interpreter
4+
// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic -Wextra -Wno-gnu -std=c11 -fexperimental-new-constant-interpreter
5+
6+
typedef __INTPTR_TYPE__ intptr_t;
7+
typedef struct S S; // expected-note 4 {{forward declaration of 'struct S'}}
8+
extern _Atomic(S*) e;
9+
void a(S* b, void* c) {
10+
void (*fp)(int) = 0;
11+
b++; // expected-error {{arithmetic on a pointer to an incomplete type}}
12+
b += 1; // expected-error {{arithmetic on a pointer to an incomplete type}}
13+
c++; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
14+
c += 1; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
15+
c--; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
16+
c -= 1; // gnu-warning {{arithmetic on a pointer to void is a GNU extension}}
17+
(void) c[1]; // gnu-warning {{subscript of a pointer to void is a GNU extension}}
18+
b = 1+b; // expected-error {{arithmetic on a pointer to an incomplete type}}
19+
/* The next couple tests are only pedantic warnings in gcc */
20+
void (*d)(S*,void*) = a;
21+
d += 1; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
22+
d++; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
23+
d--; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
24+
d -= 1; // gnu-warning {{arithmetic on a pointer to the function type 'void (S *, void *)' (aka 'void (struct S *, void *)') is a GNU extension}}
25+
(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}}
26+
e++; // expected-error {{arithmetic on a pointer to an incomplete type}}
27+
intptr_t i = (intptr_t)b;
28+
char *f = (char*)0 + i; // gnu-warning {{arithmetic on a null pointer treated as a cast from integer to pointer is a GNU extension}}
29+
// Cases that don't match the GNU inttoptr idiom get a different warning.
30+
f = (char*)0 - i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
31+
int *g = (int*)0 + i; // expected-warning {{performing pointer arithmetic on a null pointer has undefined behavior}}
32+
}

0 commit comments

Comments
 (0)