[PATCH] D37136: [clang-format] Do not format likely xml

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 29 06:48:14 PDT 2017


krasimir updated this revision to Diff 113085.
krasimir marked 3 inline comments as done.
krasimir added a comment.

- Address review comments


https://reviews.llvm.org/D37136

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp
  unittests/Format/SortIncludesTest.cpp


Index: unittests/Format/SortIncludesTest.cpp
===================================================================
--- unittests/Format/SortIncludesTest.cpp
+++ unittests/Format/SortIncludesTest.cpp
@@ -398,6 +398,17 @@
   EXPECT_EQ(26u, Ranges[0].getLength());
 }
 
+TEST_F(SortIncludesTest, DoNotSortLikelyXml) {
+  EXPECT_EQ("<!--;\n"
+            "#include <b>\n"
+            "#include <a>\n"
+            "-->",
+            sort("<!--;\n"
+                 "#include <b>\n"
+                 "#include <a>\n"
+                 "-->"));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -11253,6 +11253,13 @@
   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
 }
 
+TEST_F(FormatTest, DoNotFormatLikelyXml) {
+  EXPECT_EQ("<!-- ;> -->",
+            format("<!-- ;> -->", getGoogleStyle()));
+  EXPECT_EQ(" <!-- >; -->",
+            format(" <!-- >; -->", getGoogleStyle()));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1539,12 +1539,18 @@
   return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47;
 }
 
+bool isLikelyXml(StringRef Code) {
+  return Code.ltrim().startswith("<");
+}
+
 tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
                                    ArrayRef<tooling::Range> Ranges,
                                    StringRef FileName, unsigned *Cursor) {
   tooling::Replacements Replaces;
   if (!Style.SortIncludes)
     return Replaces;
+  if (isLikelyXml(Code))
+    return Replaces;
   if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
       isMpegTS(Code))
     return Replaces;
@@ -1894,6 +1900,8 @@
   FormatStyle Expanded = expandPresets(Style);
   if (Expanded.DisableFormat)
     return tooling::Replacements();
+  if (isLikelyXml(Code))
+    return tooling::Replacements();
   if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code))
     return tooling::Replacements();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37136.113085.patch
Type: text/x-patch
Size: 2290 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170829/05129487/attachment.bin>


More information about the cfe-commits mailing list