[analyzer] pr37204: Take signedness into account in getTruthValue().
authorArtem Dergachev <artem.dergachev@gmail.com>
Tue, 7 Aug 2018 02:27:38 +0000 (02:27 +0000)
committerArtem Dergachev <artem.dergachev@gmail.com>
Tue, 7 Aug 2018 02:27:38 +0000 (02:27 +0000)
It now actually produces a signed APSInt when the QualType passed into it is
signed, which is what any caller would expect.

Fixes a couple of crashes.

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

llvm-svn: 339088

clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
clang/test/Analysis/casts.c

index b72b158194c736d6f45dc9970a5f6f0d9c1ad41b..1c5d4eb2de32db32d4af883a5efccf2424c665a9 100644 (file)
@@ -211,7 +211,8 @@ public:
   }
 
   const llvm::APSInt &getTruthValue(bool b, QualType T) {
-    return getValue(b ? 1 : 0, Ctx.getIntWidth(T), true);
+    return getValue(b ? 1 : 0, Ctx.getIntWidth(T),
+                    T->isUnsignedIntegerOrEnumerationType());
   }
 
   const llvm::APSInt &getTruthValue(bool b) {
index 86fb7da58ec2215a4f94f206bf448cd32417df00..88bdc55908ee7431c3a12a61a1d9e3201a5f50cb 100644 (file)
@@ -182,3 +182,9 @@ void testLocNonLocSymbolRemainder(int a, int *b) {
     c += 1;
   }
 }
+
+void testSwitchWithSizeofs() {
+  switch (sizeof(char) == 1) { // expected-warning{{switch condition has boolean value}}
+  case sizeof(char):; // no-crash
+  }
+}