From fa37b18f9406c33cda01e0d60bbec6d20e20682a Mon Sep 17 00:00:00 2001 From: Martin Probst Date: Fri, 27 Jan 2017 09:09:11 +0000 Subject: [PATCH] clang-format: [JS] do not format MPEG transport streams. Summary: The MPEG transport stream file format also uses ".ts" as its file extension. This change detects its specific framing format (0x47 every 189 bytes) and simply ignores MPEG TS files. Reviewers: djasper, sammccall Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D29186 llvm-svn: 293270 --- clang/lib/Format/Format.cpp | 13 ++++++++++++- clang/tools/clang-format/ClangFormat.cpp | 1 + clang/unittests/Format/FormatTestJS.cpp | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 82647b6b2e3a..91456112f982 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1462,12 +1462,22 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code, return Replaces; } +bool isMpegTS(StringRef Code) { + // MPEG transport streams use the ".ts" file extension. clang-format should + // not attempt to format those. MPEG TS' frame format starts with 0x47 every + // 189 bytes - detect that and return. + return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47; +} + 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)) + return Replaces; if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript) return sortJavaScriptImports(Style, Code, Ranges, FileName); sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor); @@ -1813,7 +1823,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)) + return tooling::Replacements(); auto Env = Environment::CreateVirtualEnvironment(Code, FileName, Ranges); if (Style.Language == FormatStyle::LK_JavaScript && diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index b08d4fa4c186..941f90396d77 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -256,6 +256,7 @@ static bool format(StringRef FileName) { llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; return true; } + if (SortIncludes.getNumOccurrences() != 0) FormatStyle->SortIncludes = SortIncludes; unsigned CursorPosition = Cursor; diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 50ba4ffdae99..53800de9d232 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -1036,6 +1036,15 @@ TEST_F(FormatTestJS, RegexLiteralExamples) { verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); } +TEST_F(FormatTestJS, IgnoresMpegTS) { + std::string MpegTS(200, ' '); + MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "), + "nearlyLooks + like + ts + code; "); + MpegTS[0] = 0x47; + MpegTS[188] = 0x47; + verifyFormat(MpegTS, MpegTS); +} + TEST_F(FormatTestJS, TypeAnnotations) { verifyFormat("var x: string;"); verifyFormat("var x: {a: string; b: number;} = {};");