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

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 11 02:56:08 PDT 2023


================
@@ -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());
+
+  int FoldingSetID = 0;
+
+  if (S->getType()->isBitIntType())
+    FoldingSetID = S->getValue().getSExtValue();
+  else
+    FoldingSetID = S->getType()->castAs<BuiltinType>()->getKind();
+
+  ID.AddInteger(FoldingSetID);
----------------
DonatNagyE wrote:

You're right, it's better to add a boolean to distinguish between the two cases and reduce the chance of collisions. 

Also note that `S->getValue().getSExtValue()` represents the _value_ of the integer literal, which is redundant with`S->getValue().Profile(ID)` on an earlier line. Instead of it, you need to provide information that uniquely identifies the _type_ of the literal -- and `BitIntType` has a `Profile` method, which is the canonical solution for this. 

Combining these two suggestions, I'd write something like the following:
```c++
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());
}
```
(disclaimer: untested code, try it out before running it)

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


More information about the cfe-commits mailing list