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

Reid Kleckner via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 14:43:58 PDT 2019


rnk created this revision.
rnk added reviewers: vsk, arphaman.
Herald added a subscriber: dexonsmith.
Herald added a project: clang.

This allows users to explicitly request relative paths with
`-fdebug-compilation-dir .`.

Fixes PR43614


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D68733

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/foobar
+// RUN: cd %t
+// RUN: cp %s foobar/debug-dir.cpp
+
+// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -main-file-name debug-dir.cpp foobar/debug-dir.cpp  -o - | FileCheck -check-prefix=ABSOLUTE %s
+//
+// ABSOLUTE: @__llvm_coverage_mapping = {{.*"\\01.*clang.*test.*CoverageMapping.*.*foobar.*debug-dir\.cpp}}
+
+// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -main-file-name debug-dir.cpp foobar/debug-dir.cpp -fdebug-compilation-dir . -o - | FileCheck -check-prefix=RELATIVE %s
+//
+// RELATIVE: @__llvm_coverage_mapping = {{.*"\\01[^/]*foobar.*debug-dir\.cpp}}
+
+void f1() {}
Index: clang/lib/CodeGen/CoverageMappingGen.h
===================================================================
--- clang/lib/CodeGen/CoverageMappingGen.h
+++ clang/lib/CodeGen/CoverageMappingGen.h
@@ -54,10 +54,14 @@
   std::vector<llvm::Constant *> FunctionNames;
   llvm::StructType *FunctionRecordTy;
   std::vector<std::string> CoverageMappings;
+  SmallString<256> CWD;
+
+  /// Make the filename absolute, remove dots, and normalize slashes to local
+  /// path style.
+  std::string normalizeFilename(StringRef Filename);
 
 public:
-  CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
-      : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {}
+  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
@@ -1278,13 +1278,6 @@
       CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
 }
 
-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 Path.str().str();
-}
-
 } // end anonymous namespace
 
 static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
@@ -1317,6 +1310,24 @@
   }
 }
 
+CoverageMappingModuleGen::CoverageMappingModuleGen(
+    CodeGenModule &CGM, CoverageSourceInfo &SourceInfo)
+    : CGM(CGM), SourceInfo(SourceInfo), FunctionRecordTy(nullptr) {
+  // 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::make_absolute(CWD, Path);
+  llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
+  return Path.str().str();
+}
+
 void CoverageMappingModuleGen::addFunctionMappingRecord(
     llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
     const std::string &CoverageMapping, bool IsUsed) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68733.224172.patch
Type: text/x-patch
Size: 3467 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191009/938f6886/attachment.bin>


More information about the cfe-commits mailing list