llvm-cov: Support gcov's extermely lenient treatment of -o
authorJustin Bogner <mail@justinbogner.com>
Tue, 18 Feb 2014 09:19:48 +0000 (09:19 +0000)
committerJustin Bogner <mail@justinbogner.com>
Tue, 18 Feb 2014 09:19:48 +0000 (09:19 +0000)
In gcov, the -o flag can accept either a directory or a file name.
When given a directory, the gcda and gcno files are expected to be in
that directory. When given a file, the gcda and gcno files are
expected to be named based on the stem of that file. Non-existent
paths are treated as files.

This implements compatible behaviour.

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

test/tools/llvm-cov/llvm-cov.test
tools/llvm-cov/llvm-cov.cpp

index c52a9815229d0ed5c2ada2fd60ae2cccb2ce03e5..afd657be2494eb66615513425589f6bb122a1aa3 100644 (file)
@@ -21,6 +21,16 @@ RUN: llvm-cov -o objdir test.c | diff -u test_no_options.output -
 RUN: diff -aub test_objdir.cpp.gcov test.cpp.gcov
 RUN: diff -aub test_objdir.h.gcov test.h.gcov
 
+# Specifying an object file
+RUN: llvm-cov -o objdir/test.o test.c | diff -u test_no_options.output -
+RUN: diff -aub test_objdir.cpp.gcov test.cpp.gcov
+RUN: diff -aub test_objdir.h.gcov test.h.gcov
+
+# Specifying an object file that could be ambiguous with a directory
+RUN: llvm-cov -o objdir/test test.c | diff -u test_no_options.output -
+RUN: diff -aub test_objdir.cpp.gcov test.cpp.gcov
+RUN: diff -aub test_objdir.h.gcov test.h.gcov
+
 # Preserve paths. This mangles the output filenames.
 RUN: mkdir -p %t/srcdir/nested_dir
 RUN: cp test.cpp test.h %t/srcdir
index d7162c46882d9bec025244e6b79bb6286b1a02fd..ce59694ad29bb736afa7a58b30bbed49f64036f2 100644 (file)
@@ -14,6 +14,7 @@
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/GCOV.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryObject.h"
@@ -43,9 +44,11 @@ static cl::opt<bool> FuncSummary("f", cl::init(false),
                                  cl::desc("Show coverage for each function"));
 static cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
 
-static cl::opt<std::string> ObjectDir("o", cl::value_desc("DIR"), cl::init(""),
-                                      cl::desc("Search for objects in DIR"));
+static cl::opt<std::string>
+ObjectDir("o", cl::value_desc("DIR|FILE"), cl::init(""),
+          cl::desc("Find objects in DIR or based on FILE's path"));
 static cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
+static cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
 
 static cl::opt<bool> PreservePaths("p", cl::init(false),
                                    cl::desc("Preserve path components"));
@@ -75,9 +78,16 @@ int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
 
   SmallString<128> CoverageFileStem(ObjectDir);
-  if (CoverageFileStem.empty())
+  if (CoverageFileStem.empty()) {
+    // If no directory was specified with -o, look next to the source file.
     CoverageFileStem = sys::path::parent_path(SourceFile);
-  sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
+    sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
+  } else if (sys::fs::is_directory(ObjectDir))
+    // A directory name was given. Use it and the source file name.
+    sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
+  else
+    // A file was given. Ignore the source file and look next to this file.
+    sys::path::replace_extension(CoverageFileStem, "");
 
   if (InputGCNO.empty())
     InputGCNO = (CoverageFileStem.str() + ".gcno").str();