[PATCH] D55964: [clang-format][TableGen] Don't add spaces around items in square braces.

Jordan Rupprecht via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 20 15:37:16 PST 2018


rupprecht created this revision.
rupprecht added reviewers: djasper, krasimir.
Herald added a subscriber: cfe-commits.

clang-formatting wants to add spaces around items in square braces, e.g. [1, 2] -> [ 1, 2 ]. Based on a quick check [1], it seems like most cases are using the [1, 2] format, so make that the consistent one.

[1] in llvm `.td` files, the regex `\[[^ ]` (bracket followed by not-a-space) shows up ~400 times, but `\[\s[^ ]` (bracket followed by one space and one not-a-space) shows up ~40 times => ~90% uses this format.


Repository:
  rC Clang

https://reviews.llvm.org/D55964

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestTableGen.cpp


Index: unittests/Format/FormatTestTableGen.cpp
===================================================================
--- unittests/Format/FormatTestTableGen.cpp
+++ unittests/Format/FormatTestTableGen.cpp
@@ -52,5 +52,9 @@
                "               \"very long help string\">;\n");
 }
 
+TEST_F(FormatTestTableGen, NoSpacesInSquareBracketLists) {
+  verifyFormat("def flag : Flag<[\"-\", \"--\"], \"foo\">;\n");
+}
+
 } // namespace format
 } // end namespace clang
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -432,6 +432,10 @@
       } else if (CurrentToken->is(tok::r_square) && Parent &&
                  Parent->is(TT_TemplateCloser)) {
         Left->Type = TT_ArraySubscriptLSquare;
+      } else if (Style.Language == FormatStyle::LK_TableGen) {
+        // TableGen treats '[' as array subscripts to avoid spaces, e.g.
+        // def foo : Flag<["-", "--"], "foo">;
+        Left->Type = TT_ArraySubscriptLSquare;
       } else if (Style.Language == FormatStyle::LK_Proto ||
                  Style.Language == FormatStyle::LK_TextProto) {
         // Square braces in LK_Proto can either be message field attributes:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55964.179178.patch
Type: text/x-patch
Size: 1264 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181220/c225004e/attachment.bin>


More information about the cfe-commits mailing list