r346687 - [clang-format] Support breaking consecutive string literals for TableGen

Jordan Rupprecht via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 12 10:15:04 PST 2018


Author: rupprecht
Date: Mon Nov 12 10:15:04 2018
New Revision: 346687

URL: http://llvm.org/viewvc/llvm-project?rev=346687&view=rev
Log:
[clang-format] Support breaking consecutive string literals for TableGen

Summary:
clang-format can get confused by string literals in TableGen: it knows that strings can be broken up, but doesn't seem to understand how that can be indented across line breaks, and arranges them in a weird triangular pattern. Take this output example from `clang-format tools/llvm-objcopy/ObjcopyOpts.td` (which has now been formatted in rL345896 with this patch applied):

```
defm keep_global_symbols
: Eq<
      "keep-global-symbols", "Reads a list of symbols from <filename> and "
                             "runs as if " "--keep-global-symbol=<symbol> "
                                           "is set for each one. "
                                           "<filename> " "contains one "
                                                         "symbol per line "
                                                         "and may contain "
                                                         "comments "
                                                         "beginning " "with"
                                                                      " '#'"
                                                                      ". "
                                                                      "Lead"
                                                                      "ing "
```

Reviewers: alexshap, MaskRay, djasper

Reviewed By: MaskRay

Subscribers: krasimir, mgorny, cfe-commits

Differential Revision: https://reviews.llvm.org/D53952

Added:
    cfe/trunk/unittests/Format/FormatTestTableGen.cpp
Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/CMakeLists.txt

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=346687&r1=346686&r2=346687&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Nov 12 10:15:04 2018
@@ -2875,6 +2875,7 @@ bool TokenAnnotator::mustBreakBefore(con
   } else if (Style.Language == FormatStyle::LK_Cpp ||
              Style.Language == FormatStyle::LK_ObjC ||
              Style.Language == FormatStyle::LK_Proto ||
+             Style.Language == FormatStyle::LK_TableGen ||
              Style.Language == FormatStyle::LK_TextProto) {
     if (Left.isStringLiteral() && Right.isStringLiteral())
       return true;

Modified: cfe/trunk/unittests/Format/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/CMakeLists.txt?rev=346687&r1=346686&r2=346687&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/CMakeLists.txt (original)
+++ cfe/trunk/unittests/Format/CMakeLists.txt Mon Nov 12 10:15:04 2018
@@ -12,6 +12,7 @@ add_clang_unittest(FormatTests
   FormatTestProto.cpp
   FormatTestRawStrings.cpp
   FormatTestSelective.cpp
+  FormatTestTableGen.cpp
   FormatTestTextProto.cpp
   NamespaceEndCommentsFixerTest.cpp
   SortImportsTestJS.cpp

Added: cfe/trunk/unittests/Format/FormatTestTableGen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTableGen.cpp?rev=346687&view=auto
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestTableGen.cpp (added)
+++ cfe/trunk/unittests/Format/FormatTestTableGen.cpp Mon Nov 12 10:15:04 2018
@@ -0,0 +1,56 @@
+//===- unittest/Format/FormatTestTableGen.cpp -----------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "format-test"
+
+namespace clang {
+namespace format {
+
+class FormatTestTableGen : public ::testing::Test {
+protected:
+  static std::string format(llvm::StringRef Code, unsigned Offset,
+                            unsigned Length, const FormatStyle &Style) {
+    LLVM_DEBUG(llvm::errs() << "---\n");
+    LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+    std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
+    tooling::Replacements Replaces = reformat(Style, Code, Ranges);
+    auto Result = applyAllReplacements(Code, Replaces);
+    EXPECT_TRUE(static_cast<bool>(Result));
+    LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+    return *Result;
+  }
+
+  static std::string format(llvm::StringRef Code) {
+    FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
+    Style.ColumnLimit = 60; // To make writing tests easier.
+    return format(Code, 0, Code.size(), Style);
+  }
+
+  static void verifyFormat(llvm::StringRef Code) {
+    EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable";
+    EXPECT_EQ(Code.str(), format(test::messUp(Code)));
+  }
+};
+
+TEST_F(FormatTestTableGen, FormatStringBreak) {
+  verifyFormat("include \"OptParser.td\"\n"
+               "def flag : Flag<\"--foo\">,\n"
+               "           HelpText<\n"
+               "               \"This is a very, very, very, very, \"\n"
+               "               \"very, very, very, very, very, very, \"\n"
+               "               \"very long help string\">;\n");
+}
+
+} // namespace format
+} // end namespace clang




More information about the cfe-commits mailing list