Index: docs/CommandGuide/llvm-cov.rst =================================================================== --- docs/CommandGuide/llvm-cov.rst +++ docs/CommandGuide/llvm-cov.rst @@ -246,6 +246,10 @@ Show code coverage only for functions that match the given regular expression. +.. option:: -ignore-source-regex= + + Skip source code files with file paths that match the given regular expression. + .. option:: -format= Use the specified output format. The supported formats are: "text", "html". @@ -351,6 +355,10 @@ Show statistics for all function instantiations. Defaults to false. +.. option:: -ignore-source-regex= + + Skip source code files with file paths that match the given regular expression. + .. program:: llvm-cov export .. _llvm-cov-export: @@ -390,3 +398,7 @@ will not export coverage information for smaller units such as individual functions or regions. The result will be the same as produced by :program: `llvm-cov report` command, but presented in JSON format rather than text. + +.. option:: -ignore-source-regex= + + Skip source code files with file paths that match the given regular expression. Index: test/tools/llvm-cov/sources-ignored.test =================================================================== --- /dev/null +++ test/tools/llvm-cov/sources-ignored.test @@ -0,0 +1,58 @@ +######################## +# Test "report" command. +######################## +# Ignore all header files. +RUN: llvm-cov report -instr-profile %S/Inputs/sources_specified/main.profdata \ +RUN: -path-equivalence=/tmp,%S/Inputs -ignore-source-regex='.*\.h$' \ +RUN: %S/Inputs/sources_specified/main.covmapping \ +RUN: | FileCheck -check-prefix=REPORT_IGNORE_HEADERS %s + +REPORT_IGNORE_HEADERS-NOT: {{.*}}dec.h{{.*}} +REPORT_IGNORE_HEADERS-NOT: {{.*}}inc.h{{.*}} +REPORT_IGNORE_HEADERS-NOT: {{.*}}abs.h{{.*}} +REPORT_IGNORE_HEADERS: {{^}}TOTAL 1{{.*}}100.00%{{$}} + +# Ignore all files from "extra" directory. +RUN: llvm-cov report -instr-profile %S/Inputs/sources_specified/main.profdata \ +RUN: -path-equivalence=/tmp,%S/Inputs -ignore-source-regex='.*extra[/\\].*' \ +RUN: %S/Inputs/sources_specified/main.covmapping \ +RUN: | FileCheck -check-prefix=REPORT_IGNORE_DIR %s + +REPORT_IGNORE_DIR-NOT: {{.*}}extra{{[/\\]}}dec.h{{.*}} +REPORT_IGNORE_DIR-NOT: {{.*}}extra{{[/\\]}}inc.h{{.*}} +REPORT_IGNORE_DIR: {{.*}}abs.h{{.*}} +REPORT_IGNORE_DIR: {{.*}}main.cc{{.*}} +REPORT_IGNORE_DIR: {{^}}TOTAL 5{{.*}}100.00%{{$}} + +######################## +# Test "show" command. +######################## +# Ignore all ".cc" files. +RUN: llvm-cov show -instr-profile %S/Inputs/sources_specified/main.profdata \ +RUN: -path-equivalence=/tmp,%S/Inputs -ignore-source-regex='.*\.cc$' \ +RUN: %S/Inputs/sources_specified/main.covmapping \ +RUN: | FileCheck -check-prefix=SHOW_IGNORE_CC %s + +# Order of files may differ, check that there are 3 files and not abs.h. +SHOW_IGNORE_CC-NOT: {{.*}}main.cc{{.*}} +SHOW_IGNORE_CC: {{.*}}sources_specified{{.*}} +SHOW_IGNORE_CC: {{.*}}sources_specified{{.*}} +SHOW_IGNORE_CC: {{.*}}sources_specified{{.*}} + +######################## +# Test "export" command. +######################## +# Use a temp .json file as output in a single line. Ignore headers that have +# name in a format of 3 symbols followed by ".h". +RUN: llvm-cov export -instr-profile %S/Inputs/sources_specified/main.profdata \ +RUN: -path-equivalence=/tmp,%S/Inputs -ignore-source-regex='.*...\.h$' \ +RUN: %S/Inputs/sources_specified/main.covmapping \ +RUN: > %t.export.json + +RUN: FileCheck -check-prefix=NO-EXPORT_IGNORE_3_SYMBOLS_H %s < %t.export.json +RUN: FileCheck -check-prefix=EXPORT_IGNORE_3_SYMBOLS_H %s < %t.export.json + +NO-EXPORT_IGNORE_3_SYMBOLS_H-NOT: {{"filename":"(/|\\\\)tmp(/|\\\\)sources_specified(/|\\\\)abs.h"}} +NO-EXPORT_IGNORE_3_SYMBOLS_H-NOT: {{"filename":"(/|\\\\)tmp(/|\\\\)sources_specified(/|\\\\)inc.h"}} +NO-EXPORT_IGNORE_3_SYMBOLS_H-NOT: {{"filename":"(/|\\\\)tmp(/|\\\\)sources_specified(/|\\\\)dec.h"}} +EXPORT_IGNORE_3_SYMBOLS_H: {{"filename":"(/|\\\\)tmp(/|\\\\)sources_specified(/|\\\\)main.cc"}} Index: tools/llvm-cov/CMakeLists.txt =================================================================== --- tools/llvm-cov/CMakeLists.txt +++ tools/llvm-cov/CMakeLists.txt @@ -11,5 +11,6 @@ SourceCoverageView.cpp SourceCoverageViewHTML.cpp SourceCoverageViewText.cpp + SourceFilters.cpp TestingSupport.cpp ) Index: tools/llvm-cov/CodeCoverage.cpp =================================================================== --- tools/llvm-cov/CodeCoverage.cpp +++ tools/llvm-cov/CodeCoverage.cpp @@ -20,6 +20,7 @@ #include "CoverageViewOptions.h" #include "RenderingSupport.h" #include "SourceCoverageView.h" +#include "SourceFilters.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" @@ -126,6 +127,7 @@ std::vector ObjectFilenames; CoverageViewOptions ViewOpts; CoverageFiltersMatchAll Filters; + SourceRegexFilters IgnoreSourceFilters; /// The path to the indexed profile. std::string PGOFilename; @@ -189,7 +191,8 @@ return; } sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true); - SourceFiles.emplace_back(EffectivePath.str()); + if (!IgnoreSourceFilters.matches(EffectivePath)) + SourceFiles.emplace_back(EffectivePath.str()); } void CodeCoverageTool::collectPaths(const std::string &Path) { @@ -259,9 +262,9 @@ continue; auto SubViewExpansions = ExpansionCoverage.getExpansions(); - auto SubView = - SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(), - ViewOpts, std::move(ExpansionCoverage)); + auto SubView = SourceCoverageView::create(Expansion.Function.Name, + SourceBuffer.get(), ViewOpts, + std::move(ExpansionCoverage)); attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); View.addExpansion(Expansion.Region, std::move(SubView)); } @@ -278,9 +281,9 @@ return nullptr; auto Expansions = FunctionCoverage.getExpansions(); - auto View = SourceCoverageView::create(DC.demangle(Function.Name), - SourceBuffer.get(), ViewOpts, - std::move(FunctionCoverage)); + auto View = + SourceCoverageView::create(DC.demangle(Function.Name), SourceBuffer.get(), + ViewOpts, std::move(FunctionCoverage)); attachExpansionSubViews(*View, Expansions, Coverage); return View; @@ -597,6 +600,12 @@ "regular expression"), cl::ZeroOrMore, cl::cat(FilteringCategory)); + cl::list IgnoreSourceRegexFilters( + "ignore-source-regex", cl::Optional, + cl::desc("Skip source code files with file paths that match the given " + "regular expression."), + cl::ZeroOrMore, cl::cat(FilteringCategory)); + cl::opt RegionCoverageLtFilter( "region-coverage-lt", cl::Optional, cl::desc("Show code coverage only for functions with region coverage " @@ -692,7 +701,7 @@ ViewOpts.DemanglerOpts.swap(DemanglerOpts); } - // Read in -name-whitelist files. + // Read in -name-whitelist files if (!NameFilterFiles.empty()) { std::string SpecialCaseListErr; NameWhitelist = @@ -701,7 +710,7 @@ error(SpecialCaseListErr); } - // Create the function filters + // Create the function filters. if (!NameFilters.empty() || NameWhitelist || !NameRegexFilters.empty()) { auto NameFilterer = llvm::make_unique(); for (const auto &Name : NameFilters) @@ -714,6 +723,11 @@ llvm::make_unique(Regex)); Filters.push_back(std::move(NameFilterer)); } + + // Create the source files filters. + for (const auto &RE : IgnoreSourceRegexFilters) + IgnoreSourceFilters.push_back(llvm::make_unique(RE)); + if (RegionCoverageLtFilter.getNumOccurrences() || RegionCoverageGtFilter.getNumOccurrences() || LineCoverageLtFilter.getNumOccurrences() || @@ -862,8 +876,11 @@ if (SourceFiles.empty()) // Get the source files from the function coverage mapping. - for (StringRef Filename : Coverage->getUniqueSourceFiles()) - SourceFiles.push_back(Filename); + for (StringRef Filename : Coverage->getUniqueSourceFiles()) { + // Apply ignore source files filters. + if (!IgnoreSourceFilters.matches(Filename)) + SourceFiles.push_back(Filename); + } // Create an index out of the source files. if (ViewOpts.hasOutputDirectory()) { @@ -962,7 +979,7 @@ CoverageReport Report(ViewOpts, *Coverage.get()); if (!ShowFunctionSummaries) { if (SourceFiles.empty()) - Report.renderFileReports(llvm::outs()); + Report.renderFileReports(llvm::outs(), IgnoreSourceFilters); else Report.renderFileReports(llvm::outs(), SourceFiles); } else { @@ -998,7 +1015,7 @@ auto Exporter = CoverageExporterJson(*Coverage.get(), ViewOpts, outs()); if (SourceFiles.empty()) - Exporter.renderRoot(); + Exporter.renderRoot(IgnoreSourceFilters); else Exporter.renderRoot(SourceFiles); Index: tools/llvm-cov/CoverageExporter.h =================================================================== --- tools/llvm-cov/CoverageExporter.h +++ tools/llvm-cov/CoverageExporter.h @@ -16,6 +16,7 @@ #include "CoverageSummaryInfo.h" #include "CoverageViewOptions.h" +#include "SourceFilters.h" #include "llvm/ProfileData/Coverage/CoverageMapping.h" namespace llvm { @@ -40,7 +41,7 @@ virtual ~CoverageExporter(){}; /// \brief Render the CoverageMapping object. - virtual void renderRoot() = 0; + virtual void renderRoot(const SourceRegexFilters &IgnoreFilters) = 0; /// \brief Render the CoverageMapping object for specified source files. virtual void renderRoot(const std::vector &SourceFiles) = 0; Index: tools/llvm-cov/CoverageExporterJson.h =================================================================== --- tools/llvm-cov/CoverageExporterJson.h +++ tools/llvm-cov/CoverageExporterJson.h @@ -101,7 +101,7 @@ const CoverageViewOptions &Options, raw_ostream &OS); /// \brief Render the CoverageMapping object. - void renderRoot() override; + void renderRoot(const SourceRegexFilters &IgnoreFilters) override; /// \brief Render the CoverageMapping object for specified source files. void renderRoot(const std::vector &SourceFiles) override; Index: tools/llvm-cov/CoverageExporterJson.cpp =================================================================== --- tools/llvm-cov/CoverageExporterJson.cpp +++ tools/llvm-cov/CoverageExporterJson.cpp @@ -117,10 +117,12 @@ OS << "]"; } -void CoverageExporterJson::renderRoot() { +void CoverageExporterJson::renderRoot(const SourceRegexFilters &IgnoreFilters) { std::vector SourceFiles; - for (StringRef SF : Coverage.getUniqueSourceFiles()) - SourceFiles.emplace_back(SF); + for (StringRef SF : Coverage.getUniqueSourceFiles()) { + if (!IgnoreFilters.matches(SF)) + SourceFiles.emplace_back(SF); + } renderRoot(SourceFiles); } @@ -218,11 +220,11 @@ void CoverageExporterJson::renderFile(const std::string &Filename, const FileCoverageSummary &FileReport) { - // Start File. + // Start File. emitDictStart(); emitDictElement("filename", Filename); - + if (!Options.ExportSummaryOnly) { // Calculate and render detailed coverage information for given file. auto FileCoverage = Coverage.getCoverageForFile(Filename); Index: tools/llvm-cov/CoverageReport.h =================================================================== --- tools/llvm-cov/CoverageReport.h +++ tools/llvm-cov/CoverageReport.h @@ -17,6 +17,7 @@ #include "CoverageFilters.h" #include "CoverageSummaryInfo.h" #include "CoverageViewOptions.h" +#include "SourceFilters.h" namespace llvm { @@ -53,7 +54,8 @@ const CoverageFilter *Filters); /// Render file reports for every unique file in the coverage mapping. - void renderFileReports(raw_ostream &OS) const; + void renderFileReports(raw_ostream &OS, + const SourceRegexFilters &IgnoreFilters) const; /// Render file reports for the files specified in \p Files. void renderFileReports(raw_ostream &OS, ArrayRef Files) const; Index: tools/llvm-cov/CoverageReport.cpp =================================================================== --- tools/llvm-cov/CoverageReport.cpp +++ tools/llvm-cov/CoverageReport.cpp @@ -379,10 +379,14 @@ return FileReports; } -void CoverageReport::renderFileReports(raw_ostream &OS) const { +void CoverageReport::renderFileReports( + raw_ostream &OS, const SourceRegexFilters &IgnoreFilters) const { std::vector UniqueSourceFiles; - for (StringRef SF : Coverage.getUniqueSourceFiles()) - UniqueSourceFiles.emplace_back(SF.str()); + for (StringRef SF : Coverage.getUniqueSourceFiles()) { + // Apply ignore source files filters. + if (!IgnoreFilters.matches(SF)) + UniqueSourceFiles.emplace_back(SF.str()); + } renderFileReports(OS, UniqueSourceFiles); } Index: tools/llvm-cov/SourceFilters.h =================================================================== --- /dev/null +++ tools/llvm-cov/SourceFilters.h @@ -0,0 +1,59 @@ +//===- SourceFilters.h - Source file names filters ------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// These classes provide filtering for source file names. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_COV_SOURCEFILTERS_H +#define LLVM_COV_SOURCEFILTERS_H + +#include "llvm/ADT/StringRef.h" +#include + +namespace llvm { + +/// \brief Matches specific filename that pass the requirement of this filter. +class SourceFilter { +public: + virtual ~SourceFilter() {} + + /// \brief Return true if the filename passes the requirements of this filter. + virtual bool matches(StringRef Filename) const = 0; +}; + +/// \brief Matches source files whose name matches a certain regular expression. +class SourceRegexFilter : public SourceFilter { + StringRef Regex; + +public: + SourceRegexFilter(StringRef Regex) : Regex(Regex) {} + + bool matches(StringRef Filename) const override; +}; + +/// \brief A collection of filters. +/// Matches source files that match any filters contained +/// in an instance of this class. +class SourceRegexFilters : public SourceFilter { +protected: + std::vector> Filters; + +public: + /// \brief Append a filter to this collection. + void push_back(std::unique_ptr Filter); + + bool empty() const { return Filters.empty(); } + + bool matches(StringRef Filename) const override; +}; + +} // namespace llvm + +#endif // LLVM_COV_SOURCEFILTERS_H Index: tools/llvm-cov/SourceFilters.cpp =================================================================== --- /dev/null +++ tools/llvm-cov/SourceFilters.cpp @@ -0,0 +1,33 @@ +//===- SourceFilters.cpp - Source file names filters ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// These classes provide filtering for source file names. +// +//===----------------------------------------------------------------------===// + +#include "SourceFilters.h" +#include "llvm/Support/Regex.h" + +using namespace llvm; + +bool SourceRegexFilter::matches(StringRef Filename) const { + return llvm::Regex(Regex).match(Filename); +} + +void SourceRegexFilters::push_back(std::unique_ptr Filter) { + Filters.push_back(std::move(Filter)); +} + +bool SourceRegexFilters::matches(StringRef Filename) const { + for (const auto &Filter : Filters) { + if (Filter->matches(Filename)) + return true; + } + return false; +}