[clang] 881b6a0 - [analyzer][NFC] Re-enable skipped SValTests by relaxing expectations

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 19 06:16:36 PST 2022


Author: Balazs Benics
Date: 2022-01-19T15:16:18+01:00
New Revision: 881b6a009fb6e2dd5fb924524cd6eacd14148a08

URL: https://github.com/llvm/llvm-project/commit/881b6a009fb6e2dd5fb924524cd6eacd14148a08
DIFF: https://github.com/llvm/llvm-project/commit/881b6a009fb6e2dd5fb924524cd6eacd14148a08.diff

LOG: [analyzer][NFC] Re-enable skipped SValTests by relaxing expectations

Some tests were skipped in D114454 to resolve test failures on some
platforms, where the pointers have different bitwidth than expected.
This patch re-enables these tests, by relaxing the requirements on the
types of the SVal.

The issue:
There is no way to reconstruct the type of the `SVal` perfectly
accurately, since there could be multiple types having the required
bitwidth and signedness.
Consider platforms where `int` and `long` have the same bitwidth.
Additionally, we need to be careful about casting a pointer to an
integral representation, because we don't know what smallest integral
type can represent that.

To workaround these issues, I propose enforcing a type that has the
same signedness and bitwidth as the expected type, instead of perfect
equality.

In the `GetLocAsIntType` test, in case of pointer-to-integral casts
I'm using the widest standard integral type (long long) to make sure
that the pointer can be represented by the type without losing
precision. This won't affect the test in any meaningful way, since the
type of the `lvalue` remained the same.

In one case, I had to replace `getUIntPtrType()` with `UnsignedLongTy`
because on some platforms `getUIntPtrType()` is different then `long
int`.

In this patch, I also enforce that the tests must compile without
errors, to prevent narrowing conversions in the future.

Reviewed By: stevewan

Differential Revision: https://reviews.llvm.org/D115349

Added: 
    

Modified: 
    clang/unittests/StaticAnalyzer/SValTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/StaticAnalyzer/SValTest.cpp b/clang/unittests/StaticAnalyzer/SValTest.cpp
index 08853a7bb83f2..1a41faf098996 100644
--- a/clang/unittests/StaticAnalyzer/SValTest.cpp
+++ b/clang/unittests/StaticAnalyzer/SValTest.cpp
@@ -92,20 +92,15 @@ class SValCollector : public Checker<check::Bind, check::EndAnalysis> {
   mutable SVals CollectedSVals;
 };
 
+static void expectSameSignAndBitWidth(QualType ExpectedTy, QualType ActualTy,
+                                      const ASTContext &Context) {
+  EXPECT_EQ(ExpectedTy->isUnsignedIntegerType(),
+            ActualTy->isUnsignedIntegerType());
+  EXPECT_EQ(Context.getTypeSize(ExpectedTy), Context.getTypeSize(ActualTy));
+}
+
 // Fixture class for parameterized SValTest
-class SValTest : public testing::TestWithParam<TestClangConfig> {
-protected:
-  // FIXME: The tests "GetConstType" and "GetLocAsIntType" infer the type of
-  // integrals based on their bitwidth. This is not a reliable method on
-  // platforms where 
diff erent integrals have the same width.
-  bool skipTest(StringRef TestName) {
-    std::string target = GetParam().Target;
-    return (target == "powerpc-ibm-aix" || target == "i686-apple-darwin9" ||
-            target == "wasm32-unknown-unknown" ||
-            target == "wasm64-unknown-unknown") &&
-           (TestName == "GetConstType" || TestName == "GetLocAsIntType");
-  }
-};
+class SValTest : public testing::TestWithParam<TestClangConfig> {};
 
 // SVAL_TEST is a combined way of providing a short code snippet and
 // to test some programmatic predicates on symbolic values produced by the
@@ -152,14 +147,8 @@ class SValTest : public testing::TestWithParam<TestClangConfig> {
   }                                                                            \
                                                                                \
   TEST_P(SValTest, NAME) {                                                     \
-    if (skipTest(#NAME)) {                                                     \
-      std::string target = GetParam().Target;                                  \
-      GTEST_SKIP() << "certain integrals have the same bitwidth on "           \
-                   << target;                                                  \
-      return;                                                                  \
-    }                                                                          \
-    runCheckerOnCodeWithArgs<add##NAME##SValCollector>(                        \
-        CODE, GetParam().getCommandLineArgs());                                \
+    EXPECT_TRUE(runCheckerOnCodeWithArgs<add##NAME##SValCollector>(            \
+        CODE, GetParam().getCommandLineArgs()));                               \
   }                                                                            \
   void NAME##SValCollector::test(ExprEngine &Engine,                           \
                                  const ASTContext &Context) const
@@ -179,27 +168,30 @@ void foo() {
 
   SVal Y = getByName("y");
   ASSERT_FALSE(Y.getType(Context).isNull());
-  EXPECT_EQ(Context.getUIntPtrType(), Y.getType(Context));
+  expectSameSignAndBitWidth(Context.getUIntPtrType(), Y.getType(Context),
+                            Context);
 }
 
 SVAL_TEST(GetLocAsIntType, R"(
 void foo(int *x) {
-  long int a = (long int)x;
-  unsigned b = (long unsigned)&a;
-  int c = (long int)nullptr;
+  long int a = (long long int)x;
+  unsigned b = (long long unsigned)&a;
+  int c = (long long int)nullptr;
 })") {
   SVal A = getByName("a");
   ASSERT_FALSE(A.getType(Context).isNull());
+
   // TODO: Turn it into signed long
-  EXPECT_EQ(Context.getUIntPtrType(), A.getType(Context));
+  expectSameSignAndBitWidth(Context.UnsignedLongTy, A.getType(Context),
+                            Context);
 
   SVal B = getByName("b");
   ASSERT_FALSE(B.getType(Context).isNull());
-  EXPECT_EQ(Context.UnsignedIntTy, B.getType(Context));
+  expectSameSignAndBitWidth(Context.UnsignedIntTy, B.getType(Context), Context);
 
   SVal C = getByName("c");
   ASSERT_FALSE(C.getType(Context).isNull());
-  EXPECT_EQ(Context.IntTy, C.getType(Context));
+  expectSameSignAndBitWidth(Context.IntTy, C.getType(Context), Context);
 }
 
 SVAL_TEST(GetSymExprType, R"(


        


More information about the cfe-commits mailing list