[PATCH] D29186: clang-format: [JS] do not format MPEG transport streams.

Martin Probst via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 27 01:20:34 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL293270: clang-format: [JS] do not format MPEG transport streams. (authored by mprobst).

Changed prior to commit:
  https://reviews.llvm.org/D29186?vs=85934&id=86028#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D29186

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


Index: cfe/trunk/lib/Format/Format.cpp
===================================================================
--- cfe/trunk/lib/Format/Format.cpp
+++ cfe/trunk/lib/Format/Format.cpp
@@ -1462,12 +1462,22 @@
   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 @@
   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: cfe/trunk/unittests/Format/FormatTestJS.cpp
===================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp
@@ -1036,6 +1036,15 @@
   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;} = {};");
Index: cfe/trunk/tools/clang-format/ClangFormat.cpp
===================================================================
--- cfe/trunk/tools/clang-format/ClangFormat.cpp
+++ cfe/trunk/tools/clang-format/ClangFormat.cpp
@@ -256,6 +256,7 @@
     llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
     return true;
   }
+
   if (SortIncludes.getNumOccurrences() != 0)
     FormatStyle->SortIncludes = SortIncludes;
   unsigned CursorPosition = Cursor;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29186.86028.patch
Type: text/x-patch
Size: 2743 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170127/2669058d/attachment.bin>


More information about the cfe-commits mailing list