[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 04:39:17 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'm not sure how hot this code path is, but we are doing a bunch of unnecessary string copies in here (as we were before this PR as well...). For example, `message` and `severity` are both `StringRef`s that don't need to be converted into strings.

Suggestion:

```c++
std::string DiagnosticManager::GetString(char separator) {
  std::string ret;
  llvm::raw_string_ostream stream(ret);

  for (const auto &diagnostic : Diagnostics()) {
    llvm::StringRef severity = StringForSeverity(diagnostic->GetSeverity());
    stream << severity;

    llvm::StringRef message = diagnostic->GetMessage();
    std::string searchable_message = message.lower();
    auto severity_pos = message.find_first_of(severity);
    stream << message.take_front(severity_pos);

    if (severity_pos != llvm::StringRef::npos)
      stream << message.drop_front(severity_pos + severity.size());
    stream << separator;
  }

  return ret;
}
```

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


More information about the lldb-commits mailing list