[llvm] [NFC][TableGen] Refactor StringToOffsetTable (PR #105655)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 22 05:58:46 PDT 2024


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/105655

- Make `EmitString` const by not mutating `AggregateString`.
- Use C++17 structured bindings in `GetOrAddStringOffset`.
- Use StringExtras version of isDigit instead of std::isdigit.

>From 0a014399d9d72db78104a67b47bac280b6571dad Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Thu, 22 Aug 2024 05:10:23 -0700
Subject: [PATCH] [NFC][TableGen] Refactor StringToOffsetTable

- Make `EmitString` const by not mutating `AggregateString`.
- Use C++17 structured bindings in `GetOrAddStringOffset`.
- Use StringExtras version of isDigit instead of std::isdigit.
---
 .../llvm/TableGen/StringToOffsetTable.h       | 36 +++++++++----------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/TableGen/StringToOffsetTable.h b/llvm/include/llvm/TableGen/StringToOffsetTable.h
index 7fb9d02d77c704..9572573910a756 100644
--- a/llvm/include/llvm/TableGen/StringToOffsetTable.h
+++ b/llvm/include/llvm/TableGen/StringToOffsetTable.h
@@ -13,7 +13,6 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/raw_ostream.h"
-#include <cctype>
 #include <optional>
 
 namespace llvm {
@@ -31,16 +30,15 @@ class StringToOffsetTable {
   size_t size() const { return AggregateString.size(); }
 
   unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
-    auto IterBool =
-        StringOffset.insert(std::make_pair(Str, AggregateString.size()));
-    if (IterBool.second) {
+    auto [II, Inserted] = StringOffset.insert({Str, size()});
+    if (Inserted) {
       // Add the string to the aggregate if this is the first time found.
       AggregateString.append(Str.begin(), Str.end());
       if (appendZero)
         AggregateString += '\0';
     }
 
-    return IterBool.first->second;
+    return II->second;
   }
 
   // Returns the offset of `Str` in the table if its preset, else return
@@ -52,37 +50,35 @@ class StringToOffsetTable {
     return II->second;
   }
 
-  void EmitString(raw_ostream &O) {
+  void EmitString(raw_ostream &O) const {
     // Escape the string.
-    SmallString<256> Str;
-    raw_svector_ostream(Str).write_escaped(AggregateString);
-    AggregateString = std::string(Str);
+    SmallString<256> EscapedStr;
+    raw_svector_ostream(EscapedStr).write_escaped(AggregateString);
 
     O << "    \"";
     unsigned CharsPrinted = 0;
-    for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
+    for (unsigned i = 0, e = EscapedStr.size(); i != e; ++i) {
       if (CharsPrinted > 70) {
         O << "\"\n    \"";
         CharsPrinted = 0;
       }
-      O << AggregateString[i];
+      O << EscapedStr[i];
       ++CharsPrinted;
 
       // Print escape sequences all together.
-      if (AggregateString[i] != '\\')
+      if (EscapedStr[i] != '\\')
         continue;
 
-      assert(i + 1 < AggregateString.size() && "Incomplete escape sequence!");
-      if (isdigit(AggregateString[i + 1])) {
-        assert(isdigit(AggregateString[i + 2]) &&
-               isdigit(AggregateString[i + 3]) &&
+      assert(i + 1 < EscapedStr.size() && "Incomplete escape sequence!");
+      if (isDigit(EscapedStr[i + 1])) {
+        assert(isDigit(EscapedStr[i + 2]) && isDigit(EscapedStr[i + 3]) &&
                "Expected 3 digit octal escape!");
-        O << AggregateString[++i];
-        O << AggregateString[++i];
-        O << AggregateString[++i];
+        O << EscapedStr[++i];
+        O << EscapedStr[++i];
+        O << EscapedStr[++i];
         CharsPrinted += 3;
       } else {
-        O << AggregateString[++i];
+        O << EscapedStr[++i];
         ++CharsPrinted;
       }
     }



More information about the llvm-commits mailing list