Skip to content

Commit 733b17e

Browse files
authored
Merge pull request #23607 from nkcsgexi/swift-only-api-checker
swift-api-digester: simplify emit diagnostics API
2 parents 3ce5a8a + 350ed65 commit 733b17e

File tree

2 files changed

+70
-86
lines changed

2 files changed

+70
-86
lines changed

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "swift/AST/USRGeneration.h"
3939
#include "swift/AST/GenericSignature.h"
4040
#include "swift/AST/ProtocolConformance.h"
41+
#include "swift/AST/DiagnosticsModuleDiffer.h"
4142
#include "swift/Basic/ColorUtils.h"
4243
#include "swift/Basic/JSONSerialization.h"
4344
#include "swift/Basic/LLVMInitialize.h"
@@ -195,6 +196,7 @@ class SDKContext {
195196
bool shouldIgnore(Decl *D, const Decl* Parent = nullptr) const;
196197
ArrayRef<BreakingAttributeInfo> getBreakingAttributeInfo() const { return BreakingAttrs; }
197198
Optional<uint8_t> getFixedBinaryOrder(ValueDecl *VD) const;
199+
198200
template<class YAMLNodeTy, typename ...ArgTypes>
199201
void diagnose(YAMLNodeTy node, Diag<ArgTypes...> ID,
200202
typename detail::PassArgument<ArgTypes>::type... args) {
@@ -336,6 +338,19 @@ class SDKNodeDecl: public SDKNode {
336338
uint8_t getFixedBinaryOrder() const { return *FixedBinaryOrder; }
337339
virtual void jsonize(json::Output &Out) override;
338340
virtual void diagnose(SDKNode *Right) override;
341+
342+
// The first argument of the diag is always screening info.
343+
template<typename ...ArgTypes>
344+
void emitDiag(Diag<StringRef, ArgTypes...> ID,
345+
typename detail::PassArgument<ArgTypes>::type... Args) const {
346+
// Don't emit objc decls if we care about swift exclusively
347+
if (Ctx.getOpts().SwiftOnly) {
348+
if (isObjc())
349+
return;
350+
}
351+
Ctx.getDiags().diagnose(SourceLoc(), ID, getScreenInfo(),
352+
std::move(Args)...);
353+
}
339354
};
340355

341356
class SDKNodeRoot: public SDKNode {

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 55 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,9 @@ void swift::ide::api::SDKNodeDeclType::diagnose(SDKNode *Right) {
678678
auto *R = dyn_cast<SDKNodeDeclType>(Right);
679679
if (!R)
680680
return;
681-
auto &Diags = Ctx.getDiags();
682681

683682
if (getDeclKind() != R->getDeclKind()) {
684-
Diags.diagnose(SourceLoc(), diag::decl_kind_changed, getScreenInfo(),
685-
getDeclKindStr(R->getDeclKind()));
683+
emitDiag(diag::decl_kind_changed, getDeclKindStr(R->getDeclKind()));
686684
return;
687685
}
688686

@@ -694,11 +692,9 @@ void swift::ide::api::SDKNodeDeclType::diagnose(SDKNode *Right) {
694692
auto RSuperClass = R->getSuperClassName();
695693
if (!LSuperClass.empty() && LSuperClass != RSuperClass) {
696694
if (RSuperClass.empty()) {
697-
Diags.diagnose(SourceLoc(), diag::super_class_removed, getScreenInfo(),
698-
LSuperClass);
695+
emitDiag(diag::super_class_removed, LSuperClass);
699696
} else if (!contains(R->getClassInheritanceChain(), LSuperClass)) {
700-
Diags.diagnose(SourceLoc(), diag::super_class_changed, getScreenInfo(),
701-
LSuperClass, RSuperClass);
697+
emitDiag(diag::super_class_changed, LSuperClass, RSuperClass);
702698
}
703699
}
704700
break;
@@ -713,11 +709,9 @@ void swift::ide::api::SDKNodeDeclAbstractFunc::diagnose(SDKNode *Right) {
713709
auto *R = dyn_cast<SDKNodeDeclAbstractFunc>(Right);
714710
if (!R)
715711
return;
716-
auto &Diags = Ctx.getDiags();
717712

718713
if (!isThrowing() && R->isThrowing()) {
719-
Diags.diagnose(SourceLoc(), diag::decl_new_attr, getScreenInfo(),
720-
Ctx.buffer("throwing"));
714+
emitDiag(diag::decl_new_attr, Ctx.buffer("throwing"));
721715
}
722716
}
723717

@@ -726,15 +720,13 @@ void swift::ide::api::SDKNodeDeclFunction::diagnose(SDKNode *Right) {
726720
auto *R = dyn_cast<SDKNodeDeclFunction>(Right);
727721
if (!R)
728722
return;
729-
auto &Diags = Ctx.getDiags();
730723
if (getSelfAccessKind() != R->getSelfAccessKind()) {
731-
Diags.diagnose(SourceLoc(), diag::func_self_access_change, getScreenInfo(),
732-
getSelfAccessKind(), R->getSelfAccessKind());
724+
emitDiag(diag::func_self_access_change, getSelfAccessKind(),
725+
R->getSelfAccessKind());
733726
}
734727
if (Ctx.checkingABI()) {
735728
if (hasFixedBinaryOrder() != R->hasFixedBinaryOrder()) {
736-
Ctx.getDiags().diagnose(SourceLoc(), diag::func_has_fixed_order_change,
737-
getScreenInfo(), hasFixedBinaryOrder());
729+
emitDiag(diag::func_has_fixed_order_change, hasFixedBinaryOrder());
738730
}
739731
}
740732
}
@@ -745,8 +737,7 @@ void swift::ide::api::SDKNodeDeclSubscript::diagnose(SDKNode *Right) {
745737
if (!R)
746738
return;
747739
if (hasSetter() && !R->hasSetter()) {
748-
Ctx.getDiags().diagnose(SourceLoc(), diag::removed_setter,
749-
getScreenInfo());
740+
emitDiag(diag::removed_setter);
750741
}
751742
}
752743

@@ -755,16 +746,15 @@ void swift::ide::api::SDKNodeDecl::diagnose(SDKNode *Right) {
755746
auto *RD = dyn_cast<SDKNodeDecl>(Right);
756747
if (!RD)
757748
return;
758-
auto &Diags = Ctx.getDiags();
759749
detectRename(this, RD);
760750
if (isOpen() && !RD->isOpen()) {
761-
Diags.diagnose(SourceLoc(), diag::no_longer_open, getScreenInfo());
751+
emitDiag(diag::no_longer_open);
762752
}
763753

764754
// Diagnose static attribute change.
765755
if (isStatic() ^ RD->isStatic()) {
766-
Diags.diagnose(SourceLoc(), diag::decl_new_attr, getScreenInfo(),
767-
Ctx.buffer(isStatic() ? "not static" : "static"));
756+
emitDiag(diag::decl_new_attr, Ctx.buffer(isStatic() ? "not static" :
757+
"static"));
768758
}
769759

770760
// Diagnose ownership change.
@@ -775,24 +765,22 @@ void swift::ide::api::SDKNodeDecl::diagnose(SDKNode *Right) {
775765
return Ctx.buffer("strong");
776766
return keywordOf(O);
777767
};
778-
Diags.diagnose(SourceLoc(), diag::decl_attr_change, getScreenInfo(),
779-
getOwnershipDescription(getReferenceOwnership()),
780-
getOwnershipDescription(RD->getReferenceOwnership()));
768+
emitDiag(diag::decl_attr_change,
769+
getOwnershipDescription(getReferenceOwnership()),
770+
getOwnershipDescription(RD->getReferenceOwnership()));
781771
}
782772
// Diagnose generic signature change
783773
if (getGenericSignature() != RD->getGenericSignature()) {
784-
Diags.diagnose(SourceLoc(), diag::generic_sig_change, getScreenInfo(),
785-
getGenericSignature(), RD->getGenericSignature());
774+
emitDiag(diag::generic_sig_change,
775+
getGenericSignature(), RD->getGenericSignature());
786776
}
787777
if (isOptional() != RD->isOptional()) {
788778
if (Ctx.checkingABI()) {
789779
// Both adding/removing optional is ABI-breaking.
790-
Diags.diagnose(SourceLoc(), diag::optional_req_changed,
791-
getScreenInfo(), isOptional());
780+
emitDiag(diag::optional_req_changed, isOptional());
792781
} else if (isOptional()) {
793782
// Removing optional is source-breaking.
794-
Diags.diagnose(SourceLoc(), diag::optional_req_changed,
795-
getScreenInfo(), isOptional());
783+
emitDiag(diag::optional_req_changed, isOptional());
796784
}
797785
}
798786

@@ -802,18 +790,15 @@ void swift::ide::api::SDKNodeDecl::diagnose(SDKNode *Right) {
802790
auto Desc = hasDeclAttribute(Info.Kind) ?
803791
Ctx.buffer((llvm::Twine("without ") + Info.Content).str()):
804792
Ctx.buffer((llvm::Twine("with ") + Info.Content).str());
805-
Diags.diagnose(SourceLoc(), diag::decl_new_attr, getScreenInfo(),
806-
Desc);
793+
emitDiag(diag::decl_new_attr, Desc);
807794
}
808795
}
809796

810797
if (Ctx.checkingABI()) {
811798
if (hasFixedBinaryOrder() && RD->hasFixedBinaryOrder() &&
812799
getFixedBinaryOrder() != RD->getFixedBinaryOrder()) {
813-
Ctx.getDiags().diagnose(SourceLoc(), diag::decl_reorder,
814-
getScreenInfo(),
815-
getFixedBinaryOrder(),
816-
RD->getFixedBinaryOrder());
800+
emitDiag(diag::decl_reorder, getFixedBinaryOrder(),
801+
RD->getFixedBinaryOrder());
817802
}
818803
}
819804
}
@@ -824,8 +809,7 @@ void swift::ide::api::SDKNodeDeclOperator::diagnose(SDKNode *Right) {
824809
if (!RO)
825810
return;
826811
if (getDeclKind() != RO->getDeclKind()) {
827-
Ctx.getDiags().diagnose(SourceLoc(), diag::decl_kind_changed, getScreenInfo(),
828-
getDeclKindStr(RO->getDeclKind()));
812+
emitDiag(diag::decl_kind_changed, getDeclKindStr(RO->getDeclKind()));
829813
}
830814
}
831815

@@ -835,18 +819,14 @@ void swift::ide::api::SDKNodeDeclVar::diagnose(SDKNode *Right) {
835819
if (!RV)
836820
return;
837821
if (getSetter() && !RV->getSetter()) {
838-
Ctx.getDiags().diagnose(SourceLoc(), diag::removed_setter,
839-
getScreenInfo());
822+
emitDiag(diag::removed_setter);
840823
}
841824
if (Ctx.checkingABI()) {
842825
if (hasFixedBinaryOrder() != RV->hasFixedBinaryOrder()) {
843-
Ctx.getDiags().diagnose(SourceLoc(), diag::var_has_fixed_order_change,
844-
getScreenInfo(), hasFixedBinaryOrder());
826+
emitDiag(diag::var_has_fixed_order_change, hasFixedBinaryOrder());
845827
}
846828
if (isLet() != RV->isLet()) {
847-
Ctx.getDiags().diagnose(SourceLoc(), diag::var_let_changed,
848-
getScreenInfo(),
849-
isLet());
829+
emitDiag(diag::var_let_changed, isLet());
850830
}
851831
}
852832
}
@@ -857,7 +837,6 @@ static bool shouldDiagnoseType(SDKNodeType *T) {
857837

858838
void swift::ide::api::SDKNodeType::diagnose(SDKNode *Right) {
859839
SDKNode::diagnose(Right);
860-
auto &Diags = Ctx.getDiags();
861840
auto *RT = dyn_cast<SDKNodeType>(Right);
862841
if (!RT || !shouldDiagnoseType(this))
863842
return;
@@ -867,10 +846,10 @@ void swift::ide::api::SDKNodeType::diagnose(SDKNode *Right) {
867846
if (auto *Wit = dyn_cast<SDKNodeTypeWitness>(getParent())) {
868847
auto *Conform = Wit->getParent()->getAs<SDKNodeConformance>();
869848
if (Ctx.checkingABI() && getPrintedName() != RT->getPrintedName()) {
870-
Diags.diagnose(SourceLoc(), diag::type_witness_change,
871-
Conform->getNominalTypeDecl()->getScreenInfo(),
872-
Wit->getWitnessedTypeName(),
873-
getPrintedName(), RT->getPrintedName());
849+
Conform->getNominalTypeDecl()->emitDiag(diag::type_witness_change,
850+
Wit->getWitnessedTypeName(),
851+
getPrintedName(),
852+
RT->getPrintedName());
874853
}
875854
return;
876855
}
@@ -881,35 +860,31 @@ void swift::ide::api::SDKNodeType::diagnose(SDKNode *Right) {
881860
assert(LParent->getKind() == RT->getParent()->getAs<SDKNodeDecl>()->getKind());
882861

883862
if (getPrintedName() != RT->getPrintedName()) {
884-
Diags.diagnose(SourceLoc(), diag::decl_type_change, LParent->getScreenInfo(),
885-
Descriptor, getPrintedName(), RT->getPrintedName());
863+
LParent->emitDiag(diag::decl_type_change,
864+
Descriptor, getPrintedName(), RT->getPrintedName());
886865
}
887866

888867
if (hasDefaultArgument() && !RT->hasDefaultArgument()) {
889-
Diags.diagnose(SourceLoc(), diag::default_arg_removed,
890-
LParent->getScreenInfo(), Descriptor);
868+
LParent->emitDiag(diag::default_arg_removed, Descriptor);
891869
}
892870
if (getParamValueOwnership() != RT->getParamValueOwnership()) {
893-
Diags.diagnose(SourceLoc(), diag::param_ownership_change,
894-
getParent()->getAs<SDKNodeDecl>()->getScreenInfo(),
895-
getTypeRoleDescription(),
896-
getParamValueOwnership(),
897-
RT->getParamValueOwnership());
871+
getParent()->getAs<SDKNodeDecl>()->emitDiag(diag::param_ownership_change,
872+
getTypeRoleDescription(),
873+
getParamValueOwnership(),
874+
RT->getParamValueOwnership());
898875
}
899876
}
900877

901878
void swift::ide::api::SDKNodeTypeFunc::diagnose(SDKNode *Right) {
902879
SDKNode::diagnose(Right);
903-
auto &Diags = Ctx.getDiags();
904880
auto *RT = dyn_cast<SDKNodeTypeFunc>(Right);
905881
if (!RT || !shouldDiagnoseType(this))
906882
return;
907883
assert(isTopLevelType());
908884
if (Ctx.checkingABI() && isEscaping() != RT->isEscaping()) {
909-
Diags.diagnose(SourceLoc(), diag::func_type_escaping_changed,
910-
getParent()->getAs<SDKNodeDecl>()->getScreenInfo(),
911-
getTypeRoleDescription(),
912-
isEscaping());
885+
getParent()->getAs<SDKNodeDecl>()->emitDiag(diag::func_type_escaping_changed,
886+
getTypeRoleDescription(),
887+
isEscaping());
913888
}
914889
}
915890

@@ -980,8 +955,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
980955
// Any order-important decl added to a non-resilient type breaks ABI.
981956
if (auto *D = dyn_cast<SDKNodeDecl>(Right)) {
982957
if (D->hasFixedBinaryOrder()) {
983-
Ctx.getDiags().diagnose(SourceLoc(), diag::decl_added,
984-
D->getScreenInfo());
958+
D->emitDiag(diag::decl_added);
985959
}
986960
}
987961
}
@@ -995,23 +969,21 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
995969
ShouldComplain = false;
996970
}
997971
if (ShouldComplain &&
998-
ProtocolReqWhitelist.count(D->getParent()->getAs<SDKNodeDecl>()->getFullyQualifiedName())) {
972+
ProtocolReqWhitelist.count(D->getParent()->getAs<SDKNodeDecl>()->
973+
getFullyQualifiedName())) {
999974
// Ignore protocol requirement additions if the protocol has been added
1000975
// to the whitelist.
1001976
ShouldComplain = false;
1002977
}
1003978
if (ShouldComplain)
1004-
Ctx.getDiags().diagnose(SourceLoc(), diag::protocol_req_added,
1005-
D->getScreenInfo());
979+
D->emitDiag(diag::protocol_req_added);
1006980
}
1007981
}
1008982
// Diagnose an inherited protocol has been added.
1009983
if (auto *Conf = dyn_cast<SDKNodeConformance>(Right)) {
1010984
auto *TD = Conf->getNominalTypeDecl();
1011985
if (TD->isProtocol()) {
1012-
Ctx.getDiags().diagnose(SourceLoc(), diag::conformance_added,
1013-
TD->getScreenInfo(),
1014-
Conf->getName());
986+
TD->emitDiag(diag::conformance_added, Conf->getName());
1015987
}
1016988
}
1017989

@@ -1021,17 +993,16 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
1021993
Left->annotate(NodeAnnotation::Removed);
1022994
if (auto *LT = dyn_cast<SDKNodeType>(Left)) {
1023995
if (auto *AT = dyn_cast<SDKNodeDeclAssociatedType>(LT->getParent())) {
1024-
Ctx.getDiags().diagnose(SourceLoc(),
1025-
diag::default_associated_type_removed,
1026-
AT->getScreenInfo(), LT->getPrintedName());
996+
AT->emitDiag(diag::default_associated_type_removed,
997+
LT->getPrintedName());
1027998
}
1028999
}
10291000
// Diagnose a protocol conformance has been removed.
10301001
if (auto *Conf = dyn_cast<SDKNodeConformance>(Left)) {
10311002
auto *TD = Conf->getNominalTypeDecl();
1032-
Ctx.getDiags().diagnose(SourceLoc(), diag::conformance_removed,
1033-
TD->getScreenInfo(), Conf->getName(),
1034-
TD->isProtocol());
1003+
TD->emitDiag(diag::conformance_removed,
1004+
Conf->getName(),
1005+
TD->isProtocol());
10351006
}
10361007
return;
10371008
case NodeMatchReason::FuncToProperty:
@@ -1807,11 +1778,10 @@ class DiagnosisEmitter : public SDKNodeVisitor {
18071778
UpdatedNodesMap &UpdateMap;
18081779
NodeMap &TypeAliasUpdateMap;
18091780
TypeMemberDiffVector &MemberChanges;
1810-
DiagnosticEngine &Diags;
18111781
DiagnosisEmitter(SDKContext &Ctx):
18121782
UpdateMap(Ctx.getNodeUpdateMap()),
18131783
TypeAliasUpdateMap(Ctx.getTypeAliasUpdateMap()),
1814-
MemberChanges(Ctx.getTypeMemberDiffs()), Diags(Ctx.getDiags()) {}
1784+
MemberChanges(Ctx.getTypeMemberDiffs()) {}
18151785

18161786
public:
18171787
static void diagnosis(NodePtr LeftRoot, NodePtr RightRoot,
@@ -1866,7 +1836,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
18661836
return;
18671837
if (auto *Added = findAddedDecl(Node)) {
18681838
if (Node->getDeclKind() != DeclKind::Constructor) {
1869-
Diags.diagnose(SourceLoc(), diag::moved_decl, Node->getScreenInfo(),
1839+
Node->emitDiag(diag::moved_decl,
18701840
Ctx.buffer((Twine(getDeclKindStr(Added->getDeclKind())) + " " +
18711841
Added->getFullyQualifiedName()).str()));
18721842
return;
@@ -1878,7 +1848,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
18781848
auto It = std::find_if(MemberChanges.begin(), MemberChanges.end(),
18791849
[&](TypeMemberDiffItem &Item) { return Item.usr == Node->getUsr(); });
18801850
if (It != MemberChanges.end()) {
1881-
Diags.diagnose(SourceLoc(), diag::renamed_decl, Node->getScreenInfo(),
1851+
Node->emitDiag(diag::renamed_decl,
18821852
Ctx.buffer((Twine(getDeclKindStr(Node->getDeclKind())) + " " +
18831853
It->newTypeName + "." + It->newPrintedName).str()));
18841854
return;
@@ -1889,7 +1859,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
18891859
// refine diagnostics message instead of showing the type alias has been
18901860
// removed.
18911861
if (TypeAliasUpdateMap.find((SDKNode*)Node) != TypeAliasUpdateMap.end()) {
1892-
Diags.diagnose(SourceLoc(), diag::raw_type_change, Node->getScreenInfo(),
1862+
Node->emitDiag(diag::raw_type_change,
18931863
Node->getAs<SDKNodeDeclTypeAlias>()->getUnderlyingType()->getPrintedName(),
18941864
TypeAliasUpdateMap[(SDKNode*)Node]->getAs<SDKNodeDeclType>()->
18951865
getRawValueType()->getPrintedName());
@@ -1911,13 +1881,12 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
19111881
}
19121882
if (FoundInSuperclass)
19131883
return;
1914-
Diags.diagnose(SourceLoc(), diag::removed_decl, Node->getScreenInfo(),
1915-
Node->isDeprecated());
1884+
Node->emitDiag(diag::removed_decl, Node->isDeprecated());
19161885
return;
19171886
}
19181887
case NodeAnnotation::Rename: {
19191888
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
1920-
Diags.diagnose(SourceLoc(), diag::renamed_decl, Node->getScreenInfo(),
1889+
Node->emitDiag(diag::renamed_decl,
19211890
Ctx.buffer((Twine(getDeclKindStr(Count->getDeclKind())) + " " +
19221891
Count->getFullyQualifiedName()).str()));
19231892
return;

0 commit comments

Comments
 (0)