[PATCH] D20803: Displaying coverage information for all source files present in a directory

Chakshu Grover via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 3 06:29:15 PDT 2016


chakshugrover updated this revision to Diff 59540.
chakshugrover added a comment.

I have tried to incorporate all the comments. 
The code is still not what i would like it to be!

I started with this

void CodeCoverageTool::collectSourceFiles(StringRef Path) {

  llvm::sys::fs::file_status Status;
  llvm::sys::fs::status(Path, Status);
  if (llvm::sys::fs::exists(Status)) {
    // If it's a source file, collect it.
    if (llvm::sys::fs::is_regular_file(Status)) {
      SourceFiles.push_back(Path.str());
      return;
    }
    if (llvm::sys::fs::is_directory(Status)) {
      std::error_code EC;
      // Collect all source files within this directory by iterating
      // recursively.
      for (llvm::sys::fs::recursive_directory_iterator File(Path, EC), FileEnd;
           File != FileEnd && !EC; File.increment(EC)) {
        if (llvm::sys::fs::is_regular_file(File->path())) {
          SourceFiles.push_back(File->path());
        }
      }
      return;
    }
  } else
    fprintf(stderr, "Error: Can't access: %s\n", Path.str().c_str());
  return;

}

But the code above breaks the test case test/tools/llvm-cov/report.cpp. Please suggest how to go about it.
The script that it emitted works without any error if run from test/tools/llvm-cov/ directory but the test fails.


http://reviews.llvm.org/D20803

Files:
  test/tools/llvm-cov/Inputs/cov_dir.profdata
  test/tools/llvm-cov/Inputs/cov_dir/main.cpp
  test/tools/llvm-cov/Inputs/cov_dir/sub_dir/function.cpp
  test/tools/llvm-cov/Inputs/cov_dir_test
  test/tools/llvm-cov/cov_dir.cpp
  tools/llvm-cov/CodeCoverage.cpp

Index: tools/llvm-cov/CodeCoverage.cpp
===================================================================
--- tools/llvm-cov/CodeCoverage.cpp
+++ tools/llvm-cov/CodeCoverage.cpp
@@ -70,6 +70,9 @@
   /// \brief Load the coverage mapping data. Return true if an error occured.
   std::unique_ptr<CoverageMapping> load();
 
+  /// \brief Collects all source files recursively inside the directory
+  void collectSourceFiles(StringRef Path);
+
   int run(Command Cmd, int argc, const char **argv);
 
   typedef std::function<int(int, const char **)> CommandLineParserType;
@@ -239,6 +242,23 @@
   return Coverage;
 }
 
+void CodeCoverageTool::collectSourceFiles(StringRef Path) {
+  if (llvm::sys::fs::is_directory(Path)) {
+    std::error_code EC;
+    // Collect all source files within this directory by iterating
+    // recursively.
+    for (llvm::sys::fs::recursive_directory_iterator File(Path, EC), FileEnd;
+         File != FileEnd && !EC; File.increment(EC)) {
+      if (llvm::sys::fs::is_regular_file(File->path())) {
+        SourceFiles.push_back(File->path());
+      }
+    }
+    return;
+  }
+  SourceFiles.push_back(Path.str());
+  return;
+}
+
 int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
   // Print a stack trace if we signal out.
   sys::PrintStackTraceOnErrorSignal();
@@ -362,7 +382,7 @@
           errs() << "error: " << File << ": " << EC.message();
           return 1;
         }
-      SourceFiles.push_back(Path.str());
+      collectSourceFiles(Path.str());
     }
     return 0;
   };
Index: test/tools/llvm-cov/cov_dir.cpp
===================================================================
--- test/tools/llvm-cov/cov_dir.cpp
+++ test/tools/llvm-cov/cov_dir.cpp
@@ -0,0 +1,13 @@
+// RUN: llvm-cov report %S/Inputs/cov_dir_test -instr-profile %S/Inputs/cov_dir.profdata %S/Inputs/cov_dir/ 2>&1 | FileCheck %s
+
+// CHECK:      Name                        Regions    Miss   Cover     Lines    Miss   Cover
+// CHECK-NEXT: ---
+// CHECK-NEXT: main                              2       1  50.00%         8       2  75.00%
+// CHECK-NEXT: ---
+// CHECK-NEXT: TOTAL                             2       1  50.00%         8       2  75.00%
+
+// CHECK:      Name                        Regions    Miss   Cover     Lines    Miss   Cover
+// CHECK-NEXT: ---
+// CHECK-NEXT: _Z1fi                             2       1  50.00%         5       2  60.00%
+// CHECK-NEXT: ---
+// CHECK-NEXT: TOTAL                             2       1  50.00%         5       2  60.00%
Index: test/tools/llvm-cov/Inputs/cov_dir/sub_dir/function.cpp
===================================================================
--- test/tools/llvm-cov/Inputs/cov_dir/sub_dir/function.cpp
+++ test/tools/llvm-cov/Inputs/cov_dir/sub_dir/function.cpp
@@ -0,0 +1,6 @@
+int f(int g)
+{
+	return g+2;
+	g+=4;
+	return g;
+}
Index: test/tools/llvm-cov/Inputs/cov_dir/main.cpp
===================================================================
--- test/tools/llvm-cov/Inputs/cov_dir/main.cpp
+++ test/tools/llvm-cov/Inputs/cov_dir/main.cpp
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+extern int f(int g);
+int main(int argc, char **argv)
+{
+	int result = 0;
+	int a; scanf("%d",&a);
+	result=f(a);
+	return result;
+	result=10;
+	return 0;
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20803.59540.patch
Type: text/x-patch
Size: 3252 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160603/9580294d/attachment.bin>


More information about the llvm-commits mailing list