[clang] [clang-format] Fix a bug that always returns error for JSON (PR #112839)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 18 18:25:54 PDT 2024


https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/112839

>From 9eb81c845aa102e28c87eeefe82fac3f029ae29e Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 17 Oct 2024 21:52:16 -0700
Subject: [PATCH 1/3] [clang-format] Fix a bug that always returns error for
 JSON

Fixes #108556.
---
 clang/test/Format/dry-run-warning.cpp    | 12 ++++++++++++
 clang/tools/clang-format/ClangFormat.cpp | 13 +++++++------
 2 files changed, 19 insertions(+), 6 deletions(-)
 create mode 100644 clang/test/Format/dry-run-warning.cpp

diff --git a/clang/test/Format/dry-run-warning.cpp b/clang/test/Format/dry-run-warning.cpp
new file mode 100644
index 00000000000000..d71ac9a328f4ee
--- /dev/null
+++ b/clang/test/Format/dry-run-warning.cpp
@@ -0,0 +1,12 @@
+// RUN: echo '{' > %t.json
+// RUN: echo '  "married": true' >> %t.json
+// RUN: echo '}' >> %t.json
+
+// RUN: clang-format -n -style=LLVM %t.json 2>&1 | FileCheck %s -allow-empty
+// CHECK-NOT: warning
+
+// RUN: clang-format -n -style=LLVM < %t.json 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK2 -strict-whitespace
+// CHECK2: warning: code should be clang-formatted
+
+// RUN: rm %t.json
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 6aed46328f3469..a1a4d73dfe9eb5 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -351,9 +351,6 @@ static void outputReplacementsXML(const Replacements &Replaces) {
 static bool
 emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
                         const std::unique_ptr<llvm::MemoryBuffer> &Code) {
-  if (Replaces.empty())
-    return false;
-
   unsigned Errors = 0;
   if (WarnFormat && !NoWarnFormat) {
     SourceMgr Mgr;
@@ -490,9 +487,11 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
   Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges,
                                        AssumedFileName, &CursorPosition);
 
+  const bool IsJson = FormatStyle->isJson();
+
   // To format JSON insert a variable to trick the code into thinking its
   // JavaScript.
-  if (FormatStyle->isJson() && !FormatStyle->DisableFormat) {
+  if (IsJson && !FormatStyle->DisableFormat) {
     auto Err = Replaces.add(tooling::Replacement(
         tooling::Replacement(AssumedFileName, 0, 0, "x = ")));
     if (Err)
@@ -511,8 +510,10 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
       reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
   Replaces = Replaces.merge(FormatChanges);
   if (OutputXML || DryRun) {
-    if (DryRun)
-      return emitReplacementWarnings(Replaces, AssumedFileName, Code);
+    if (DryRun) {
+      return (Replaces.size() > IsJson ? 1 : 0) &&
+             emitReplacementWarnings(Replaces, AssumedFileName, Code);
+    }
     outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition);
   } else {
     IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(

>From a9e2e26dacf5ef1171bf2b0baeaf149ce42f4a91 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 18 Oct 2024 08:35:50 -0700
Subject: [PATCH 2/3] Fix a misplaced `(` and add more tests

---
 clang/test/Format/dry-run-warning.cpp    | 14 ++++++++++++--
 clang/tools/clang-format/ClangFormat.cpp |  2 +-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/clang/test/Format/dry-run-warning.cpp b/clang/test/Format/dry-run-warning.cpp
index d71ac9a328f4ee..4b85de40b8cd08 100644
--- a/clang/test/Format/dry-run-warning.cpp
+++ b/clang/test/Format/dry-run-warning.cpp
@@ -3,10 +3,20 @@
 // RUN: echo '}' >> %t.json
 
 // RUN: clang-format -n -style=LLVM %t.json 2>&1 | FileCheck %s -allow-empty
-// CHECK-NOT: warning
 
 // RUN: clang-format -n -style=LLVM < %t.json 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=CHECK2 -strict-whitespace
-// CHECK2: warning: code should be clang-formatted
+
+// RUN: echo '{' > %t.json
+// RUN: echo '  "married" : true' >> %t.json
+// RUN: echo '}' >> %t.json
+
+// RUN: clang-format -n -style=LLVM < %t.json 2>&1 | FileCheck %s -allow-empty
+
+// RUN: clang-format -n -style=LLVM %t.json 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK2 -strict-whitespace
 
 // RUN: rm %t.json
+
+// CHECK-NOT: warning
+// CHECK2: warning: code should be clang-formatted
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index a1a4d73dfe9eb5..87f38c907d3719 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -511,7 +511,7 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
   Replaces = Replaces.merge(FormatChanges);
   if (OutputXML || DryRun) {
     if (DryRun) {
-      return (Replaces.size() > IsJson ? 1 : 0) &&
+      return Replaces.size() > (IsJson ? 1 : 0) &&
              emitReplacementWarnings(Replaces, AssumedFileName, Code);
     }
     outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition);

>From 4c227b12bd601fc2fcf0508c2f3ae33e0f746101 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 18 Oct 2024 18:23:24 -0700
Subject: [PATCH 3/3] Simplify the original logic even though it's "unrelated"

---
 clang/tools/clang-format/ClangFormat.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 87f38c907d3719..108db7204aa68a 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -509,11 +509,11 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
   Replacements FormatChanges =
       reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
   Replaces = Replaces.merge(FormatChanges);
-  if (OutputXML || DryRun) {
-    if (DryRun) {
-      return Replaces.size() > (IsJson ? 1 : 0) &&
-             emitReplacementWarnings(Replaces, AssumedFileName, Code);
-    }
+  if (DryRun) {
+    return Replaces.size() > (IsJson ? 1 : 0) &&
+           emitReplacementWarnings(Replaces, AssumedFileName, Code);
+  }
+  if (OutputXML) {
     outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition);
   } else {
     IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(



More information about the cfe-commits mailing list