Skip to content

Commit bd67413

Browse files
authored
Merge pull request swiftlang#40039 from adrian-prantl/83753143
2 parents 52df427 + bc8bb83 commit bd67413

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,17 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
447447
createFile(StringRef FileName,
448448
Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo,
449449
Optional<StringRef> Source) {
450-
StringRef Dir;
451-
StringRef File;
452-
SmallString<128> DirBuf;
453-
SmallString<128> FileBuf;
454-
std::string RemappedFileString = DebugPrefixMap.remapPath(FileName);
455-
SmallString<128> RemappedFile = StringRef(RemappedFileString);
456-
llvm::sys::path::remove_dots(RemappedFile);
457-
std::string CurDir = DebugPrefixMap.remapPath(Opts.DebugCompilationDir);
458-
if (llvm::sys::path::is_absolute(RemappedFile)) {
450+
StringRef File, Dir;
451+
StringRef CurDir = Opts.DebugCompilationDir;
452+
SmallString<128> NormalizedFile(FileName);
453+
SmallString<128> FileBuf, DirBuf;
454+
llvm::sys::path::remove_dots(NormalizedFile);
455+
if (llvm::sys::path::is_absolute(NormalizedFile) &&
456+
llvm::sys::path::is_absolute(CurDir)) {
459457
// Strip the common prefix (if it is more than just "/") from current
460458
// directory and FileName for a more space-efficient encoding.
461-
auto FileIt = llvm::sys::path::begin(RemappedFile);
462-
auto FileE = llvm::sys::path::end(RemappedFile);
459+
auto FileIt = llvm::sys::path::begin(NormalizedFile);
460+
auto FileE = llvm::sys::path::end(NormalizedFile);
463461
auto CurDirIt = llvm::sys::path::begin(CurDir);
464462
auto CurDirE = llvm::sys::path::end(CurDir);
465463
for (; CurDirIt != CurDirE && *CurDirIt == *FileIt; ++CurDirIt, ++FileIt)
@@ -468,20 +466,22 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
468466
// Don't strip the common prefix if it is only the root "/"
469467
// since that would make LLVM diagnostic locations confusing.
470468
Dir = {};
471-
File = RemappedFile;
469+
File = NormalizedFile;
472470
} else {
473471
for (; FileIt != FileE; ++FileIt)
474472
llvm::sys::path::append(FileBuf, *FileIt);
475473
Dir = DirBuf;
476474
File = FileBuf;
477475
}
478476
} else {
479-
File = RemappedFile;
477+
File = NormalizedFile;
480478
// Leave <compiler-generated> & friends as is, without directory.
481479
if (!(File.startswith("<") && File.endswith(">")))
482480
Dir = CurDir;
483481
}
484-
llvm::DIFile *F = DBuilder.createFile(File, Dir, CSInfo, Source);
482+
llvm::DIFile *F =
483+
DBuilder.createFile(DebugPrefixMap.remapPath(File),
484+
DebugPrefixMap.remapPath(Dir), CSInfo, Source);
485485
DIFileCache[FileName].reset(F);
486486
return F;
487487
}
@@ -1880,9 +1880,13 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
18801880
StringRef SplitName = StringRef();
18811881
// Note that File + Dir need not result in a valid path.
18821882
// The directory part of the main file is the current working directory.
1883-
MainFile =
1884-
DBuilder.createFile(DebugPrefixMap.remapPath(SourcePath),
1885-
DebugPrefixMap.remapPath(Opts.DebugCompilationDir));
1883+
std::string RemappedFile = DebugPrefixMap.remapPath(SourcePath);
1884+
std::string RemappedDir = DebugPrefixMap.remapPath(Opts.DebugCompilationDir);
1885+
bool RelFile = llvm::sys::path::is_relative(RemappedFile);
1886+
bool RelDir = llvm::sys::path::is_relative(RemappedDir);
1887+
MainFile = (RelFile && RelDir)
1888+
? createFile(SourcePath, {}, {})
1889+
: DBuilder.createFile(RemappedFile, RemappedDir);
18861890

18871891
StringRef Sysroot = IGM.Context.SearchPathOpts.SDKPath;
18881892
StringRef SDK;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t
3+
// RUN: cd %t
4+
// RUN: cp %s .
5+
// RUN: %target-swiftc_driver -g -debug-prefix-map %t=. \
6+
// RUN: debug_prefix_map_abs_rel.swift \
7+
// RUN: -emit-ir -o - | %FileCheck %s
8+
9+
public func f() {}
10+
11+
// CHECK-NOT: debug_prefix_map_abs_rel.swift
12+
// CHECK: !DIFile(filename: "debug_prefix_map_abs_rel.swift", directory: ".")
13+
// CHECK-NOT: debug_prefix_map_abs_rel.swift
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t/SubdirectoryWithAVeryUniqueName
3+
// RUN: cd %t/SubdirectoryWithAVeryUniqueName
4+
// RUN: cp %s .
5+
// RUN: %target-swiftc_driver -g -debug-prefix-map %t=. \
6+
// RUN: %t/SubdirectoryWithAVeryUniqueName/debug_prefix_map_abs_rel_subdir.swift \
7+
// RUN: -emit-ir -o - | %FileCheck %s
8+
9+
public func f() {}
10+
11+
// CHECK-NOT: debug_prefix_map_abs_rel_subdir.swift
12+
// CHECK: !DIFile(filename: "debug_prefix_map_abs_rel_subdir.swift", directory: ".{{/|\\\\}}SubdirectoryWithAVeryUniqueName")
13+
// CHECK-NOT: debug_prefix_map_abs_rel_subdir.swift

0 commit comments

Comments
 (0)