[llvm-cov] Fix issues with segment highlighting in the html view
authorVedant Kumar <vsk@apple.com>
Thu, 8 Sep 2016 19:18:23 +0000 (19:18 +0000)
committerVedant Kumar <vsk@apple.com>
Thu, 8 Sep 2016 19:18:23 +0000 (19:18 +0000)
The text and html coverage views take different approaches to emitting
highlighted regions. That's because this problem is easier in the text
view: there's no need to worry about escaping text or adding tooltip
content to a highlighted snippet.

Unfortunately, the html view didn't get region highlighting quite right.

This patch fixes the situation, bringing parity between the two views.

llvm-svn: 280981

llvm/test/tools/llvm-cov/showHighlightedRanges.cpp
llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp
llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp

index c53a6c9..4ed3f17 100644 (file)
@@ -1,38 +1,38 @@
 // RUN: llvm-cov show %S/Inputs/highlightedRanges.covmapping -instr-profile %S/Inputs/highlightedRanges.profdata -dump -filename-equivalence %s 2>&1 | FileCheck %s -check-prefixes=TEXT,SHARED
 
 void func() {
-  return;                        // TEXT: Highlighted line [[@LINE+1]], 3 -> ?
-  int i = 0;                     // HTML: Highlighted line [[@LINE]], 1 ->
+  return;
+  int i = 0;                     // SHARED: Highlighted line [[@LINE]], 3 ->
 }                                // SHARED: Highlighted line [[@LINE]], 1 -> 2
 
 void func2(int x) {
   if(x > 5) {
     while(x >= 9) {
       return;
-      --x;                       // TEXT: Highlighted line [[@LINE]], 7 -> ?
+      --x;                       // SHARED: Highlighted line [[@LINE]], 7 ->
     }                            // SHARED: Highlighted line [[@LINE]], 1 -> 6
-    int i = 0;                   // TEXT: Highlighted line [[@LINE]], 5 -> ?
+    int i = 0;                   // SHARED: Highlighted line [[@LINE]], 5 ->
   }                              // SHARED: Highlighted line [[@LINE]], 1 -> 4
 }
 
 void test() {
   int x = 0;
 
-  if (x) {                       // TEXT: Highlighted line [[@LINE]], 10 -> ?
-    x = 0;                       // SHARED: Highlighted line [[@LINE]], 1 -> ?
-  } else {                       // TEXT: Highlighted line [[@LINE]], 1 -> 4
+  if (x) {                       // SHARED: Highlighted line [[@LINE]], 10 ->
+    x = 0;                       // SHARED: Highlighted line [[@LINE]], 1 ->
+  } else {                       // SHARED: Highlighted line [[@LINE]], 1 -> 4
     x = 1;
   }
 
-                                  // TEXT: Highlighted line [[@LINE+1]], 26 -> 29
-  for (int i = 0; i < 0; ++i) {   // TEXT: Highlighted line [[@LINE]], 31 -> ?
-    x = 1;                        // TEXT: Highlighted line [[@LINE]], 1 -> ?
+                                  // SHARED: Highlighted line [[@LINE+1]], 26 ->
+  for (int i = 0; i < 0; ++i) {   // SHARED: Highlighted line [[@LINE]], 31 ->
+    x = 1;                        // SHARED: Highlighted line [[@LINE]], 1 ->
   }                               // SHARED: Highlighted line [[@LINE]], 1 -> 4
 
   x = x < 10 ? x +
                1
-             : x - 1;             // TEXT: Highlighted line [[@LINE]], 16 -> 21
-  x = x > 10 ? x +                // TEXT: Highlighted line [[@LINE]], 16 -> ?
+             : x - 1;             // SHARED: Highlighted line [[@LINE]], 16 -> 21
+  x = x > 10 ? x +                // SHARED: Highlighted line [[@LINE]], 16 ->
                1                  // SHARED: Highlighted line [[@LINE]], 1 -> 17
              : x - 1;
 }
index 9637af4..f544832 100644 (file)
@@ -50,7 +50,7 @@ int main() {                              // TEXT: [[@LINE]]|   161|int main(
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>int main() {
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>  int x = 0
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>
-// HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre><span class='red'>  if (x) {
+// HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>  if (x) {
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre><span class='red'>  }</span>
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>    x = 1;
@@ -62,7 +62,7 @@ int main() {                              // TEXT: [[@LINE]]|   161|int main(
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>  x = x &lt; 10
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>  x = x &gt; 10
-// HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre><span class='red'>        x - 1:
+// HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>        x - 1:
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>        x + 1;
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>
 // HTML: <td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>161</pre></td><td class='code'><pre>  return 0;
index ca3f350..17353b5 100644 (file)
@@ -415,26 +415,21 @@ void SourceCoverageViewHTML::renderLine(
   //    snippet 3, and so on.
 
   Optional<std::string> Color;
-  SmallVector<std::pair<unsigned, unsigned>, 4> HighlightedRanges;
+  SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
   auto Highlight = [&](const std::string &Snippet, unsigned LC, unsigned RC) {
-    if (getOptions().Debug) {
-      if (!HighlightedRanges.empty() &&
-          HighlightedRanges.back().second == LC - 1) {
-        HighlightedRanges.back().second = RC;
-      } else
-        HighlightedRanges.emplace_back(LC, RC);
-    }
+    if (getOptions().Debug)
+      HighlightedRanges.emplace_back(LC, RC);
     return tag("span", Snippet, Color.getValue());
   };
 
   auto CheckIfUncovered = [](const coverage::CoverageSegment *S) {
-    return S && (S->HasCount && S->Count == 0);
+    return S && S->HasCount && S->Count == 0;
   };
 
-  if (CheckIfUncovered(WrappedSegment) ||
-      CheckIfUncovered(Segments.empty() ? nullptr : Segments.front())) {
+  if (CheckIfUncovered(WrappedSegment)) {
     Color = "red";
-    Snippets[0] = Highlight(Snippets[0], 0, Snippets[0].size());
+    if (!Snippets[0].empty())
+      Snippets[0] = Highlight(Snippets[0], 1, 1 + Snippets[0].size());
   }
 
   for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
@@ -452,16 +447,15 @@ void SourceCoverageViewHTML::renderLine(
   }
 
   if (Color.hasValue() && Segments.empty())
-    Snippets.back() = Highlight(Snippets.back(), Snippets[0].size(), 0);
+    Snippets.back() = Highlight(Snippets.back(), 1, 1 + Snippets.back().size());
 
   if (getOptions().Debug) {
     for (const auto &Range : HighlightedRanges) {
-      errs() << "Highlighted line " << LineNo << ", " << Range.first + 1
-             << " -> ";
+      errs() << "Highlighted line " << LineNo << ", " << Range.first << " -> ";
       if (Range.second == 0)
         errs() << "?";
       else
-        errs() << Range.second + 1;
+        errs() << Range.second;
       errs() << "\n";
     }
   }