Skip to content

Commit a83444b

Browse files
beccadaxglessard
authored andcommitted
Add flag for dumping ClangImporter lookup tables
ClangImporter’s SwiftLookupTables map Swift names to their corresponding Clang declarations. These tables are built into a module’s clang .pcm file and missing or inaccurate entries can cause name lookup to fail to find an imported declaration. Swift has always included a helper function that would dump these tables, and swift-ide-test has a command-line switch that would invoke it, but these tools are clumsy to use in many debugging scenarios. Add a frontend flag that dumps the tables at the end of the frontend job, making it a lot easier to get at this information in the context of a specific compilation.
1 parent aee545d commit a83444b

File tree

9 files changed

+27
-6
lines changed

9 files changed

+27
-6
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class ClangModuleLoader : public ModuleLoader {
144144
virtual clang::Sema &getClangSema() const = 0;
145145
virtual const clang::CompilerInstance &getClangInstance() const = 0;
146146
virtual void printStatistics() const = 0;
147+
virtual void dumpSwiftLookupTables() const = 0;
147148

148149
/// Returns the module that contains imports and declarations from all loaded
149150
/// Objective-C header files.

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ class ClangImporter final : public ClangModuleLoader {
538538
void printStatistics() const override;
539539

540540
/// Dump Swift lookup tables.
541-
void dumpSwiftLookupTables();
541+
void dumpSwiftLookupTables() const override;
542542

543543
/// Given the path of a Clang module, collect the names of all its submodules.
544544
/// Calling this function does not load the module.

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ class FrontendOptions {
280280
/// termination.
281281
bool PrintClangStats = false;
282282

283+
/// Indicates whether or not the Clang importer should dump lookup tables
284+
/// upon termination.
285+
bool DumpClangLookupTables = false;
286+
283287
/// Indicates whether standard help should be shown.
284288
bool PrintHelp = false;
285289

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,10 @@ def stack_promotion_limit : Separate<["-"], "stack-promotion-limit">,
580580
def dump_clang_diagnostics : Flag<["-"], "dump-clang-diagnostics">,
581581
HelpText<"Dump Clang diagnostics to stderr">;
582582

583+
def dump_clang_lookup_tables : Flag<["-"], "dump-clang-lookup-tables">,
584+
HelpText<"Dump the importer's Swift-name-to-Clang-name lookup tables to "
585+
"stderr">;
586+
583587
def disable_modules_validate_system_headers : Flag<["-"], "disable-modules-validate-system-headers">,
584588
HelpText<"Disable validating system headers in the Clang importer">;
585589

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6269,7 +6269,7 @@ EffectiveClangContext ClangImporter::Implementation::getEffectiveClangContext(
62696269
return EffectiveClangContext();
62706270
}
62716271

6272-
void ClangImporter::dumpSwiftLookupTables() {
6272+
void ClangImporter::dumpSwiftLookupTables() const {
62736273
Impl.dumpSwiftLookupTables();
62746274
}
62756275

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ bool ArgsToFrontendOptionsConverter::convert(
170170
computeDebugTimeOptions();
171171
computeTBDOptions();
172172

173+
Opts.DumpClangLookupTables |= Args.hasArg(OPT_dump_clang_lookup_tables);
174+
173175
Opts.CheckOnoneSupportCompleteness = Args.hasArg(OPT_check_onone_completeness);
174176

175177
Opts.ParseStdlib |= Args.hasArg(OPT_parse_stdlib);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,9 @@ static void performEndOfPipelineActions(CompilerInstance &Instance) {
11171117
if (auto *stats = ctx.Stats)
11181118
countASTStats(*stats, Instance);
11191119

1120+
if (opts.DumpClangLookupTables && ctx.getClangModuleLoader())
1121+
ctx.getClangModuleLoader()->dumpSwiftLookupTables();
1122+
11201123
// Report mangling stats if there was no error.
11211124
if (!ctx.hadError())
11221125
Mangle::printManglingStats();

test/IDE/dump_swift_lookup_tables.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
// RUN: %target-swift-ide-test -dump-importer-lookup-table -source-filename %s -import-objc-header %S/Inputs/swift_name.h -I %S/Inputs/custom-modules > %t.log 2>&1
2-
// RUN: %FileCheck %s < %t.log
1+
// RUN: %target-swift-ide-test -dump-importer-lookup-table -source-filename %s -import-objc-header %S/Inputs/swift_name.h -I %S/Inputs/custom-modules > %t.ide-test.log 2>&1
2+
// RUN: %FileCheck %s < %t.ide-test.log
3+
4+
// RUN: %target-typecheck-verify-swift -dump-clang-lookup-tables -import-objc-header %S/Inputs/swift_name.h -I %S/Inputs/custom-modules > %t.frontend.log 2>&1
5+
// RUN: %FileCheck %s < %t.frontend.log
6+
// RUN: false
37

48
// REQUIRES: objc_interop
59
import ImportAsMember

test/IDE/dump_swift_lookup_tables_objc.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// RUN: %target-swift-ide-test -dump-importer-lookup-table -source-filename %s -import-objc-header %S/Inputs/swift_name_objc.h > %t.log 2>&1
2-
// RUN: %FileCheck %s < %t.log
1+
// RUN: %target-swift-ide-test -dump-importer-lookup-table -source-filename %s -import-objc-header %S/Inputs/swift_name_objc.h > %t.ide-test.log 2>&1
2+
// RUN: %FileCheck %s < %t.ide-test.log
3+
4+
// RUN: %target-typecheck-verify-swift -dump-clang-lookup-tables -import-objc-header %S/Inputs/swift_name_objc.h > %t.frontend.log 2>&1
5+
// RUN: %FileCheck %s < %t.frontend.log
36

47
// REQUIRES: objc_interop
58
// REQUIRES: OS=macosx

0 commit comments

Comments
 (0)