1
- // ===--- swift-api-digester.cpp - API change detector -------===//
2
- // //
3
- // // This source file is part of the Swift.org open source project
4
- // //
5
- // // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6
- // // Licensed under Apache License v2.0 with Runtime Library Exception
7
- // //
8
- // // See http://swift.org/LICENSE.txt for license information
9
- // // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
- // //
11
- // // ===----------------------------------------------------------------------===//
1
+ // ===--- swift-api-digester.cpp - API change detector --------------------- ===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See http://swift.org/LICENSE.txt for license information
9
+ // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ // ===----------------------------------------------------------------------===//
12
12
13
13
// swift-api-digester is a test utility to detect source-breaking API changes
14
- // during the evolution of a swift library. The tool works on two phases:
15
- // (1) dumping library contents as a json file, and (2) comparing two json
14
+ // during the evolution of a Swift library. The tool works on two phases:
15
+ // (1) dumping library contents as a JSON file, and (2) comparing two JSON
16
16
// files textually to report interesting changes.
17
17
//
18
18
// During phase (1), the api-digester looks up every declarations inside
19
19
// a module and outputs a singly-rooted tree that encloses interesting
20
20
// details of the API level.
21
21
//
22
- // During phase (2), api-digester applies structure-information comparision
22
+ // During phase (2), api-digester applies structure-information comparison
23
23
// algorithms on two given singly root trees, trying to figure out, as
24
24
// precise as possible, the branches/leaves in the trees that differ from
25
25
// each other. Further analysis decides whether the changed leaves/branches
@@ -78,7 +78,7 @@ ModuleNames("module", llvm::cl::ZeroOrMore, llvm::cl::desc("Names of modules"));
78
78
79
79
static llvm::cl::opt<std::string>
80
80
ModuleList (" module-list-file" ,
81
- llvm::cl::desc (" File containing new-line separated list of modules" ));
81
+ llvm::cl::desc (" File containing a new-line separated list of modules" ));
82
82
83
83
static llvm::cl::opt<std::string>
84
84
OutputFile (" o" , llvm::cl::desc(" Output file" ));
@@ -114,21 +114,21 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
114
114
llvm::cl::values(
115
115
clEnumValN (ActionType::DumpSDK,
116
116
" dump-sdk" ,
117
- " Dump SDK content to json file" ),
117
+ " Dump SDK content to JSON file" ),
118
118
clEnumValN(ActionType::DumpSwiftModules,
119
119
" dump-swift" ,
120
120
" dump swift modules in SDK" ),
121
121
clEnumValN(ActionType::CompareSDKs,
122
122
" compare-sdk" ,
123
- " Compare SDK content in json file" ),
123
+ " Compare SDK content in JSON file" ),
124
124
clEnumValN(ActionType::DiagnoseSDKs,
125
125
" diagnose-sdk" ,
126
- " Diagnose SDK content in json file" ),
126
+ " Diagnose SDK content in JSON file" ),
127
127
clEnumValEnd));
128
128
129
129
static llvm::cl::list<std::string>
130
130
SDKJsonPaths (" input-paths" ,
131
- llvm::cl::desc (" The SDK contents under conparison " ));
131
+ llvm::cl::desc (" The SDK contents under comparison " ));
132
132
133
133
static llvm::cl::list<std::string>
134
134
ApisPrintUsrs (" api-usrs" ,
@@ -142,7 +142,7 @@ IgnoreRemovedDeclUSRs("ignored-usrs",
142
142
143
143
static llvm::cl::opt<std::string>
144
144
SwiftVersion (" swift-version" ,
145
- llvm::cl::desc (" The swift compiler version to invoke" ));
145
+ llvm::cl::desc (" The Swift compiler version to invoke" ));
146
146
}
147
147
148
148
namespace {
@@ -196,7 +196,6 @@ class SDKNodeVisitor {
196
196
struct MatchedNodeListener {
197
197
virtual void foundMatch (NodePtr Left, NodePtr Right) = 0;
198
198
virtual void foundRemoveAddMatch (NodePtr Removed, NodePtr Added) {}
199
- virtual bool isMatched (NodePtr Left, NodePtr Right) {return false ;};
200
199
virtual ~MatchedNodeListener () = default ;
201
200
};
202
201
@@ -209,7 +208,7 @@ struct NodeMatcher {
209
208
#define KEY (NAME ) static const char * Key_##NAME = #NAME;
210
209
#include " DigesterEnums.def"
211
210
212
- // The node kind apparing in the tree that describes the content of the SDK
211
+ // The node kind appearing in the tree that describes the content of the SDK
213
212
enum class SDKNodeKind {
214
213
#define NODE_KIND (NAME ) NAME,
215
214
#include " DigesterEnums.def"
@@ -301,9 +300,7 @@ class SDKNode {
301
300
StringRef getPrintedName () const { return PrintedName; }
302
301
void removeChild (ChildIt CI) { Children.erase (CI); }
303
302
ChildIt getChildBegin () const { return Children.begin (); }
304
- ChildIt getChildEnd () const { return Children.end (); }
305
303
void annotate (NodeAnnotation Anno) { Annotations.insert (Anno); }
306
- bool isName (StringRef N) const { return getName () == N; }
307
304
NodePtr getParent () const { return Parent; };
308
305
unsigned getChildrenCount () const { return Children.size (); }
309
306
NodePtr childAt (unsigned I) const ;
@@ -470,28 +467,28 @@ class SDKNodeVectorViewer {
470
467
llvm::function_ref<bool (NodePtr)> Selector;
471
468
typedef ArrayRef<SDKNode*>::const_iterator VectorIt;
472
469
VectorIt getNext (VectorIt Start);
473
- class ViwerIterator ;
470
+ class ViewerIterator ;
474
471
475
472
public:
476
473
SDKNodeVectorViewer (ArrayRef<SDKNode*> Collection,
477
474
llvm::function_ref<bool (NodePtr)> Selector) :
478
475
Collection (Collection),
479
476
Selector (Selector) {}
480
- ViwerIterator begin ();
481
- ViwerIterator end ();
477
+ ViewerIterator begin ();
478
+ ViewerIterator end ();
482
479
};
483
480
484
- class SDKNodeVectorViewer ::ViwerIterator :
481
+ class SDKNodeVectorViewer ::ViewerIterator :
485
482
public std::iterator<std::input_iterator_tag, VectorIt> {
486
483
SDKNodeVectorViewer &Viewer;
487
484
VectorIt P;
488
485
public:
489
- ViwerIterator (SDKNodeVectorViewer &Viewer, VectorIt P) : Viewer(Viewer), P(P) {}
490
- ViwerIterator (const ViwerIterator & mit) : Viewer(mit.Viewer), P(mit.P) {}
491
- ViwerIterator & operator ++();
492
- ViwerIterator operator ++(int ) {ViwerIterator tmp (*this ); operator ++(); return tmp;}
493
- bool operator ==(const ViwerIterator & rhs) {return P==rhs.P ;}
494
- bool operator !=(const ViwerIterator & rhs) {return P!=rhs.P ;}
486
+ ViewerIterator (SDKNodeVectorViewer &Viewer, VectorIt P) : Viewer(Viewer), P(P) {}
487
+ ViewerIterator (const ViewerIterator & mit) : Viewer(mit.Viewer), P(mit.P) {}
488
+ ViewerIterator & operator ++();
489
+ ViewerIterator operator ++(int ) {ViewerIterator tmp (*this ); operator ++(); return tmp;}
490
+ bool operator ==(const ViewerIterator & rhs) {return P==rhs.P ;}
491
+ bool operator !=(const ViewerIterator & rhs) {return P!=rhs.P ;}
495
492
const NodePtr& operator *() {return *P;}
496
493
};
497
494
@@ -503,18 +500,18 @@ SDKNodeVectorViewer::getNext(VectorIt Start) {
503
500
return Collection.end ();
504
501
}
505
502
506
- SDKNodeVectorViewer::ViwerIterator &
507
- SDKNodeVectorViewer::ViwerIterator ::operator ++() {
503
+ SDKNodeVectorViewer::ViewerIterator &
504
+ SDKNodeVectorViewer::ViewerIterator ::operator ++() {
508
505
P = Viewer.getNext (P + 1 );
509
506
return *this ;
510
507
}
511
508
512
- SDKNodeVectorViewer::ViwerIterator SDKNodeVectorViewer::begin () {
513
- return ViwerIterator (*this , getNext (Collection.begin ()));
509
+ SDKNodeVectorViewer::ViewerIterator SDKNodeVectorViewer::begin () {
510
+ return ViewerIterator (*this , getNext (Collection.begin ()));
514
511
}
515
512
516
- SDKNodeVectorViewer::ViwerIterator SDKNodeVectorViewer::end () {
517
- return ViwerIterator (*this , Collection.end ());
513
+ SDKNodeVectorViewer::ViewerIterator SDKNodeVectorViewer::end () {
514
+ return ViewerIterator (*this , Collection.end ());
518
515
}
519
516
520
517
class SDKNodeDecl ;
@@ -867,15 +864,6 @@ class SDKNodeDumpVisitor : public SDKNodeVisitor {
867
864
SDKNodeDumpVisitor () {};
868
865
};
869
866
870
- class DumpMatchListener : public MatchedNodeListener {
871
- void foundMatch (NodePtr Left, NodePtr Right) override {
872
- llvm::outs () << Left->getName () << " ->" << Right->getName () << " \n " ;
873
- };
874
- bool isMatched (NodePtr Left, NodePtr Right) override {
875
- return true ;
876
- };
877
- };
878
-
879
867
static StringRef getPrintedName (Type Ty) {
880
868
std::string S;
881
869
llvm::raw_string_ostream OS (S);
@@ -1008,7 +996,7 @@ case SDKNodeKind::X: \
1008
996
}
1009
997
1010
998
// Recursively construct a node that represents a type, for instance,
1011
- // representing the the return value type of a function decl.
999
+ // representing the return value type of a function decl.
1012
1000
static NodeUniquePtr constructTypeNode (Type T) {
1013
1001
NodeUniquePtr Root = SDKNodeInitInfo (T).createSDKNode (SDKNodeKind::TypeNominal);
1014
1002
@@ -1182,15 +1170,15 @@ class SwiftDeclCollector : public VisibleDeclConsumer {
1182
1170
RootNode = std::move (Pair.second );
1183
1171
}
1184
1172
1185
- // Serialize the content of all roots to a given file using json format.
1173
+ // Serialize the content of all roots to a given file using JSON format.
1186
1174
void serialize (StringRef Filename) {
1187
1175
std::error_code EC;
1188
1176
llvm::raw_fd_ostream fs (Filename, EC, llvm::sys::fs::F_None);
1189
1177
emitSDKNodeRoot (fs, RootNode);
1190
1178
}
1191
1179
1192
1180
// After collecting decls, either from imported modules or from a previously
1193
- // serialized json file, using this function to get the root of the SDK.
1181
+ // serialized JSON file, using this function to get the root of the SDK.
1194
1182
NodePtr getSDKRoot () {
1195
1183
return RootNode.get ();
1196
1184
}
@@ -1261,7 +1249,7 @@ class SwiftDeclCollector : public VisibleDeclConsumer {
1261
1249
namespace swift {
1262
1250
namespace json {
1263
1251
// In the namespace of swift::json, we define several functions so that the
1264
- // json serializer will know how to interpret and dump types defined in this
1252
+ // JSON serializer will know how to interpret and dump types defined in this
1265
1253
// file.
1266
1254
template <>
1267
1255
struct ScalarEnumerationTraits <SDKNodeKind> {
@@ -1395,7 +1383,7 @@ parseJsonEmit(StringRef FileName) {
1395
1383
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
1396
1384
llvm::MemoryBuffer::getFileOrSTDIN (FileName);
1397
1385
if (!FileBufOrErr) {
1398
- llvm_unreachable (" Failed to read json file" );
1386
+ llvm_unreachable (" Failed to read JSON file" );
1399
1387
}
1400
1388
StringRef Buffer = FileBufOrErr->get ()->getBuffer ();
1401
1389
llvm::SourceMgr SM;
@@ -1712,7 +1700,7 @@ class SameNameNodeMatcher : public NodeMatcher {
1712
1700
PrintedNameAndUSR,
1713
1701
};
1714
1702
1715
- // Given two sdk nodes, figure out the reason for why they have the same name.
1703
+ // Given two SDK nodes, figure out the reason for why they have the same name.
1716
1704
Optional<NameMatchKind> getNameMatchKind (SDKNode *L, SDKNode *R) {
1717
1705
if (L->getKind () != R->getKind ())
1718
1706
return None;
@@ -1751,7 +1739,7 @@ class SameNameNodeMatcher : public NodeMatcher {
1751
1739
}
1752
1740
}
1753
1741
1754
- // Given a list and a priority, find the best matched candidate sdk node.
1742
+ // Given a list and a priority, find the best matched candidate SDK node.
1755
1743
SDKNode* findBestNameMatch (ArrayRef<NameMatchCandidate> Candidates,
1756
1744
ArrayRef<NameMatchKind> Kinds) {
1757
1745
for (auto Kind : Kinds)
@@ -1902,7 +1890,7 @@ static void detectRename(NodePtr L, NodePtr R) {
1902
1890
// This is first pass on two given SDKNode trees. This pass removes the common part
1903
1891
// of two versions of SDK, leaving only the changed part.
1904
1892
class PrunePass : public MatchedNodeListener , public SDKTreeDiffPass {
1905
- static void removeCommomChildren (NodePtr Left, NodePtr Right) {
1893
+ static void removeCommonChildren (NodePtr Left, NodePtr Right) {
1906
1894
llvm::SmallPtrSet<NodePtr, 16 > LeftToRemove;
1907
1895
llvm::SmallPtrSet<NodePtr, 16 > RightToRemove;
1908
1896
for (auto &LC : Left->getChildren ()) {
@@ -1959,7 +1947,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
1959
1947
// If the matched nodes are both modules, remove the contained
1960
1948
// type decls that are identical. If the matched nodes are both type decls,
1961
1949
// remove the contained function decls that are identical.
1962
- removeCommomChildren (Left, Right);
1950
+ removeCommonChildren (Left, Right);
1963
1951
NodeVector LeftChildren;
1964
1952
NodeVector RightChildren;
1965
1953
Left->collectChildren (LeftChildren);
@@ -1975,7 +1963,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
1975
1963
case SDKNodeKind::TypeAlias:
1976
1964
case SDKNodeKind::TypeFunc:
1977
1965
case SDKNodeKind::TypeNominal: {
1978
- // If mactched nodes are both function/var/TypeAlias decls, mapping their
1966
+ // If matched nodes are both function/var/TypeAlias decls, mapping their
1979
1967
// parameters sequentially.
1980
1968
SequentialNodeMatcher (Left->getChildren (),
1981
1969
Right->getChildren (), *this ).match ();
@@ -2097,8 +2085,6 @@ class TypeMemberDiffFinder : public SDKNodeVisitor {
2097
2085
2098
2086
};
2099
2087
2100
- typedef std::unique_ptr<NodePairVector> RenamedNodes;
2101
-
2102
2088
// Given a condition, search whether a node satisfies that condition exists
2103
2089
// in a tree.
2104
2090
class SearchVisitor : public SDKNodeVisitor {
@@ -2233,12 +2219,12 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
2233
2219
}
2234
2220
};
2235
2221
2236
- // DiffItem describes how an element in sdk evolves in a way that migrator can
2237
- // read conveniently. Each DiffItem corresponds to one json element and contains
2222
+ // DiffItem describes how an element in SDK evolves in a way that migrator can
2223
+ // read conveniently. Each DiffItem corresponds to one JSON element and contains
2238
2224
// sub fields explaining how migrator can assist client code to cope with such
2239
- // sdk change. For instance, the following first json element describes an unwrap
2225
+ // SDK change. For instance, the following first JSON element describes an unwrap
2240
2226
// optional change in the first parameter of function "c:@F@CTTextTabGetOptions".
2241
- // Similarly, the second json element describes a type parameter down cast in the
2227
+ // Similarly, the second JSON element describes a type parameter down cast in the
2242
2228
// second parameter of function "c:objc(cs)NSXMLDocument(im)insertChildren:atIndex:".
2243
2229
// We keep both usrs because in the future this may support auto-rename.
2244
2230
class DiffItem {
@@ -3391,21 +3377,21 @@ static int prepareForDump(const char *Main,
3391
3377
}
3392
3378
3393
3379
if (Modules.empty ()) {
3394
- llvm::errs () << " Need to specifiy -include-all or -module <name>\n " ;
3380
+ llvm::errs () << " Need to specify -include-all or -module <name>\n " ;
3395
3381
return 1 ;
3396
3382
}
3397
3383
return 0 ;
3398
3384
}
3399
3385
3400
- static void readIgnoredUsrs (llvm::StringSet<> &IgoredUsrs ) {
3386
+ static void readIgnoredUsrs (llvm::StringSet<> &IgnoredUsrs ) {
3401
3387
StringRef Path = options::IgnoreRemovedDeclUSRs;
3402
3388
if (Path.empty ())
3403
3389
return ;
3404
3390
if (!fs::exists (Path)) {
3405
3391
llvm::errs () << Path << " does not exist.\n " ;
3406
3392
return ;
3407
3393
}
3408
- readFileLineByLine (Path, IgoredUsrs );
3394
+ readFileLineByLine (Path, IgnoredUsrs );
3409
3395
}
3410
3396
3411
3397
int main (int argc, char *argv[]) {
0 commit comments