[llvm] c0a6f7a - Use precise types in DWARF BestForm methods (#126309)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 12:23:50 PST 2025


Author: Tom Tromey
Date: 2025-02-21T12:23:47-08:00
New Revision: c0a6f7acf8b630df438fc0de04028148af093cf1

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

LOG: Use precise types in DWARF BestForm methods (#126309)

I noticed that DIEInteger::BestForm used a cast to char:

      if ((char)Int == SignedInt)

If 'char' happens to be unsigned, this will not behave correctly. Then I
also noticed that this code assumes the size of 'short' and 'int'.

This patch changes this code to use more precise types. No functional
change should be visible on ordinary hosts.

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/DIE.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/DIE.h b/llvm/include/llvm/CodeGen/DIE.h
index 952c38cd22dbe..90493bab21641 100644
--- a/llvm/include/llvm/CodeGen/DIE.h
+++ b/llvm/include/llvm/CodeGen/DIE.h
@@ -175,18 +175,18 @@ class DIEInteger {
   static dwarf::Form BestForm(bool IsSigned, uint64_t Int) {
     if (IsSigned) {
       const int64_t SignedInt = Int;
-      if ((char)Int == SignedInt)
+      if ((int8_t)Int == SignedInt)
         return dwarf::DW_FORM_data1;
-      if ((short)Int == SignedInt)
+      if ((int16_t)Int == SignedInt)
         return dwarf::DW_FORM_data2;
-      if ((int)Int == SignedInt)
+      if ((int32_t)Int == SignedInt)
         return dwarf::DW_FORM_data4;
     } else {
-      if ((unsigned char)Int == Int)
+      if ((uint8_t)Int == Int)
         return dwarf::DW_FORM_data1;
-      if ((unsigned short)Int == Int)
+      if ((uint16_t)Int == Int)
         return dwarf::DW_FORM_data2;
-      if ((unsigned int)Int == Int)
+      if ((uint32_t)Int == Int)
         return dwarf::DW_FORM_data4;
     }
     return dwarf::DW_FORM_data8;
@@ -1025,11 +1025,11 @@ class DIELoc : public DIEValueList {
     if (DwarfVersion > 3)
       return dwarf::DW_FORM_exprloc;
     // Pre-DWARF4 location expressions were blocks and not exprloc.
-    if ((unsigned char)Size == Size)
+    if ((uint8_t)Size == Size)
       return dwarf::DW_FORM_block1;
-    if ((unsigned short)Size == Size)
+    if ((uint16_t)Size == Size)
       return dwarf::DW_FORM_block2;
-    if ((unsigned int)Size == Size)
+    if ((uint32_t)Size == Size)
       return dwarf::DW_FORM_block4;
     return dwarf::DW_FORM_block;
   }
@@ -1058,11 +1058,11 @@ class DIEBlock : public DIEValueList {
   /// BestForm - Choose the best form for data.
   ///
   dwarf::Form BestForm() const {
-    if ((unsigned char)Size == Size)
+    if ((uint8_t)Size == Size)
       return dwarf::DW_FORM_block1;
-    if ((unsigned short)Size == Size)
+    if ((uint16_t)Size == Size)
       return dwarf::DW_FORM_block2;
-    if ((unsigned int)Size == Size)
+    if ((uint32_t)Size == Size)
       return dwarf::DW_FORM_block4;
     return dwarf::DW_FORM_block;
   }


        


More information about the llvm-commits mailing list