Skip to content

Commit 901ffb7

Browse files
committed
Add static checking to ensure every DeclAttribute subclass has a properly-typed clone()
Code review identified some incorrect UNIMPLEMENTED_CLONEs in DeclAttribute (thank you Hamish and Rintaro). Fix those, and make sure this can't happen again by checking the type signatures of clone() in every DeclAttribute subclass.
1 parent f8f14b4 commit 901ffb7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

include/swift/AST/Attr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ class TypeEraserAttr final : public DeclAttribute {
11641164
return DA->getKind() == DeclAttrKind::TypeEraser;
11651165
}
11661166

1167-
UNIMPLEMENTED_CLONE(DynamicReplacementAttr)
1167+
UNIMPLEMENTED_CLONE(TypeEraserAttr)
11681168
};
11691169

11701170
/// Represents any sort of access control modifier.
@@ -2814,7 +2814,7 @@ class RawLayoutAttr final : public DeclAttribute {
28142814
return DA->getKind() == DeclAttrKind::RawLayout;
28152815
}
28162816

2817-
UNIMPLEMENTED_CLONE(MacroRoleAttr)
2817+
UNIMPLEMENTED_CLONE(RawLayoutAttr)
28182818
};
28192819

28202820
class LifetimeAttr final : public DeclAttribute {

lib/AST/Attr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,16 @@ bool DeclAttribute::canAttributeAppearOnDeclKind(DeclAttrKind DAK, DeclKind DK)
353353
llvm_unreachable("bad DeclKind");
354354
}
355355

356+
// Ensure that every DeclAttribute subclass implements its own CloneAttr.
357+
static void checkDeclAttributeClones() {
358+
#define DECL_ATTR(_,CLASS,...) \
359+
CLASS##Attr *(CLASS##Attr::*ptr##CLASS)(ASTContext &) const = &CLASS##Attr::clone; \
360+
(void)ptr##CLASS;
361+
#include "swift/AST/DeclAttr.def"
362+
}
363+
356364
DeclAttribute *DeclAttribute::clone(ASTContext &ctx) const {
365+
(void)checkDeclAttributeClones;
357366
switch (getKind()) {
358367
#define DECL_ATTR(_,CLASS, ...) \
359368
case DeclAttrKind::CLASS: return static_cast<const CLASS##Attr *>(this)->clone(ctx);

0 commit comments

Comments
 (0)