diff options
Diffstat (limited to 'llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp')
| -rw-r--r-- | llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp index 53a18b42505..588b83850ac 100644 --- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -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 += ">"; else if (C == '\"') Result += """; - 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 += " "; + 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 |

