From 96ec9b6ff2f0bb9078306e3857f09974603ccbe4 Mon Sep 17 00:00:00 2001 From: Gabor Marton Date: Wed, 15 Sep 2021 18:32:19 +0200 Subject: [PATCH] [Analyzer] ConversionChecker: track back the cast expression Adding trackExpressionValue to the checker so it tracks the value of the implicit cast's DeclRefExpression up to initialization/assignment. This way the report becomes cleaner. Differential Revision: https://reviews.llvm.org/D109836 --- .../StaticAnalyzer/Checkers/ConversionChecker.cpp | 12 +++++----- clang/test/Analysis/conversion-tracking-notes.c | 26 ++++++++++++++++++++++ clang/test/Analysis/conversion.c | 5 ++++- 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 clang/test/Analysis/conversion-tracking-notes.c diff --git a/clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp index 4216a68..8da482a 100644 --- a/clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp @@ -49,7 +49,8 @@ private: bool isLossOfSign(const ImplicitCastExpr *Cast, CheckerContext &C) const; - void reportBug(ExplodedNode *N, CheckerContext &C, const char Msg[]) const; + void reportBug(ExplodedNode *N, const Expr *E, CheckerContext &C, + const char Msg[]) const; }; } @@ -108,20 +109,21 @@ void ConversionChecker::checkPreStmt(const ImplicitCastExpr *Cast, if (!N) return; if (LossOfSign) - reportBug(N, C, "Loss of sign in implicit conversion"); + reportBug(N, Cast, C, "Loss of sign in implicit conversion"); if (LossOfPrecision) - reportBug(N, C, "Loss of precision in implicit conversion"); + reportBug(N, Cast, C, "Loss of precision in implicit conversion"); } } -void ConversionChecker::reportBug(ExplodedNode *N, CheckerContext &C, - const char Msg[]) const { +void ConversionChecker::reportBug(ExplodedNode *N, const Expr *E, + CheckerContext &C, const char Msg[]) const { if (!BT) BT.reset( new BuiltinBug(this, "Conversion", "Possible loss of sign/precision.")); // Generate a report for this bug. auto R = std::make_unique(*BT, Msg, N); + bugreporter::trackExpressionValue(N, E, *R); C.emitReport(std::move(R)); } diff --git a/clang/test/Analysis/conversion-tracking-notes.c b/clang/test/Analysis/conversion-tracking-notes.c new file mode 100644 index 0000000..94b3dc1 --- /dev/null +++ b/clang/test/Analysis/conversion-tracking-notes.c @@ -0,0 +1,26 @@ +// RUN: %clang_analyze_cc1 %s \ +// RUN: -Wno-conversion -Wno-tautological-constant-compare \ +// RUN: -analyzer-checker=core,apiModeling,alpha.core.Conversion \ +// RUN: -analyzer-output=text \ +// RUN: -verify + +unsigned char U8; +signed char S8; + +void track_assign() { + unsigned long L = 1000; // expected-note {{'L' initialized to 1000}} + int I = -1; // expected-note {{'I' initialized to -1}} + U8 *= L; // expected-warning {{Loss of precision in implicit conversion}} + // expected-note@-1 {{Loss of precision in implicit conversion}} + L *= I; // expected-warning {{Loss of sign in implicit conversion}} + // expected-note@-1 {{Loss of sign in implicit conversion}} +} + +void track_relational(unsigned U, signed S) { + if (S < -10) { // expected-note {{Taking true branch}} + // expected-note@-1 {{Assuming the condition is true}} + if (U < S) { // expected-warning {{Loss of sign in implicit conversion}} + // expected-note@-1 {{Loss of sign in implicit conversion}} + } + } +} diff --git a/clang/test/Analysis/conversion.c b/clang/test/Analysis/conversion.c index 8b77e25..84eccb7 100644 --- a/clang/test/Analysis/conversion.c +++ b/clang/test/Analysis/conversion.c @@ -1,4 +1,7 @@ -// RUN: %clang_analyze_cc1 -Wno-conversion -Wno-tautological-constant-compare -analyzer-checker=core,apiModeling,alpha.core.Conversion -verify %s +// RUN: %clang_analyze_cc1 %s \ +// RUN: -Wno-conversion -Wno-tautological-constant-compare \ +// RUN: -analyzer-checker=core,apiModeling,alpha.core.Conversion \ +// RUN: -verify unsigned char U8; signed char S8; -- 2.7.4