llvm-project/clang/test/Sema/scope-check.c

150 lines
3.7 KiB
C
Raw Normal View History

// RUN: clang-cc -fsyntax-only -verify -std=gnu99 %s
Start of checking for gotos which jump to an illegal destination. As far as I know, this catches all cases of jumping into the scope of a variable with a variably modified type (excluding statement expressions) in C. This is missing some stuff we probably want to check (other kinds of variably modified declarations, statement expressions, indirect gotos/addresses of labels in a scope, ObjC @try/@finally, cleanup attribute), the diagnostics aren't very good, and it's not particularly efficient, but it's a decent start. This patch is a slightly modified version of the patch I attached to PR3259, and it fixes that bug. I was sort of planning on improving it, but I think it's okay as-is, especially since it looks like CodeGen doesn't have any use for this sort of data structure. The only significant change I can think of from the version I attached to PR3259 is that this version skips running the checking code when a function doesn't contain any labels. This patch doesn't cover case statements, which also need similar checking; I'm not sure how we should deal with that. Extending the goto checking to also check case statements wouldn't be too hard; it's just a matter of keeping track of the scope of the closest switch and checking that the scope of every case is the same as the scope of the switch. That said, it would likely be a performance hit to run this check on every function (it's an extra pass over the entire function), so we probably want some other solution. llvm-svn: 65678
2009-02-28 13:41:13 +08:00
int test1(int x) {
goto L; // expected-error{{illegal goto into protected scope}}
int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
int b[x]; // expected-note {{jump bypasses initialization of variable length array}}
Start of checking for gotos which jump to an illegal destination. As far as I know, this catches all cases of jumping into the scope of a variable with a variably modified type (excluding statement expressions) in C. This is missing some stuff we probably want to check (other kinds of variably modified declarations, statement expressions, indirect gotos/addresses of labels in a scope, ObjC @try/@finally, cleanup attribute), the diagnostics aren't very good, and it's not particularly efficient, but it's a decent start. This patch is a slightly modified version of the patch I attached to PR3259, and it fixes that bug. I was sort of planning on improving it, but I think it's okay as-is, especially since it looks like CodeGen doesn't have any use for this sort of data structure. The only significant change I can think of from the version I attached to PR3259 is that this version skips running the checking code when a function doesn't contain any labels. This patch doesn't cover case statements, which also need similar checking; I'm not sure how we should deal with that. Extending the goto checking to also check case statements wouldn't be too hard; it's just a matter of keeping track of the scope of the closest switch and checking that the scope of every case is the same as the scope of the switch. That said, it would likely be a performance hit to run this check on every function (it's an extra pass over the entire function), so we probably want some other solution. llvm-svn: 65678
2009-02-28 13:41:13 +08:00
L:
return sizeof a;
}
int test2(int x) {
goto L; // expected-error{{illegal goto into protected scope}}
typedef int a[x]; // expected-note {{jump bypasses initialization of VLA typedef}}
L:
return sizeof(a);
}
void test3clean(int*);
int test3() {
goto L; // expected-error{{illegal goto into protected scope}}
int a __attribute((cleanup(test3clean))); // expected-note {{jump bypasses initialization of declaration with __attribute__((cleanup))}}
2009-04-18 15:54:11 +08:00
L:
return a;
}
int test4(int x) {
goto L; // expected-error{{illegal goto into protected scope}}
int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
2009-04-18 15:54:11 +08:00
test4(x);
L:
return sizeof a;
}
2009-04-18 15:54:11 +08:00
int test5(int x) {
int a[x];
test5(x);
goto L; // Ok.
L:
goto L; // Ok.
return sizeof a;
}
int test6() {
// just plain invalid.
goto x; // expected-error {{use of undeclared label 'x'}}
}
void test7(int x) {
switch (x) {
case 1: ;
int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
case 2: // expected-error {{illegal switch case into protected scope}}
a[1] = 2;
break;
}
}
int test8(int x) {
2009-04-19 06:56:52 +08:00
// For statement.
goto L2; // expected-error {{illegal goto into protected scope}}
for (int arr[x]; // expected-note {{jump bypasses initialization of variable length array}}
2009-04-19 06:56:52 +08:00
; ++x)
L2:;
2009-04-19 06:56:52 +08:00
// Statement expressions.
goto L3; // expected-error {{illegal goto into protected scope}}
int Y = ({ int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
L3: 4; });
goto L4; // expected-error {{illegal goto into protected scope}}
{
int A[x], // expected-note {{jump bypasses initialization of variable length array}}
B[x]; // expected-note {{jump bypasses initialization of variable length array}}
L4: ;
}
{
L5: ;// ok
int A[x], B = ({ if (x)
goto L5;
else
goto L6;
4; });
L6:; // ok.
if (x) goto L6; // ok
}
{
L7: ;// ok
int A[x], B = ({ if (x)
goto L7;
else
goto L8; // expected-error {{illegal goto into protected scope}}
4; }),
C[x]; // expected-note {{jump bypasses initialization of variable length array}}
L8:; // bad
}
{
L9: ;// ok
int A[({ if (x)
goto L9;
else
// FIXME:
goto L10; // fixme-error {{illegal goto into protected scope}}
4; })];
L10:; // bad
}
{
// FIXME: Crashes goto checker.
//goto L11;// ok
//int A[({ L11: 4; })];
}
{
goto L12;
int y = 4; // fixme-warn: skips initializer.
L12:
;
}
2009-04-19 06:56:52 +08:00
// Statement expressions 2.
goto L1; // expected-error {{illegal goto into protected scope}}
return x == ({
int a[x]; // expected-note {{jump bypasses initialization of variable length array}}
2009-04-19 06:56:52 +08:00
L1:
42; });
}
void test9(int n, void *P) {
int Y;
int Z = 4;
goto *P; // ok.
L2: ;
int a[n]; // expected-note {{jump bypasses initialization of variable length array}}
L3:
goto *P; // expected-error {{illegal indirect goto in protected scope, unknown effect on scopes}}
void *Ptrs[] = {
&&L2,
&&L3 // FIXME: Not Ok.
};
}