Skip to content

Commit c9beaf0

Browse files
authored
Merge pull request #64496 from hyp/eng/skip-inline-ns
[interop] do not print inline C++ namespaces when printing module int…
2 parents 201c4b6 + 259763c commit c9beaf0

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ struct PrintOptions {
276276
/// Whether to skip printing 'import' declarations.
277277
bool SkipImports = false;
278278

279+
/// Whether to skip over the C++ inline namespace when printing its members or
280+
/// when printing it out as a qualifier.
281+
bool SkipInlineCXXNamespace = false;
282+
279283
/// Whether to skip printing overrides and witnesses for
280284
/// protocol requirements.
281285
bool SkipOverrides = false;

lib/AST/ASTPrinter.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,12 @@ class PrintAST : public ASTVisitor<PrintAST> {
913913
void printPatternType(const Pattern *P);
914914
void printAccessors(const AbstractStorageDecl *ASD);
915915
void printSelfAccessKindModifiersIfNeeded(const FuncDecl *FD);
916-
void printMembersOfDecl(Decl * NTD, bool needComma = false,
917-
bool openBracket = true, bool closeBracket = true);
916+
void printMembersOfDecl(Decl *NTD, bool needComma = false,
917+
bool openBracket = true, bool closeBracket = true,
918+
bool doIndent = true);
918919
void printMembers(ArrayRef<Decl *> members, bool needComma = false,
919-
bool openBracket = true, bool closeBracket = true);
920+
bool openBracket = true, bool closeBracket = true,
921+
bool doIndent = true);
920922
void printGenericDeclGenericParams(GenericContext *decl);
921923
void printDeclGenericRequirements(GenericContext *decl);
922924
void printPrimaryAssociatedTypes(ProtocolDecl *decl);
@@ -2378,9 +2380,8 @@ static void addNamespaceMembers(Decl *decl,
23782380
}
23792381
}
23802382

2381-
void PrintAST::printMembersOfDecl(Decl *D, bool needComma,
2382-
bool openBracket,
2383-
bool closeBracket) {
2383+
void PrintAST::printMembersOfDecl(Decl *D, bool needComma, bool openBracket,
2384+
bool closeBracket, bool doIndent) {
23842385
llvm::SmallVector<Decl *, 16> Members;
23852386
auto AddMembers = [&](IterableDeclContext *idc) {
23862387
if (Options.PrintCurrentMembersOnly) {
@@ -2413,18 +2414,19 @@ void PrintAST::printMembersOfDecl(Decl *D, bool needComma,
24132414
if (isa_and_nonnull<clang::NamespaceDecl>(D->getClangDecl()))
24142415
addNamespaceMembers(D, Members);
24152416
}
2416-
printMembers(Members, needComma, openBracket, closeBracket);
2417+
printMembers(Members, needComma, openBracket, closeBracket, doIndent);
24172418
}
24182419

24192420
void PrintAST::printMembers(ArrayRef<Decl *> members, bool needComma,
2420-
bool openBracket, bool closeBracket) {
2421+
bool openBracket, bool closeBracket,
2422+
bool doIndent) {
24212423
if (openBracket) {
24222424
Printer << " {";
24232425
if (!Options.PrintEmptyMembersOnSameLine || !members.empty())
24242426
Printer.printNewline();
24252427
}
24262428
{
2427-
IndentRAII indentMore(*this);
2429+
IndentRAII indentMore(*this, /*DoIndent=*/doIndent);
24282430
for (auto i = members.begin(), iEnd = members.end(); i != iEnd; ++i) {
24292431
auto member = *i;
24302432

@@ -3713,6 +3715,13 @@ void PrintAST::visitEnumDecl(EnumDecl *decl) {
37133715
if (!Printer.shouldPrintRedeclaredClangDecl(
37143716
namespaceDecl->getOriginalNamespace()))
37153717
return;
3718+
3719+
if (Options.SkipInlineCXXNamespace && namespaceDecl->isInline()) {
3720+
// Print members directly if this is an inline namespace.
3721+
printMembersOfDecl(decl, false, /*openBracket=*/false,
3722+
/*closeBracket=*/false, /*doIndent=*/false);
3723+
return;
3724+
}
37163725
}
37173726
printDocumentationComment(decl);
37183727
printAttributes(decl);
@@ -6003,6 +6012,20 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60036012
return;
60046013
}
60056014
}
6015+
if (Options.SkipInlineCXXNamespace) {
6016+
// Don't print the parent type if it's a reference to an inline C++
6017+
// namespace.
6018+
if (auto *enumTy = T->getAs<EnumType>()) {
6019+
if (const auto *namespaceDecl = dyn_cast_or_null<clang::NamespaceDecl>(
6020+
enumTy->getDecl()->getClangDecl())) {
6021+
if (namespaceDecl->isInline()) {
6022+
if (auto parent = enumTy->getParent())
6023+
visitParentType(parent);
6024+
return;
6025+
}
6026+
}
6027+
}
6028+
}
60066029
PrintOptions innerOptions = Options;
60076030
innerOptions.SynthesizeSugarOnTypes = false;
60086031

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@ void swift::ide::printSymbolicSwiftClangModuleInterface(
11491149
PrintOptions::printModuleInterface(/*printFullConvention=*/false);
11501150
popts.PrintDocumentationComments = false;
11511151
popts.PrintRegularClangComments = false;
1152+
popts.SkipInlineCXXNamespace = true;
11521153

11531154
auto &SwiftContext = M->getTopLevelModule()->getASTContext();
11541155
auto &Importer =

test/Interop/Cxx/symbolic-imports/indexing-emit-symbolic-module-interface.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ namespace ns {
8181

8282
InnerTemplate<int> returnsTemplateMethod();
8383
};
84+
85+
inline namespace __1 {
86+
struct StructInInlineNamespace {
87+
};
88+
89+
using TypealiasInInlineNamespace = TemplateRecord<StructInInlineNamespace>;
90+
}
91+
92+
using TypealiasOfInlineNamespace = __1::StructInInlineNamespace;
8493
}
8594

8695
using MyType = ns::TemplateRecord<int>;
@@ -128,6 +137,15 @@ import CxxModule
128137
// CHECK-EMPTY:
129138
// CHECK-NEXT: mutating func returnsTemplateMethod()
130139
// CHECK-NEXT: }
140+
// CHECK: public struct StructInInlineNamespace {
141+
// CHECK-EMPTY:
142+
// CHECK-NEXT: public init()
143+
// CHECK-NEXT: }
144+
// CHECK-EMPTY:
145+
// CHECK-NEXT: public typealias TypealiasInInlineNamespace = ns.TemplateRecord
146+
// CHECK-EMPTY:
147+
// CHECK-EMPTY:
148+
// CHECK-NEXT: public typealias TypealiasOfInlineNamespace = ns.StructInInlineNamespace
131149
// CHECK-EMPTY:
132150
// CHECK-NEXT: static func freeFunction(_ x: Int32, _ y: Int32) -> Int32
133151
// CHECK-NEXT: }

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,12 @@ static bool getModuleInterfaceInfo(ASTContext &Ctx,
291291
PrintOptions Options = PrintOptions::printModuleInterface(
292292
Ctx.TypeCheckerOpts.PrintFullConvention);
293293
if (Mod->findUnderlyingClangModule()) {
294-
// Show unavailable C++ APIs.
295-
if (Ctx.LangOpts.EnableCXXInterop)
294+
if (Ctx.LangOpts.EnableCXXInterop) {
295+
// Show unavailable C++ APIs.
296296
Options.SkipUnavailable = false;
297+
// Skip over inline namespaces.
298+
Options.SkipInlineCXXNamespace = true;
299+
}
297300
}
298301
ModuleTraversalOptions TraversalOptions = None; // Don't print submodules.
299302
SmallString<128> Text;

0 commit comments

Comments
 (0)