[clang] 85df281 - [clang-format] Fix a bug that always returns error for JSON (#112839)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 18 21:10:04 PDT 2024
Author: Owen Pan
Date: 2024-10-18T21:10:00-07:00
New Revision: 85df28180bd38d3fd5356efe6022eebec31e0814
URL: https://github.com/llvm/llvm-project/commit/85df28180bd38d3fd5356efe6022eebec31e0814
DIFF: https://github.com/llvm/llvm-project/commit/85df28180bd38d3fd5356efe6022eebec31e0814.diff
LOG: [clang-format] Fix a bug that always returns error for JSON (#112839)
Fixes #108556.
Added:
clang/test/Format/dry-run-warning.cpp
Modified:
clang/tools/clang-format/ClangFormat.cpp
Removed:
################################################################################
diff --git a/clang/test/Format/dry-run-warning.cpp b/clang/test/Format/dry-run-warning.cpp
new file mode 100644
index 00000000000000..4b85de40b8cd08
--- /dev/null
+++ b/clang/test/Format/dry-run-warning.cpp
@@ -0,0 +1,22 @@
+// 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: 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 6aed46328f3469..108db7204aa68a 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)
@@ -510,9 +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 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