Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
30 changes: 23 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,37 @@ class SMTConv {
// TODO: Refactor to put elsewhere
static inline QualType getAPSIntType(ASTContext &Ctx,
const llvm::APSInt &Int) {
return Ctx.getIntTypeForBitwidth(Int.getBitWidth(), Int.isSigned());
QualType Ty;
if (!(Ty = Ctx.getIntTypeForBitwidth(Int.getBitWidth(), Int.isSigned()))
.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());
// Ty = Ctx.getIntTypeForBitwidth(Pow2DestWidth, Int.isSigned());
// return Ty;
}

// 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;
if (a)
b = d; // expected-warning{{Assigned value is uninitialized [core.uninitialized.Assign]}}
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}}
}
Loading