Skip to content

[clang][analyzer] Correct SMT Layer for _BitInt cases refutations #143310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions clang/include/clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
#include "llvm/Support/SMTAPI.h"

#include <algorithm>

namespace clang {
namespace ento {

Expand Down Expand Up @@ -570,23 +572,35 @@ class SMTConv {
// TODO: Refactor to put elsewhere
static inline QualType getAPSIntType(ASTContext &Ctx,
const llvm::APSInt &Int) {
return Ctx.getIntTypeForBitwidth(Int.getBitWidth(), Int.isSigned());
const QualType Ty =
Ctx.getIntTypeForBitwidth(Int.getBitWidth(), Int.isSigned());
if (!Ty.isNull())
return Ty;
// If Ty is Null, could be because the original type was a _BitInt.
// Get the size of the _BitInt type (expressed in bits) and round it up to
// the next power of 2 that is at least the bit size of 'char' (usually 8).
unsigned CharTypeSize = Ctx.getTypeSize(Ctx.CharTy);
unsigned Pow2DestWidth =
std::max(llvm::bit_ceil(Int.getBitWidth()), CharTypeSize);
return Ctx.getIntTypeForBitwidth(Pow2DestWidth, Int.isSigned());
}

// Get the QualTy for the input APSInt, and fix it if it has a bitwidth of 1.
static inline std::pair<llvm::APSInt, QualType>
fixAPSInt(ASTContext &Ctx, const llvm::APSInt &Int) {
llvm::APSInt NewInt;
unsigned APSIntBitwidth = Int.getBitWidth();
QualType Ty = getAPSIntType(Ctx, Int);

// FIXME: This should be a cast from a 1-bit integer type to a boolean type,
// but the former is not available in Clang. Instead, extend the APSInt
// directly.
if (Int.getBitWidth() == 1 && getAPSIntType(Ctx, Int).isNull()) {
NewInt = Int.extend(Ctx.getTypeSize(Ctx.BoolTy));
} else
NewInt = Int;

return std::make_pair(NewInt, getAPSIntType(Ctx, NewInt));
if (APSIntBitwidth == 1 && Ty.isNull())
return {Int.extend(Ctx.getTypeSize(Ctx.BoolTy)),
getAPSIntType(Ctx, NewInt)};
if (llvm::isPowerOf2_32(APSIntBitwidth) || Ty.isNull())
return {Int, Ty};
return {Int.extend(Ctx.getTypeSize(Ty)), Ty};
}

// Perform implicit type conversion on binary symbolic expressions.
Expand Down
22 changes: 22 additions & 0 deletions clang/test/Analysis/bitint-z3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -w \
// RUN: -analyzer-config crosscheck-with-z3=true -verify %s
// REQUIRES: z3

// Previously these tests were crashing because the SMTConv layer did not
// comprehend the _BitInt types.

void clang_analyzer_warnIfReached();

void c(int b, _BitInt(35) a) {
int d = 0;
if (a)
b = d;
clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
}

void f(int *d, _BitInt(3) e) {
int g;
d = &g;
e ?: 0;
clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
}