Skip to content

Commit 88c8893

Browse files
committed
[interop] AST printer should only visit one original namespace decl to avoid printing redecls over and over again
Also, lookup namespace members directly without doing a lookup. This helps us print the libc++ module interface in < 1s on M1
1 parent 25bcd26 commit 88c8893

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
// ambiguities with type names, in AliasModuleNames mode.
2929
#define MODULE_DISAMBIGUATING_PREFIX "Module___"
3030

31+
namespace clang {
32+
class Decl;
33+
}
34+
3135
namespace swift {
3236
class Decl;
3337
class DeclContext;
@@ -111,6 +115,7 @@ class ASTPrinter {
111115
unsigned CurrentIndentation = 0;
112116
unsigned PendingNewlines = 0;
113117
TypeOrExtensionDecl SynthesizeTarget;
118+
llvm::SmallPtrSet<const clang::Decl *, 8> printedClangDecl;
114119

115120
void printTextImpl(StringRef Text);
116121

@@ -331,6 +336,12 @@ class ASTPrinter {
331336
printStructurePre(Kind, D);
332337
}
333338

339+
/// Return true when the given redeclared clang decl is being printed for the
340+
/// first time.
341+
bool shouldPrintRedeclaredClangDecl(const clang::Decl *d) {
342+
return printedClangDecl.insert(d).second;
343+
}
344+
334345
private:
335346
virtual void anchor();
336347
};

lib/AST/ASTPrinter.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "InlinableText.h"
1817
#include "swift/AST/ASTPrinter.h"
18+
#include "InlinableText.h"
1919
#include "swift/AST/ASTContext.h"
2020
#include "swift/AST/ASTMangler.h"
2121
#include "swift/AST/ASTVisitor.h"
@@ -61,6 +61,7 @@
6161
#include "clang/AST/DeclObjC.h"
6262
#include "clang/Basic/Module.h"
6363
#include "clang/Basic/SourceManager.h"
64+
#include "clang/Lex/MacroInfo.h"
6465
#include "llvm/ADT/STLExtras.h"
6566
#include "llvm/ADT/StringSwitch.h"
6667
#include "llvm/Support/Compiler.h"
@@ -2323,10 +2324,16 @@ static void addNamespaceMembers(Decl *decl,
23232324
if (!name)
23242325
continue;
23252326

2326-
CXXNamespaceMemberLookup lookupRequest({cast<EnumDecl>(decl), name});
2327-
for (auto found : evaluateOrDefault(ctx.evaluator, lookupRequest, {})) {
2328-
if (addedMembers.insert(found).second)
2329-
members.push_back(found);
2327+
auto allResults = evaluateOrDefault(
2328+
ctx.evaluator, ClangDirectLookupRequest({decl, redecl, name}), {});
2329+
2330+
for (auto found : allResults) {
2331+
auto clangMember = found.get<clang::NamedDecl *>();
2332+
if (auto importedDecl =
2333+
ctx.getClangModuleLoader()->importDeclDirectly(clangMember)) {
2334+
if (addedMembers.insert(importedDecl).second)
2335+
members.push_back(importedDecl);
2336+
}
23302337
}
23312338
}
23322339
}
@@ -3593,6 +3600,13 @@ void PrintAST::visitAssociatedTypeDecl(AssociatedTypeDecl *decl) {
35933600
}
35943601

35953602
void PrintAST::visitEnumDecl(EnumDecl *decl) {
3603+
if (const auto *namespaceDecl =
3604+
dyn_cast_or_null<clang::NamespaceDecl>(decl->getClangDecl())) {
3605+
// Enum that correponds to the C++ namespace should only be printed once.
3606+
if (!Printer.shouldPrintRedeclaredClangDecl(
3607+
namespaceDecl->getOriginalNamespace()))
3608+
return;
3609+
}
35963610
printDocumentationComment(decl);
35973611
printAttributes(decl);
35983612
printAccess(decl);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=std -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop -enable-objc-interop -module-print-submodules | %FileCheck %s
2+
3+
// REQUIRES: OS=macosx
4+
5+
// CHECK: enum std {
6+
// CHECK-NEXT: enum __1 {
7+
8+
// CHECK: typealias string =
9+
10+
// CHECK-NOT: enum std

0 commit comments

Comments
 (0)