]> granicus.if.org Git - clang/commitdiff
Re-land "Use -fdebug-compilation-dir to form absolute paths in coverage mappings"
authorReid Kleckner <rnk@google.com>
Thu, 10 Oct 2019 18:01:20 +0000 (18:01 +0000)
committerReid Kleckner <rnk@google.com>
Thu, 10 Oct 2019 18:01:20 +0000 (18:01 +0000)
This reverts r374324 (git commit 62808631acceaa8b78f8ab9b407eb6b943ff5f77)

I changed the test to not rely on finding the sequence "clang, test,
CoverageMapping" in the CWD used to run the test. Instead it makes its
own internal directory hierarchy of foo/bar/baz and looks for that.

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

lib/CodeGen/CoverageMappingGen.cpp
lib/CodeGen/CoverageMappingGen.h
test/CoverageMapping/debug-dir.cpp [new file with mode: 0644]

index 0a7a4fe33ac2d2b45ff41085abb3678dfbef9429..a6f6e38d5f14839588320d37c78f15a65c691611 100644 (file)
@@ -1278,13 +1278,6 @@ std::string getCoverageSection(const CodeGenModule &CGM) {
       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 @@ static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
   }
 }
 
+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::fs::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) {
index 3bf51f590479ff2937d602a3e8a0bd69f165014c..2bdc00e256689f997ba0945e30941282af5ddb6b 100644 (file)
@@ -54,10 +54,14 @@ class CoverageMappingModuleGen {
   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;
diff --git a/test/CoverageMapping/debug-dir.cpp b/test/CoverageMapping/debug-dir.cpp
new file mode 100644 (file)
index 0000000..657c9f2
--- /dev/null
@@ -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 -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 -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() {}