[analyzer] Fix crash when reasoning about C11 atomics (PR49422)
authorValeriy Savchenko <vsavchenko@apple.com>
Wed, 24 Mar 2021 15:58:22 +0000 (18:58 +0300)
committerValeriy Savchenko <vsavchenko@apple.com>
Tue, 30 Mar 2021 13:04:19 +0000 (16:04 +0300)
rdar://75020762

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

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

index 9f464e8..f59b254 100644 (file)
@@ -139,6 +139,12 @@ public:
 
   /// Returns the type of the APSInt used to store values of the given QualType.
   APSIntType getAPSIntType(QualType T) const {
+    // For the purposes of the analysis and constraints, we treat atomics
+    // as their underlying types.
+    if (const AtomicType *AT = T->getAs<AtomicType>()) {
+      T = AT->getValueType();
+    }
+
     assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
     return APSIntType(Ctx.getIntWidth(T),
                       !T->isSignedIntegerOrEnumerationType());
index b3d2d35..ef1a216 100644 (file)
@@ -93,3 +93,11 @@ void test_atomic_compare_exchange_weak(struct RefCountedStruct *s) {
   clang_analyzer_eval(s->refCount == 3); // expected-warning {{UNKNOWN}}
   clang_analyzer_eval(expected == 2); // expected-warning {{UNKNOWN}}
 }
+
+// PR49422
+void test_atomic_compare(int input) {
+  _Atomic(int) x = input;
+  if (x > 0) {
+    // no crash
+  }
+}