shafik created this revision.
shafik added reviewers: teemperor, JDevlieghere, labath.

C-style casts hide intent and when code changes they can also hide logic errors.

This change converts most of the C-style cast into `static_cast` and the icky 
ones are converted to `reinterpret_cast` and we can deal with fixing those icky 
cases in another change.

One case was moved to use `std::numeric_limits<unsigned>::max()` since it was 
simply using the fact that `-1` converted to `unsigned` results in the maximum 
unsigned value.


https://reviews.llvm.org/D72190

Files:
  lldb/include/lldb/Breakpoint/BreakpointOptions.h
  lldb/include/lldb/Breakpoint/BreakpointResolver.h
  lldb/include/lldb/Core/SearchFilter.h
  lldb/include/lldb/Core/Value.h
  lldb/include/lldb/DataFormatters/TypeSynthetic.h
  lldb/include/lldb/Expression/IRExecutionUnit.h
  lldb/include/lldb/Symbol/Symbol.h
  lldb/source/Expression/IRExecutionUnit.cpp
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h

Index: lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
===================================================================
--- lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
+++ lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
@@ -42,7 +42,7 @@
         Parse();
       if (m_parse_error)
         return false;
-      return (bool)m_full;
+      return static_cast<bool>(m_full);
     }
 
     ConstString GetFullName() const { return m_full; }
Index: lldb/source/Expression/IRExecutionUnit.cpp
===================================================================
--- lldb/source/Expression/IRExecutionUnit.cpp
+++ lldb/source/Expression/IRExecutionUnit.cpp
@@ -125,7 +125,8 @@
   LLDB_LOGF(log,
             "Found function, has local address 0x%" PRIx64
             " and remote address 0x%" PRIx64,
-            (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
+            static_cast<uint64_t>(func_local_addr),
+            static_cast<uint64_t>(func_remote_addr));
 
   std::pair<lldb::addr_t, lldb::addr_t> func_range;
 
@@ -302,7 +303,8 @@
       llvm::SmallVector<char, 256> result_path;
       std::string object_name_model =
           "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
-      (void)llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path);
+      static_cast<void>(
+          llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path));
       llvm::raw_fd_ostream fds(fd, true);
       fds.write(object.getBufferStart(), object.getBufferSize());
     }
@@ -466,8 +468,9 @@
       } else {
         record.dump(log);
 
-        DataExtractor my_extractor((const void *)record.m_host_address,
-                                   record.m_size, lldb::eByteOrderBig, 8);
+        DataExtractor my_extractor(
+            reinterpret_cast<const void *>(record.m_host_address),
+            record.m_size, lldb::eByteOrderBig, 8);
         my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
                               DataExtractor::TypeUInt8);
       }
@@ -593,7 +596,7 @@
       Size, Alignment, SectionID, SectionName);
 
   m_parent.m_records.push_back(AllocationRecord(
-      (uintptr_t)return_value,
+      reinterpret_cast<uintptr_t>(return_value),
       lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
       GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
       Alignment, SectionID, SectionName.str().c_str()));
@@ -601,7 +604,8 @@
   LLDB_LOGF(log,
             "IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
             ", Alignment=%u, SectionID=%u) = %p",
-            (uint64_t)Size, Alignment, SectionID, (void *)return_value);
+            static_cast<uint64_t>(Size), Alignment, SectionID,
+            static_cast<void *>(return_value));
 
   if (m_parent.m_reported_allocations) {
     Status err;
@@ -626,13 +630,14 @@
   if (!IsReadOnly)
     permissions |= lldb::ePermissionsWritable;
   m_parent.m_records.push_back(AllocationRecord(
-      (uintptr_t)return_value, permissions,
+      reinterpret_cast<uintptr_t>(return_value), permissions,
       GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
       Alignment, SectionID, SectionName.str().c_str()));
   LLDB_LOGF(log,
             "IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
             ", Alignment=%u, SectionID=%u) = %p",
-            (uint64_t)Size, Alignment, SectionID, (void *)return_value);
+            static_cast<uint64_t>(Size), Alignment, SectionID,
+            static_cast<void *>(return_value));
 
   if (m_parent.m_reported_allocations) {
     Status err;
@@ -1065,7 +1070,7 @@
 
 void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
     const std::string &Name, bool AbortOnFailure) {
-  return (void *)getSymbolAddress(Name);
+  return reinterpret_cast<void *>(getSymbolAddress(Name));
 }
 
 lldb::addr_t
@@ -1085,9 +1090,10 @@
                 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
                 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
                 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
-                local_address, (uint64_t)record.m_host_address,
-                (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
-                record.m_process_address,
+                local_address, static_cast<uint64_t>(record.m_host_address),
+                static_cast<uint64_t>(record.m_host_address) +
+                    static_cast<uint64_t>(record.m_size),
+                ret, record.m_process_address,
                 record.m_process_address + record.m_size);
 
       return ret;
@@ -1189,7 +1195,7 @@
     if (record.m_section_id == eSectionIDInvalid)
       continue;
 
-    engine.mapSectionAddress((void *)record.m_host_address,
+    engine.mapSectionAddress(reinterpret_cast<void *>(record.m_host_address),
                              record.m_process_address);
   }
 
@@ -1202,7 +1208,8 @@
   for (AllocationRecord &record : m_records) {
     if (record.m_process_address != LLDB_INVALID_ADDRESS) {
       lldb_private::Status err;
-      WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
+      WriteMemory(record.m_process_address,
+                  reinterpret_cast<uint8_t *>(record.m_host_address),
                   record.m_size, err);
       if (err.Success())
         wrote_something = true;
@@ -1217,9 +1224,11 @@
 
   LLDB_LOGF(log,
             "[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
-            (unsigned long long)m_host_address, (unsigned long long)m_size,
-            (unsigned long long)m_process_address, (unsigned)m_alignment,
-            (unsigned)m_section_id, m_name.c_str());
+            static_cast<unsigned long long>(m_host_address),
+            static_cast<unsigned long long>(m_size),
+            static_cast<unsigned long long>(m_process_address),
+            static_cast<unsigned>(m_alignment),
+            static_cast<unsigned>(m_section_id), m_name.c_str());
 }
 
 lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
Index: lldb/include/lldb/Symbol/Symbol.h
===================================================================
--- lldb/include/lldb/Symbol/Symbol.h
+++ lldb/include/lldb/Symbol/Symbol.h
@@ -134,9 +134,13 @@
 
   uint32_t GetSiblingIndex() const;
 
-  lldb::SymbolType GetType() const { return (lldb::SymbolType)m_type; }
+  lldb::SymbolType GetType() const {
+    return static_cast<lldb::SymbolType>(m_type);
+  }
 
-  void SetType(lldb::SymbolType type) { m_type = (lldb::SymbolType)type; }
+  void SetType(lldb::SymbolType type) {
+    m_type = static_cast<lldb::SymbolType>(type);
+  }
 
   const char *GetTypeAsString() const;
 
Index: lldb/include/lldb/Expression/IRExecutionUnit.h
===================================================================
--- lldb/include/lldb/Expression/IRExecutionUnit.h
+++ lldb/include/lldb/Expression/IRExecutionUnit.h
@@ -10,6 +10,7 @@
 #define liblldb_IRExecutionUnit_h_
 
 #include <atomic>
+#include <limits>
 #include <memory>
 #include <string>
 #include <vector>
@@ -327,7 +328,8 @@
     IRExecutionUnit &m_parent; ///< The execution unit this is a proxy for.
   };
 
-  static const unsigned eSectionIDInvalid = (unsigned)-1;
+  static const unsigned eSectionIDInvalid =
+      std::numeric_limits<unsigned>::max();
 
   enum class AllocationKind { Stub, Code, Data, Global, Bytes };
 
Index: lldb/include/lldb/DataFormatters/TypeSynthetic.h
===================================================================
--- lldb/include/lldb/DataFormatters/TypeSynthetic.h
+++ lldb/include/lldb/DataFormatters/TypeSynthetic.h
@@ -442,7 +442,7 @@
   GetFrontEnd(ValueObject &backend) override {
     auto synth_ptr = SyntheticChildrenFrontEnd::AutoPointer(
         new FrontEnd(m_python_class, backend));
-    if (synth_ptr && ((FrontEnd *)synth_ptr.get())->IsValid())
+    if (synth_ptr && (static_cast<FrontEnd *>(synth_ptr.get()))->IsValid())
       return synth_ptr;
     return nullptr;
   }
Index: lldb/include/lldb/Core/Value.h
===================================================================
--- lldb/include/lldb/Core/Value.h
+++ lldb/include/lldb/Core/Value.h
@@ -100,18 +100,18 @@
     // Returns a default constructed Scalar if the Vector data is internally
     // inconsistent.
     llvm::APInt rhs = llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
-                                  ((type128 *)bytes)->x);
+                                  (reinterpret_cast<type128 *>(bytes))->x);
     Scalar GetAsScalar() const {
       Scalar scalar;
       if (IsValid()) {
         if (length == 1)
-          scalar = *(const uint8_t *)bytes;
+          scalar = *reinterpret_cast<const uint8_t *>(bytes);
         else if (length == 2)
-          scalar = *(const uint16_t *)bytes;
+          scalar = *reinterpret_cast<const uint16_t *>(bytes);
         else if (length == 4)
-          scalar = *(const uint32_t *)bytes;
+          scalar = *reinterpret_cast<const uint32_t *>(bytes);
         else if (length == 8)
-          scalar = *(const uint64_t *)bytes;
+          scalar = *reinterpret_cast<const uint64_t *>(bytes);
         else if (length >= 16)
           scalar = rhs;
       }
Index: lldb/include/lldb/Core/SearchFilter.h
===================================================================
--- lldb/include/lldb/Core/SearchFilter.h
+++ lldb/include/lldb/Core/SearchFilter.h
@@ -220,7 +220,7 @@
     if (SubclassID > FilterTy::LastKnownFilterType)
       return FilterTy::UnknownFilter;
     else
-      return (enum FilterTy)SubclassID;
+      return static_cast<enum FilterTy>(SubclassID);
   }
 
   const char *GetFilterName() { return FilterTyToName(GetFilterTy()); }
Index: lldb/include/lldb/Breakpoint/BreakpointResolver.h
===================================================================
--- lldb/include/lldb/Breakpoint/BreakpointResolver.h
+++ lldb/include/lldb/Breakpoint/BreakpointResolver.h
@@ -145,7 +145,7 @@
     if (SubclassID > ResolverTy::LastKnownResolverType)
       return ResolverTy::UnknownResolver;
     else
-      return (enum ResolverTy)SubclassID;
+      return static_cast<enum ResolverTy>(SubclassID);
   }
 
   const char *GetResolverName() { return ResolverTyToName(GetResolverTy()); }
Index: lldb/include/lldb/Breakpoint/BreakpointOptions.h
===================================================================
--- lldb/include/lldb/Breakpoint/BreakpointOptions.h
+++ lldb/include/lldb/Breakpoint/BreakpointOptions.h
@@ -363,10 +363,11 @@
     AutoContinue,
     LastOptionName
   };
-  static const char *g_option_names[(size_t)OptionNames::LastOptionName];
+  static const char
+      *g_option_names[static_cast<size_t>(OptionNames::LastOptionName)];
 
   static const char *GetKey(OptionNames enum_value) {
-    return g_option_names[(size_t)enum_value];
+    return g_option_names[static_cast<size_t>(enum_value)];
   }
 
   static bool BreakpointOptionsCallbackFunction(
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to