[llvm] [FileCheck] Improve printing variables with escapes (PR #145865)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 26 03:17:53 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- llvm/lib/FileCheck/FileCheck.cpp llvm/lib/FileCheck/FileCheckImpl.h
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index 5ddb74208..39fbb88bc 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -276,7 +276,8 @@ Expected<std::string> NumericSubstitution::getResultRegex() const {
 
 Expected<std::string> NumericSubstitution::getResultForDiagnostics() const {
   // The "regex" returned by getResultRegex() is just a numeric value
-  // like '42', '0x2A', '-17', 'DEADBEEF' etc. This is already suitable for use in diagnostics.
+  // like '42', '0x2A', '-17', 'DEADBEEF' etc. This is already suitable for use
+  // in diagnostics.
   Expected<std::string> Literal = getResultRegex();
   if (!Literal)
     return Literal;
@@ -293,34 +294,34 @@ Expected<std::string> StringSubstitution::getResultRegex() const {
 }
 
 Expected<std::string> StringSubstitution::getResultForDiagnostics() const {
-    Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
-    if (!VarVal)
-      return VarVal.takeError();
-
-    std::string Result;
-    Result.reserve(VarVal->size() + 2);
-    raw_string_ostream OS(Result);
-
-    OS << '"';
-    // Escape the string if it contains any characters that
-    // make it hard to read, such as tabs, newlines, quotes, and non-printable characters.
-    // Note that we do not include backslashes in this set, because they are
-    // common in Windows paths and escaping them would make the output
-    // harder to read.
-    // However, when we do escape, backslashes are escaped as well,
-    // otherwise the output would be ambiguous.
-    const bool NeedsEscaping = llvm::any_of(*VarVal, [](char C) {
-      return C == '\t' || C == '\n' || C == '"' || !isPrint(C);
-    });
-    if (NeedsEscaping)
-      OS.write_escaped(*VarVal);
-    else
-      OS << *VarVal;
-    OS << '"';
-    if (NeedsEscaping)
-      OS << " (escaped value)";
-    
-    return Result;
+  Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
+  if (!VarVal)
+    return VarVal.takeError();
+
+  std::string Result;
+  Result.reserve(VarVal->size() + 2);
+  raw_string_ostream OS(Result);
+
+  OS << '"';
+  // Escape the string if it contains any characters that
+  // make it hard to read, such as tabs, newlines, quotes, and non-printable
+  // characters. Note that we do not include backslashes in this set, because
+  // they are common in Windows paths and escaping them would make the output
+  // harder to read.
+  // However, when we do escape, backslashes are escaped as well,
+  // otherwise the output would be ambiguous.
+  const bool NeedsEscaping = llvm::any_of(*VarVal, [](char C) {
+    return C == '\t' || C == '\n' || C == '"' || !isPrint(C);
+  });
+  if (NeedsEscaping)
+    OS.write_escaped(*VarVal);
+  else
+    OS << *VarVal;
+  OS << '"';
+  if (NeedsEscaping)
+    OS << " (escaped value)";
+
+  return Result;
 }
 
 bool Pattern::isValidVarNameStart(char C) { return C == '_' || isAlpha(C); }
@@ -1251,7 +1252,8 @@ void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
       SmallString<256> Msg;
       raw_svector_ostream OS(Msg);
 
-      Expected<std::string> MatchedValue = Substitution->getResultForDiagnostics();
+      Expected<std::string> MatchedValue =
+          Substitution->getResultForDiagnostics();
       // Substitution failures are handled in printNoMatch().
       if (!MatchedValue) {
         consumeError(MatchedValue.takeError());
diff --git a/llvm/lib/FileCheck/FileCheckImpl.h b/llvm/lib/FileCheck/FileCheckImpl.h
index 176668a3e..5cf5548cd 100644
--- a/llvm/lib/FileCheck/FileCheckImpl.h
+++ b/llvm/lib/FileCheck/FileCheckImpl.h
@@ -366,8 +366,9 @@ public:
   /// \returns the index where the substitution is to be performed in RegExStr.
   size_t getIndex() const { return InsertIdx; }
 
-  /// \returns a regular expression string that matches the result of the substitution represented
-  /// by this class instance or an error if substitution failed.
+  /// \returns a regular expression string that matches the result of the
+  /// substitution represented by this class instance or an error if
+  /// substitution failed.
   virtual Expected<std::string> getResultRegex() const = 0;
 
   /// \returns a string containing the result of the substitution represented
@@ -387,7 +388,8 @@ public:
   Expected<std::string> getResultRegex() const override;
 
   /// \returns the text that the string variable in this substitution matched
-  /// when defined, in a form suitable for diagnostics, or an error if the variable is undefined.
+  /// when defined, in a form suitable for diagnostics, or an error if the
+  /// variable is undefined.
   Expected<std::string> getResultForDiagnostics() const override;
 };
 
@@ -409,7 +411,8 @@ public:
   Expected<std::string> getResultRegex() const override;
 
   /// \returns a string containing the result of evaluating the expression in
-  /// this substitution, in a form suitable for diagnostics, or an error if evaluation failed.
+  /// this substitution, in a form suitable for diagnostics, or an error if
+  /// evaluation failed.
   Expected<std::string> getResultForDiagnostics() const override;
 };
 

``````````

</details>


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


More information about the llvm-commits mailing list