Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
3 changed files
with
112 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 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; | ||
} |