[Lldb-commits] [lldb] [lldb] Remove redundant severity substring within a diagnostic message. (PR #76111)

Felipe de Azevedo Piovezan via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 21 13:53:55 PST 2023


================
@@ -48,8 +48,17 @@ std::string DiagnosticManager::GetString(char separator) {
   std::string ret;
 
   for (const auto &diagnostic : Diagnostics()) {
-    ret.append(StringForSeverity(diagnostic->GetSeverity()));
-    ret.append(std::string(diagnostic->GetMessage()));
+    std::string message(diagnostic->GetMessage());
+    std::string searchable_message(diagnostic->GetMessage().lower());
+    std::string severity(StringForSeverity(diagnostic->GetSeverity()));
+
+    // Erase the (first) redundant severity string in the message.
+    size_t position = searchable_message.find(severity);
+    if (position != std::string::npos)
+      message.erase(position, severity.length());
+
+    ret.append(severity);
+    ret.append(message);
----------------
felipepiovezan wrote:

> I thought StringRef was better for strings that get passed around and reused and std::string is better for transient work, such as this.

> Is general, is there an advantage to using StringRef over std::string?


A StringRef is a _non-owning view_ into a string. When we see one of those, that means the author of the code is making a few promises:
1. They don't need to mutate the string
2. They don't need to extend its lifetime past "the current" context.

Conversely, when the author conjures a `std::string` out of `StringRef`, the author is saying that they intend to break one of those promises. Note that your code doesn't need to, with the exception `searchable_message`: we had to modify the string with "to_lower"!


https://github.com/llvm/llvm-project/pull/76111


More information about the lldb-commits mailing list