Skip to content

Commit 17c0e56

Browse files
committed
Add hidden --dump-source-file-imports flag
This flag dumps all imports for each SourceFile after it's gone through import resolution. It is only intended for testing purposes. There are other ways to print imports, but they don't correspond 1:1 to the imports actually resolved, which is a bit problematic when testing implicit clang module imports.
1 parent ef1af64 commit 17c0e56

File tree

7 files changed

+435
-0
lines changed

7 files changed

+435
-0
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ namespace swift {
643643
/// Enables dumping macro expansions.
644644
bool DumpMacroExpansions = false;
645645

646+
/// Enables dumping imports for each SourceFile.
647+
bool DumpSourceFileImports = false;
648+
646649
/// The model of concurrency to be used.
647650
ConcurrencyModel ActiveConcurrencyModel = ConcurrencyModel::Standard;
648651

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ def dump_macro_expansions : Flag<["-"], "dump-macro-expansions">,
433433
def emit_macro_expansion_files : Separate<["-"], "emit-macro-expansion-files">,
434434
HelpText<"Specify when to emit macro expansion file: 'none', 'debug', or 'diagnostics'">;
435435

436+
def dump_source_file_imports : Flag<["-"], "dump-source-file-imports">,
437+
HelpText<"Dumps the list of imports for each source file">;
438+
436439
def analyze_request_evaluator : Flag<["-"], "analyze-request-evaluator">,
437440
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
438441
HelpText<"Print out request evaluator cache statistics at the end of the compilation job">;

lib/AST/Module.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,11 @@ void
26652665
SourceFile::setImports(ArrayRef<AttributedImport<ImportedModule>> imports) {
26662666
assert(!Imports && "Already computed imports");
26672667
Imports = getASTContext().AllocateCopy(imports);
2668+
if (getASTContext().LangOpts.DumpSourceFileImports) {
2669+
llvm::errs() << "imports for " << getFilename() << ":\n";
2670+
for (auto Import : imports)
2671+
llvm::errs() << "\t" << Import.module.importedModule->getName() << "\n";
2672+
}
26682673
}
26692674

26702675
std::optional<AttributedImport<ImportedModule>>

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
17301730
Opts.DumpMacroExpansions = Args.hasArg(
17311731
OPT_dump_macro_expansions);
17321732

1733+
Opts.DumpSourceFileImports = Args.hasArg(
1734+
OPT_dump_source_file_imports);
1735+
17331736
if (const Arg *A = Args.getLastArg(OPT_debug_requirement_machine))
17341737
Opts.DebugRequirementMachine = A->getValue();
17351738

test/Interop/C/swiftify-import/clang-includes-aliasing-imports.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// RUN: %empty-directory(%t)
44
// RUN: split-file %s %t
55

6+
// RUN: %target-swift-frontend -dump-source-file-imports -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesExplicit.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/test.swift 2>&1 | %FileCheck %s --check-prefix DUMP
7+
// RUN: %target-swift-frontend -dump-source-file-imports -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesExplicit.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/test.swift -cxx-interoperability-mode=default 2>&1 | %FileCheck %s --check-prefix DUMP --check-prefix DUMP-CXX
8+
69
// RUN: %target-swift-ide-test -print-module -module-print-hidden -module-to-print=A1 -plugin-path %swift-plugin-dir -I %t -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s --check-prefix CHECK-A1 --implicit-check-not=import --match-full-lines
710
// RUN: %target-swift-ide-test -print-module -module-print-hidden -module-to-print=A2 -plugin-path %swift-plugin-dir -I %t -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s --check-prefix CHECK-A2 --implicit-check-not=import --match-full-lines
811

@@ -11,6 +14,17 @@ import A1
1114

1215
import A2
1316

17+
// DUMP: imports for {{.*}}/test.swift:
18+
// DUMP-NEXT: Swift
19+
// DUMP-CXX-NEXT: CxxShim
20+
// DUMP-CXX-NEXT: Cxx
21+
// DUMP-NEXT: _StringProcessing
22+
// DUMP-NEXT: _SwiftConcurrencyShims
23+
// DUMP-NEXT: _Concurrency
24+
// DUMP-NEXT: SwiftOnoneSupport
25+
// DUMP-NEXT: A1
26+
// DUMP-NEXT: A2
27+
1428
public func callUnsafe(_ p: UnsafeMutableRawPointer) {
1529
let _ = a1(p, 13)
1630
let _ = a2(p, 13)
@@ -60,6 +74,19 @@ module A4 {
6074

6175
aliasing1_t a1(void * _Nonnull __sized_by(size), aliasing2_t size);
6276

77+
// DUMP-NEXT: imports for A1.a1:
78+
// DUMP-NEXT: imports for @__swiftmacro_So2a115_SwiftifyImportfMp_.swift:
79+
// DUMP-NEXT: Swift
80+
// DUMP-NEXT: Aliasing
81+
// DUMP-NEXT: Aliasing
82+
// DUMP-CXX-NEXT: CxxShim
83+
// DUMP-CXX-NEXT: Cxx
84+
// DUMP-NEXT: _StringProcessing
85+
// DUMP-NEXT: _SwiftConcurrencyShims
86+
// DUMP-NEXT: _Concurrency
87+
// DUMP-NEXT: SwiftOnoneSupport
88+
// DUMP-NEXT: Swift
89+
6390
//--- Inputs/Aliasing1.h
6491
#pragma once
6592

@@ -91,3 +118,20 @@ typedef int aliasing4_t;
91118
#define __sized_by(s) __attribute__((__sized_by__(s)))
92119

93120
aliasing3_t a2(void * _Nonnull __sized_by(size), aliasing4_t size);
121+
122+
// DUMP-NEXT: imports for A2.a2:
123+
// DUMP-NEXT: imports for @__swiftmacro_So2a215_SwiftifyImportfMp_.swift:
124+
// DUMP-NEXT: Swift
125+
// DUMP-NEXT: Aliasing
126+
// DUMP-NEXT: A4
127+
// DUMP-NEXT: Aliasing
128+
// DUMP-NEXT: A3
129+
// DUMP-CXX-NEXT: CxxShim
130+
// DUMP-CXX-NEXT: Cxx
131+
// DUMP-NEXT: _StringProcessing
132+
// DUMP-NEXT: _SwiftConcurrencyShims
133+
// DUMP-NEXT: _Concurrency
134+
// DUMP-NEXT: SwiftOnoneSupport
135+
// DUMP-NEXT: Swift
136+
137+
// DUMP-NOT: imports

test/Interop/C/swiftify-import/clang-includes-indirect-explicit-modules.swift

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// RUN: %empty-directory(%t)
44
// RUN: split-file %s %t
55

6+
// RUN: %target-swift-frontend -dump-source-file-imports -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesExplicit.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/test.swift 2>&1 | %FileCheck %s --check-prefix DUMP
7+
// RUN: %target-swift-frontend -dump-source-file-imports -emit-module -plugin-path %swift-plugin-dir -o %t/ClangIncludesExplicit.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers %t/test.swift -cxx-interoperability-mode=default 2>&1 | %FileCheck %s --check-prefix DUMP --check-prefix DUMP-CXX
8+
9+
610
// RUN: %target-swift-ide-test -print-module -module-print-hidden -module-to-print=A1.B1 -plugin-path %swift-plugin-dir -I %t -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s --check-prefix CHECK-B1 --implicit-check-not=import --match-full-lines
711
// RUN: %target-swift-ide-test -print-module -module-print-hidden -module-to-print=A1.B1.C1 -plugin-path %swift-plugin-dir -I %t -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s --check-prefix CHECK-C1 --implicit-check-not=import --match-full-lines
812

@@ -12,6 +16,19 @@
1216
import A1.B1
1317
import A2.B2
1418

19+
// DUMP: imports for {{.*}}/test.swift:
20+
// DUMP-NEXT: Swift
21+
// DUMP-CXX-NEXT: CxxShim
22+
// DUMP-CXX-NEXT: Cxx
23+
// DUMP-NEXT: _StringProcessing
24+
// DUMP-NEXT: _SwiftConcurrencyShims
25+
// DUMP-NEXT: _Concurrency
26+
// DUMP-NEXT: SwiftOnoneSupport
27+
// DUMP-NEXT: B1
28+
// DUMP-NEXT: A1
29+
// DUMP-NEXT: B2
30+
// DUMP-NEXT: A2
31+
1532
public func callUnsafe(_ p: UnsafeMutableRawPointer) {
1633
let _ = b1b(p, 13)
1734
let _ = b1c(p, 13)
@@ -62,6 +79,48 @@ b1_t b1b(void * _Nonnull __sized_by(size), int size);
6279
c1_t b1c(void * _Nonnull __sized_by(size), int size);
6380
d1_t b1d(void * _Nonnull __sized_by(size), int size);
6481

82+
// DUMP-NEXT: imports for A1.b1b:
83+
// DUMP-NEXT: imports for @__swiftmacro_So3b1b15_SwiftifyImportfMp_.swift:
84+
// DUMP-NEXT: Swift
85+
// DUMP-NEXT: D1
86+
// DUMP-NEXT: D1
87+
// DUMP-NEXT: C1
88+
// DUMP-CXX-NEXT: CxxShim
89+
// DUMP-CXX-NEXT: Cxx
90+
// DUMP-NEXT: _StringProcessing
91+
// DUMP-NEXT: _SwiftConcurrencyShims
92+
// DUMP-NEXT: _Concurrency
93+
// DUMP-NEXT: SwiftOnoneSupport
94+
// DUMP-NEXT: Swift
95+
96+
// DUMP-NEXT: imports for A1.b1c:
97+
// DUMP-NEXT: imports for @__swiftmacro_So3b1c15_SwiftifyImportfMp_.swift:
98+
// DUMP-NEXT: Swift
99+
// DUMP-NEXT: D1
100+
// DUMP-NEXT: D1
101+
// DUMP-NEXT: C1
102+
// DUMP-CXX-NEXT: CxxShim
103+
// DUMP-CXX-NEXT: Cxx
104+
// DUMP-NEXT: _StringProcessing
105+
// DUMP-NEXT: _SwiftConcurrencyShims
106+
// DUMP-NEXT: _Concurrency
107+
// DUMP-NEXT: SwiftOnoneSupport
108+
// DUMP-NEXT: Swift
109+
110+
// DUMP-NEXT: imports for A1.b1d:
111+
// DUMP-NEXT: imports for @__swiftmacro_So3b1d15_SwiftifyImportfMp_.swift:
112+
// DUMP-NEXT: Swift
113+
// DUMP-NEXT: D1
114+
// DUMP-NEXT: D1
115+
// DUMP-NEXT: C1
116+
// DUMP-CXX-NEXT: CxxShim
117+
// DUMP-CXX-NEXT: Cxx
118+
// DUMP-NEXT: _StringProcessing
119+
// DUMP-NEXT: _SwiftConcurrencyShims
120+
// DUMP-NEXT: _Concurrency
121+
// DUMP-NEXT: SwiftOnoneSupport
122+
// DUMP-NEXT: Swift
123+
65124
//--- Inputs/C1.h
66125
#pragma once
67126

@@ -91,3 +150,56 @@ typedef int d1_t;
91150
b1_t b2b(void * _Nonnull __sized_by(size), int size);
92151
c1_t b2c(void * _Nonnull __sized_by(size), int size);
93152
d1_t b2d(void * _Nonnull __sized_by(size), int size);
153+
154+
// DUMP-NEXT: imports for A2.b2b:
155+
// DUMP-NEXT: imports for @__swiftmacro_So3b2b15_SwiftifyImportfMp_.swift:
156+
// DUMP-NEXT: Swift
157+
// DUMP-NEXT: D1
158+
// DUMP-NEXT: A1
159+
// DUMP-NEXT: C1
160+
// DUMP-NEXT: A1
161+
// DUMP-NEXT: B1
162+
// DUMP-NEXT: A1
163+
// DUMP-CXX-NEXT: CxxShim
164+
// DUMP-CXX-NEXT: Cxx
165+
// DUMP-NEXT: _StringProcessing
166+
// DUMP-NEXT: _SwiftConcurrencyShims
167+
// DUMP-NEXT: _Concurrency
168+
// DUMP-NEXT: SwiftOnoneSupport
169+
// DUMP-NEXT: Swift
170+
171+
// DUMP-NEXT: imports for A2.b2c:
172+
// DUMP-NEXT: imports for @__swiftmacro_So3b2c15_SwiftifyImportfMp_.swift:
173+
// DUMP-NEXT: Swift
174+
// DUMP-NEXT: D1
175+
// DUMP-NEXT: A1
176+
// DUMP-NEXT: C1
177+
// DUMP-NEXT: A1
178+
// DUMP-NEXT: B1
179+
// DUMP-NEXT: A1
180+
// DUMP-CXX-NEXT: CxxShim
181+
// DUMP-CXX-NEXT: Cxx
182+
// DUMP-NEXT: _StringProcessing
183+
// DUMP-NEXT: _SwiftConcurrencyShims
184+
// DUMP-NEXT: _Concurrency
185+
// DUMP-NEXT: SwiftOnoneSupport
186+
// DUMP-NEXT: Swift
187+
188+
// DUMP-NEXT: imports for A2.b2d:
189+
// DUMP-NEXT: imports for @__swiftmacro_So3b2d15_SwiftifyImportfMp_.swift:
190+
// DUMP-NEXT: Swift
191+
// DUMP-NEXT: D1
192+
// DUMP-NEXT: A1
193+
// DUMP-NEXT: C1
194+
// DUMP-NEXT: A1
195+
// DUMP-NEXT: B1
196+
// DUMP-NEXT: A1
197+
// DUMP-CXX-NEXT: CxxShim
198+
// DUMP-CXX-NEXT: Cxx
199+
// DUMP-NEXT: _StringProcessing
200+
// DUMP-NEXT: _SwiftConcurrencyShims
201+
// DUMP-NEXT: _Concurrency
202+
// DUMP-NEXT: SwiftOnoneSupport
203+
// DUMP-NEXT: Swift
204+
205+
// DUMP-NOT: imports

0 commit comments

Comments
 (0)