From 2df120784af40c9cfebaf2a505a5ebe74c51b44a Mon Sep 17 00:00:00 2001 From: Gabor Marton Date: Wed, 1 Jun 2022 16:29:51 +0200 Subject: [PATCH] [analyzer] Fix assertion in simplifySymbolCast Depends on D128068. Added a new test code that fails an assertion in the baseline. That is because `getAPSIntType` works only with integral types. Differential Revision: https://reviews.llvm.org/D126779 --- clang/lib/StaticAnalyzer/Core/SValBuilder.cpp | 4 ++++ clang/test/Analysis/produce-symbolcast_x86.cpp | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp b/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp index 7b08acd..cf3d13f 100644 --- a/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp +++ b/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp @@ -1103,6 +1103,10 @@ nonloc::SymbolVal SValBuilder::simplifySymbolCast(nonloc::SymbolVal V, SymbolRef RootSym = cast(SE)->getOperand(); QualType RT = RootSym->getType().getCanonicalType(); + // FIXME support simplification from non-integers. + if (!RT->isIntegralOrEnumerationType()) + return makeNonLoc(SE, T, CastTy); + BasicValueFactory &BVF = getBasicValueFactory(); APSIntType CTy = BVF.getAPSIntType(CastTy); APSIntType TTy = BVF.getAPSIntType(T); diff --git a/clang/test/Analysis/produce-symbolcast_x86.cpp b/clang/test/Analysis/produce-symbolcast_x86.cpp index 0db1007..e4968f7 100644 --- a/clang/test/Analysis/produce-symbolcast_x86.cpp +++ b/clang/test/Analysis/produce-symbolcast_x86.cpp @@ -11,6 +11,15 @@ using ullong = unsigned long long; template void clang_analyzer_dump(T); +void test_double(int n) { + double D = n / 30; + clang_analyzer_dump(D); // expected-warning{{(double) ((reg_$0) / 30)}} + char C = D; + clang_analyzer_dump(C); // expected-warning{{(char) ((double) ((reg_$0) / 30))}} + int I = C; // assertion should not fail here! + clang_analyzer_dump(I); // expected-warning{{(int) ((char) ((double) ((reg_$0) / 30)))}} +} + void test_schar(schar x) { clang_analyzer_dump(x); // expected-warning{{reg_$0}} -- 2.7.4