]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] do not format MPEG transport streams.
authorMartin Probst <martin@probst.io>
Fri, 27 Jan 2017 09:09:11 +0000 (09:09 +0000)
committerMartin Probst <martin@probst.io>
Fri, 27 Jan 2017 09:09:11 +0000 (09:09 +0000)
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

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

lib/Format/Format.cpp
tools/clang-format/ClangFormat.cpp
unittests/Format/FormatTestJS.cpp

index 82647b6b2e3a0299d48cc94b3ea4d2adcf2b1610..91456112f982794d94195afdded3ba608bf09531 100644 (file)
@@ -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<tooling::Range> 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 &&
index b08d4fa4c186dbee8cd68fb6e83db6b60c48f2df..941f90396d7798fce0365f4857e3447458fa9f43 100644 (file)
@@ -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;
index 50ba4ffdae99a0c0454e4f8a21b8d319e6263ad5..53800de9d2327973cdf2dcefdb7edb95795faa3f 100644 (file)
@@ -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;} = {};");