mprobst created this revision.
Herald added a subscriber: klimek.

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.


https://reviews.llvm.org/D29186

Files:
  tools/clang-format/ClangFormat.cpp
  unittests/Format/FormatTestJS.cpp


Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -998,6 +998,13 @@
   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
 }
 
+TEST_F(FormatTestJS, IgnoresMpegTS) {
+  char mpegTS[200];
+  mpegTS[0] = 0x47;
+  mpegTS[188] = 0x47;
+  verifyFormat(mpegTS);
+}
+
 TEST_F(FormatTestJS, TypeAnnotations) {
   verifyFormat("var x: string;");
   verifyFormat("var x: {a: string; b: number;} = {};");
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -251,6 +251,14 @@
   StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
   FormatStyle FormatStyle =
       getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());
+
+  // 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.
+  if (AssumedFileName.endswith(".ts") && Code->getBufferSize() > 188 &&
+          Code->getBuffer()[0] == 0x47 && Code->getBuffer()[188] == 0x47)
+    return true;
+
   if (SortIncludes.getNumOccurrences() != 0)
     FormatStyle.SortIncludes = SortIncludes;
   unsigned CursorPosition = Cursor;


Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -998,6 +998,13 @@
   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
 }
 
+TEST_F(FormatTestJS, IgnoresMpegTS) {
+  char mpegTS[200];
+  mpegTS[0] = 0x47;
+  mpegTS[188] = 0x47;
+  verifyFormat(mpegTS);
+}
+
 TEST_F(FormatTestJS, TypeAnnotations) {
   verifyFormat("var x: string;");
   verifyFormat("var x: {a: string; b: number;} = {};");
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp
+++ tools/clang-format/ClangFormat.cpp
@@ -251,6 +251,14 @@
   StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
   FormatStyle FormatStyle =
       getStyle(Style, AssumedFileName, FallbackStyle, Code->getBuffer());
+
+  // 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.
+  if (AssumedFileName.endswith(".ts") && Code->getBufferSize() > 188 &&
+          Code->getBuffer()[0] == 0x47 && Code->getBuffer()[188] == 0x47)
+    return true;
+
   if (SortIncludes.getNumOccurrences() != 0)
     FormatStyle.SortIncludes = SortIncludes;
   unsigned CursorPosition = Cursor;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to