[llvm-branch-commits] [cfe-branch] r371058 - Merging r369760:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Sep 5 04:37:40 PDT 2019


Author: hans
Date: Thu Sep  5 04:37:40 2019
New Revision: 371058

URL: http://llvm.org/viewvc/llvm-project?rev=371058&view=rev
Log:
Merging r369760:
------------------------------------------------------------------------
r369760 | szelethus | 2019-08-23 16:21:13 +0200 (Fri, 23 Aug 2019) | 13 lines

[analyzer] Avoid unnecessary enum range check on LValueToRValue casts

Summary: EnumCastOutOfRangeChecker should not perform enum range checks on LValueToRValue casts, since this type of cast does not actually change the underlying type.   Performing the unnecessary check actually triggered an assertion failure deeper in EnumCastOutOfRange for certain input (which is captured in the accompanying test code).

Reviewers: #clang, Szelethus, gamesh411, NoQ

Reviewed By: Szelethus, gamesh411, NoQ

Subscribers: NoQ, gamesh411, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp, Charusso, bjope, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D66014
------------------------------------------------------------------------

Added:
    cfe/branches/release_90/test/Analysis/enum-cast-out-of-range.c
      - copied unchanged from r369760, cfe/trunk/test/Analysis/enum-cast-out-of-range.c
Modified:
    cfe/branches/release_90/   (props changed)
    cfe/branches/release_90/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp
    cfe/branches/release_90/test/Analysis/enum-cast-out-of-range.cpp

Propchange: cfe/branches/release_90/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Sep  5 04:37:40 2019
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:366429,366447-366448,366457,366474,366480,366483,366511,366670,366694,366699,366878,367008,367039,367055,367103,367134,367301,367305,367323,367387,367403,367520,367530,367661,367675,367802,367823,367906,368104,368202,368552,368561,368874,368940,369043,369093,369251,369641,369749,369829,369834,370035,370073,370850
+/cfe/trunk:366429,366447-366448,366457,366474,366480,366483,366511,366670,366694,366699,366878,367008,367039,367055,367103,367134,367301,367305,367323,367387,367403,367520,367530,367661,367675,367802,367823,367906,368104,368202,368552,368561,368874,368940,369043,369093,369251,369641,369749,369760,369829,369834,370035,370073,370850
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_90/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_90/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp?rev=371058&r1=371057&r2=371058&view=diff
==============================================================================
--- cfe/branches/release_90/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp (original)
+++ cfe/branches/release_90/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp Thu Sep  5 04:37:40 2019
@@ -91,6 +91,22 @@ void EnumCastOutOfRangeChecker::reportWa
 
 void EnumCastOutOfRangeChecker::checkPreStmt(const CastExpr *CE,
                                              CheckerContext &C) const {
+
+  // Only perform enum range check on casts where such checks are valid.  For
+  // all other cast kinds (where enum range checks are unnecessary or invalid),
+  // just return immediately.  TODO: The set of casts whitelisted for enum
+  // range checking may be incomplete.  Better to add a missing cast kind to
+  // enable a missing check than to generate false negatives and have to remove
+  // those later.
+  switch (CE->getCastKind()) {
+  case CK_IntegralCast:
+    break;
+
+  default:
+    return;
+    break;
+  }
+
   // Get the value of the expression to cast.
   const llvm::Optional<DefinedOrUnknownSVal> ValueToCast =
       C.getSVal(CE->getSubExpr()).getAs<DefinedOrUnknownSVal>();

Modified: cfe/branches/release_90/test/Analysis/enum-cast-out-of-range.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_90/test/Analysis/enum-cast-out-of-range.cpp?rev=371058&r1=371057&r2=371058&view=diff
==============================================================================
--- cfe/branches/release_90/test/Analysis/enum-cast-out-of-range.cpp (original)
+++ cfe/branches/release_90/test/Analysis/enum-cast-out-of-range.cpp Thu Sep  5 04:37:40 2019
@@ -150,7 +150,15 @@ void scopedSpecifiedCStyle() {
   scoped_specified_t InvalidAfterRangeEnd = (scoped_specified_t)(5); // expected-warning {{The value provided to the cast expression is not in the valid range of values for the enum}}
 }
 
-void rangeContstrained1(int input) {
+unscoped_unspecified_t unused;
+void unusedExpr() {
+  // following line is not something that EnumCastOutOfRangeChecker should evaluate.  checker should either ignore this line
+  // or process it without producing any warnings.  However, compilation will (and should) still generate a warning having
+  // nothing to do with this checker.
+  unused; // expected-warning {{expression result unused}}
+}
+
+void rangeConstrained1(int input) {
   if (input > -5 && input < 5)
     auto value = static_cast<scoped_specified_t>(input); // OK. Being conservative, this is a possibly good value.
 }




More information about the llvm-branch-commits mailing list