From: Vedant Kumar Date: Thu, 21 Jul 2016 23:26:15 +0000 (+0000) Subject: [llvm-cov] Use relative paths to the stylesheet (for html reports) X-Git-Tag: llvmorg-4.0.0-rc1~14556 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c076c490761998da900ed955e60dba1befc84866;p=platform%2Fupstream%2Fllvm.git [llvm-cov] Use relative paths to the stylesheet (for html reports) This makes it easy to swap out the default stylesheet for a custom one. It also shaves ~6.62 MB out of the report directory for a full coverage build of llvm+clang. While we're at it, prune the CSS and add tests for it. llvm-svn: 276359 --- diff --git a/llvm/test/tools/llvm-cov/style.test b/llvm/test/tools/llvm-cov/style.test new file mode 100644 index 0000000..3cad21f --- /dev/null +++ b/llvm/test/tools/llvm-cov/style.test @@ -0,0 +1,22 @@ +RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -filename-equivalence %S/showTemplateInstantiations.cpp -format html -o %t.dir + +RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -filename-equivalence -name=_Z4funcIbEiT_ %S/showTemplateInstantiations.cpp -format html -o %t.dir + +RUN: FileCheck %s -input-file=%t.dir/style.css -check-prefix=STYLE +RUN: FileCheck %s -input-file=%t.dir/functions.html -check-prefix=FUNCTIONS +RUN: FileCheck %s -input-file=%t.dir/coverage/tmp/showTemplateInstantiations.cpp.html -check-prefix=FILEVIEW + +STYLE-DAG: .red +STYLE-DAG: .cyan +STYLE-DAG: .source-name-title +STYLE-DAG: .centered +STYLE-DAG: .expansion-view +STYLE-DAG: .line-number +STYLE-DAG: .covered-line +STYLE-DAG: .uncovered-line +STYLE-DAG: .tooltip +STYLE-DAG: .tooltip span.tooltip-content + +FUNCTIONS: + +FILEVIEW: diff --git a/llvm/tools/llvm-cov/SourceCoverageView.h b/llvm/tools/llvm-cov/SourceCoverageView.h index feef959..6f2c400 100644 --- a/llvm/tools/llvm-cov/SourceCoverageView.h +++ b/llvm/tools/llvm-cov/SourceCoverageView.h @@ -99,8 +99,6 @@ struct LineCoverageStats { /// \brief A file manager that handles format-aware file creation. class CoveragePrinter { - const CoverageViewOptions &Opts; - public: struct StreamDestructor { void operator()(raw_ostream *OS) const; @@ -109,6 +107,8 @@ public: using OwnedStream = std::unique_ptr; protected: + const CoverageViewOptions &Opts; + CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {} /// \brief Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp index fb4ad2d..ceab556 100644 --- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -21,33 +21,51 @@ using namespace llvm; namespace { +// Return a string with the special characters in \p Str escaped. +std::string escape(StringRef Str) { + std::string Result; + for (char C : Str) { + if (C == '&') + Result += "&"; + else if (C == '<') + Result += "<"; + else if (C == '>') + Result += ">"; + else if (C == '\"') + Result += """; + else + Result += C; + } + return Result; +} + +// Create a \p Name tag around \p Str, and optionally set its \p ClassName. +std::string tag(const std::string &Name, const std::string &Str, + const std::string &ClassName = "") { + std::string Tag = "<" + Name; + if (ClassName != "") + Tag += " class='" + ClassName + "'"; + return Tag + ">" + Str + ""; +} + +// Create an anchor to \p Link with the label \p Str. +std::string a(const std::string &Link, const std::string &Str, + const std::string &TargetType = "href") { + return "" + Str + ""; +} + const char *BeginHeader = "" "" ""; const char *CSSForCoverage = - ""; +)"; const char *EndHeader = ""; @@ -170,11 +186,28 @@ const char *BeginTable = ""; const char *EndTable = "
"; -void emitPrelude(raw_ostream &OS) { +std::string getPathToStyle(StringRef ViewPath) { + std::string PathToStyle = ""; + std::string PathSep = sys::path::get_separator(); + unsigned NumSeps = ViewPath.count(PathSep); + for (unsigned I = 0, E = NumSeps; I < E; ++I) + PathToStyle += ".." + PathSep; + return PathToStyle + "style.css"; +} + +void emitPrelude(raw_ostream &OS, const std::string &PathToStyle = "") { OS << "" "" - << BeginHeader << CSSForCoverage << EndHeader << "" - << BeginCenteredDiv; + << BeginHeader; + + // Link to a stylesheet if one is available. Otherwise, use the default style. + if (PathToStyle.empty()) + OS << ""; + else + OS << ""; + + OS << EndHeader << "" << BeginCenteredDiv; } void emitEpilog(raw_ostream &OS) { @@ -182,39 +215,6 @@ void emitEpilog(raw_ostream &OS) { ""; } -// Return a string with the special characters in \p Str escaped. -std::string escape(StringRef Str) { - std::string Result; - for (char C : Str) { - if (C == '&') - Result += "&"; - else if (C == '<') - Result += "<"; - else if (C == '>') - Result += ">"; - else if (C == '\"') - Result += """; - else - Result += C; - } - return Result; -} - -// Create a \p Name tag around \p Str, and optionally set its \p ClassName. -std::string tag(const std::string &Name, const std::string &Str, - const std::string &ClassName = "") { - std::string Tag = "<" + Name; - if (ClassName != "") - Tag += " class='" + ClassName + "'"; - return Tag + ">" + Str + ""; -} - -// Create an anchor to \p Link with the label \p Str. -std::string a(const std::string &Link, const std::string &Str, - const std::string &TargetType = "href") { - return "" + Str + ""; -} - } // anonymous namespace Expected @@ -224,7 +224,14 @@ CoveragePrinterHTML::createViewFile(StringRef Path, bool InToplevel) { return OSOrErr; OwnedStream OS = std::move(OSOrErr.get()); - emitPrelude(*OS.get()); + + if (!Opts.hasOutputDirectory()) { + emitPrelude(*OS.get()); + } else { + std::string ViewPath = getOutputPath(Path, "html", InToplevel); + emitPrelude(*OS.get(), getPathToStyle(ViewPath)); + } + return std::move(OS); } @@ -252,6 +259,14 @@ Error CoveragePrinterHTML::createIndexFile(ArrayRef SourceFiles) { OSRef << EndTable; emitEpilog(OSRef); + // Emit the default stylesheet. + auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true); + if (Error E = CSSOrErr.takeError()) + return E; + + OwnedStream CSS = std::move(CSSOrErr.get()); + CSS->operator<<(CSSForCoverage); + return Error::success(); }