Skip to content

Commit af7e1f0

Browse files
[analyzer] Fix crash when reasoning about C11 atomics (PR49422)
rdar://75020762 Differential Revision: https://reviews.llvm.org/D99274
1 parent 180e9e5 commit af7e1f0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clang/include/clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ class BasicValueFactory {
139139

140140
/// Returns the type of the APSInt used to store values of the given QualType.
141141
APSIntType getAPSIntType(QualType T) const {
142+
// For the purposes of the analysis and constraints, we treat atomics
143+
// as their underlying types.
144+
if (const AtomicType *AT = T->getAs<AtomicType>()) {
145+
T = AT->getValueType();
146+
}
147+
142148
assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T));
143149
return APSIntType(Ctx.getIntWidth(T),
144150
!T->isSignedIntegerOrEnumerationType());

clang/test/Analysis/atomics.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,11 @@ void test_atomic_compare_exchange_weak(struct RefCountedStruct *s) {
9393
clang_analyzer_eval(s->refCount == 3); // expected-warning {{UNKNOWN}}
9494
clang_analyzer_eval(expected == 2); // expected-warning {{UNKNOWN}}
9595
}
96+
97+
// PR49422
98+
void test_atomic_compare(int input) {
99+
_Atomic(int) x = input;
100+
if (x > 0) {
101+
// no crash
102+
}
103+
}

0 commit comments

Comments
 (0)