From: Valeriy Savchenko Date: Wed, 24 Mar 2021 15:58:22 +0000 (+0300) Subject: [analyzer] Fix crash when reasoning about C11 atomics (PR49422) X-Git-Tag: llvmorg-14-init~10914 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=af7e1f07ac03074647897498358aaec165c1aaea;p=platform%2Fupstream%2Fllvm.git [analyzer] Fix crash when reasoning about C11 atomics (PR49422) rdar://75020762 Differential Revision: https://reviews.llvm.org/D99274 --- diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h index 9f464e8..f59b254 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h @@ -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()) { + 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 b3d2d35..ef1a216 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 + } +}