Skip to content

ASTPrinter: Add an option to qualify ClangImported types. #32099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ struct PrintOptions {
/// The information for converting archetypes to specialized types.
llvm::Optional<TypeTransformContext> TransformContext;

/// Whether to display (Clang-)imported module names;
bool QualifyImportedTypes = false;

/// Whether cross-import overlay modules are printed with their own name (e.g.
/// _MyFrameworkYourFrameworkAdditions) or that of their underlying module
/// (e.g. MyFramework).
Expand Down
11 changes: 6 additions & 5 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3641,11 +3641,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
return false;

// Don't print qualifiers for imported types.
for (auto File : M->getFiles()) {
if (File->getKind() == FileUnitKind::ClangModule ||
File->getKind() == FileUnitKind::DWARFModule)
return false;
}
if (!Options.QualifyImportedTypes)
for (auto File : M->getFiles()) {
if (File->getKind() == FileUnitKind::ClangModule ||
File->getKind() == FileUnitKind::DWARFModule)
return false;
}

return true;
}
Expand Down
12 changes: 12 additions & 0 deletions test/DebugInfo/ASTPrinter-imported-names.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// REQUIRES: executable_test
// REQUIRES: objc_interop
// RUN: %empty-directory(%t)
// RUN: echo '$sSo3FooVD' > %t/list
// RUN: %target-build-swift -emit-executable %s -g -o %t/a.out -I %S/Inputs \
// RUN: -module-name ASTPrinter -emit-module
// RUN: %lldb-moduleimport-test -qualify-types \
// RUN: -type-from-mangled=%t/list %t/a.out | %FileCheck %s
// This name should come out fully qualified.
// CHECK: ClangModule.Foo
import ClangModule
let foo = Foo()
17 changes: 12 additions & 5 deletions tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@ static void resolveDeclFromMangledNameList(
}
}

static void resolveTypeFromMangledNameList(
swift::ASTContext &Ctx, llvm::ArrayRef<std::string> MangledNames) {
static void
resolveTypeFromMangledNameList(swift::ASTContext &Ctx,
llvm::ArrayRef<std::string> MangledNames,
bool QualifyTypes) {
for (auto &Mangled : MangledNames) {
swift::Type ResolvedType =
swift::Demangle::getTypeForMangling(Ctx, Mangled);
if (!ResolvedType) {
llvm::outs() << "Can't resolve type of " << Mangled << "\n";
} else {
swift::PrintOptions PO;
PO.FullyQualifiedTypesIfAmbiguous = QualifyTypes;
PO.QualifyImportedTypes = QualifyTypes;
PO.PrintStorageRepresentationAttrs = true;
ResolvedType->print(llvm::outs(), PO);
llvm::outs() << "\n";
Expand Down Expand Up @@ -233,6 +237,9 @@ int main(int argc, char **argv) {
"dummy-dwarfimporter",
desc("Install a dummy DWARFImporterDelegate"), cat(Visible));

opt<bool> QualifyTypes("qualify-types", desc("Qualify dumped types"),
cat(Visible));

ParseCommandLineOptions(argc, argv);

// Unregister our options so they don't interfere with the command line
Expand Down Expand Up @@ -352,14 +359,14 @@ int main(int argc, char **argv) {
if (DumpModule) {
llvm::SmallVector<swift::Decl*, 10> Decls;
Module->getTopLevelDecls(Decls);
for (auto Decl : Decls) {
for (auto Decl : Decls)
Decl->dump(llvm::outs());
}
}
if (!DumpTypeFromMangled.empty()) {
llvm::SmallVector<std::string, 8> MangledNames;
collectMangledNames(DumpTypeFromMangled, MangledNames);
resolveTypeFromMangledNameList(CI.getASTContext(), MangledNames);
resolveTypeFromMangledNameList(CI.getASTContext(), MangledNames,
QualifyTypes);
}
if (!DumpDeclFromMangled.empty()) {
llvm::SmallVector<std::string, 8> MangledNames;
Expand Down