This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Remove spurious JSON binding when DisableFormat = true
ClosedPublic

Authored by Andrew-William-Smith on Dec 14 2021, 4:51 PM.

Details

Summary

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.

Diff Detail

Event Timeline

Andrew-William-Smith edited the summary of this revision. (Show Details)Dec 14 2021, 4:52 PM
Andrew-William-Smith added a project: Restricted Project.
Andrew-William-Smith published this revision for review.Dec 14 2021, 4:54 PM
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: Restricted Project.
owenpan added a subscriber: owenpan.

Can you add a test case?

Thank you for the patch, this looks good but we do need a unit test in clang/unittest/Format/FormatTestJson.cpp you can then build them with ninja FormatTests, if you need help let me know

The following will show the issue

TEST_F(FormatTestJson, DisableJsonFormat) {
  FormatStyle Style = getLLVMStyle(FormatStyle::LK_Json);
  verifyFormat("{}", Style);
  verifyFormat("{\n"
               "  \"name\": 1\n"
               "}",
               Style);
  Style.DisableFormat = true;
  verifyFormat("{}", Style);
  verifyFormat("{\n"
               "  \"name\": 1\n"
               "}",
               Style);
}
MyDeveloperDay requested changes to this revision.Dec 15 2021, 12:26 AM
This revision now requires changes to proceed.Dec 15 2021, 12:26 AM

Added a test with formatting disabled. I had to port the updated logic from ClangFormat.cpp into FormatTestJson::format to show the desired effect; I also couldn't call verifyFormat as-is with formatting disabled since the second assertion involving test::messUp would modify the formatting of the input JSON with no way to format it back. I've added a new function FormatTestJson::verifyFormatStable that only checks that formatting is stable for this scenario.

I think your previous change is dropped from the review, but the test in combination with your original change were good.

Apologies, I undid the actual changes to ClangFormat.cpp. Still learning how to use arc...

MyDeveloperDay accepted this revision.EditedDec 15 2021, 9:06 AM

LGTM, thank you for the patch

Do you need help landing this? if so we'll need your name and email address

This revision is now accepted and ready to land.Dec 15 2021, 9:06 AM

I'm going to assume that I don't have commit access to main, so that would be much appreciated.

  • Name: Andrew Smith
  • Email: aws <at> awsmith <dot> us
This revision was landed with ongoing or failed builds.Dec 15 2021, 3:09 PM
This revision was automatically updated to reflect the committed changes.