obj2yaml: Use the correct relocation type for different machine types
[oota-llvm.git] / lib / IR / GCOV.cpp
index 466828113b8f7861494bae1d1536830606d11971..f69bdc4f8cc90308772de43188c4a35764d19825 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Support/Debug.h"
-#include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/GCOV.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Format.h"
-#include "llvm/Support/GCOV.h"
 #include "llvm/Support/MemoryObject.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/system_error.h"
 #include <algorithm>
 using namespace llvm;
@@ -307,6 +308,11 @@ void GCOVFunction::dump() const {
 /// collectLineCounts - Collect line counts. This must be used after
 /// reading .gcno and .gcda files.
 void GCOVFunction::collectLineCounts(FileInfo &FI) {
+  // If the line number is zero, this is a function that doesn't actually appear
+  // in the source file, so there isn't anything we can do with it.
+  if (LineNumber == 0)
+    return;
+
   for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
          E = Blocks.end(); I != E; ++I)
     (*I)->collectLineCounts(FI);
@@ -429,21 +435,58 @@ static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
   return OS;
 }
 
+/// Convert a path to a gcov filename. If PreservePaths is true, this
+/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
+static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
+  if (!PreservePaths)
+    return (sys::path::filename(Filename) + ".gcov").str();
+
+  // This behaviour is defined by gcov in terms of text replacements, so it's
+  // not likely to do anything useful on filesystems with different textual
+  // conventions.
+  llvm::SmallString<256> Result("");
+  StringRef::iterator I, S, E;
+  for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
+    if (*I != '/')
+      continue;
+
+    if (I - S == 1 && *S == '.') {
+      // ".", the current directory, is skipped.
+    } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
+      // "..", the parent directory, is replaced with "^".
+      Result.append("^#");
+    } else {
+      if (S < I)
+        // Leave other components intact,
+        Result.append(S, I);
+      // And separate with "#".
+      Result.push_back('#');
+    }
+    S = I + 1;
+  }
+
+  if (S < I)
+    Result.append(S, I);
+  Result.append(".gcov");
+  return Result.str();
+}
+
 /// print -  Print source files with collected line count information.
 void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) {
   for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
          E = LineInfo.end(); I != E; ++I) {
     StringRef Filename = I->first();
-    OwningPtr<MemoryBuffer> Buff;
+    std::unique_ptr<MemoryBuffer> Buff;
     if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
       errs() << Filename << ": " << ec.message() << "\n";
       return;
     }
     StringRef AllLines = Buff->getBuffer();
 
-    std::string CovFilename = Filename.str() + ".gcov";
+    std::string CoveragePath = mangleCoveragePath(Filename,
+                                                  Options.PreservePaths);
     std::string ErrorInfo;
-    raw_fd_ostream OS(CovFilename.c_str(), ErrorInfo);
+    raw_fd_ostream OS(CoveragePath.c_str(), ErrorInfo, sys::fs::F_Text);
     if (!ErrorInfo.empty())
       errs() << ErrorInfo << "\n";
 
@@ -555,7 +598,7 @@ void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) {
         }
       }
     }
-    FileCoverages.push_back(FileCoverage);
+    FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
   }
 
   // FIXME: There is no way to detect calls given current instrumentation.
@@ -656,8 +699,8 @@ void FileInfo::printCoverage(const GCOVCoverage &Coverage) const {
 
 // printFuncCoverage - Print per-function coverage info.
 void FileInfo::printFuncCoverage() const {
-  for (MapVector<const GCOVFunction *, GCOVCoverage>::const_iterator I =
-         FuncCoverages.begin(), E = FuncCoverages.end(); I != E; ++I) {
+  for (FuncCoverageMap::const_iterator I = FuncCoverages.begin(),
+                                       E = FuncCoverages.end(); I != E; ++I) {
     const GCOVCoverage &Coverage = I->second;
     outs() << "Function '" << Coverage.Name << "'\n";
     printCoverage(Coverage);
@@ -667,12 +710,12 @@ void FileInfo::printFuncCoverage() const {
 
 // printFileCoverage - Print per-file coverage info.
 void FileInfo::printFileCoverage() const {
-  for (SmallVectorImpl<GCOVCoverage>::const_iterator I =
-         FileCoverages.begin(), E = FileCoverages.end(); I != E; ++I) {
-    const GCOVCoverage &Coverage = *I;
+  for (FileCoverageList::const_iterator I = FileCoverages.begin(),
+                                        E = FileCoverages.end(); I != E; ++I) {
+    const std::string &Filename = I->first;
+    const GCOVCoverage &Coverage = I->second;
     outs() << "File '" << Coverage.Name << "'\n";
     printCoverage(Coverage);
-    outs() << Coverage.Name << ":creating '" << Coverage.Name
-           << ".gcov'\n\n";
+    outs() << Coverage.Name << ":creating '" << Filename << "'\n\n";
   }
 }