Skip to content

Commit

Permalink
[LLVM-COV]Replace tabs to the space indentations in the HTML coverage…
Browse files Browse the repository at this point in the history
… report.

When using orbis-llvm-cov.exe to generate the HTML report, the HTML report 
can look quite different to the source file if it includes tabs.The default
tab size is 2 spaces instead of 8 spaces. A command line switch is
be added to set the tab size.

Differential Revision: https://reviews.llvm.org/D23087

llvm-svn: 277715
  • Loading branch information
MaggieYingYi committed Aug 4, 2016
1 parent 3d88f0c commit 0ef31b7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
Binary file not shown.
8 changes: 8 additions & 0 deletions llvm/test/tools/llvm-cov/Inputs/showTabsHTML.proftext
@@ -0,0 +1,8 @@
main
# Func Hash:
0
# Num Counters:
1
# Counter Values:
1

16 changes: 16 additions & 0 deletions llvm/test/tools/llvm-cov/showTabsHTML.cpp
@@ -0,0 +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 -filename-equivalence %s | FileCheck -check-prefix=CHECK %s

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";

return 0;
}

// RUN: llvm-cov show %S/Inputs/showTabsHTML.covmapping -format html -tab-size=3 -instr-profile %t.profdata -filename-equivalence %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";
5 changes: 5 additions & 0 deletions llvm/tools/llvm-cov/CodeCoverage.cpp
Expand Up @@ -584,6 +584,10 @@ int CodeCoverageTool::show(int argc, const char **argv,
cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
cl::aliasopt(ShowOutputDirectory));

cl::opt<uint32_t> TabSize(
"tab-size", cl::Hidden, cl::init(2),
cl::desc("Set tab size for the HTML coverage report (default = 2)"));

auto Err = commandLineParser(argc, argv);
if (Err)
return Err;
Expand All @@ -596,6 +600,7 @@ int CodeCoverageTool::show(int argc, const char **argv,
ViewOpts.ShowExpandedRegions = ShowExpansions;
ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
ViewOpts.TabSize = TabSize;

if (ViewOpts.hasOutputDirectory()) {
if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
Expand Down
1 change: 1 addition & 0 deletions llvm/tools/llvm-cov/CoverageViewOptions.h
Expand Up @@ -34,6 +34,7 @@ struct CoverageViewOptions {
OutputFormat Format;
std::string ShowOutputDirectory;
std::vector<std::string> DemanglerOpts;
uint32_t TabSize;

/// \brief Change the output's stream color if the colors are enabled.
ColoredRawOstream colored_ostream(raw_ostream &OS,
Expand Down
36 changes: 24 additions & 12 deletions llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
Expand Up @@ -22,9 +22,11 @@ using namespace llvm;
namespace {

// Return a string with the special characters in \p Str escaped.
std::string escape(StringRef Str) {
std::string escape(StringRef Str, const CoverageViewOptions &Opts) {
std::string Result;
unsigned ColNum = 0; // Record the column number.
for (char C : Str) {
++ColNum;
if (C == '&')
Result += "&amp;";
else if (C == '<')
Expand All @@ -33,7 +35,16 @@ std::string escape(StringRef Str) {
Result += "&gt;";
else if (C == '\"')
Result += "&quot;";
else
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);
for (unsigned I = 0; I < NumSpaces; ++I)
Result += "&nbsp;";
ColNum += NumSpaces;
} else
Result += C;
}
return Result;
Expand Down Expand Up @@ -195,7 +206,8 @@ std::string getPathToStyle(StringRef ViewPath) {
return PathToStyle + "style.css";
}

void emitPrelude(raw_ostream &OS, const std::string &PathToStyle = "") {
void emitPrelude(raw_ostream &OS, const CoverageViewOptions &Opts,
const std::string &PathToStyle = "") {
OS << "<!doctype html>"
"<html>"
<< BeginHeader;
Expand All @@ -204,8 +216,8 @@ void emitPrelude(raw_ostream &OS, const std::string &PathToStyle = "") {
if (PathToStyle.empty())
OS << "<style>" << CSSForCoverage << "</style>";
else
OS << "<link rel='stylesheet' type='text/css' href='" << escape(PathToStyle)
<< "'>";
OS << "<link rel='stylesheet' type='text/css' href='"
<< escape(PathToStyle, Opts) << "'>";

OS << EndHeader << "<body>" << BeginCenteredDiv;
}
Expand All @@ -226,10 +238,10 @@ CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) {
OwnedStream OS = std::move(OSOrErr.get());

if (!Opts.hasOutputDirectory()) {
emitPrelude(*OS.get());
emitPrelude(*OS.get(), Opts);
} else {
std::string ViewPath = getOutputPath(Path, "html", InToplevel);
emitPrelude(*OS.get(), getPathToStyle(ViewPath));
emitPrelude(*OS.get(), Opts, getPathToStyle(ViewPath));
}

return std::move(OS);
Expand All @@ -248,13 +260,13 @@ Error CoveragePrinterHTML::createIndexFile(ArrayRef<StringRef> SourceFiles) {

// Emit a table containing links to reports for each file in the covmapping.
assert(Opts.hasOutputDirectory() && "No output directory for index file");
emitPrelude(OSRef, getPathToStyle(""));
emitPrelude(OSRef, Opts, getPathToStyle(""));
OSRef << BeginSourceNameDiv << "Index" << EndSourceNameDiv;
OSRef << BeginTable;
for (StringRef SF : SourceFiles) {
std::string LinkText = escape(sys::path::relative_path(SF));
std::string LinkText = escape(sys::path::relative_path(SF), Opts);
std::string LinkTarget =
escape(getOutputPath(SF, "html", /*InToplevel=*/false));
escape(getOutputPath(SF, "html", /*InToplevel=*/false), Opts);
OSRef << tag("tr", tag("td", tag("pre", a(LinkTarget, LinkText), "code")));
}
OSRef << EndTable;
Expand All @@ -280,7 +292,7 @@ void SourceCoverageViewHTML::renderViewFooter(raw_ostream &OS) {
}

void SourceCoverageViewHTML::renderSourceName(raw_ostream &OS) {
OS << BeginSourceNameDiv << tag("pre", escape(getSourceName()))
OS << BeginSourceNameDiv << tag("pre", escape(getSourceName(), getOptions()))
<< EndSourceNameDiv;
}

Expand Down Expand Up @@ -336,7 +348,7 @@ void SourceCoverageViewHTML::renderLine(
// 2. Escape all of the snippets.

for (unsigned I = 0, E = Snippets.size(); I < E; ++I)
Snippets[I] = escape(Snippets[I]);
Snippets[I] = escape(Snippets[I], getOptions());

// 3. Use \p WrappedSegment to set the highlight for snippet 0. Use segment
// 1 to set the highlight for snippet 2, segment 2 to set the highlight for
Expand Down

0 comments on commit 0ef31b7

Please sign in to comment.