[PATCH] D81122: Reland: Use -fdebug-compilation-dir to form absolute paths in coverage mappings

Keith Smiley via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 4 09:53:15 PDT 2020


keith updated this revision to Diff 268510.
keith added a comment.

Update relative paths to include the leading ./

This makes these remappings more analogous to lldb's source remapping, and
makes using `-path-equivalence` with `llvm-cov` more familiar.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81122/new/

https://reviews.llvm.org/D81122

Files:
  clang/lib/CodeGen/CoverageMappingGen.cpp
  clang/lib/CodeGen/CoverageMappingGen.h
  clang/test/CoverageMapping/debug-dir.cpp


Index: clang/test/CoverageMapping/debug-dir.cpp
===================================================================
--- /dev/null
+++ clang/test/CoverageMapping/debug-dir.cpp
@@ -0,0 +1,16 @@
+// %s expands to an absolute path, so to test relative paths we need to create a
+// clean directory, put the source there, and cd into it.
+// RUN: rm -rf %t
+// RUN: mkdir -p %t/foo/bar/baz
+// RUN: cp %s %t/foo/bar/baz/debug-dir.cpp
+// RUN: cd %t/foo/bar
+
+// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -mllvm -enable-name-compression=false -main-file-name debug-dir.cpp baz/debug-dir.cpp  -o - | FileCheck -check-prefix=ABSOLUTE %s
+//
+// ABSOLUTE: @__llvm_coverage_mapping = {{.*"\\01.*foo.*bar.*baz.*debug-dir\.cpp}}
+
+// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -mllvm -enable-name-compression=false -main-file-name debug-dir.cpp baz/debug-dir.cpp -fdebug-compilation-dir . -o - | FileCheck -check-prefix=RELATIVE %s
+//
+// RELATIVE: @__llvm_coverage_mapping = {{.*"\\01[^/]*\.(/|\\\\)baz.*debug-dir\.cpp}}
+
+void f1() {}
Index: clang/lib/CodeGen/CoverageMappingGen.h
===================================================================
--- clang/lib/CodeGen/CoverageMappingGen.h
+++ clang/lib/CodeGen/CoverageMappingGen.h
@@ -60,14 +60,16 @@
   llvm::SmallDenseMap<const FileEntry *, unsigned, 8> FileEntries;
   std::vector<llvm::Constant *> FunctionNames;
   std::vector<FunctionInfo> FunctionRecords;
+  SmallString<256> CWD;
+
+  std::string normalizeFilename(StringRef Filename);
 
   /// Emit a function record.
   void emitFunctionMappingRecord(const FunctionInfo &Info,
                                  uint64_t FilenamesRef);
 
 public:
-  CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
-      : CGM(CGM), SourceInfo(SourceInfo) {}
+  CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo);
 
   CoverageSourceInfo &getSourceInfo() const {
     return SourceInfo;
Index: clang/lib/CodeGen/CoverageMappingGen.cpp
===================================================================
--- clang/lib/CodeGen/CoverageMappingGen.cpp
+++ clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1279,13 +1279,6 @@
   }
 };
 
-std::string normalizeFilename(StringRef Filename) {
-  llvm::SmallString<256> Path(Filename);
-  llvm::sys::fs::make_absolute(Path);
-  llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
-  return std::string(Path);
-}
-
 } // end anonymous namespace
 
 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
@@ -1318,6 +1311,24 @@
   }
 }
 
+CoverageMappingModuleGen::CoverageMappingModuleGen(
+    CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
+    : CGM(CGM), SourceInfo(SourceInfo) {
+  // Honor -fdebug-compilation-dir in paths in coverage data. Otherwise, use the
+  // regular working directory when normalizing paths.
+  if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
+    CWD = CGM.getCodeGenOpts().DebugCompilationDir;
+  else
+    llvm::sys::fs::current_path(CWD);
+}
+
+std::string CoverageMappingModuleGen::normalizeFilename(StringRef Filename) {
+  llvm::SmallString<256> Path(Filename);
+  llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
+  llvm::sys::fs::make_absolute(CWD, Path);
+  return Path.str().str();
+}
+
 static std::string getInstrProfSection(const CodeGenModule &CGM,
                                        llvm::InstrProfSectKind SK) {
   return llvm::getInstrProfSectionName(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81122.268510.patch
Type: text/x-patch
Size: 3488 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200604/ce97915e/attachment-0001.bin>


More information about the cfe-commits mailing list