llvm/clang/test/Analysis/main.c
Artem Dergachev 8b2a39e937 [analyzer] Trust global initializers when analyzing main().
If the global variable has an initializer, we'll ignore it because we're usually
not analyzing the program from the beginning, which means that the global
variable may have changed before we start our analysis.

However when we're analyzing main() as the top-level function, we can rely
on global initializers to still be valid. At least in C; in C++ we have global
constructors that can still break this logic.

This patch allows the Static Analyzer to load constant initializers from
global variables if the top-level function of the current analysis is main().

Differential Revision: https://reviews.llvm.org/D65361

llvm-svn: 370244
2019-08-28 18:44:32 +00:00

33 lines
1.2 KiB
C

// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
int x = 1;
struct {
int a, b;
} s = {2, 3};
int arr[] = {4, 5, 6};
void clang_analyzer_eval(int);
int main() {
// In main() we know that the initial values are still valid.
clang_analyzer_eval(x == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(s.a == 2); // expected-warning{{TRUE}}
clang_analyzer_eval(s.b == 3); // expected-warning{{TRUE}}
clang_analyzer_eval(arr[0] == 4); // expected-warning{{TRUE}}
clang_analyzer_eval(arr[1] == 5); // expected-warning{{TRUE}}
clang_analyzer_eval(arr[2] == 6); // expected-warning{{TRUE}}
return 0;
}
void foo() {
// In other functions these values may already be overwritten.
clang_analyzer_eval(x == 1); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
clang_analyzer_eval(s.a == 2); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
clang_analyzer_eval(s.b == 3); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
clang_analyzer_eval(arr[0] == 4); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
clang_analyzer_eval(arr[1] == 5); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
clang_analyzer_eval(arr[2] == 6); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
}