martong created this revision. martong added reviewers: steakhal, Szelethus, NoQ. Herald added subscribers: manas, ASDenysPetrov, gamesh411, dkrupp, donat.nagy, mikhail.ramalho, a.sidorin, rnkovacs, szepet, baloghadamsoftware, xazax.hun, whisperity. martong requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D109836 Files: clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp clang/test/Analysis/conversion-tracking-notes.c clang/test/Analysis/conversion.c Index: clang/test/Analysis/conversion.c =================================================================== --- clang/test/Analysis/conversion.c +++ 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; Index: clang/test/Analysis/conversion-tracking-notes.c =================================================================== --- /dev/null +++ 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}} + } + } +} Index: clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp @@ -49,7 +49,8 @@ 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 @@ 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<PathSensitiveBugReport>(*BT, Msg, N); + bugreporter::trackExpressionValue(N, E, *R); C.emitReport(std::move(R)); }
Index: clang/test/Analysis/conversion.c =================================================================== --- clang/test/Analysis/conversion.c +++ 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; Index: clang/test/Analysis/conversion-tracking-notes.c =================================================================== --- /dev/null +++ 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}} + } + } +} Index: clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp =================================================================== --- clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/ConversionChecker.cpp @@ -49,7 +49,8 @@ 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 @@ 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<PathSensitiveBugReport>(*BT, Msg, N); + bugreporter::trackExpressionValue(N, E, *R); C.emitReport(std::move(R)); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits