[PATCH] D20803: Displaying coverage information for all source files present in a directory
Justin Bogner via llvm-commits
llvm-commits at lists.llvm.org
Tue May 31 11:55:08 PDT 2016
Chakshu Grover <chakshu.gro at samsung.com> writes:
> chakshugrover created this revision.
> chakshugrover added reviewers: davidxl, bogner, vsk.
> chakshugrover added a subscriber: llvm-commits.
>
> The llvm-cov tool currently reads code coverage data files and
> displays the coverage information for **a specified source
> file**. This patch will enable us to provide it with a **directory**
> and it will display the information for all the source files inside
> that directory by iterating over all of them using directory_iterator.
> It still works if you provide a single source file.
>
> http://reviews.llvm.org/D20803
>
> Files:
> tools/llvm-cov/CodeCoverage.cpp
>
> Index: tools/llvm-cov/CodeCoverage.cpp
> ===================================================================
> --- tools/llvm-cov/CodeCoverage.cpp
> +++ tools/llvm-cov/CodeCoverage.cpp
> @@ -32,7 +32,10 @@
> #include "llvm/Support/Process.h"
> #include "llvm/Support/Signals.h"
> #include <functional>
> +#include <string>
> #include <system_error>
> +#include <sys/types.h>
> +#include <sys/stat.h>
>
> using namespace llvm;
> using namespace coverage;
> @@ -70,6 +73,8 @@
> /// \brief Load the coverage mapping data. Return true if an error occured.
> std::unique_ptr<CoverageMapping> load();
>
> + void walk(SmallString<128> Path);
> +
> int run(Command Cmd, int argc, const char **argv);
>
> typedef std::function<int(int, const char **)> CommandLineParserType;
> @@ -239,6 +244,30 @@
> return Coverage;
> }
>
> +void CodeCoverageTool::walk(SmallString<128> Path) {
> + struct stat info;
> + if (stat(std::string(Path.str()).c_str(), &info) != 0)
> + errs() << "cannot access " << Path << "\n";
You can avoid some indentation with early returns here. Something like:
if (stat(...)) {
// error stuff
return;
}
if (!S_IFDIR) {
// single file stuff
return;
}
// directory stuff
> + else if (info.st_mode & S_IFDIR) {
> + std::error_code EC;
> + struct stat StatBuf;
> + // Walk all of the files within this directory.
> + for (llvm::sys::fs::directory_iterator File(Path, EC), FileEnd;
> + File != FileEnd && !EC; File.increment(EC)) {
> + if (stat(File->path().c_str(), &StatBuf))
> + continue;
> + // If we have a directory, look inside.
> + if (llvm::sys::fs::is_directory(File->path())) {
> + walk((SmallString<128>)File->path());
> + continue;
> + }
> + SourceFiles.push_back(File->path().c_str());
> + }
> + } else
> + 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 +391,7 @@
> errs() << "error: " << File << ": " << EC.message();
> return 1;
> }
> - SourceFiles.push_back(Path.str());
> + walk(Path);
> }
> return 0;
> };
>
>
More information about the llvm-commits
mailing list