mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-29 11:46:06 +00:00

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
23 lines
807 B
C++
23 lines
807 B
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() {
|
|
// Do not trust global initializers in C++.
|
|
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}}
|
|
return 0;
|
|
}
|