|
| 1 | +From 85df28180bd38d3fd5356efe6022eebec31e0814 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Owen Pan <owenpiano@gmail.com> |
| 3 | +Date: Fri, 18 Oct 2024 21:10:00 -0700 |
| 4 | +Subject: [PATCH] [clang-format] Fix a bug that always returns error for JSON |
| 5 | + (#112839) |
| 6 | + |
| 7 | +Fixes #108556. |
| 8 | + |
| 9 | +--- |
| 10 | + clang/test/Format/dry-run-warning.cpp | 22 ++++++++++++++++++++++ |
| 11 | + clang/tools/clang-format/ClangFormat.cpp | 18 +++++++++--------- |
| 12 | + 2 files changed, 31 insertions(+), 9 deletions(-) |
| 13 | + create mode 100644 clang/test/Format/dry-run-warning.cpp |
| 14 | + |
| 15 | +diff --git a/clang/test/Format/dry-run-warning.cpp b/clang/test/Format/dry-run-warning.cpp |
| 16 | +new file mode 100644 |
| 17 | +index 000000000..4b85de40b |
| 18 | +--- /dev/null |
| 19 | ++++ b/clang/test/Format/dry-run-warning.cpp |
| 20 | +@@ -0,0 +1,22 @@ |
| 21 | ++// RUN: echo '{' > %t.json |
| 22 | ++// RUN: echo ' "married": true' >> %t.json |
| 23 | ++// RUN: echo '}' >> %t.json |
| 24 | ++ |
| 25 | ++// RUN: clang-format -n -style=LLVM %t.json 2>&1 | FileCheck %s -allow-empty |
| 26 | ++ |
| 27 | ++// RUN: clang-format -n -style=LLVM < %t.json 2>&1 \ |
| 28 | ++// RUN: | FileCheck %s -check-prefix=CHECK2 -strict-whitespace |
| 29 | ++ |
| 30 | ++// RUN: echo '{' > %t.json |
| 31 | ++// RUN: echo ' "married" : true' >> %t.json |
| 32 | ++// RUN: echo '}' >> %t.json |
| 33 | ++ |
| 34 | ++// RUN: clang-format -n -style=LLVM < %t.json 2>&1 | FileCheck %s -allow-empty |
| 35 | ++ |
| 36 | ++// RUN: clang-format -n -style=LLVM %t.json 2>&1 \ |
| 37 | ++// RUN: | FileCheck %s -check-prefix=CHECK2 -strict-whitespace |
| 38 | ++ |
| 39 | ++// RUN: rm %t.json |
| 40 | ++ |
| 41 | ++// CHECK-NOT: warning |
| 42 | ++// CHECK2: warning: code should be clang-formatted |
| 43 | +diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp |
| 44 | +index e122cea50..d40d8f5d1 100644 |
| 45 | +--- a/clang/tools/clang-format/ClangFormat.cpp |
| 46 | ++++ b/clang/tools/clang-format/ClangFormat.cpp |
| 47 | +@@ -341,9 +341,6 @@ static void outputReplacementsXML(const Replacements &Replaces) { |
| 48 | + static bool |
| 49 | + emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName, |
| 50 | + const std::unique_ptr<llvm::MemoryBuffer> &Code) { |
| 51 | +- if (Replaces.empty()) |
| 52 | +- return false; |
| 53 | +- |
| 54 | + unsigned Errors = 0; |
| 55 | + if (WarnFormat && !NoWarnFormat) { |
| 56 | + llvm::SourceMgr Mgr; |
| 57 | +@@ -479,9 +476,11 @@ static bool format(StringRef FileName) { |
| 58 | + Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges, |
| 59 | + AssumedFileName, &CursorPosition); |
| 60 | + |
| 61 | ++ const bool IsJson = FormatStyle->isJson(); |
| 62 | ++ |
| 63 | + // To format JSON insert a variable to trick the code into thinking its |
| 64 | + // JavaScript. |
| 65 | +- if (FormatStyle->isJson() && !FormatStyle->DisableFormat) { |
| 66 | ++ if (IsJson && !FormatStyle->DisableFormat) { |
| 67 | + auto Err = Replaces.add(tooling::Replacement( |
| 68 | + tooling::Replacement(AssumedFileName, 0, 0, "x = "))); |
| 69 | + if (Err) |
| 70 | +@@ -499,11 +498,12 @@ static bool format(StringRef FileName) { |
| 71 | + Replacements FormatChanges = |
| 72 | + reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status); |
| 73 | + Replaces = Replaces.merge(FormatChanges); |
| 74 | +- if (OutputXML || DryRun) { |
| 75 | +- if (DryRun) |
| 76 | +- return emitReplacementWarnings(Replaces, AssumedFileName, Code); |
| 77 | +- else |
| 78 | +- outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition); |
| 79 | ++ if (DryRun) { |
| 80 | ++ return Replaces.size() > (IsJson ? 1 : 0) && |
| 81 | ++ emitReplacementWarnings(Replaces, AssumedFileName, Code); |
| 82 | ++ } |
| 83 | ++ if (OutputXML) { |
| 84 | ++ outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition); |
| 85 | + } else { |
| 86 | + IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 87 | + new llvm::vfs::InMemoryFileSystem); |
| 88 | +-- |
| 89 | +2.34.1 |
| 90 | + |
0 commit comments