Skip to content

Commit 2cb2760

Browse files
committed
ASTPrinter: Add an option to qualify ClangImported types.
This part of a series of patches to bring ASTPrinter and Swift Demangler to feature parity, which is needed by LLDB, which depends on using the strings produced by either interchangibly. rdar://problem/63700540
1 parent 456775f commit 2cb2760

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ struct PrintOptions {
435435
/// The information for converting archetypes to specialized types.
436436
llvm::Optional<TypeTransformContext> TransformContext;
437437

438+
/// Whether to display (Clang-)imported module names;
439+
bool QualifyImportedTypes = false;
440+
438441
/// Whether cross-import overlay modules are printed with their own name (e.g.
439442
/// _MyFrameworkYourFrameworkAdditions) or that of their underlying module
440443
/// (e.g. MyFramework).

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,11 +3641,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36413641
return false;
36423642

36433643
// Don't print qualifiers for imported types.
3644-
for (auto File : M->getFiles()) {
3645-
if (File->getKind() == FileUnitKind::ClangModule ||
3646-
File->getKind() == FileUnitKind::DWARFModule)
3647-
return false;
3648-
}
3644+
if (!Options.QualifyImportedTypes)
3645+
for (auto File : M->getFiles()) {
3646+
if (File->getKind() == FileUnitKind::ClangModule ||
3647+
File->getKind() == FileUnitKind::DWARFModule)
3648+
return false;
3649+
}
36493650

36503651
return true;
36513652
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// REQUIRES: executable_test
2+
// REQUIRES: objc_interop
3+
// RUN: %empty-directory(%t)
4+
// RUN: echo '$sSo3FooVD' > %t/list
5+
// RUN: %target-build-swift -emit-executable %s -g -o %t/a.out -I %S/Inputs \
6+
// RUN: -module-name ASTPrinter -emit-module
7+
// RUN: %lldb-moduleimport-test -qualify-types \
8+
// RUN: -type-from-mangled=%t/list %t/a.out | %FileCheck %s
9+
// This name should come out fully qualified.
10+
// CHECK: ClangModule.Foo
11+
import ClangModule
12+
let foo = Foo()

tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,19 @@ static void resolveDeclFromMangledNameList(
9595
}
9696
}
9797

98-
static void resolveTypeFromMangledNameList(
99-
swift::ASTContext &Ctx, llvm::ArrayRef<std::string> MangledNames) {
98+
static void
99+
resolveTypeFromMangledNameList(swift::ASTContext &Ctx,
100+
llvm::ArrayRef<std::string> MangledNames,
101+
bool QualifyTypes) {
100102
for (auto &Mangled : MangledNames) {
101103
swift::Type ResolvedType =
102104
swift::Demangle::getTypeForMangling(Ctx, Mangled);
103105
if (!ResolvedType) {
104106
llvm::outs() << "Can't resolve type of " << Mangled << "\n";
105107
} else {
106108
swift::PrintOptions PO;
109+
PO.FullyQualifiedTypesIfAmbiguous = QualifyTypes;
110+
PO.QualifyImportedTypes = QualifyTypes;
107111
PO.PrintStorageRepresentationAttrs = true;
108112
ResolvedType->print(llvm::outs(), PO);
109113
llvm::outs() << "\n";
@@ -233,6 +237,9 @@ int main(int argc, char **argv) {
233237
"dummy-dwarfimporter",
234238
desc("Install a dummy DWARFImporterDelegate"), cat(Visible));
235239

240+
opt<bool> QualifyTypes("qualify-types", desc("Qualify dumped types"),
241+
cat(Visible));
242+
236243
ParseCommandLineOptions(argc, argv);
237244

238245
// Unregister our options so they don't interfere with the command line
@@ -352,14 +359,14 @@ int main(int argc, char **argv) {
352359
if (DumpModule) {
353360
llvm::SmallVector<swift::Decl*, 10> Decls;
354361
Module->getTopLevelDecls(Decls);
355-
for (auto Decl : Decls) {
362+
for (auto Decl : Decls)
356363
Decl->dump(llvm::outs());
357-
}
358364
}
359365
if (!DumpTypeFromMangled.empty()) {
360366
llvm::SmallVector<std::string, 8> MangledNames;
361367
collectMangledNames(DumpTypeFromMangled, MangledNames);
362-
resolveTypeFromMangledNameList(CI.getASTContext(), MangledNames);
368+
resolveTypeFromMangledNameList(CI.getASTContext(), MangledNames,
369+
QualifyTypes);
363370
}
364371
if (!DumpDeclFromMangled.empty()) {
365372
llvm::SmallVector<std::string, 8> MangledNames;

0 commit comments

Comments
 (0)