[clang] 8feb842 - [Clang][Type] Add static assertion to guard future expansion for BuiltinType numbering

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 8 09:37:14 PDT 2023


Author: eopXD
Date: 2023-06-08T09:37:07-07:00
New Revision: 8feb8423602351afa190f88977f71444cf15304f

URL: https://github.com/llvm/llvm-project/commit/8feb8423602351afa190f88977f71444cf15304f
DIFF: https://github.com/llvm/llvm-project/commit/8feb8423602351afa190f88977f71444cf15304f.diff

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

Recent expansion 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 assertion to keep an eye for overflows when we
expand more types in the future.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D152429

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 87e69bf61a8e0..49ea77429e7a1 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -1649,7 +1649,8 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
     unsigned : NumTypeBits;
 
     /// The kind (BuiltinType::Kind) of builtin type this is.
-    unsigned Kind : 8;
+    static constexpr unsigned NumOfBuiltinTypeBits = 8;
+    unsigned Kind : NumOfBuiltinTypeBits;
   };
 
   /// FunctionTypeBitfields store various bits belonging to FunctionProtoType.
@@ -2677,6 +2678,10 @@ class BuiltinType : public Type {
       : Type(Builtin, QualType(),
              K == Dependent ? TypeDependence::DependentInstantiation
                             : TypeDependence::None) {
+    static_assert(Kind::LastKind <
+                      (1 << BuiltinTypeBitfields::NumOfBuiltinTypeBits) &&
+                  "Defined builtin type exceeds the allocated space for serial "
+                  "numbering");
     BuiltinTypeBits.Kind = K;
   }
 


        


More information about the cfe-commits mailing list