[PATCH] D53952: [clang-format] Support breaking consecutive string literals for TableGen

Jordan Rupprecht via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 31 14:58:38 PDT 2018


rupprecht created this revision.
rupprecht added a reviewer: krasimir.
Herald added subscribers: cfe-commits, mgorny.
Herald added a reviewer: alexshap.

clang-format can get confused by string literals in TableGen -- e.g. try `clang-format tools/llvm-objcopy/ObjcopyOpts.td`; the multiline string literal for keep_global_symbols is broken up into tiny little pieces over 15 lines. This patch seems to fix that.


Repository:
  rC Clang

https://reviews.llvm.org/D53952

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


Index: unittests/Format/FormatTestTableGen.cpp
===================================================================
--- /dev/null
+++ unittests/Format/FormatTestTableGen.cpp
@@ -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
Index: unittests/Format/CMakeLists.txt
===================================================================
--- unittests/Format/CMakeLists.txt
+++ unittests/Format/CMakeLists.txt
@@ -12,6 +12,7 @@
   FormatTestProto.cpp
   FormatTestRawStrings.cpp
   FormatTestSelective.cpp
+  FormatTestTableGen.cpp
   FormatTestTextProto.cpp
   NamespaceEndCommentsFixerTest.cpp
   SortImportsTestJS.cpp
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2874,6 +2874,7 @@
   } 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53952.172029.patch
Type: text/x-patch
Size: 3214 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181031/51cb057f/attachment-0001.bin>


More information about the cfe-commits mailing list