[PATCH] D152429: [Clang][Type] Add static assertion to guard future expansion for BuiltinType numbering

Yueh-Ting (eop) Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 8 01:39:57 PDT 2023


eopXD created this revision.
eopXD added reviewers: craig.topper, aaron.ballman, erichkeane.
Herald added a project: All.
eopXD requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Recent expansion D152070 <https://reviews.llvm.org/D152070> exceeds the limit and I had a hard time
triaging the bug because the overflow just creates unexpected
behaviors within the compiler.

This patch adds a static assertionto keep an eye for overflows when we
expand more types in the future.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152429

Files:
  clang/include/clang/AST/Type.h


Index: clang/include/clang/AST/Type.h
===================================================================
--- clang/include/clang/AST/Type.h
+++ clang/include/clang/AST/Type.h
@@ -1539,6 +1539,8 @@
   GNUAutoType
 };
 
+static const unsigned NumOfBuiltinTypeBits = 8;
+
 /// The base class of the type hierarchy.
 ///
 /// A central concept with types is that each type always has a canonical
@@ -1649,7 +1651,7 @@
     unsigned : NumTypeBits;
 
     /// The kind (BuiltinType::Kind) of builtin type this is.
-    unsigned Kind : 8;
+    unsigned Kind : NumOfBuiltinTypeBits;
   };
 
   /// FunctionTypeBitfields store various bits belonging to FunctionProtoType.
@@ -2677,6 +2679,9 @@
       : Type(Builtin, QualType(),
              K == Dependent ? TypeDependence::DependentInstantiation
                             : TypeDependence::None) {
+    static_assert(Kind::LastKind < (1 << NumOfBuiltinTypeBits) &&
+                  "Defined builtin type exceeds the allocated space for serial "
+                  "numbering");
     BuiltinTypeBits.Kind = K;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152429.529531.patch
Type: text/x-patch
Size: 1066 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230608/78bf4548/attachment.bin>


More information about the cfe-commits mailing list