[clang] a1025e6 - [analyzer] Introduce clang_analyzer_dumpSvalType introspection function
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Fri May 13 08:08:27 PDT 2022
Author: Balazs Benics
Date: 2022-05-13T17:07:58+02:00
New Revision: a1025e6ffe9b43afb9c6569423c4ea1d0ba8ecf2
URL: https://github.com/llvm/llvm-project/commit/a1025e6ffe9b43afb9c6569423c4ea1d0ba8ecf2
DIFF: https://github.com/llvm/llvm-project/commit/a1025e6ffe9b43afb9c6569423c4ea1d0ba8ecf2.diff
LOG: [analyzer] Introduce clang_analyzer_dumpSvalType introspection function
In some rare cases the type of an SVal might be interesting.
This introspection function exposes this information in tests.
Reviewed By: martong
Differential Revision: https://reviews.llvm.org/D125532
Added:
Modified:
clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp
clang/test/Analysis/expr-inspection.c
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp
index 9253e9e6abfe..ea72d1dbb317 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp
@@ -40,6 +40,7 @@ class ExprInspectionChecker
void analyzerNumTimesReached(const CallExpr *CE, CheckerContext &C) const;
void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
+ void analyzerDumpSValType(const CallExpr *CE, CheckerContext &C) const;
void analyzerDump(const CallExpr *CE, CheckerContext &C) const;
void analyzerExplain(const CallExpr *CE, CheckerContext &C) const;
void analyzerPrintState(const CallExpr *CE, CheckerContext &C) const;
@@ -98,6 +99,8 @@ bool ExprInspectionChecker::evalCall(const CallEvent &Call,
&ExprInspectionChecker::analyzerDumpExtent)
.Case("clang_analyzer_dumpElementCount",
&ExprInspectionChecker::analyzerDumpElementCount)
+ .StartsWith("clang_analyzer_dumpSvalType",
+ &ExprInspectionChecker::analyzerDumpSValType)
.StartsWith("clang_analyzer_dump",
&ExprInspectionChecker::analyzerDump)
.Case("clang_analyzer_getExtent",
@@ -255,6 +258,16 @@ void ExprInspectionChecker::analyzerExplain(const CallExpr *CE,
reportBug(Ex.Visit(V), C);
}
+void ExprInspectionChecker::analyzerDumpSValType(const CallExpr *CE,
+ CheckerContext &C) const {
+ const Expr *Arg = getArgExpr(CE, C);
+ if (!Arg)
+ return;
+
+ QualType Ty = C.getSVal(Arg).getType(C.getASTContext());
+ reportBug(Ty.getAsString(), C);
+}
+
void ExprInspectionChecker::analyzerDump(const CallExpr *CE,
CheckerContext &C) const {
const Expr *Arg = getArgExpr(CE, C);
diff --git a/clang/test/Analysis/expr-inspection.c b/clang/test/Analysis/expr-inspection.c
index 33c90f95bc78..e6dbbabff0ff 100644
--- a/clang/test/Analysis/expr-inspection.c
+++ b/clang/test/Analysis/expr-inspection.c
@@ -6,12 +6,16 @@
void clang_analyzer_dump(int x);
void clang_analyzer_dump_pointer(int *p);
+void clang_analyzer_dumpSvalType(int x);
+void clang_analyzer_dumpSvalType_pointer(int *p);
void clang_analyzer_printState(void);
void clang_analyzer_numTimesReached(void);
void foo(int x) {
clang_analyzer_dump(x); // expected-warning{{reg_$0<int x>}}
clang_analyzer_dump(x + (-1)); // expected-warning{{(reg_$0<int x>) - 1}}
+ clang_analyzer_dumpSvalType(x); // expected-warning {{int}}
+
int y = 1;
for (; y < 3; ++y) {
clang_analyzer_numTimesReached(); // expected-warning{{2}}
@@ -53,4 +57,5 @@ struct S {
void test_field_dumps(struct S s, struct S *p) {
clang_analyzer_dump_pointer(&s.x); // expected-warning{{&s.x}}
clang_analyzer_dump_pointer(&p->x); // expected-warning{{&SymRegion{reg_$1<struct S * p>}.x}}
+ clang_analyzer_dumpSvalType_pointer(&s.x); // expected-warning {{int *}}
}
More information about the cfe-commits
mailing list