Skip to content

Commit 4e84693

Browse files
authored
swift-api-digester: print header file names along with source-breaking changes. (#14788)
When diagnosing API source-breaking changes, we should also output the header file name from where the affected Clang declaration is defined. This may expedite screening process.
1 parent c234759 commit 4e84693

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

test/api-digester/Inputs/APINotesLeft/APINotesTest.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ extern int ANTGlobalValue;
1010
+(void) plusPrint;
1111
-(int) getPropertyA;
1212
@end
13+
14+
@protocol ObjcProt
15+
-(void) ProtMemberFunc;
16+
-(void) ProtMemberFunc2;
17+
-(void) ProtMemberFunc3;
18+
@end

test/api-digester/Inputs/APINotesRight/APINotesTest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ extern int ANTGlobalValue;
1010
+(void) plusPrint;
1111
@property int PropertyA;
1212
@end
13+
14+
@protocol ObjcProt
15+
-(void) ProtMemberFunc;
16+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
/* Removed Decls */
3+
APINotesTest(APINotesTest.h): Var OldType.oldMember has been removed
4+
APINotesTest(APINotesTest.h): Func ObjcProt.protMemberFunc2() has been removed
5+
APINotesTest(APINotesTest.h): Func ObjcProt.protMemberFunc3() has been removed
6+
APINotesTest(APINotesTest.h): Func SwiftTypeWithMethodLeft.getPropertyA() has been removed
7+
8+
/* Moved Decls */
9+
10+
/* Renamed Decls */
11+
APINotesTest(APINotesTest.h): Protocol SwiftTypeWithMethodLeft has been renamed to Protocol SwiftTypeWithMethodRight
12+
13+
/* Type Changes */
14+
15+
/* Decl Attribute changes */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t.mod)
3+
// RUN: %empty-directory(%t.sdk)
4+
// RUN: %empty-directory(%t.module-cache)
5+
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump1.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %S/Inputs/APINotesLeft
6+
// RUN: %api-digester -dump-sdk -module APINotesTest -o %t.dump2.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3 -I %S/Inputs/APINotesRight
7+
// RUN: %api-digester -diagnose-sdk -print-module -input-paths %t.dump1.json -input-paths %t.dump2.json > %t.result
8+
// RUN: diff -u %S/Outputs/apinotes-diags.txt %t.result

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ class SDKNodeDecl : public SDKNode {
383383
StringRef getUsr() const { return Usr; }
384384
StringRef getLocation() const { return Location; }
385385
StringRef getModuleName() const {return ModuleName;}
386+
StringRef getHeaderName() const;
386387
void addDeclAttribute(SDKDeclAttrKind DAKind);
387388
ArrayRef<SDKDeclAttrKind> getDeclAttributes() const;
388389
swift::Ownership getOwnership() const { return swift::Ownership(Ownership); }
@@ -396,6 +397,12 @@ class SDKNodeDecl : public SDKNode {
396397
bool isStatic() const { return IsStatic; };
397398
};
398399

400+
StringRef SDKNodeDecl::getHeaderName() const {
401+
if (Location.empty())
402+
return StringRef();
403+
return llvm::sys::path::filename(Location.split(":").first);
404+
}
405+
399406
class SDKNodeRoot :public SDKNode {
400407
/// This keeps track of all decl descendants with USRs.
401408
llvm::StringMap<llvm::SmallSetVector<SDKNodeDecl*, 2>> DescendantDeclTable;
@@ -1540,12 +1547,12 @@ namespace swift {
15401547
StringRef Usr = D->getUsr();
15411548
StringRef Location = D->getLocation();
15421549
StringRef ModuleName = D->getModuleName();
1543-
15441550
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_declKind).data(), DK);
15451551
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_usr).data(), Usr);
15461552
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_location).data(), Location);
15471553
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_moduleName).data(),
15481554
ModuleName);
1555+
15491556
if (auto isStatic = D->isStatic())
15501557
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_static).data(), isStatic);
15511558

@@ -2665,13 +2672,24 @@ class DiagnosisEmitter : public SDKNodeVisitor {
26652672
}
26662673
};
26672674

2668-
struct DiagBase {
2675+
struct MetaInfo {
26692676
StringRef ModuleName;
2670-
DiagBase(StringRef ModuleName): ModuleName(ModuleName) {}
2677+
StringRef HeaderName;
2678+
MetaInfo(const SDKNodeDecl *Node):
2679+
ModuleName(Node->getModuleName()), HeaderName(Node->getHeaderName()) {}
2680+
};
2681+
2682+
struct DiagBase {
2683+
MetaInfo Info;
2684+
DiagBase(MetaInfo Info): Info(Info) {}
26712685
virtual ~DiagBase() = default;
26722686
void outputModule() const {
2673-
if (options::PrintModule)
2674-
llvm::outs() << ModuleName << ": ";
2687+
if (options::PrintModule) {
2688+
llvm::outs() << Info.ModuleName;
2689+
if (!Info.HeaderName.empty())
2690+
llvm::outs() << "(" << Info.HeaderName << ")";
2691+
llvm::outs() << ": ";
2692+
}
26752693
}
26762694
virtual void output() const = 0;
26772695
};
@@ -2680,8 +2698,8 @@ class DiagnosisEmitter : public SDKNodeVisitor {
26802698
DeclKind Kind;
26812699
StringRef Name;
26822700
bool IsDeprecated;
2683-
RemovedDeclDiag(StringRef ModuleName, DeclKind Kind, StringRef Name,
2684-
bool IsDeprecated): DiagBase(ModuleName), Kind(Kind),
2701+
RemovedDeclDiag(MetaInfo Info, DeclKind Kind, StringRef Name,
2702+
bool IsDeprecated): DiagBase(Info), Kind(Kind),
26852703
Name(Name), IsDeprecated(IsDeprecated) {}
26862704
bool operator<(RemovedDeclDiag Other) const;
26872705
void output() const override;
@@ -2693,9 +2711,9 @@ class DiagnosisEmitter : public SDKNodeVisitor {
26932711
DeclKind AddedKind;
26942712
StringRef RemovedName;
26952713
StringRef AddedName;
2696-
MovedDeclDiag(StringRef ModuleName, DeclKind RemovedKind, DeclKind AddedKind,
2714+
MovedDeclDiag(MetaInfo Info, DeclKind RemovedKind, DeclKind AddedKind,
26972715
StringRef RemovedName, StringRef AddedName):
2698-
DiagBase(ModuleName), RemovedKind(RemovedKind), AddedKind(AddedKind),
2716+
DiagBase(Info), RemovedKind(RemovedKind), AddedKind(AddedKind),
26992717
RemovedName(RemovedName), AddedName(AddedName) {}
27002718
bool operator<(MovedDeclDiag other) const;
27012719
void output() const override;
@@ -2707,9 +2725,9 @@ class DiagnosisEmitter : public SDKNodeVisitor {
27072725
DeclKind KindAfter;
27082726
StringRef NameBefore;
27092727
StringRef NameAfter;
2710-
RenamedDeclDiag(StringRef ModuleName, DeclKind KindBefore, DeclKind KindAfter,
2728+
RenamedDeclDiag(MetaInfo Info, DeclKind KindBefore, DeclKind KindAfter,
27112729
StringRef NameBefore, StringRef NameAfter):
2712-
DiagBase(ModuleName),
2730+
DiagBase(Info),
27132731
KindBefore(KindBefore), KindAfter(KindAfter),
27142732
NameBefore(NameBefore), NameAfter(NameAfter) {}
27152733
bool operator<(RenamedDeclDiag Other) const;
@@ -2722,12 +2740,12 @@ class DiagnosisEmitter : public SDKNodeVisitor {
27222740
StringRef DeclName;
27232741
StringRef AttrBefore;
27242742
StringRef AttrAfter;
2725-
DeclAttrDiag(StringRef ModuleName, DeclKind Kind, StringRef DeclName,
2743+
DeclAttrDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
27262744
StringRef AttrBefore, StringRef AttrAfter):
2727-
DiagBase(ModuleName), Kind(Kind), DeclName(DeclName),
2745+
DiagBase(Info), Kind(Kind), DeclName(DeclName),
27282746
AttrBefore(AttrBefore), AttrAfter(AttrAfter) {}
2729-
DeclAttrDiag(StringRef ModuleName, DeclKind Kind, StringRef DeclName,
2730-
StringRef AttrAfter): DeclAttrDiag(ModuleName, Kind, DeclName,
2747+
DeclAttrDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
2748+
StringRef AttrAfter): DeclAttrDiag(Info, Kind, DeclName,
27312749
StringRef(), AttrAfter) {}
27322750

27332751
bool operator<(DeclAttrDiag Other) const;
@@ -2741,9 +2759,9 @@ class DiagnosisEmitter : public SDKNodeVisitor {
27412759
StringRef TypeNameBefore;
27422760
StringRef TypeNameAfter;
27432761
StringRef Description;
2744-
DeclTypeChangeDiag(StringRef ModuleName, DeclKind Kind, StringRef DeclName,
2762+
DeclTypeChangeDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
27452763
StringRef TypeNameBefore, StringRef TypeNameAfter,
2746-
StringRef Description): DiagBase(ModuleName),
2764+
StringRef Description): DiagBase(Info),
27472765
Kind(Kind), DeclName(DeclName), TypeNameBefore(TypeNameBefore),
27482766
TypeNameAfter(TypeNameAfter), Description(Description) {}
27492767
bool operator<(DeclTypeChangeDiag Other) const;
@@ -2888,6 +2906,7 @@ void DiagnosisEmitter::diagnosis(NodePtr LeftRoot, NodePtr RightRoot,
28882906
void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
28892907
assert(Node->isAnnotatedAs(Anno));
28902908
auto &Ctx = Node->getSDKContext();
2909+
MetaInfo ScreenInfo(Node);
28912910
switch(Anno) {
28922911
case NodeAnnotation::Removed: {
28932912
// If we can find a type alias decl with the same name of this type, we
@@ -2896,7 +2915,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
28962915
return;
28972916
if (auto *Added = findAddedDecl(Node)) {
28982917
if (Node->getDeclKind() != DeclKind::Constructor) {
2899-
MovedDecls.Diags.emplace_back(Node->getModuleName(),
2918+
MovedDecls.Diags.emplace_back(ScreenInfo,
29002919
Node->getDeclKind(),
29012920
Added->getDeclKind(),
29022921
Node->getFullyQualifiedName(),
@@ -2920,36 +2939,36 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
29202939
}
29212940
if (FoundInSuperclass)
29222941
return;
2923-
RemovedDecls.Diags.emplace_back(Node->getModuleName(),
2942+
RemovedDecls.Diags.emplace_back(ScreenInfo,
29242943
Node->getDeclKind(),
29252944
Node->getFullyQualifiedName(),
29262945
Node->isDeprecated());
29272946
return;
29282947
}
29292948
case NodeAnnotation::Rename: {
29302949
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
2931-
RenamedDecls.Diags.emplace_back(Node->getModuleName(),
2950+
RenamedDecls.Diags.emplace_back(ScreenInfo,
29322951
Node->getDeclKind(), Count->getDeclKind(),
29332952
Node->getFullyQualifiedName(),
29342953
Count->getFullyQualifiedName());
29352954
return;
29362955
}
29372956
case NodeAnnotation::NowMutating: {
2938-
AttrChangedDecls.Diags.emplace_back(Node->getModuleName(),
2957+
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
29392958
Node->getDeclKind(),
29402959
Node->getFullyQualifiedName(),
29412960
Ctx.buffer("mutating"));
29422961
return;
29432962
}
29442963
case NodeAnnotation::NowThrowing: {
2945-
AttrChangedDecls.Diags.emplace_back(Node->getModuleName(),
2964+
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
29462965
Node->getDeclKind(),
29472966
Node->getFullyQualifiedName(),
29482967
Ctx.buffer("throwing"));
29492968
return;
29502969
}
29512970
case NodeAnnotation::StaticChange: {
2952-
AttrChangedDecls.Diags.emplace_back(Node->getModuleName(),
2971+
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
29532972
Node->getDeclKind(),
29542973
Node->getFullyQualifiedName(),
29552974
Ctx.buffer(Node->isStatic() ? "not static" : "static"));
@@ -2967,7 +2986,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
29672986
llvm_unreachable("Unhandled Ownership in switch.");
29682987
};
29692988
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
2970-
AttrChangedDecls.Diags.emplace_back(Node->getModuleName(),
2989+
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
29712990
Node->getDeclKind(),
29722991
Node->getFullyQualifiedName(),
29732992
getOwnershipDescription(Node->getOwnership()),
@@ -2991,6 +3010,7 @@ void DiagnosisEmitter::visitType(SDKNodeType *Node) {
29913010
auto *Parent = dyn_cast<SDKNodeDecl>(Node->getParent());
29923011
if (!Parent || Parent->isSDKPrivate())
29933012
return;
3013+
MetaInfo ScreenInfo(Parent);
29943014
SDKContext &Ctx = Node->getSDKContext();
29953015
if (Node->isAnnotatedAs(NodeAnnotation::Updated)) {
29963016
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeType>();
@@ -3003,7 +3023,7 @@ void DiagnosisEmitter::visitType(SDKNodeType *Node) {
30033023
SDKNodeAbstractFunc::getTypeRoleDescription(Ctx, Parent->getChildIndex(Node)) :
30043024
Ctx.buffer("declared");
30053025
if (Node->getPrintedName() != Count->getPrintedName())
3006-
TypeChangedDecls.Diags.emplace_back(Parent->getModuleName(),
3026+
TypeChangedDecls.Diags.emplace_back(ScreenInfo,
30073027
Parent->getDeclKind(),
30083028
Parent->getFullyQualifiedName(),
30093029
Node->getPrintedName(),

0 commit comments

Comments
 (0)