Skip to content

Commit 1c05651

Browse files
authored
[clang] [C23] Fix crash with _BitInt running clang-tidy (#65889)
This crash was exposed recently in our randomized testing. _BitInts were not being handled properly during IntegerLiteral visitation. This patch addresses the problem for now. The BitIntType has no getKind() method, so the FoldingSetID is taken from the APInt value representing the _BitInt(), similar to other methods in StmtProfile.cpp. Crash seen (summary form): clang-tidy: <src-root>/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(const From&) [with To = clang::BuiltinType; From = clang::QualType]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed ``` #9 <address> decltype(auto) llvm::cast<clang::BuiltinType, clang::QualType>(clang::QualType const&) <src-root>/llvm/include/llvm/Support/Casting.h:566:3 #10 <address> clang::BuiltinType const* clang::Type::castAs<clang::BuiltinType>() const <bin-root>/tools/clang/include/clang/AST/TypeNodes.inc:86:1 #11 <address> (anonymous namespace)::StmtProfiler::VisitIntegerLiteral( clang::IntegerLiteral const*) <src-root>/clang/lib/AST/StmtProfile.cpp:1362:64 #12 <address> clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::StmtProfiler, void>::Visit(clang::Stmt const*) <src-root>/clang/include/clang/AST/StmtNodes.inc:1225:1 ``` Reviewed By: donat.nagy
1 parent 3d422c4 commit 1c05651

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %check_clang_tidy %s bugprone-inc-dec-in-conditions %t
2+
3+
_BitInt(8) v_401_0() {
4+
0 && ({
5+
_BitInt(5) y = 0;
6+
16777215wb ?: ++y;
7+
});
8+
}
9+
// CHECK-MESSAGES: warning

clang/include/clang/AST/Type.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6642,7 +6642,7 @@ class BitIntType final : public Type, public llvm::FoldingSetNode {
66426642
bool isSugared() const { return false; }
66436643
QualType desugar() const { return QualType(this, 0); }
66446644

6645-
void Profile(llvm::FoldingSetNodeID &ID) {
6645+
void Profile(llvm::FoldingSetNodeID &ID) const {
66466646
Profile(ID, isUnsigned(), getNumBits());
66476647
}
66486648

clang/lib/AST/StmtProfile.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,13 @@ void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
13331333
void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
13341334
VisitExpr(S);
13351335
S->getValue().Profile(ID);
1336-
ID.AddInteger(S->getType()->castAs<BuiltinType>()->getKind());
1336+
1337+
QualType T = S->getType();
1338+
ID.AddInteger(T->getTypeClass());
1339+
if (auto BitIntT = T->getAs<BitIntType>())
1340+
BitIntT->Profile(ID);
1341+
else
1342+
ID.AddInteger(T->castAs<BuiltinType>()->getKind());
13371343
}
13381344

13391345
void StmtProfiler::VisitFixedPointLiteral(const FixedPointLiteral *S) {

0 commit comments

Comments
 (0)