[LLVM-COV]Replace tabs to the space indentations in the HTML coverage 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
This commit is contained in:
Ying Yi
2016-08-04 10:39:43 +00:00
parent 3d88f0c3fb
commit 0ef31b7960
6 changed files with 54 additions and 12 deletions

View File

@@ -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 += "&";
else if (C == '<')
@@ -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;
@@ -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;
@@ -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;
}
@@ -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);
@@ -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;
@@ -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;
}
@@ -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