[clang-tools-extra] [clang] [C23] Fix crash with _BitInt running clang-tidy (PR #65889)

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 18 13:14:09 PDT 2023


https://github.com/vabridgers updated https://github.com/llvm/llvm-project/pull/65889

>From 73d3a59e98ca78931e07428f945c26107812383b Mon Sep 17 00:00:00 2001
From: Vince Bridgers <vince.a.bridgers at gmail.com>
Date: Sat, 9 Sep 2023 12:19:07 +0200
Subject: [PATCH] [clang] [C23] Fix crash with _BitInt running clang-tidy

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. Seems also the const qualifier was missing
on the nonstatic profile method of the BitIntType class.

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
---
 .../bugprone/inc-dec-in-conditions-bitint-no-crash.c   |  9 +++++++++
 clang/include/clang/AST/Type.h                         |  2 +-
 clang/lib/AST/StmtProfile.cpp                          | 10 +++++++++-
 3 files changed, 19 insertions(+), 2 deletions(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/inc-dec-in-conditions-bitint-no-crash.c

diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/inc-dec-in-conditions-bitint-no-crash.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inc-dec-in-conditions-bitint-no-crash.c
new file mode 100644
index 000000000000000..cfb64c10fe46cea
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inc-dec-in-conditions-bitint-no-crash.c
@@ -0,0 +1,9 @@
+// RUN: %check_clang_tidy %s bugprone-inc-dec-in-conditions %t
+
+_BitInt(8) v_401_0() {
+  0 && ({
+    _BitInt(5) y = 0;
+    16777215wb ?: ++y;
+  });
+}
+// CHECK-MESSAGES: warning 
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 46dbadd8b878bf0..4799f89db82fa7f 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -6642,7 +6642,7 @@ class BitIntType final : public Type, public llvm::FoldingSetNode {
   bool isSugared() const { return false; }
   QualType desugar() const { return QualType(this, 0); }
 
-  void Profile(llvm::FoldingSetNodeID &ID) {
+  void Profile(llvm::FoldingSetNodeID &ID) const {
     Profile(ID, isUnsigned(), getNumBits());
   }
 
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 27f71edd6f99b32..6f76edb43edeb8f 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -1333,7 +1333,15 @@ void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
 void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
   VisitExpr(S);
   S->getValue().Profile(ID);
-  ID.AddInteger(S->getType()->castAs<BuiltinType>()->getKind());
+
+  QualType T = S->getType();
+  if (auto BitIntT = T->getAs<BitIntType>()) {
+    ID.AddBoolean(true);
+    BitIntT->Profile(ID);
+  } else {
+    ID.AddBoolean(false);
+    ID.AddInteger(T->castAs<BuiltinType>()->getKind());
+  }
 }
 
 void StmtProfiler::VisitFixedPointLiteral(const FixedPointLiteral *S) {



More information about the cfe-commits mailing list