From a2e7d0dee30d60bb212b6f362d54c1840427abe9 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Tue, 29 Aug 2017 13:51:38 +0000 Subject: [clang-format] Do not format likely xml 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 llvm-svn: 311999 --- clang/lib/Format/Format.cpp | 12 +++++++++--- clang/unittests/Format/FormatTest.cpp | 7 +++++++ clang/unittests/Format/SortIncludesTest.cpp | 11 +++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) (limited to 'clang') diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index aa4ed8c42a7..4b067fcea3e 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -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 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 diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index a216c803f1f..1c57f033f46 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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 diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp index 1128ed829ff..ff3988776bd 100644 --- a/clang/unittests/Format/SortIncludesTest.cpp +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -398,6 +398,17 @@ TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) { EXPECT_EQ(26u, Ranges[0].getLength()); } +TEST_F(SortIncludesTest, DoNotSortLikelyXml) { + EXPECT_EQ("", + sort("")); +} + } // end namespace } // end namespace format } // end namespace clang -- cgit v1.2.3