[llvm] r333608 - [llvm-cov] Use the new PrintHTMLEscaped utility

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Wed May 30 16:35:14 PDT 2018


Author: vedantk
Date: Wed May 30 16:35:14 2018
New Revision: 333608

URL: http://llvm.org/viewvc/llvm-project?rev=333608&view=rev
Log:
[llvm-cov] Use the new PrintHTMLEscaped utility

This removes some duplicate logic to escape characters in HTML output.

Modified:
    llvm/trunk/test/tools/llvm-cov/showTabsHTML.cpp
    llvm/trunk/tools/llvm-cov/SourceCoverageViewHTML.cpp

Modified: llvm/trunk/test/tools/llvm-cov/showTabsHTML.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/showTabsHTML.cpp?rev=333608&r1=333607&r2=333608&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/showTabsHTML.cpp (original)
+++ llvm/trunk/test/tools/llvm-cov/showTabsHTML.cpp Wed May 30 16:35:14 2018
@@ -1,16 +1,16 @@
 // RUN: llvm-profdata merge -o %t.profdata %S/Inputs/showTabsHTML.proftext
-// RUN: llvm-cov show %S/Inputs/showTabsHTML.covmapping -format html -instr-profile %t.profdata -path-equivalence=/tmp,%S %s | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/showTabsHTML.covmapping -format html -instr-profile %t.profdata -path-equivalence=/tmp,%S %s | FileCheck %s --strict-whitespace
 
 int main(int argc, char ** argv) {
-	(void) "This tab starts at column 0";            // CHECK:   (void) "This tab starts at column 0";
-  (void) "	This tab starts at column 10";           // CHECK: (void) "  This tab starts at column 10";
-  (void) "This 	 tab starts at column 15";           // CHECK: (void) "This   tab starts at column 15";
+	(void) "This tab starts at column 0";              // CHECK: >  (void) "This tab starts at column 0";
+  (void) "	This tab starts at column 10";           // CHECK: >  (void) "  This tab starts at column 10";
+  (void) "This 	 tab starts at column 15";           // CHECK: >  (void) "This   tab starts at column 15";
 
   return 0;
 }
 
 // RUN: llvm-cov show %S/Inputs/showTabsHTML.covmapping -format html -tab-size=3 -instr-profile %t.profdata -path-equivalence=/tmp,%S %s | FileCheck -check-prefix=CHECK-TABSIZE %s
 
-// CHECK-TABSIZE:    (void) "This tab starts at column 0";
-// CHECK-TABSIZE: (void) "  This tab starts at column 10";
-// CHECK-TABSIZE: (void) "This     tab starts at column 15";
+// CHECK-TABSIZE: >  (void) "This tab starts at column 0";
+// CHECK-TABSIZE: >  (void) "   This tab starts at column 10";
+// CHECK-TABSIZE: >  (void) "This    tab starts at column 15";

Modified: llvm/trunk/tools/llvm-cov/SourceCoverageViewHTML.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cov/SourceCoverageViewHTML.cpp?rev=333608&r1=333607&r2=333608&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cov/SourceCoverageViewHTML.cpp (original)
+++ llvm/trunk/tools/llvm-cov/SourceCoverageViewHTML.cpp Wed May 30 16:35:14 2018
@@ -25,31 +25,29 @@ namespace {
 
 // Return a string with the special characters in \p Str escaped.
 std::string escape(StringRef Str, const CoverageViewOptions &Opts) {
-  std::string Result;
+  std::string TabExpandedResult;
   unsigned ColNum = 0; // Record the column number.
   for (char C : Str) {
-    ++ColNum;
-    if (C == '&')
-      Result += "&";
-    else if (C == '<')
-      Result += "<";
-    else if (C == '>')
-      Result += ">";
-    else if (C == '\"')
-      Result += """;
-    else if (C == '\n' || C == '\r') {
-      Result += C;
-      ColNum = 0;
-    } else if (C == '\t') {
-      // Replace '\t' with TabSize spaces.
-      unsigned NumSpaces = Opts.TabSize - (--ColNum % Opts.TabSize);
+    if (C == '\t') {
+      // Replace '\t' with up to TabSize spaces.
+      unsigned NumSpaces = Opts.TabSize - (ColNum % Opts.TabSize);
       for (unsigned I = 0; I < NumSpaces; ++I)
-        Result += " ";
+        TabExpandedResult += ' ';
       ColNum += NumSpaces;
-    } else
-      Result += C;
+    } else {
+      TabExpandedResult += C;
+      if (C == '\n' || C == '\r')
+        ColNum = 0;
+      else
+        ++ColNum;
+    }
   }
-  return Result;
+  std::string EscapedHTML;
+  {
+    raw_string_ostream OS{EscapedHTML};
+    PrintHTMLEscaped(TabExpandedResult, OS);
+  }
+  return EscapedHTML;
 }
 
 // Create a \p Name tag around \p Str, and optionally set its \p ClassName.




More information about the llvm-commits mailing list