[PATCH] D97226: [clangd] Show hex value of numeric constants

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 22 14:06:35 PST 2021


sammccall created this revision.
sammccall added a reviewer: kadircet.
Herald added subscribers: usaxena95, arphaman.
sammccall requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Don't show negative numbers
Don't show numbers <10 (hex is the same as decimal)
Show numeric enum values in hex too


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97226

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -513,7 +513,7 @@
          HI.Definition = "Color x = GREEN";
          HI.Kind = index::SymbolKind::Variable;
          HI.Type = "enum Color";
-         HI.Value = "GREEN (1)"; // Symbolic when hovering on an expression.
+         HI.Value = "GREEN (0x1)"; // Symbolic when hovering on an expression.
        }},
       {R"cpp(
         template<int a, int b> struct Add {
@@ -543,7 +543,7 @@
          HI.ReturnType = "int";
          HI.Parameters.emplace();
          HI.NamespaceScope = "";
-         HI.Value = "42";
+         HI.Value = "42 (0x2a)";
        }},
       {R"cpp(
         const char *[[ba^r]] = "1234";
@@ -1468,7 +1468,7 @@
             HI.Definition = "static int hey = 10";
             HI.Documentation = "Global variable";
             // FIXME: Value shouldn't be set in this case
-            HI.Value = "10";
+            HI.Value = "10 (0xa)";
           }},
       {
           R"cpp(// Global variable in namespace
@@ -1485,7 +1485,7 @@
             HI.NamespaceScope = "ns1::";
             HI.Type = "int";
             HI.Definition = "static int hey = 10";
-            HI.Value = "10";
+            HI.Value = "10 (0xa)";
           }},
       {
           R"cpp(// Field in anonymous struct
Index: clang-tools-extra/clangd/Hover.cpp
===================================================================
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -381,8 +381,15 @@
     for (const EnumConstantDecl *ECD :
          T->castAs<EnumType>()->getDecl()->enumerators())
       if (ECD->getInitVal() == Val)
-        return llvm::formatv("{0} ({1})", ECD->getNameAsString(), Val).str();
+        return llvm::formatv("{0} ({1:x})", ECD->getNameAsString(), Val).str();
   }
+  // Show hex value of integers if they're at least 10 (not negative!)
+  if (T->isIntegralOrEnumerationType() &&
+      Constant.Val.getInt().getMinSignedBits() <= 64 &&
+      !Constant.Val.getInt().isNegative() && Constant.Val.getInt().uge(10))
+    return llvm::formatv("{0} ({1:x})", Constant.Val.getAsString(Ctx, T),
+                         Constant.Val.getInt().getExtValue())
+        .str();
   return Constant.Val.getAsString(Ctx, T);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97226.325563.patch
Type: text/x-patch
Size: 2429 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210222/6d1375fe/attachment.bin>


More information about the cfe-commits mailing list