Skip to content

Commit cac6d1b

Browse files
committed
[NFC] Move UnitTestRunner::withTest definition out of the class
1 parent b2ec57f commit cac6d1b

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,55 @@ class UnitTest {
106106
virtual void invoke(Arguments &arguments) = 0;
107107
};
108108

109+
class UnitTestRunner : public SILFunctionTransform {
110+
void printTestLifetime(bool begin, unsigned testIndex, unsigned testCount,
111+
StringRef name, ArrayRef<StringRef> components) {
112+
StringRef word = begin ? "begin" : "end";
113+
llvm::errs() << word << " running test " << testIndex + 1 << " of "
114+
<< testCount << " on " << getFunction()->getName() << ": "
115+
<< name << " with: ";
116+
for (unsigned long index = 0, size = components.size(); index < size;
117+
++index) {
118+
llvm::errs() << components[index];
119+
if (index != size - 1) {
120+
llvm::errs() << ", ";
121+
}
122+
}
123+
llvm::errs() << "\n";
124+
}
125+
126+
template <typename Doit>
127+
void withTest(StringRef name, Doit doit);
128+
129+
void runTest(StringRef name, Arguments &arguments) {
130+
withTest(name, [&](auto *test) { test->invoke(arguments); });
131+
}
132+
133+
void run() override {
134+
llvm::SmallVector<std::string, 2> testSpecifications;
135+
getTestSpecifications(getFunction(), testSpecifications);
136+
Arguments arguments;
137+
SmallVector<StringRef, 4> components;
138+
for (unsigned long index = 0, size = testSpecifications.size();
139+
index < size; ++index) {
140+
components.clear();
141+
arguments.clear();
142+
auto testSpecification = testSpecifications[index];
143+
test::parseTestArgumentsFromSpecification(
144+
getFunction(), testSpecification, arguments, components);
145+
auto name = arguments.takeString();
146+
ArrayRef<StringRef> argumentStrings = components;
147+
argumentStrings = argumentStrings.drop_front();
148+
printTestLifetime(/*begin=*/true, /*index=*/index, /*size=*/size, name,
149+
argumentStrings);
150+
runTest(name, arguments);
151+
printTestLifetime(/*begin=*/false, /*index=*/index, /*size=*/size, name,
152+
argumentStrings);
153+
}
154+
}
155+
friend class UnitTest;
156+
};
157+
109158
// Arguments:
110159
// - string: list of characters, each of which specifies subsequent arguments
111160
// - F: function
@@ -276,25 +325,8 @@ struct IsDeinitBarrierTest : UnitTest {
276325

277326
/// [new_tests] Add the new UnitTest subclass above this line.
278327

279-
class UnitTestRunner : public SILFunctionTransform {
280-
void printTestLifetime(bool begin, unsigned testIndex, unsigned testCount,
281-
StringRef name, ArrayRef<StringRef> components) {
282-
StringRef word = begin ? "begin" : "end";
283-
llvm::errs() << word << " running test " << testIndex + 1 << " of "
284-
<< testCount << " on " << getFunction()->getName() << ": "
285-
<< name << " with: ";
286-
for (unsigned long index = 0, size = components.size(); index < size;
287-
++index) {
288-
llvm::errs() << components[index];
289-
if (index != size - 1) {
290-
llvm::errs() << ", ";
291-
}
292-
}
293-
llvm::errs() << "\n";
294-
}
295-
296-
template <typename Doit>
297-
void withTest(StringRef name, Doit doit) {
328+
template <typename Doit>
329+
void UnitTestRunner::withTest(StringRef name, Doit doit) {
298330
#define ADD_UNIT_TEST_SUBCLASS(STRING, SUBCLASS) \
299331
if (name == STRING) { \
300332
SUBCLASS it{this}; \
@@ -320,35 +352,7 @@ class UnitTestRunner : public SILFunctionTransform {
320352
#undef ADD_UNIT_TEST_SUBCLASS
321353
}
322354

323-
void runTest(StringRef name, Arguments &arguments) {
324-
withTest(name, [&](auto *test) { test->invoke(arguments); });
325-
}
326-
327-
void run() override {
328-
llvm::SmallVector<UnparsedSpecification, 2> testSpecifications;
329-
getTestSpecifications(getFunction(), testSpecifications);
330-
Arguments arguments;
331-
SmallVector<StringRef, 4> components;
332-
for (unsigned long index = 0, size = testSpecifications.size();
333-
index < size; ++index) {
334-
components.clear();
335-
arguments.clear();
336-
auto testSpecification = testSpecifications[index];
337-
test::parseTestArgumentsFromSpecification(
338-
getFunction(), testSpecification, arguments, components);
339-
auto name = arguments.takeString();
340-
ArrayRef<StringRef> argumentStrings = components;
341-
argumentStrings = argumentStrings.drop_front();
342-
printTestLifetime(/*begin=*/true, /*index=*/index, /*size=*/size, name,
343-
argumentStrings);
344-
runTest(name, arguments);
345-
printTestLifetime(/*begin=*/false, /*index=*/index, /*size=*/size, name,
346-
argumentStrings);
347-
}
348-
}
349-
friend class UnitTest;
350-
};
351-
355+
llvm::SmallVector<std::string, 2> testSpecifications;
352356
template <typename Analysis>
353357
Analysis *UnitTest::getAnalysis() {
354358
return pass->getAnalysis<Analysis>();

0 commit comments

Comments
 (0)