Skip to content

Commit 37faed9

Browse files
committed
[analyzer] Improve usability of ExprInspectionChecker
Some of the magic functions take arguments of arbitrary type. However, for semantic correctness, the compiler still requires a declaration of these functions with the correct type. Since C does not have argument-type-overloaded function, this made those functions hard to use in C code. Improve this situation by allowing arbitrary suffixes in the affected magic functions' names, thus allowing the user to create different declarations for different types. A patch by Keno Fischer! Differential Revision: https://reviews.llvm.org/D30589 llvm-svn: 297325
1 parent d4e43ae commit 37faed9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clang/docs/analyzer/DebugChecks.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,21 @@ ExprInspection checks
178178
This function explains the value of its argument in a human-readable manner
179179
in the warning message. You can make as many overrides of its prototype
180180
in the test code as necessary to explain various integral, pointer,
181-
or even record-type values.
181+
or even record-type values. To simplify usage in C code (where overloading
182+
the function declaration is not allowed), you may append an arbitrary suffix
183+
to the function name, without affecting functionality.
182184

183185
Example usage::
184186

185187
void clang_analyzer_explain(int);
186188
void clang_analyzer_explain(void *);
187189

190+
// Useful in C code
191+
void clang_analyzer_explain_int(int);
192+
188193
void foo(int param, void *ptr) {
189194
clang_analyzer_explain(param); // expected-warning{{argument 'param'}}
195+
clang_analyzer_explain_int(param); // expected-warning{{argument 'param'}}
190196
if (!ptr)
191197
clang_analyzer_explain(ptr); // expected-warning{{memory address '0'}}
192198
}

clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ bool ExprInspectionChecker::evalCall(const CallExpr *CE,
7272
&ExprInspectionChecker::analyzerWarnIfReached)
7373
.Case("clang_analyzer_warnOnDeadSymbol",
7474
&ExprInspectionChecker::analyzerWarnOnDeadSymbol)
75-
.Case("clang_analyzer_explain", &ExprInspectionChecker::analyzerExplain)
76-
.Case("clang_analyzer_dump", &ExprInspectionChecker::analyzerDump)
75+
.StartsWith("clang_analyzer_explain", &ExprInspectionChecker::analyzerExplain)
76+
.StartsWith("clang_analyzer_dump", &ExprInspectionChecker::analyzerDump)
7777
.Case("clang_analyzer_getExtent", &ExprInspectionChecker::analyzerGetExtent)
7878
.Case("clang_analyzer_printState",
7979
&ExprInspectionChecker::analyzerPrintState)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core.builtin,debug.ExprInspection,unix.cstring -verify %s
2+
3+
struct S {
4+
int z;
5+
};
6+
7+
void clang_analyzer_explain_int(int);
8+
void clang_analyzer_explain_voidp(void *);
9+
void clang_analyzer_explain_S(struct S);
10+
11+
int glob;
12+
13+
void test_1(int param, void *ptr) {
14+
clang_analyzer_explain_voidp(&glob); // expected-warning-re{{{{^pointer to global variable 'glob'$}}}}
15+
clang_analyzer_explain_int(param); // expected-warning-re{{{{^argument 'param'$}}}}
16+
clang_analyzer_explain_voidp(ptr); // expected-warning-re{{{{^argument 'ptr'$}}}}
17+
if (param == 42)
18+
clang_analyzer_explain_int(param); // expected-warning-re{{{{^signed 32-bit integer '42'$}}}}
19+
}
20+
21+
void test_2(struct S s) {
22+
clang_analyzer_explain_S(s); //expected-warning-re{{{{^lazily frozen compound value of parameter 's'$}}}}
23+
clang_analyzer_explain_voidp(&s); // expected-warning-re{{{{^pointer to parameter 's'$}}}}
24+
clang_analyzer_explain_int(s.z); // expected-warning-re{{{{^initial value of field 'z' of parameter 's'$}}}}
25+
}

0 commit comments

Comments
 (0)