Skip to content

Commit 09a5c7d

Browse files
Merge pull request #32099 from adrian-prantl/astprinter-qualify-imported
ASTPrinter: Add an option to qualify ClangImported types.
2 parents f983d85 + 2cb2760 commit 09a5c7d

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
@@ -437,6 +437,9 @@ struct PrintOptions {
437437
/// The information for converting archetypes to specialized types.
438438
llvm::Optional<TypeTransformContext> TransformContext;
439439

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

lib/AST/ASTPrinter.cpp

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

36523652
// Don't print qualifiers for imported types.
3653-
for (auto File : M->getFiles()) {
3654-
if (File->getKind() == FileUnitKind::ClangModule ||
3655-
File->getKind() == FileUnitKind::DWARFModule)
3656-
return false;
3657-
}
3653+
if (!Options.QualifyImportedTypes)
3654+
for (auto File : M->getFiles()) {
3655+
if (File->getKind() == FileUnitKind::ClangModule ||
3656+
File->getKind() == FileUnitKind::DWARFModule)
3657+
return false;
3658+
}
36583659

36593660
return true;
36603661
}
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)