[Lldb-commits] [PATCH] D35036: switch on enum should be exhaustive and warning-free

Tim Hammerquist via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Jul 5 15:28:33 PDT 2017


penryu created this revision.

Testing the value of type_code against the closed enum TypeCodes
provides statically verifiable completeness of testing. However, one
branch assigns to type_code by casting directly from a masked integer
value. This is currently handled by adding a default: case after
checking each TypeCodes instance. This patch introduces a bool variable
containing the "default" state value, allowing the switch to be
exhaustive, protect against future instances not being handled in the
switch, and preserves the original logic.

As an issue of maintainability, the bitmask on line 524 handles the
current values of TypeCodes enum, but this will be invalid if the enum
is extended. This patch does not address this, and a more closed
conversion from cfinfoa -> TypeCodes would help protect against this.


https://reviews.llvm.org/D35036

Files:
  source/Plugins/Language/ObjC/Cocoa.cpp


Index: source/Plugins/Language/ObjC/Cocoa.cpp
===================================================================
--- source/Plugins/Language/ObjC/Cocoa.cpp
+++ source/Plugins/Language/ObjC/Cocoa.cpp
@@ -543,35 +543,40 @@
       }
       
       uint64_t value = 0;
+      bool success = false;
       switch (type_code) {
         case TypeCodes::sint8:
         value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 1, 0,
                                                           error);
         if (error.Fail())
           return false;
         NSNumber_FormatChar(valobj, stream, (char)value, options.GetLanguage());
+        success = true;
         break;
         case TypeCodes::sint16:
         value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 2, 0,
                                                           error);
         if (error.Fail())
           return false;
         NSNumber_FormatShort(valobj, stream, (short)value,
                              options.GetLanguage());
+        success = true;
         break;
       case TypeCodes::sint32:
         value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 4, 0,
                                                           error);
         if (error.Fail())
           return false;
         NSNumber_FormatInt(valobj, stream, (int)value, options.GetLanguage());
+        success = true;
         break;
       case TypeCodes::sint64:
         value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 8, 0,
                                                           error);
         if (error.Fail())
           return false;
         NSNumber_FormatLong(valobj, stream, value, options.GetLanguage());
+        success = true;
         break;
       case TypeCodes::f32:
       {
@@ -582,6 +587,7 @@
         float flt_value = 0.0f;
         memcpy(&flt_value, &flt_as_int, sizeof(flt_as_int));
         NSNumber_FormatFloat(valobj, stream, flt_value, options.GetLanguage());
+        success = true;
         break;
       }
       case TypeCodes::f64:
@@ -593,6 +599,7 @@
         double dbl_value = 0.0;
         memcpy(&dbl_value, &dbl_as_lng, sizeof(dbl_as_lng));
         NSNumber_FormatDouble(valobj, stream, dbl_value, options.GetLanguage());
+        success = true;
         break;
       }
       case TypeCodes::sint128: // internally, this is the same
@@ -608,12 +615,11 @@
           return false;
         llvm::APInt i128_value(128, words);
         NSNumber_FormatInt128(valobj, stream, i128_value, options.GetLanguage());
+        success = true;
         break;
       }
-      default:
-        return false;
       }
-      return true;
+      return success;
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35036.105347.patch
Type: text/x-patch
Size: 2714 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20170705/e8f807c8/attachment.bin>


More information about the lldb-commits mailing list