Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[llvm] Use StringRef::{starts_with,find} (NFC) #139661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kazutakahirata
Copy link
Contributor

Calling find/contains in the StringRef domain allows us to avoid
creating temporary instances of std::string.

Calling find/contains in the StringRef domain allows us to avoid
creating temporary instances of std::string.
@llvmbot
Copy link
Member

llvmbot commented May 13, 2025

@llvm/pr-subscribers-debuginfo

Author: Kazu Hirata (kazutakahirata)

Changes

Calling find/contains in the StringRef domain allows us to avoid
creating temporary instances of std::string.


Full diff: https://github.com/llvm/llvm-project/pull/139661.diff

2 Files Affected:

  • (modified) llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp (+1-1)
  • (modified) llvm/lib/TableGen/Record.cpp (+4-4)
diff --git a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
index 22137ea172240..7a0256f10ea60 100644
--- a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
+++ b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
@@ -780,7 +780,7 @@ llvm::Error DwarfTransformer::verify(StringRef GsymPath,
           const auto &dii = DwarfInlineInfos.getFrame(Idx);
           gsymFilename = LR->getSourceFile(Idx);
           // Verify function name
-          if (dii.FunctionName.find(gii.Name.str()) != 0)
+          if (!StringRef(dii.FunctionName).starts_with(gii.Name))
             Out << "error: address " << HEX64(Addr) << " DWARF function \""
                 << dii.FunctionName.c_str()
                 << "\" doesn't match GSYM function \"" << gii.Name << "\"\n";
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 51ed259132497..0d9fcb0e63dae 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -1796,11 +1796,11 @@ const Init *TernOpInit::Fold(const Record *CurRec) const {
     if (LHSs && MHSs && RHSs) {
       std::string Val = RHSs->getValue().str();
 
-      std::string::size_type found;
-      std::string::size_type idx = 0;
+      StringRef::size_type found;
+      StringRef::size_type idx = 0;
       while (true) {
-        found = Val.find(LHSs->getValue().str(), idx);
-        if (found == std::string::npos)
+        found = StringRef(Val).find(LHSs->getValue(), idx);
+        if (found == StringRef::npos)
           break;
         Val.replace(found, LHSs->getValue().size(), MHSs->getValue().str());
         idx = found + MHSs->getValue().size();

@llvmbot
Copy link
Member

llvmbot commented May 13, 2025

@llvm/pr-subscribers-tablegen

Author: Kazu Hirata (kazutakahirata)

Changes

Calling find/contains in the StringRef domain allows us to avoid
creating temporary instances of std::string.


Full diff: https://github.com/llvm/llvm-project/pull/139661.diff

2 Files Affected:

  • (modified) llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp (+1-1)
  • (modified) llvm/lib/TableGen/Record.cpp (+4-4)
diff --git a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
index 22137ea172240..7a0256f10ea60 100644
--- a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
+++ b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
@@ -780,7 +780,7 @@ llvm::Error DwarfTransformer::verify(StringRef GsymPath,
           const auto &dii = DwarfInlineInfos.getFrame(Idx);
           gsymFilename = LR->getSourceFile(Idx);
           // Verify function name
-          if (dii.FunctionName.find(gii.Name.str()) != 0)
+          if (!StringRef(dii.FunctionName).starts_with(gii.Name))
             Out << "error: address " << HEX64(Addr) << " DWARF function \""
                 << dii.FunctionName.c_str()
                 << "\" doesn't match GSYM function \"" << gii.Name << "\"\n";
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 51ed259132497..0d9fcb0e63dae 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -1796,11 +1796,11 @@ const Init *TernOpInit::Fold(const Record *CurRec) const {
     if (LHSs && MHSs && RHSs) {
       std::string Val = RHSs->getValue().str();
 
-      std::string::size_type found;
-      std::string::size_type idx = 0;
+      StringRef::size_type found;
+      StringRef::size_type idx = 0;
       while (true) {
-        found = Val.find(LHSs->getValue().str(), idx);
-        if (found == std::string::npos)
+        found = StringRef(Val).find(LHSs->getValue(), idx);
+        if (found == StringRef::npos)
           break;
         Val.replace(found, LHSs->getValue().size(), MHSs->getValue().str());
         idx = found + MHSs->getValue().size();

@kazutakahirata kazutakahirata merged commit c95745f into llvm:main May 13, 2025
14 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_StringRef_find_llvm_TableGen branch May 13, 2025 06:24
kazutakahirata added a commit to kazutakahirata/llvm-project that referenced this pull request May 13, 2025
This patch partially reverts llvm#139661 for a better solution.
Specifically, we can take advantage of the fact that std::string::find
accepts anything that can be converted to std::string_view, including
StringRef, starting with C++17.  This way, we do not need to cast Val
to StringRef or LHSs->getValue() to std::string.
kazutakahirata added a commit that referenced this pull request May 13, 2025
This patch partially reverts #139661 for a better solution.
Specifically, we can take advantage of the fact that std::string::find
accepts anything that can be converted to std::string_view, including
StringRef, starting with C++17.  This way, we do not need to cast Val
to StringRef or LHSs->getValue() to std::string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.