]> granicus.if.org Git - clang/commitdiff
[clang-format] Do not format likely xml
authorKrasimir Georgiev <krasimir@google.com>
Tue, 29 Aug 2017 13:51:38 +0000 (13:51 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Tue, 29 Aug 2017 13:51:38 +0000 (13:51 +0000)
Summary:
This patch detects the leading '<' in likely xml files and stops formatting in
that case. A recent use of a Qt xml file with a .ts extension triggered this:
http://doc.qt.io/qt-4.8/linguist-ts-file-format.html

Reviewers: djasper

Reviewed By: djasper

Subscribers: sammccall, cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311999 91177308-0d34-0410-b5e6-96231b3b80d8

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

index aa4ed8c42a7007729222d556b28b93f01cc04225..4b067fcea3e0603202dae0564da74c324db5cecd 100644 (file)
@@ -1539,14 +1539,19 @@ bool isMpegTS(StringRef Code) {
   return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47;
 }
 
+bool likelyXml(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 (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
-      isMpegTS(Code))
+  if (likelyXml(Code) ||
+      (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
+       isMpegTS(Code)))
     return Replaces;
   if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript)
     return sortJavaScriptImports(Style, Code, Ranges, FileName);
@@ -1894,7 +1899,8 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
   FormatStyle Expanded = expandPresets(Style);
   if (Expanded.DisableFormat)
     return tooling::Replacements();
-  if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code))
+  if (likelyXml(Code) ||
+      (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code)))
     return tooling::Replacements();
 
   typedef std::function<tooling::Replacements(const Environment &)>
index a216c803f1f64f5dce41191514807c076208d393..1c57f033f460c6dae2b41165dabeac1e0a6d377c 100644 (file)
@@ -11253,6 +11253,13 @@ TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
   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 1128ed829ff2d21c9aae5148beccf9796870a0bd..ff3988776bd5ffb90e7dcd61f513e9754eb050b3 100644 (file)
@@ -398,6 +398,17 @@ TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) {
   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