@@ -220,28 +220,49 @@ swift::ide::collectModuleGroups(ModuleDecl *M, std::vector<StringRef> &Scratch)
220
220
return llvm::makeArrayRef (Scratch);
221
221
}
222
222
223
- // / Determine whether the given extension has a Clang node that
224
- // / created it (vs. being a Swift extension).
225
- static bool extensionHasClangNode (ExtensionDecl *ext) {
226
- // If it has a Clang node (directly),
227
- if (ext->hasClangNode ()) return true ;
223
+ // / Retrieve the effective Clang node for the given declaration, which
224
+ // / copes with the odd case of imported Error enums.
225
+ static ClangNode getEffectiveClangNode (const Decl *decl) {
226
+ // Directly...
227
+ if (auto clangNode = decl->getClangNode ())
228
+ return clangNode;
229
+
230
+ // Or via the nested "Code" enum.
231
+ if (auto nominal =
232
+ const_cast <NominalTypeDecl *>(dyn_cast<NominalTypeDecl>(decl))) {
233
+ auto &ctx = nominal->getASTContext ();
234
+ for (auto code : nominal->lookupDirect (ctx.Id_Code ,
235
+ /* ignoreNewExtensions=*/ true )) {
236
+ if (auto clangDecl = code->getClangDecl ())
237
+ return clangDecl;
238
+ }
239
+ }
228
240
229
- // If it has a global imported as a member.
230
- auto members = ext->getMembers ();
231
- if (members.empty ()) return false ;
232
- return members.front ()->hasClangNode ();
241
+ return ClangNode ();
233
242
}
234
243
235
244
// / Retrieve the Clang node for the given extension, if it has one.
236
- // / created it (vs. being a Swift extension).
237
245
static ClangNode extensionGetClangNode (ExtensionDecl *ext) {
238
246
// If it has a Clang node (directly),
239
247
if (ext->hasClangNode ()) return ext->getClangNode ();
240
248
241
- // If it has a global imported as a member.
242
- auto members = ext->getMembers ();
243
- if (members.empty ()) return ClangNode ();
244
- return members.front ()->getClangNode ();
249
+ // Check whether it was syntheszed into a module-scope context.
250
+ if (!isa<ClangModuleUnit>(ext->getModuleScopeContext ()))
251
+ return ClangNode ();
252
+
253
+ // It may have a global imported as a member.
254
+ for (auto member : ext->getMembers ()) {
255
+ if (auto clangNode = getEffectiveClangNode (member))
256
+ return clangNode;
257
+ }
258
+
259
+ return ClangNode ();
260
+ }
261
+
262
+ // / Determine whether the given extension has a Clang node that
263
+ // / created it (vs. being a Swift extension).
264
+ static bool extensionHasClangNode (ExtensionDecl *ext) {
265
+ return static_cast <bool >(extensionGetClangNode (ext));
245
266
}
246
267
247
268
Optional<StringRef>
@@ -393,8 +414,8 @@ void swift::ide::printSubmoduleInterface(
393
414
}
394
415
};
395
416
396
- if (D-> hasClangNode ( )) {
397
- addToClangDecls (D, D-> getClangNode () );
417
+ if (auto clangNode = getEffectiveClangNode (D )) {
418
+ addToClangDecls (D, clangNode );
398
419
continue ;
399
420
}
400
421
@@ -739,8 +760,8 @@ void swift::ide::printHeaderInterface(
739
760
std::sort (ClangDecls.begin (), ClangDecls.end (),
740
761
[&](Decl *LHS, Decl *RHS) -> bool {
741
762
return ClangSM.isBeforeInTranslationUnit (
742
- LHS-> getClangNode ( ).getLocation (),
743
- RHS-> getClangNode ( ).getLocation ());
763
+ getEffectiveClangNode (LHS ).getLocation (),
764
+ getEffectiveClangNode (RHS ).getLocation ());
744
765
});
745
766
746
767
ASTPrinter *PrinterToUse = &Printer;
@@ -789,7 +810,7 @@ void ClangCommentPrinter::printDeclPre(const Decl *D,
789
810
// single line.
790
811
// FIXME: we should fix that, since it also affects struct members, etc.
791
812
if (!isa<ParamDecl>(D)) {
792
- if (auto ClangN = D-> getClangNode ( )) {
813
+ if (auto ClangN = getEffectiveClangNode (D )) {
793
814
printCommentsUntil (ClangN);
794
815
if (shouldPrintNewLineBefore (ClangN)) {
795
816
*this << " \n " ;
@@ -813,7 +834,7 @@ void ClangCommentPrinter::printDeclPost(const Decl *D,
813
834
*this << " " << ASTPrinter::sanitizeUtf8 (CommentText);
814
835
}
815
836
PendingComments.clear ();
816
- if (auto ClangN = D-> getClangNode ( ))
837
+ if (auto ClangN = getEffectiveClangNode (D ))
817
838
updateLastEntityLine (ClangN.getSourceRange ().getEnd ());
818
839
}
819
840
0 commit comments