Skip to content

Commit 70140a2

Browse files
Merge pull request #22392 from adrian-prantl/DIFiles
Bring the Swift frontend's handling of DIFiles in synch with CFE.
2 parents 3ace09d + 21f2c3d commit 70140a2

11 files changed

+86
-45
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -371,28 +371,66 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
371371
}
372372

373373
// Detect the main file.
374-
if (MainFile && Filename.endswith(MainFile->getFilename())) {
374+
StringRef MainFileName = MainFile->getFilename();
375+
if (MainFile && Filename.endswith(MainFileName)) {
375376
SmallString<256> AbsThisFile, AbsMainFile;
376377
AbsThisFile = Filename;
377378
llvm::sys::fs::make_absolute(AbsThisFile);
378-
llvm::sys::path::append(AbsMainFile, MainFile->getDirectory(),
379-
MainFile->getFilename());
380-
if (AbsThisFile == AbsMainFile) {
379+
if (llvm::sys::path::is_absolute(MainFileName))
380+
AbsMainFile = MainFileName;
381+
else
382+
llvm::sys::path::append(AbsMainFile, MainFile->getDirectory(),
383+
MainFileName);
384+
if (AbsThisFile == DebugPrefixMap.remapPath(AbsMainFile)) {
381385
DIFileCache[Filename] = llvm::TrackingMDNodeRef(MainFile);
382386
return MainFile;
383387
}
384388
}
385389

386-
// Create a new one.
387-
StringRef File = llvm::sys::path::filename(Filename);
388-
llvm::SmallString<512> Path(Filename);
389-
llvm::sys::path::remove_filename(Path);
390-
llvm::sys::path::remove_dots(Path);
391-
llvm::DIFile *F = DBuilder.createFile(DebugPrefixMap.remapPath(File),
392-
DebugPrefixMap.remapPath(Path));
393-
394-
// Cache it.
395-
DIFileCache[Filename] = llvm::TrackingMDNodeRef(F);
390+
return createFile(Filename, None, None);
391+
}
392+
393+
/// This is effectively \p clang::CGDebugInfo::createFile().
394+
llvm::DIFile *
395+
createFile(StringRef FileName,
396+
Optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo,
397+
Optional<StringRef> Source) {
398+
StringRef Dir;
399+
StringRef File;
400+
SmallString<128> DirBuf;
401+
SmallString<128> FileBuf;
402+
std::string RemappedFileString = DebugPrefixMap.remapPath(FileName);
403+
SmallString<128> RemappedFile = StringRef(RemappedFileString);
404+
llvm::sys::path::remove_dots(RemappedFile);
405+
std::string CurDir = DebugPrefixMap.remapPath(Opts.DebugCompilationDir);
406+
if (llvm::sys::path::is_absolute(RemappedFile)) {
407+
// Strip the common prefix (if it is more than just "/") from current
408+
// directory and FileName for a more space-efficient encoding.
409+
auto FileIt = llvm::sys::path::begin(RemappedFile);
410+
auto FileE = llvm::sys::path::end(RemappedFile);
411+
auto CurDirIt = llvm::sys::path::begin(CurDir);
412+
auto CurDirE = llvm::sys::path::end(CurDir);
413+
for (; CurDirIt != CurDirE && *CurDirIt == *FileIt; ++CurDirIt, ++FileIt)
414+
llvm::sys::path::append(DirBuf, *CurDirIt);
415+
if (std::distance(llvm::sys::path::begin(CurDir), CurDirIt) == 1) {
416+
// Don't strip the common prefix if it is only the root "/"
417+
// since that would make LLVM diagnostic locations confusing.
418+
Dir = {};
419+
File = RemappedFile;
420+
} else {
421+
for (; FileIt != FileE; ++FileIt)
422+
llvm::sys::path::append(FileBuf, *FileIt);
423+
Dir = DirBuf;
424+
File = FileBuf;
425+
}
426+
} else {
427+
File = RemappedFile;
428+
// Leave <compiler-generated> & friends as is, without directory.
429+
if (!(File.startswith("<") && File.endswith(">")))
430+
Dir = CurDir;
431+
}
432+
llvm::DIFile *F = DBuilder.createFile(File, Dir, CSInfo, Source);
433+
DIFileCache[FileName.data()].reset(F);
396434
return F;
397435
}
398436

@@ -1620,16 +1658,17 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
16201658
// No split DWARF on Darwin.
16211659
StringRef SplitName = StringRef();
16221660
// Note that File + Dir need not result in a valid path.
1623-
// Clang is doing the same thing here.
1661+
// The directory part of the main file is the current working directory.
1662+
MainFile =
1663+
DBuilder.createFile(DebugPrefixMap.remapPath(SourcePath),
1664+
DebugPrefixMap.remapPath(Opts.DebugCompilationDir));
1665+
16241666
TheCU = DBuilder.createCompileUnit(
1625-
Lang, DBuilder.createFile(
1626-
DebugPrefixMap.remapPath(SourcePath),
1627-
DebugPrefixMap.remapPath(Opts.DebugCompilationDir)),
1667+
Lang, MainFile,
16281668
Producer, Opts.shouldOptimize(), Flags, MajorRuntimeVersion, SplitName,
16291669
Opts.DebugInfoLevel > IRGenDebugInfoLevel::LineTables
16301670
? llvm::DICompileUnit::FullDebug
16311671
: llvm::DICompileUnit::LineTablesOnly);
1632-
MainFile = getOrCreateFile(SourcePath);
16331672

16341673
// Because the swift compiler relies on Clang to setup the Module,
16351674
// the clang CU is always created first. Several dwarf-reading

test/DebugInfo/ClangPathDots.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
// RUN: %target-swift-frontend -emit-ir %s -g -I %S/Inputs -o - \
66
// RUN: -module-cache-path %t.cache | %FileCheck %s --check-prefix=CACHED
77

8-
// FIRST: !DIFile(filename: "NSObject.h", directory: {{.*}}/include/objc")
9-
// CACHED: !DIFile(filename: "NSObject.h", directory: {{.*}}/include/objc")
8+
// Test that the paths have no extra "./" components on rebuild.
9+
10+
// FIRST: !DIFile(filename: "{{.*}}/include/objc/NSObject.h",
11+
// CACHED: !DIFile(filename: "{{.*}}/include/objc/NSObject.h",
1012

1113
import ObjectiveC
1214

test/DebugInfo/Imports.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
// CHECK-DAG: ![[FOOMODULE:[0-9]+]] = !DIModule({{.*}}, name: "Foo", includePath: "{{.*}}test{{.*}}DebugInfo{{.*}}"
99
// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_module, scope: ![[THISFILE:[0-9]+]], entity: ![[FOOMODULE]]
10-
// CHECK-DAG: ![[THISFILE]] = !DIFile(filename: "Imports.swift", directory: "{{.*}}test{{/|\\5C}}DebugInfo")
11-
// CHECK-DAG: ![[SWIFTFILE:[0-9]+]] = !DIFile(filename: "Swift.swiftmodule"
10+
// CHECK-DAG: ![[THISFILE]] = !DIFile(filename: "{{.*}}test{{/|\\5C}}DebugInfo{{/|\\5C}}Imports.swift",
11+
// CHECK-DAG: ![[SWIFTFILE:[0-9]+]] = !DIFile(filename: "{{.*}}Swift.swiftmodule"
1212
// CHECK-DAG: ![[SWIFTMODULE:[0-9]+]] = !DIModule({{.*}}, name: "Swift"
1313
// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_module, scope: ![[THISFILE]], entity: ![[SWIFTMODULE]]
1414
// CHECK-DAG: ![[BASICMODULE:[0-9]+]] = !DIModule({{.*}}, name: "basic"

test/DebugInfo/ImportsStdlib.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
// CHECK-DAG: ![[MODULE:[0-9]+]] = !DIModule({{.*}}, name: "NotTheStdlib", includePath: "{{.*}}test{{.*}}DebugInfo{{.*}}"
1313
// CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_module, scope: ![[THISFILE:[0-9]+]], entity: ![[MODULE]]
14-
// CHECK-DAG: ![[THISFILE]] = !DIFile(filename: "ImportsStdlib.swift", directory: "{{.*}}test{{/|\\5C}}DebugInfo")
14+
// CHECK-DAG: ![[THISFILE]] = !DIFile(filename: "{{.*}}test{{/|\\5C}}DebugInfo{{/|\\5C}}ImportsStdlib.swift"
1515

16-
// NEGATIVE-NOT: !DIFile(filename: "Swift.swiftmodule"
16+
// NEGATIVE-NOT: !DIFile(filename: "{{.*}}Swift.swiftmodule"
1717
// NEGATIVE-NOT: !DIModule({{.*}}, name: "Swift"
1818

1919
// DWARF: .debug_info

test/DebugInfo/basic.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// Verify that we don't emit any debug info by default.
44
// RUN: %target-swift-frontend %s -emit-ir -o - \
55
// RUN: | %FileCheck %s --check-prefix NDEBUG
6+
// NDEBUG: source_filename
67
// NDEBUG-NOT: !dbg
7-
// NDEBUG-NOT: DW_TAG
8+
// NDEBUG-NOT: DICompileUnit
89
// --------------------------------------------------------------------
910
// Verify that we don't emit any debug info with -gnone.
1011
// RUN: %target-swift-frontend %s -emit-ir -gnone -o - \
@@ -14,9 +15,9 @@
1415
// RUN: %target-swift-frontend %s -emit-ir -gline-tables-only -o - \
1516
// RUN: | %FileCheck %s --check-prefix CHECK-LINETABLES
1617
// CHECK: !dbg
17-
// CHECK-LINETABLES-NOT: DW_TAG_{{.*}}variable
18+
// CHECK-LINETABLES-NOT: DI{{.*}}Variable
1819
// CHECK-LINETABLES-NOT: DW_TAG_structure_type
19-
// CHECK-LINETABLES-NOT: DW_TAG_basic_type
20+
// CHECK-LINETABLES-NOT: DIBasicType
2021
// --------------------------------------------------------------------
2122
// Now check that we do generate line+scope info with -g.
2223
// RUN: %target-swift-frontend %/s -emit-ir -g -o - \
@@ -71,9 +72,8 @@ func foo(_ a: Int64, _ b: Int64) -> Int64 {
7172
}
7273
}
7374

74-
// CHECK-DAG: ![[FILE_CWD:[0-9]+]] = !DIFile(filename: "{{.*}}DebugInfo/basic.swift", directory: "{{.*}}")
75-
// CHECK-DAG: ![[MAINFILE:[0-9]+]] = !DIFile(filename: "basic.swift", directory: "{{.*}}DebugInfo")
76-
// CHECK-DAG: !DICompileUnit(language: DW_LANG_Swift, file: ![[FILE_CWD]],{{.*}} producer: "{{.*}}Swift version{{.*}},{{.*}}
75+
// CHECK-DAG: ![[MAINFILE:[0-9]+]] = !DIFile(filename: "{{.*}}DebugInfo/basic.swift", directory: "{{.*}}")
76+
// CHECK-DAG: !DICompileUnit(language: DW_LANG_Swift, file: ![[MAINFILE]],{{.*}} producer: "{{.*}}Swift version{{.*}},{{.*}}
7777
// CHECK-DAG: !DISubprogram(name: "main", {{.*}}file: ![[MAINFILE]],
7878

7979
// Function type for foo.

test/DebugInfo/gsil.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: %FileCheck %s < %t/out.ir
44
// RUN: %FileCheck %s --check-prefix=CHECK_OUT_SIL < %t/out.ir.gsil_0.sil
55

6-
// CHECK: [[F:![0-9]+]] = !DIFile(filename: "out.ir.gsil_0.sil", directory: "{{.+}}")
6+
// CHECK: [[F:![0-9]+]] = !DIFile(filename: "{{.+}}out.ir.gsil_0.sil",
77
// CHECK: !DISubprogram(linkageName: "$s3out6testityyF", scope: !{{[0-9]+}}, file: [[F]], line: {{[1-9][0-9]+}},
88

99
// CHECK_OUT_SIL: sil @$s3out6testityyF : $@convention(thin) () -> () {

test/DebugInfo/inlinescopes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// CHECK: define{{( dllexport)?}}{{( protected)?( signext)?}} i32 @main
99
// CHECK: call {{.*}}noinline{{.*}}, !dbg ![[CALL:.*]]
10-
// CHECK-DAG: ![[TOPLEVEL:.*]] = !DIFile(filename: "inlinescopes.swift"
10+
// CHECK-DAG: ![[TOPLEVEL:.*]] = !DIFile(filename: "{{.*}}inlinescopes.swift"
1111

1212
import FooBar
1313

test/DebugInfo/line-directive.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ func f() {
88
markUsed("Test")
99
#sourceLocation(file: "abc.swift", line: 142)
1010
markUsed("abc again")
11-
#sourceLocation(file: "def.swift", line: 142)
11+
#sourceLocation(file: "/absolute/path/def.swift", line: 142)
1212
markUsed("jump directly to def")
1313
}
1414

1515
// RUN: %target-swift-frontend -primary-file %/s -S -g -o - | %FileCheck %s
1616
// CHECK: .file [[MAIN:.*]] "{{.*}}line-directive.swift"
1717
// CHECK: .loc [[MAIN]] 1
18-
// CHECK: .file [[ABC:.*]] "abc.swift"
18+
// CHECK: .file [[ABC:.*]] "{{.*}}abc.swift"
1919
// CHECK: .loc [[ABC]] 42
2020
// CHECK: .loc [[MAIN]] 8
2121
// CHECK: .loc [[ABC]] 142
22-
// CHECK: .file [[DEF:.*]] "def.swift"
22+
// CHECK: .file [[DEF:.*]] "/absolute/path/def.swift"
2323
// CHECK: .loc [[DEF]] 142
2424
// CHECK: .asciz "{{.*}}test/DebugInfo"
2525

@@ -28,10 +28,10 @@ func f() {
2828
// RUN: %target-swift-frontend -vfsoverlay %t/overlay.yaml -primary-file %/S/vfs-relocated-line-directive.swift -S -g -o - | %FileCheck -check-prefix=VFS %s
2929
// VFS: .file [[MAIN:.*]] "{{.*}}vfs-relocated-line-directive.swift"
3030
// VFS: .loc [[MAIN]] 1
31-
// VFS: .file [[ABC:.*]] "abc.swift"
31+
// VFS: .file [[ABC:.*]] "{{.*}}abc.swift"
3232
// VFS: .loc [[ABC]] 42
3333
// VFS: .loc [[MAIN]] 8
3434
// VFS: .loc [[ABC]] 142
35-
// VFS: .file [[DEF:.*]] "def.swift"
35+
// VFS: .file [[DEF:.*]] "/absolute/path/def.swift"
3636
// VFS: .loc [[DEF]] 142
3737
// VFS: .asciz "{{.*}}test/DebugInfo"

test/DebugInfo/test-foundation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MyObject : NSObject {
1717
// LOC-CHECK: ret {{.*}}, !dbg ![[DBG:.*]]
1818
// LOC-CHECK: ret
1919
@objc var MyArr = NSArray()
20-
// IMPORT-CHECK: filename: "test-foundation.swift"
20+
// IMPORT-CHECK: filename: "{{.*}}test-foundation.swift"
2121
// IMPORT-CHECK-DAG: [[FOUNDATION:[0-9]+]] = !DIModule({{.*}} name: "Foundation",{{.*}} includePath: {{.*}}Foundation.framework
2222
// IMPORT-CHECK-DAG: [[OVERLAY:[0-9]+]] = !DIModule({{.*}} name: "Foundation",{{.*}} includePath: {{.*}}Foundation.swiftmodule
2323
// IMPORT-CHECK-DAG: !DICompositeType(tag: DW_TAG_structure_type, name: "NSArray", scope: ![[NSARRAY:[0-9]+]]

test/DebugInfo/variables.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ print(", \(glob_b)", terminator: "")
4141
print(", \(glob_s)", terminator: "")
4242
var unused: Int32 = -1
4343

44-
// CHECK-DAG: ![[RT:[0-9]+]] ={{.*}}"Swift.swiftmodule"
44+
// CHECK-DAG: ![[RT:[0-9]+]] ={{.*}}"{{.*}}Swift.swiftmodule"
4545

4646

4747
// Stack variables.
@@ -114,4 +114,4 @@ func myprint(_ value: TriValue) {
114114
}
115115
myprint(unknown)
116116

117-
// CHECK-DAG: !DIFile(filename: "variables.swift"
117+
// CHECK-DAG: !DIFile(filename: "{{.*}}variables.swift"

test/IRGen/multithread_module.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func callproto(_ p: MyProto) {
6262

6363
// Check if the DI filename is correct and not "<unknown>".
6464

65-
// CHECK-MAINLL: DICompileUnit(language: DW_LANG_Swift, file: [[F:![0-9]+]]
66-
// CHECK-MAINLL: [[F]] = !DIFile(filename: "{{.*}}IRGen/Inputs/multithread_module/main.swift", directory: "{{.*}}")
65+
// CHECK-MAINLL: [[F:![0-9]+]] = !DIFile(filename: "{{.*}}IRGen/Inputs/multithread_module/main.swift", directory: "{{.*}}")
66+
// CHECK-MAINLL: DICompileUnit(language: DW_LANG_Swift, file: [[F]],
6767

68-
// CHECK-MODULELL: DICompileUnit(language: DW_LANG_Swift, file: [[F:![0-9]+]]
69-
// CHECK-MODULELL: [[F]] = !DIFile(filename: "{{.*}}IRGen/multithread_module.swift", directory: "{{.*}}")
68+
// CHECK-MODULELL: [[F:![0-9]]] = !DIFile(filename: "{{.*}}IRGen/multithread_module.swift", directory: "{{.*}}")
69+
// CHECK-MODULELL: DICompileUnit(language: DW_LANG_Swift, file: [[F]],

0 commit comments

Comments
 (0)