r275913 - [Coverage] Normalize '..' out of filename strings

Vedant Kumar via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 18 14:01:27 PDT 2016


Author: vedantk
Date: Mon Jul 18 16:01:27 2016
New Revision: 275913

URL: http://llvm.org/viewvc/llvm-project?rev=275913&view=rev
Log:
[Coverage] Normalize '..' out of filename strings

This fixes the issue of having duplicate entries for the same file in a
coverage report s.t none of the entries actually displayed the correct
coverage information.

Added:
    cfe/trunk/test/CoverageMapping/abspath.cpp
Modified:
    cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp

Modified: cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp?rev=275913&r1=275912&r2=275913&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp (original)
+++ cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp Mon Jul 18 16:01:27 2016
@@ -23,6 +23,7 @@
 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
 #include "llvm/ProfileData/InstrProfReader.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
 
 using namespace clang;
 using namespace CodeGen;
@@ -920,16 +921,24 @@ struct CounterCoverageMappingBuilder
     // propagate counts into them.
   }
 };
-}
 
-static bool isMachO(const CodeGenModule &CGM) {
+bool isMachO(const CodeGenModule &CGM) {
   return CGM.getTarget().getTriple().isOSBinFormatMachO();
 }
 
-static StringRef getCoverageSection(const CodeGenModule &CGM) {
+StringRef getCoverageSection(const CodeGenModule &CGM) {
   return llvm::getInstrProfCoverageSectionName(isMachO(CGM));
 }
 
+std::string normalizeFilename(StringRef Filename) {
+  llvm::SmallString<256> Path(Filename);
+  llvm::sys::path::remove_dots(Path, /*remove_dot_dots=*/true);
+  llvm::sys::fs::make_absolute(Path);
+  return Path.str().str();
+}
+
+} // end anonymous namespace
+
 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
                  ArrayRef<CounterExpression> Expressions,
                  ArrayRef<CounterMappingRegion> Regions) {
@@ -994,7 +1003,7 @@ void CoverageMappingModuleGen::addFuncti
     llvm::SmallVector<StringRef, 16> FilenameRefs;
     FilenameRefs.resize(FileEntries.size());
     for (const auto &Entry : FileEntries)
-      FilenameRefs[Entry.second] = Entry.first->getName();
+      FilenameRefs[Entry.second] = normalizeFilename(Entry.first->getName());
     RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
                                     Expressions, Regions);
     if (Reader.read())
@@ -1015,11 +1024,8 @@ void CoverageMappingModuleGen::emit() {
   FilenameStrs.resize(FileEntries.size());
   FilenameRefs.resize(FileEntries.size());
   for (const auto &Entry : FileEntries) {
-    llvm::SmallString<256> Path(Entry.first->getName());
-    llvm::sys::fs::make_absolute(Path);
-
     auto I = Entry.second;
-    FilenameStrs[I] = std::string(Path.begin(), Path.end());
+    FilenameStrs[I] = normalizeFilename(Entry.first->getName());
     FilenameRefs[I] = FilenameStrs[I];
   }
 

Added: cfe/trunk/test/CoverageMapping/abspath.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CoverageMapping/abspath.cpp?rev=275913&view=auto
==============================================================================
--- cfe/trunk/test/CoverageMapping/abspath.cpp (added)
+++ cfe/trunk/test/CoverageMapping/abspath.cpp Mon Jul 18 16:01:27 2016
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -main-file-name abspath.cpp %S/Inputs/../abspath.cpp -o - | FileCheck %s
+
+// CHECK: @__llvm_coverage_mapping = {{.*}}"\01
+// CHECK-NOT: Inputs
+// CHECK: "
+
+void f1() {}




More information about the cfe-commits mailing list