]> granicus.if.org Git - llvm/commitdiff
[llvm-cov] Read in function names for filtering from a text file.
authorSean Eveson <eveson.sean@gmail.com>
Thu, 31 Aug 2017 09:11:31 +0000 (09:11 +0000)
committerSean Eveson <eveson.sean@gmail.com>
Thu, 31 Aug 2017 09:11:31 +0000 (09:11 +0000)
Summary: Add a -name-whitelist option, which behaves in the same way as -name, but it reads in multiple function names from the given input file(s).

Reviewers: vsk

Reviewed By: vsk

Subscribers: llvm-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312227 91177308-0d34-0410-b5e6-96231b3b80d8

docs/CommandGuide/llvm-cov.rst
test/tools/llvm-cov/Inputs/name_whitelist.covmapping [new file with mode: 0644]
test/tools/llvm-cov/Inputs/name_whitelist.cpp [new file with mode: 0644]
test/tools/llvm-cov/Inputs/name_whitelist.proftext [new file with mode: 0644]
test/tools/llvm-cov/Inputs/whitelist1.txt [new file with mode: 0644]
test/tools/llvm-cov/Inputs/whitelist2.txt [new file with mode: 0644]
test/tools/llvm-cov/name_whitelist.test [new file with mode: 0644]
tools/llvm-cov/CodeCoverage.cpp
tools/llvm-cov/CoverageFilters.cpp
tools/llvm-cov/CoverageFilters.h

index 082f510375169b2db8453877f7dfd70b484ae8ff..4b5860a4e6ab8b9a4e455955721edcec20e72e7d 100644 (file)
@@ -235,6 +235,12 @@ OPTIONS
 
  Show code coverage only for functions with the given name.
 
+.. option:: -name-whitelist=<FILE>
+
+ Show code coverage only for functions listed in the given file. Each line in
+ the file should start with `whitelist_fun:`, immediately followed by the name
+ of the function to accept. This name can be a wildcard expression.
+
 .. option:: -name-regex=<PATTERN>
 
  Show code coverage only for functions that match the given regular expression.
diff --git a/test/tools/llvm-cov/Inputs/name_whitelist.covmapping b/test/tools/llvm-cov/Inputs/name_whitelist.covmapping
new file mode 100644 (file)
index 0000000..6c067ab
Binary files /dev/null and b/test/tools/llvm-cov/Inputs/name_whitelist.covmapping differ
diff --git a/test/tools/llvm-cov/Inputs/name_whitelist.cpp b/test/tools/llvm-cov/Inputs/name_whitelist.cpp
new file mode 100644 (file)
index 0000000..dec10ea
--- /dev/null
@@ -0,0 +1,18 @@
+int func1() {
+    return 1;
+}
+int func2() {
+    return 1;
+}
+int func3() {
+    return 1;
+}
+int func4() {
+    return 1;
+}
+int func5() {
+    return 1;
+}
+int func6() {
+    return 1;
+}
diff --git a/test/tools/llvm-cov/Inputs/name_whitelist.proftext b/test/tools/llvm-cov/Inputs/name_whitelist.proftext
new file mode 100644 (file)
index 0000000..c806d15
--- /dev/null
@@ -0,0 +1,56 @@
+_Z5func1v
+# Func Hash:
+0
+# Num Counters:
+1
+# Counter Values:
+0
+
+_Z5func2v
+# Func Hash:
+0
+# Num Counters:
+1
+# Counter Values:
+0
+
+_Z5func3v
+# Func Hash:
+0
+# Num Counters:
+1
+# Counter Values:
+0
+
+_Z5func4v
+# Func Hash:
+0
+# Num Counters:
+1
+# Counter Values:
+0
+
+main
+# Func Hash:
+0
+# Num Counters:
+1
+# Counter Values:
+1
+
+_Z5func5v
+# Func Hash:
+0
+# Num Counters:
+1
+# Counter Values:
+0
+
+_Z5func6v
+# Func Hash:
+0
+# Num Counters:
+1
+# Counter Values:
+0
+
diff --git a/test/tools/llvm-cov/Inputs/whitelist1.txt b/test/tools/llvm-cov/Inputs/whitelist1.txt
new file mode 100644 (file)
index 0000000..2646323
--- /dev/null
@@ -0,0 +1,4 @@
+# Comment
+
+whitelist_fun:*func1*
+whitelist_fun:*func2*
diff --git a/test/tools/llvm-cov/Inputs/whitelist2.txt b/test/tools/llvm-cov/Inputs/whitelist2.txt
new file mode 100644 (file)
index 0000000..5809820
--- /dev/null
@@ -0,0 +1,2 @@
+whitelist_fun:*func3*
+whitelist_fun:*func4*
diff --git a/test/tools/llvm-cov/name_whitelist.test b/test/tools/llvm-cov/name_whitelist.test
new file mode 100644 (file)
index 0000000..3eb21e5
--- /dev/null
@@ -0,0 +1,21 @@
+RUN: llvm-profdata merge %S/Inputs/name_whitelist.proftext -o %t.profdata
+
+RUN: llvm-cov show %S/Inputs/name_whitelist.covmapping -instr-profile=%t.profdata -path-equivalence=/tmp,%S/Inputs -name-whitelist=%S/Inputs/whitelist1.txt %S/Inputs/name_whitelist.cpp > %t.one_list
+RUN: FileCheck -input-file=%t.one_list -check-prefix=ONE_WHITELIST %s
+RUN: FileCheck -input-file=%t.one_list -check-prefix=ONE_WHITELIST_NEG %s
+ONE_WHITELIST: _Z5func1v:
+ONE_WHITELIST: _Z5func2v:
+ONE_WHITELIST_NEG-NOT: _Z5func3v:
+ONE_WHITELIST_NEG-NOT: _Z5func4v:
+ONE_WHITELIST_NEG-NOT: _Z5func5v:
+ONE_WHITELIST_NEG-NOT: _Z5func6v:
+
+RUN: llvm-cov show %S/Inputs/name_whitelist.covmapping -instr-profile=%t.profdata -path-equivalence=/tmp,%S/Inputs -name-whitelist=%S/Inputs/whitelist1.txt -name-whitelist=%S/Inputs/whitelist2.txt %S/Inputs/name_whitelist.cpp  > %t.two_list
+RUN: FileCheck -input-file=%t.two_list -check-prefix=TWO_WHITELIST %s
+RUN: FileCheck -input-file=%t.two_list -check-prefix=TWO_WHITELIST_NEG %s
+TWO_WHITELIST: _Z5func1v:
+TWO_WHITELIST: _Z5func2v:
+TWO_WHITELIST: _Z5func3v:
+TWO_WHITELIST: _Z5func4v:
+TWO_WHITELIST_NEG-NOT: _Z5func5v:
+TWO_WHITELIST_NEG-NOT: _Z5func6v:
index e5878df6c154fc88ce66b57ed77ed6fa40d16a6d..f75fcb8884c47bfbbfbdf46e2222d75ecbcb65a4 100644 (file)
@@ -150,6 +150,9 @@ private:
   std::mutex LoadedSourceFilesLock;
   std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
       LoadedSourceFiles;
+
+  /// Whitelist from -name-whitelist to be used for filtering.
+  std::unique_ptr<SpecialCaseList> NameWhitelist;
 };
 }
 
@@ -561,6 +564,12 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
       cl::desc("Show code coverage only for functions with the given name"),
       cl::ZeroOrMore, cl::cat(FilteringCategory));
 
+  cl::list<std::string> NameFilterFiles(
+      "name-whitelist", cl::Optional,
+      cl::desc("Show code coverage only for functions listed in the given "
+               "file"),
+      cl::ZeroOrMore, cl::cat(FilteringCategory));
+
   cl::list<std::string> NameRegexFilters(
       "name-regex", cl::Optional,
       cl::desc("Show code coverage only for functions that match the given "
@@ -643,11 +652,23 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
       ViewOpts.DemanglerOpts.swap(DemanglerOpts);
     }
 
+    // Read in -name-whitelist files.
+    if (!NameFilterFiles.empty()) {
+      std::string SpecialCaseListErr;
+      NameWhitelist =
+          SpecialCaseList::create(NameFilterFiles, SpecialCaseListErr);
+      if (!NameWhitelist)
+        error(SpecialCaseListErr);
+    }
+
     // Create the function filters
-    if (!NameFilters.empty() || !NameRegexFilters.empty()) {
+    if (!NameFilters.empty() || NameWhitelist || !NameRegexFilters.empty()) {
       auto NameFilterer = llvm::make_unique<CoverageFilters>();
       for (const auto &Name : NameFilters)
         NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
+      if (NameWhitelist)
+        NameFilterer->push_back(
+            llvm::make_unique<NameWhitelistCoverageFilter>(*NameWhitelist));
       for (const auto &Regex : NameRegexFilters)
         NameFilterer->push_back(
             llvm::make_unique<NameRegexCoverageFilter>(Regex));
index 325dd7235789a83e3c085a0078b05a5db2c0fb6f..606d4af1687b2248a657afee0aed16ff5ce52f58 100644 (file)
@@ -27,6 +27,11 @@ NameRegexCoverageFilter::matches(const coverage::FunctionRecord &Function) {
   return llvm::Regex(Regex).match(Function.Name);
 }
 
+bool NameWhitelistCoverageFilter::matches(
+    const coverage::FunctionRecord &Function) {
+  return Whitelist.inSection("whitelist_fun", Function.Name);
+}
+
 bool RegionCoverageFilter::matches(const coverage::FunctionRecord &Function) {
   return PassesThreshold(FunctionCoverageSummary::get(Function)
                              .RegionCoverage.getPercentCovered());
index 756c4b47872c1ac9b04c805fc0b1d05f3c7c9a94..e5c52fe877ad48b6bdca1f8d5182d58d212eaed4 100644 (file)
@@ -15,6 +15,7 @@
 #define LLVM_COV_COVERAGEFILTERS_H
 
 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
+#include "llvm/Support/SpecialCaseList.h"
 #include <memory>
 #include <vector>
 
@@ -51,6 +52,18 @@ public:
   bool matches(const coverage::FunctionRecord &Function) override;
 };
 
+/// \brief Matches functions whose name appears in a SpecialCaseList in the
+/// whitelist_fun section.
+class NameWhitelistCoverageFilter : public CoverageFilter {
+  const SpecialCaseList &Whitelist;
+
+public:
+  NameWhitelistCoverageFilter(const SpecialCaseList &Whitelist)
+      : Whitelist(Whitelist) {}
+
+  bool matches(const coverage::FunctionRecord &Function) override;
+};
+
 /// \brief Matches numbers that pass a certain threshold.
 template <typename T> class StatisticThresholdFilter {
 public: