[clang] af7e1f0 - [analyzer] Fix crash when reasoning about C11 atomics (PR49422)

Valeriy Savchenko via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 30 06:04:49 PDT 2021


Author: Valeriy Savchenko
Date: 2021-03-30T16:04:19+03:00
New Revision: af7e1f07ac03074647897498358aaec165c1aaea

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

LOG: [analyzer] Fix crash when reasoning about C11 atomics (PR49422)

rdar://75020762

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
index 9f464e82304f4..f59b254094db8 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h
@@ -139,6 +139,12 @@ class BasicValueFactory {
 
   /// 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());

diff  --git a/clang/test/Analysis/atomics.c b/clang/test/Analysis/atomics.c
index b3d2d352a2283..ef1a216c7d577 100644
--- a/clang/test/Analysis/atomics.c
+++ b/clang/test/Analysis/atomics.c
@@ -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
+  }
+}


        


More information about the cfe-commits mailing list