Skip to content
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

[clang-doc][nfc] Avoid constructing SmallString in ToString method #96921

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Jun 27, 2024

This patch updates the return type of HTMLTag::toString from
SmallString<16> to StringRef, since this API only returns string
literals. As a result, there is no need to construct a full heap
allocated or owned string.

Additionally, this patch renames ToString to toString to match the LLVM
style guide.

Created using spr 1.3.4
@llvmbot
Copy link
Collaborator

llvmbot commented Jun 27, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: Paul Kirth (ilovepi)

Changes

This patch updates the return type HTMLTag::toString from
SmallString<16> to const char*, since this API only has a single use to
emit the string into an output stream. As a result, there is no need to
construct a full heap allocated or owned string. Arguably we should
instead provide an overload to the output stream operator that performs
this action directly. However, this change is minimal, and functionally
equivalent for now.

Additionally, this patch renames ToString in the LLVM style as toString.


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-doc/HTMLGenerator.cpp (+21-21)
diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index c0faf5f7e8fd9..cc5b48077faea 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -56,7 +56,7 @@ class HTMLTag {
   operator bool() = delete;
 
   bool IsSelfClosing() const;
-  llvm::SmallString<16> ToString() const;
+  const char* toString() const;
 
 private:
   TagType Value;
@@ -137,42 +137,42 @@ bool HTMLTag::IsSelfClosing() const {
   llvm_unreachable("Unhandled HTMLTag::TagType");
 }
 
-llvm::SmallString<16> HTMLTag::ToString() const {
+const char* HTMLTag::toString() const {
   switch (Value) {
   case HTMLTag::TAG_A:
-    return llvm::SmallString<16>("a");
+    return "a";
   case HTMLTag::TAG_DIV:
-    return llvm::SmallString<16>("div");
+    return "div";
   case HTMLTag::TAG_FOOTER:
-    return llvm::SmallString<16>("footer");
+    return "footer";
   case HTMLTag::TAG_H1:
-    return llvm::SmallString<16>("h1");
+    return "h1";
   case HTMLTag::TAG_H2:
-    return llvm::SmallString<16>("h2");
+    return "h2";
   case HTMLTag::TAG_H3:
-    return llvm::SmallString<16>("h3");
+    return "h3";
   case HTMLTag::TAG_HEADER:
-    return llvm::SmallString<16>("header");
+    return "header";
   case HTMLTag::TAG_LI:
-    return llvm::SmallString<16>("li");
+    return "li";
   case HTMLTag::TAG_LINK:
-    return llvm::SmallString<16>("link");
+    return "link";
   case HTMLTag::TAG_MAIN:
-    return llvm::SmallString<16>("main");
+    return "main";
   case HTMLTag::TAG_META:
-    return llvm::SmallString<16>("meta");
+    return "meta";
   case HTMLTag::TAG_OL:
-    return llvm::SmallString<16>("ol");
+    return "ol";
   case HTMLTag::TAG_P:
-    return llvm::SmallString<16>("p");
+    return "p";
   case HTMLTag::TAG_SCRIPT:
-    return llvm::SmallString<16>("script");
+    return "script";
   case HTMLTag::TAG_SPAN:
-    return llvm::SmallString<16>("span");
+    return "span";
   case HTMLTag::TAG_TITLE:
-    return llvm::SmallString<16>("title");
+    return "title";
   case HTMLTag::TAG_UL:
-    return llvm::SmallString<16>("ul");
+    return "ul";
   }
   llvm_unreachable("Unhandled HTMLTag::TagType");
 }
@@ -191,7 +191,7 @@ void TagNode::Render(llvm::raw_ostream &OS, int IndentationLevel) {
       break;
     }
   OS.indent(IndentationLevel * 2);
-  OS << "<" << Tag.ToString();
+  OS << "<" << Tag.toString();
   for (const auto &A : Attributes)
     OS << " " << A.first << "=\"" << A.second << "\"";
   if (Tag.IsSelfClosing()) {
@@ -216,7 +216,7 @@ void TagNode::Render(llvm::raw_ostream &OS, int IndentationLevel) {
   }
   if (!InlineChildren)
     OS.indent(IndentationLevel * 2);
-  OS << "</" << Tag.ToString() << ">";
+  OS << "</" << Tag.toString() << ">";
 }
 
 template <typename Derived, typename Base,

@ilovepi ilovepi requested a review from petrhosek June 27, 2024 16:13
@ilovepi
Copy link
Contributor Author

ilovepi commented Jun 27, 2024

@PeterChou1 please take a look and provide some feedback.

@@ -56,7 +56,7 @@ class HTMLTag {
operator bool() = delete;

bool IsSelfClosing() const;
llvm::SmallString<16> ToString() const;
const char* toString() const;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be renamed to c_str() or str()?

Copy link
Contributor

@PeterChou1 PeterChou1 Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more appropriate to rename it c_str since in my mind c_str equals char* and str means std::string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Created using spr 1.3.4
Copy link

github-actions bot commented Jun 28, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Created using spr 1.3.4
@petrhosek
Copy link
Member

I'd use std::string_view as a return value instead of const char * as a more idiomatic C++ in which case you don't need to rename the method to c_str().

Created using spr 1.3.4
@ilovepi
Copy link
Contributor Author

ilovepi commented Jul 1, 2024

I'd use std::string_view as a return value instead of const char * as a more idiomatic C++ in which case you don't need to rename the method to c_str().

I opted for StringRef, since that seems more in line w/ the conventions here, but I'm happy to switch to std::string_view if you'd prefer.

@petrhosek
Copy link
Member

I'd use std::string_view as a return value instead of const char * as a more idiomatic C++ in which case you don't need to rename the method to c_str().

I opted for StringRef, since that seems more in line w/ the conventions here, but I'm happy to switch to std::string_view if you'd prefer.

AFAIK the long-term plan is to replace StringRef with std::string_view but I'm fine using StringRef for consistency with the rest of the codebase.

@ilovepi ilovepi merged commit 46a2abb into main Jul 8, 2024
7 checks passed
@ilovepi ilovepi deleted the users/ilovepi/spr/clang-docnfc-avoid-constructing-smallstring-in-tostring-method branch July 8, 2024 23:30
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
…lvm#96921)

This patch updates the return type of HTMLTag::toString from
SmallString<16> to StringRef, since this API only returns string
literals. As a result, there is no need to construct a full heap
allocated or owned string.

Additionally, this patch renames ToString to toString to match the LLVM
style guide.
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.

4 participants