Skip to content

Commit 6d3e79b

Browse files
author
einvbri
committed
Address latest round of comments, rebase to latest origin/main
1 parent fa97609 commit 6d3e79b

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,16 @@ class SMTConv {
572572
// TODO: Refactor to put elsewhere
573573
static inline QualType getAPSIntType(ASTContext &Ctx,
574574
const llvm::APSInt &Int) {
575-
QualType Ty = Ctx.getIntTypeForBitwidth(Int.getBitWidth(), Int.isSigned());
575+
QualType Ty;
576+
if (!(Ty = Ctx.getIntTypeForBitwidth(Int.getBitWidth(), Int.isSigned()))
577+
.isNull())
578+
return Ty;
576579
// If Ty is Null, could be because the original type was a _BitInt.
577580
// Get the bit size and round up to next power of 2, max char size
578-
if (Ty.isNull()) {
579-
unsigned CharTypeSize = Ctx.getTypeSize(Ctx.CharTy);
580-
unsigned Pow2DestWidth =
581-
std::max(llvm::bit_ceil(Int.getBitWidth()), CharTypeSize);
582-
Ty = Ctx.getIntTypeForBitwidth(Pow2DestWidth, Int.isSigned());
583-
}
581+
unsigned CharTypeSize = Ctx.getTypeSize(Ctx.CharTy);
582+
unsigned Pow2DestWidth =
583+
std::max(llvm::bit_ceil(Int.getBitWidth()), CharTypeSize);
584+
Ty = Ctx.getIntTypeForBitwidth(Pow2DestWidth, Int.isSigned());
584585
return Ty;
585586
}
586587

@@ -594,15 +595,12 @@ class SMTConv {
594595
// FIXME: This should be a cast from a 1-bit integer type to a boolean type,
595596
// but the former is not available in Clang. Instead, extend the APSInt
596597
// directly.
597-
if (APSIntBitwidth == 1 && Ty.isNull()) {
598-
NewInt = Int.extend(Ctx.getTypeSize(Ctx.BoolTy));
599-
Ty = getAPSIntType(Ctx, NewInt);
600-
} else if (!llvm::isPowerOf2_32(APSIntBitwidth) && !Ty.isNull()) {
601-
NewInt = Int.extend(Ctx.getTypeSize(Ty));
602-
} else
603-
NewInt = Int;
604-
605-
return std::make_pair(NewInt, Ty);
598+
if (APSIntBitwidth == 1 && Ty.isNull())
599+
return {Int.extend(Ctx.getTypeSize(Ctx.BoolTy)),
600+
getAPSIntType(Ctx, NewInt)};
601+
if (!llvm::isPowerOf2_32(APSIntBitwidth) && !Ty.isNull())
602+
return {Int.extend(Ctx.getTypeSize(Ty)), Ty};
603+
return {Int, Ty};
606604
}
607605

608606
// Perform implicit type conversion on binary symbolic expressions.

clang/test/Analysis/bitint-z3.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
// RUN: %clang_cc1 -analyze -analyzer-checker=core -w -DNO_CROSSCHECK -verify %s
2-
// RUN: %clang_cc1 -analyze -analyzer-checker=core -w -analyzer-config crosscheck-with-z3=true -verify %s
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -w \
2+
// RUN: -analyzer-config crosscheck-with-z3=true -verify %s
33
// REQUIRES: z3
44

5-
// The SMTConv layer did not comprehend _BitInt types (because this was an
6-
// evolved feature) and was crashing due to needed updates in 2 places.
7-
// Analysis is expected to find these cases using _BitInt without crashing.
5+
// Previously these tests were crashing because the SMTConv layer did not
6+
// comprehend the _BitInt types.
87

9-
_BitInt(35) a;
10-
int b;
11-
void c() {
8+
void clang_analyzer_warnIfReached();
9+
10+
void c(int b, _BitInt(35) a) {
1211
int d;
1312
if (a)
1413
b = d; // expected-warning{{Assigned value is uninitialized [core.uninitialized.Assign]}}
14+
clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
1515
}
1616

17-
int *d;
18-
_BitInt(3) e;
19-
void f() {
17+
void f(int *d, _BitInt(3) e) {
2018
int g;
2119
d = &g;
22-
e ?: 0; // expected-warning{{Address of stack memory associated with local variable 'g' is still referred to by the global variable 'd' upon returning to the caller. This will be a dangling reference [core.StackAddressEscape]}}
20+
e ?: 0;
21+
clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
2322
}

0 commit comments

Comments
 (0)