Skip to content

Commit 6a61476

Browse files
practicalswiftnkcsgexi
authored andcommitted
Clean up swift-api-digester: Remove dead code. Fix typos. Fix headers. (#5280)
1 parent 3cbdcef commit 6a61476

File tree

1 file changed

+54
-68
lines changed

1 file changed

+54
-68
lines changed

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

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
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+
//===----------------------------------------------------------------------===//
1212

1313
// 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
1616
// files textually to report interesting changes.
1717
//
1818
// During phase (1), the api-digester looks up every declarations inside
1919
// a module and outputs a singly-rooted tree that encloses interesting
2020
// details of the API level.
2121
//
22-
// During phase (2), api-digester applies structure-information comparision
22+
// During phase (2), api-digester applies structure-information comparison
2323
// algorithms on two given singly root trees, trying to figure out, as
2424
// precise as possible, the branches/leaves in the trees that differ from
2525
// 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"));
7878

7979
static llvm::cl::opt<std::string>
8080
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"));
8282

8383
static llvm::cl::opt<std::string>
8484
OutputFile("o", llvm::cl::desc("Output file"));
@@ -114,21 +114,21 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
114114
llvm::cl::values(
115115
clEnumValN(ActionType::DumpSDK,
116116
"dump-sdk",
117-
"Dump SDK content to json file"),
117+
"Dump SDK content to JSON file"),
118118
clEnumValN(ActionType::DumpSwiftModules,
119119
"dump-swift",
120120
"dump swift modules in SDK"),
121121
clEnumValN(ActionType::CompareSDKs,
122122
"compare-sdk",
123-
"Compare SDK content in json file"),
123+
"Compare SDK content in JSON file"),
124124
clEnumValN(ActionType::DiagnoseSDKs,
125125
"diagnose-sdk",
126-
"Diagnose SDK content in json file"),
126+
"Diagnose SDK content in JSON file"),
127127
clEnumValEnd));
128128

129129
static llvm::cl::list<std::string>
130130
SDKJsonPaths("input-paths",
131-
llvm::cl::desc("The SDK contents under conparison"));
131+
llvm::cl::desc("The SDK contents under comparison"));
132132

133133
static llvm::cl::list<std::string>
134134
ApisPrintUsrs("api-usrs",
@@ -142,7 +142,7 @@ IgnoreRemovedDeclUSRs("ignored-usrs",
142142

143143
static llvm::cl::opt<std::string>
144144
SwiftVersion("swift-version",
145-
llvm::cl::desc("The swift compiler version to invoke"));
145+
llvm::cl::desc("The Swift compiler version to invoke"));
146146
}
147147

148148
namespace {
@@ -196,7 +196,6 @@ class SDKNodeVisitor {
196196
struct MatchedNodeListener {
197197
virtual void foundMatch(NodePtr Left, NodePtr Right) = 0;
198198
virtual void foundRemoveAddMatch(NodePtr Removed, NodePtr Added) {}
199-
virtual bool isMatched(NodePtr Left, NodePtr Right) {return false;};
200199
virtual ~MatchedNodeListener() = default;
201200
};
202201

@@ -209,7 +208,7 @@ struct NodeMatcher {
209208
#define KEY(NAME) static const char* Key_##NAME = #NAME;
210209
#include "DigesterEnums.def"
211210

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
213212
enum class SDKNodeKind {
214213
#define NODE_KIND(NAME) NAME,
215214
#include "DigesterEnums.def"
@@ -301,9 +300,7 @@ class SDKNode {
301300
StringRef getPrintedName() const { return PrintedName; }
302301
void removeChild(ChildIt CI) { Children.erase(CI); }
303302
ChildIt getChildBegin() const { return Children.begin(); }
304-
ChildIt getChildEnd() const { return Children.end(); }
305303
void annotate(NodeAnnotation Anno) { Annotations.insert(Anno); }
306-
bool isName(StringRef N) const { return getName() == N; }
307304
NodePtr getParent() const { return Parent; };
308305
unsigned getChildrenCount() const { return Children.size(); }
309306
NodePtr childAt(unsigned I) const;
@@ -470,28 +467,28 @@ class SDKNodeVectorViewer {
470467
llvm::function_ref<bool(NodePtr)> Selector;
471468
typedef ArrayRef<SDKNode*>::const_iterator VectorIt;
472469
VectorIt getNext(VectorIt Start);
473-
class ViwerIterator;
470+
class ViewerIterator;
474471

475472
public:
476473
SDKNodeVectorViewer(ArrayRef<SDKNode*> Collection,
477474
llvm::function_ref<bool(NodePtr)> Selector) :
478475
Collection(Collection),
479476
Selector(Selector) {}
480-
ViwerIterator begin();
481-
ViwerIterator end();
477+
ViewerIterator begin();
478+
ViewerIterator end();
482479
};
483480

484-
class SDKNodeVectorViewer::ViwerIterator :
481+
class SDKNodeVectorViewer::ViewerIterator :
485482
public std::iterator<std::input_iterator_tag, VectorIt> {
486483
SDKNodeVectorViewer &Viewer;
487484
VectorIt P;
488485
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;}
495492
const NodePtr& operator*() {return *P;}
496493
};
497494

@@ -503,18 +500,18 @@ SDKNodeVectorViewer::getNext(VectorIt Start) {
503500
return Collection.end();
504501
}
505502

506-
SDKNodeVectorViewer::ViwerIterator&
507-
SDKNodeVectorViewer::ViwerIterator::operator++() {
503+
SDKNodeVectorViewer::ViewerIterator&
504+
SDKNodeVectorViewer::ViewerIterator::operator++() {
508505
P = Viewer.getNext(P + 1);
509506
return *this;
510507
}
511508

512-
SDKNodeVectorViewer::ViwerIterator SDKNodeVectorViewer::begin() {
513-
return ViwerIterator(*this, getNext(Collection.begin()));
509+
SDKNodeVectorViewer::ViewerIterator SDKNodeVectorViewer::begin() {
510+
return ViewerIterator(*this, getNext(Collection.begin()));
514511
}
515512

516-
SDKNodeVectorViewer::ViwerIterator SDKNodeVectorViewer::end() {
517-
return ViwerIterator(*this, Collection.end());
513+
SDKNodeVectorViewer::ViewerIterator SDKNodeVectorViewer::end() {
514+
return ViewerIterator(*this, Collection.end());
518515
}
519516

520517
class SDKNodeDecl;
@@ -867,15 +864,6 @@ class SDKNodeDumpVisitor : public SDKNodeVisitor {
867864
SDKNodeDumpVisitor() {};
868865
};
869866

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-
879867
static StringRef getPrintedName(Type Ty) {
880868
std::string S;
881869
llvm::raw_string_ostream OS(S);
@@ -1008,7 +996,7 @@ case SDKNodeKind::X: \
1008996
}
1009997

1010998
// 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.
10121000
static NodeUniquePtr constructTypeNode(Type T) {
10131001
NodeUniquePtr Root = SDKNodeInitInfo(T).createSDKNode(SDKNodeKind::TypeNominal);
10141002

@@ -1182,15 +1170,15 @@ class SwiftDeclCollector : public VisibleDeclConsumer {
11821170
RootNode = std::move(Pair.second);
11831171
}
11841172

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.
11861174
void serialize(StringRef Filename) {
11871175
std::error_code EC;
11881176
llvm::raw_fd_ostream fs(Filename, EC, llvm::sys::fs::F_None);
11891177
emitSDKNodeRoot(fs, RootNode);
11901178
}
11911179

11921180
// 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.
11941182
NodePtr getSDKRoot() {
11951183
return RootNode.get();
11961184
}
@@ -1261,7 +1249,7 @@ class SwiftDeclCollector : public VisibleDeclConsumer {
12611249
namespace swift {
12621250
namespace json {
12631251
// 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
12651253
// file.
12661254
template<>
12671255
struct ScalarEnumerationTraits<SDKNodeKind> {
@@ -1395,7 +1383,7 @@ parseJsonEmit(StringRef FileName) {
13951383
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
13961384
llvm::MemoryBuffer::getFileOrSTDIN(FileName);
13971385
if (!FileBufOrErr) {
1398-
llvm_unreachable("Failed to read json file");
1386+
llvm_unreachable("Failed to read JSON file");
13991387
}
14001388
StringRef Buffer = FileBufOrErr->get()->getBuffer();
14011389
llvm::SourceMgr SM;
@@ -1712,7 +1700,7 @@ class SameNameNodeMatcher : public NodeMatcher {
17121700
PrintedNameAndUSR,
17131701
};
17141702

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.
17161704
Optional<NameMatchKind> getNameMatchKind(SDKNode *L, SDKNode *R) {
17171705
if (L->getKind() != R->getKind())
17181706
return None;
@@ -1751,7 +1739,7 @@ class SameNameNodeMatcher : public NodeMatcher {
17511739
}
17521740
}
17531741

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.
17551743
SDKNode* findBestNameMatch(ArrayRef<NameMatchCandidate> Candidates,
17561744
ArrayRef<NameMatchKind> Kinds) {
17571745
for (auto Kind : Kinds)
@@ -1902,7 +1890,7 @@ static void detectRename(NodePtr L, NodePtr R) {
19021890
// This is first pass on two given SDKNode trees. This pass removes the common part
19031891
// of two versions of SDK, leaving only the changed part.
19041892
class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
1905-
static void removeCommomChildren(NodePtr Left, NodePtr Right) {
1893+
static void removeCommonChildren(NodePtr Left, NodePtr Right) {
19061894
llvm::SmallPtrSet<NodePtr, 16> LeftToRemove;
19071895
llvm::SmallPtrSet<NodePtr, 16> RightToRemove;
19081896
for (auto &LC : Left->getChildren()) {
@@ -1959,7 +1947,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
19591947
// If the matched nodes are both modules, remove the contained
19601948
// type decls that are identical. If the matched nodes are both type decls,
19611949
// remove the contained function decls that are identical.
1962-
removeCommomChildren(Left, Right);
1950+
removeCommonChildren(Left, Right);
19631951
NodeVector LeftChildren;
19641952
NodeVector RightChildren;
19651953
Left->collectChildren(LeftChildren);
@@ -1975,7 +1963,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
19751963
case SDKNodeKind::TypeAlias:
19761964
case SDKNodeKind::TypeFunc:
19771965
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
19791967
// parameters sequentially.
19801968
SequentialNodeMatcher(Left->getChildren(),
19811969
Right->getChildren(), *this).match();
@@ -2097,8 +2085,6 @@ class TypeMemberDiffFinder : public SDKNodeVisitor {
20972085

20982086
};
20992087

2100-
typedef std::unique_ptr<NodePairVector> RenamedNodes;
2101-
21022088
// Given a condition, search whether a node satisfies that condition exists
21032089
// in a tree.
21042090
class SearchVisitor : public SDKNodeVisitor {
@@ -2233,12 +2219,12 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
22332219
}
22342220
};
22352221

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
22382224
// 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
22402226
// 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
22422228
// second parameter of function "c:objc(cs)NSXMLDocument(im)insertChildren:atIndex:".
22432229
// We keep both usrs because in the future this may support auto-rename.
22442230
class DiffItem {
@@ -3391,21 +3377,21 @@ static int prepareForDump(const char *Main,
33913377
}
33923378

33933379
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";
33953381
return 1;
33963382
}
33973383
return 0;
33983384
}
33993385

3400-
static void readIgnoredUsrs(llvm::StringSet<> &IgoredUsrs) {
3386+
static void readIgnoredUsrs(llvm::StringSet<> &IgnoredUsrs) {
34013387
StringRef Path = options::IgnoreRemovedDeclUSRs;
34023388
if (Path.empty())
34033389
return;
34043390
if (!fs::exists(Path)) {
34053391
llvm::errs() << Path << " does not exist.\n";
34063392
return;
34073393
}
3408-
readFileLineByLine(Path, IgoredUsrs);
3394+
readFileLineByLine(Path, IgnoredUsrs);
34093395
}
34103396

34113397
int main(int argc, char *argv[]) {

0 commit comments

Comments
 (0)