2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,deadcode.DeadStores,alpha.deadcode.UnreachableCode -verify -analyzer-opt-analyze-nested-blocks -Wno-unused-value %s
|
2010-07-24 07:04:53 +08:00
|
|
|
|
|
|
|
extern void foo(int a);
|
|
|
|
|
2010-07-28 07:30:21 +08:00
|
|
|
// The first few tests are non-path specific - we should be able to find them
|
|
|
|
|
2010-07-24 07:04:53 +08:00
|
|
|
void test(unsigned a) {
|
|
|
|
switch (a) {
|
|
|
|
a += 5; // expected-warning{{never executed}}
|
|
|
|
case 2:
|
|
|
|
a *= 10;
|
|
|
|
case 3:
|
|
|
|
a %= 2;
|
|
|
|
}
|
|
|
|
foo(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test2(unsigned a) {
|
|
|
|
help:
|
|
|
|
if (a > 0)
|
|
|
|
return;
|
|
|
|
if (a == 0)
|
|
|
|
return;
|
|
|
|
foo(a); // expected-warning{{never executed}}
|
|
|
|
goto help;
|
|
|
|
}
|
|
|
|
|
2010-07-28 07:30:21 +08:00
|
|
|
void test3(unsigned a) {
|
|
|
|
while(1);
|
|
|
|
if (a > 5) { // expected-warning{{never executed}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These next tests are path-sensitive
|
|
|
|
|
|
|
|
void test4() {
|
2010-07-24 07:04:53 +08:00
|
|
|
int a = 5;
|
|
|
|
|
|
|
|
while (a > 1)
|
|
|
|
a -= 2;
|
|
|
|
|
|
|
|
if (a > 1) {
|
|
|
|
a = a + 56; // expected-warning{{never executed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void bar(char c);
|
|
|
|
|
|
|
|
void test5(const char *c) {
|
|
|
|
foo(c[0]);
|
|
|
|
|
|
|
|
if (!c) {
|
|
|
|
bar(1); // expected-warning{{never executed}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 07:30:21 +08:00
|
|
|
// These next tests are false positives and should not generate warnings
|
|
|
|
|
2010-07-27 11:39:53 +08:00
|
|
|
void test6(const char *c) {
|
|
|
|
if (c) return;
|
|
|
|
if (!c) return;
|
|
|
|
__builtin_unreachable(); // no-warning
|
2018-02-14 05:31:47 +08:00
|
|
|
__builtin_assume(0); // no-warning
|
2010-07-27 11:39:53 +08:00
|
|
|
}
|
|
|
|
|
2010-07-28 07:30:21 +08:00
|
|
|
// Compile-time constant false positives
|
|
|
|
#define CONSTANT 0
|
|
|
|
enum test_enum { Off, On };
|
|
|
|
void test7() {
|
|
|
|
if (CONSTANT)
|
|
|
|
return; // no-warning
|
|
|
|
|
|
|
|
if (sizeof(int))
|
|
|
|
return; // no-warning
|
|
|
|
|
|
|
|
if (Off)
|
|
|
|
return; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void test8() {
|
|
|
|
static unsigned a = 0;
|
|
|
|
|
|
|
|
if (a)
|
|
|
|
a = 123; // no-warning
|
|
|
|
|
|
|
|
a = 5;
|
|
|
|
}
|
|
|
|
|
2010-08-06 01:53:44 +08:00
|
|
|
// Check for bugs where multiple statements are reported
|
|
|
|
void test9(unsigned a) {
|
|
|
|
switch (a) {
|
|
|
|
if (a) // expected-warning{{never executed}}
|
|
|
|
foo(a + 5); // no-warning
|
2010-10-07 07:02:25 +08:00
|
|
|
else // no-warning
|
|
|
|
foo(a); // no-warning
|
2010-08-06 01:53:44 +08:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-10-07 07:02:25 +08:00
|
|
|
|
|
|
|
// Tests from flow-sensitive version
|
|
|
|
void test10() {
|
|
|
|
goto c;
|
|
|
|
d:
|
|
|
|
goto e; // expected-warning {{never executed}}
|
|
|
|
c: ;
|
|
|
|
int i;
|
|
|
|
return;
|
|
|
|
goto b; // expected-warning {{never executed}}
|
|
|
|
goto a; // expected-warning {{never executed}}
|
|
|
|
b:
|
2011-02-12 07:24:26 +08:00
|
|
|
i = 1; // no-warning
|
2010-10-07 07:02:25 +08:00
|
|
|
a:
|
2011-02-12 07:24:26 +08:00
|
|
|
i = 2; // no-warning
|
2010-10-07 07:02:25 +08:00
|
|
|
goto f;
|
|
|
|
e:
|
|
|
|
goto d;
|
|
|
|
f: ;
|
|
|
|
}
|
2012-02-29 14:05:28 +08:00
|
|
|
|
|
|
|
// test11: we can actually end up in the default case, even if it is not
|
|
|
|
// obvious: there might be something wrong with the given argument.
|
|
|
|
enum foobar { FOO, BAR };
|
|
|
|
extern void error();
|
|
|
|
void test11(enum foobar fb) {
|
|
|
|
switch (fb) {
|
|
|
|
case FOO:
|
|
|
|
break;
|
|
|
|
case BAR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(); // no-warning
|
|
|
|
return;
|
|
|
|
error(); // expected-warning {{never executed}}
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 01:03:12 +08:00
|
|
|
|
|
|
|
void inlined(int condition) {
|
|
|
|
if (condition) {
|
|
|
|
foo(5); // no-warning
|
|
|
|
} else {
|
|
|
|
foo(6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testInlined() {
|
|
|
|
extern int coin();
|
|
|
|
int cond = coin();
|
|
|
|
if (!cond) {
|
|
|
|
inlined(0);
|
|
|
|
if (cond) {
|
|
|
|
foo(5); // expected-warning {{never executed}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-28 18:39:53 +08:00
|
|
|
|
|
|
|
// Don't warn about unreachable VarDecl.
|
|
|
|
void dostuff(int*A);
|
2016-10-03 17:45:35 +08:00
|
|
|
void varDecl1(int X) {
|
2016-09-28 18:39:53 +08:00
|
|
|
switch (X) {
|
|
|
|
int A; // No warning here.
|
|
|
|
case 1:
|
|
|
|
dostuff(&A);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
dostuff(&A);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 17:45:35 +08:00
|
|
|
void varDecl2(int X) {
|
|
|
|
switch (X) {
|
|
|
|
int A=1; // expected-warning {{never executed}}
|
|
|
|
case 1:
|
|
|
|
dostuff(&A);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
dostuff(&A);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-28 18:39:53 +08:00
|
|
|
|
2016-10-03 16:28:51 +08:00
|
|
|
// Ensure that ExplodedGraph and unoptimized CFG match.
|
|
|
|
void test12(int x) {
|
|
|
|
switch (x) {
|
|
|
|
case 1:
|
|
|
|
break; // not unreachable
|
|
|
|
case 2:
|
|
|
|
do { } while (0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-07 22:21:08 +08:00
|
|
|
|
|
|
|
// Don't merge return nodes in ExplodedGraph unless they are same.
|
|
|
|
extern int table[];
|
|
|
|
static int inlineFunction(const int i) {
|
|
|
|
if (table[i] != 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void test13(int i) {
|
|
|
|
int x = inlineFunction(i);
|
|
|
|
x && x < 10; // no-warning
|
|
|
|
}
|
2016-10-18 21:16:53 +08:00
|
|
|
|
|
|
|
// Don't warn in a macro
|
|
|
|
#define RETURN(X) do { return; } while (0)
|
|
|
|
void macro(void) {
|
|
|
|
RETURN(1); // no-warning
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:26:56 +08:00
|
|
|
// Avoid FP when macro argument is known
|
|
|
|
void writeSomething(int *x);
|
|
|
|
#define MACRO(C) \
|
|
|
|
if (!C) { \
|
|
|
|
static int x; \
|
|
|
|
writeSomething(&x); \
|
|
|
|
}
|
|
|
|
void macro2(void) {
|
|
|
|
MACRO(1);
|
|
|
|
}
|