[PATCH] D115769: [clang-format] Remove spurious JSON binding when DisableFormat = true

Andrew Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 14 16:56:06 PST 2021


Andrew-William-Smith created this revision.
Andrew-William-Smith added reviewers: MyDeveloperDay, HazardyKnusperkeks, jbcoe.
Andrew-William-Smith edited the summary of this revision.
Andrew-William-Smith added a project: clang-format.
Andrew-William-Smith retitled this revision from "[clang-format] Remove spurious JSON binding when DisableFormat: true" to "[clang-format] Remove spurious JSON binding when DisableFormat = true".
Andrew-William-Smith added a project: clang.
Andrew-William-Smith published this revision for review.
Herald added a subscriber: cfe-commits.

Relevant issue: https://github.com/llvm/llvm-project/issues/52705

When the `DisableFormat` option of `clang-format` is set to `true` and a JSON file is formatted, the ephemeral variable binding that is added to the top-level object is not removed from the formatted file.  For example, this JSON:

  {
    "key": "value"
  }

Is reformatted to:

  x = {
    "key": "value"
  }

Which is not valid JSON syntax.  This fix avoids the addition of this binding when `DisableFormat` is set to `true`, ensuring that it cannot be left behind when formatting is disabled.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115769

Files:
  clang/tools/clang-format/ClangFormat.cpp


Index: clang/tools/clang-format/ClangFormat.cpp
===================================================================
--- clang/tools/clang-format/ClangFormat.cpp
+++ clang/tools/clang-format/ClangFormat.cpp
@@ -460,7 +460,7 @@
 
   // To format JSON insert a variable to trick the code into thinking its
   // JavaScript.
-  if (FormatStyle->isJson()) {
+  if (FormatStyle->isJson() && !FormatStyle->DisableFormat) {
     auto Err = Replaces.add(tooling::Replacement(
         tooling::Replacement(AssumedFileName, 0, 0, "x = ")));
     if (Err) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115769.394414.patch
Type: text/x-patch
Size: 547 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211215/e378fe1f/attachment.bin>


More information about the cfe-commits mailing list