gn format: Support # NOSORT to disable sorting of blocks R=brettw@chromium.org BUG=591953 Review URL: https://codereview.chromium.org/1766163002 Cr-Original-Commit-Position: refs/heads/master@{#380709} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: aa7f7d038156bdf3f317773b784b4a308efd0132
diff --git a/tools/gn/command_format.cc b/tools/gn/command_format.cc index d7301c9..ae36e37 100644 --- a/tools/gn/command_format.cc +++ b/tools/gn/command_format.cc
@@ -35,6 +35,16 @@ "\n" " Formats .gn file to a standard format.\n" "\n" + " The contents of some lists ('sources', 'deps', etc.) will be sorted to\n" + " a canonical order. To suppress this, you can add a comment of the form\n" + " \"# NOSORT\" immediately preceeding the assignment. e.g.\n" + "\n" + " # NOSORT\n" + " sources = [\n" + " \"z.cc\",\n" + " \"a.cc\",\n" + " ]\n" + "\n" "Arguments\n" " --dry-run\n" " Does not change or output anything, but sets the process exit code\n" @@ -315,6 +325,12 @@ } void Printer::SortIfSourcesOrDeps(const BinaryOpNode* binop) { + if (binop && binop->comments() && !binop->comments()->before().empty() && + binop->comments()->before()[0].value().as_string() == "# NOSORT") { + // Allow disabling of sort for specific actions that might be + // order-sensitive. + return; + } const IdentifierNode* ident = binop->left()->AsIdentifier(); const ListNode* list = binop->right()->AsList(); if ((binop->op().value() == "=" || binop->op().value() == "+=" ||
diff --git a/tools/gn/command_format_unittest.cc b/tools/gn/command_format_unittest.cc index 8e8c256..7c79d41 100644 --- a/tools/gn/command_format_unittest.cc +++ b/tools/gn/command_format_unittest.cc
@@ -103,3 +103,4 @@ FORMAT_TEST(063) FORMAT_TEST(064) FORMAT_TEST(065) +FORMAT_TEST(066)
diff --git a/tools/gn/format_test_data/066.gn b/tools/gn/format_test_data/066.gn new file mode 100644 index 0000000..c62eb2a --- /dev/null +++ b/tools/gn/format_test_data/066.gn
@@ -0,0 +1,30 @@ +# Suppress sorting based on comment. + +# NOSORT +sources = [] + +# NOSORT +sources = [ + "a", +] + +# NOSORT +sources += [ + "a", +] + +# NOSORT +sources = [ + "z", + "z2", + "a", + "y.cc", +] + +# NOSORT +sources += [ + "z", + "z2", + "a", + "y.cc", +]
diff --git a/tools/gn/format_test_data/066.golden b/tools/gn/format_test_data/066.golden new file mode 100644 index 0000000..45467b8 --- /dev/null +++ b/tools/gn/format_test_data/066.golden
@@ -0,0 +1,28 @@ +# Suppress sorting based on comment. + +# NOSORT +sources = [] + +# NOSORT +sources = [ + "a", +] + +# NOSORT +sources += [ "a" ] + +# NOSORT +sources = [ + "z", + "z2", + "a", + "y.cc", +] + +# NOSORT +sources += [ + "z", + "z2", + "a", + "y.cc", +]