Skip to content

Commit f12373b

Browse files
committed
[Serialization] Minor cleanups, no functionality change
- Use Optional::getValueOr instead of a handrolled version. - Use swift::reversed for reverse iteration instead of reversing a buffer mutably. - Use StringRef::withNullAsEmpty instead of checking both kinds of empty string
1 parent 31d5802 commit f12373b

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

lib/Serialization/Serialization.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,12 +1972,6 @@ static void verifyAttrSerializable(const KIND ## Decl *D) {\
19721972
static void verifyAttrSerializable(const Decl *D) {}
19731973
#endif
19741974

1975-
static inline unsigned getOptionalOrZero(const llvm::Optional<unsigned> &X) {
1976-
if (X.hasValue())
1977-
return X.getValue();
1978-
return 0;
1979-
}
1980-
19811975
bool Serializer::isDeclXRef(const Decl *D) const {
19821976
const DeclContext *topLevel = D->getDeclContext()->getModuleScopeContext();
19831977
if (topLevel->getParentModule() != M)
@@ -2382,8 +2376,8 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
23822376
if (X##_Val.hasValue()) {\
23832377
const auto &Y = X##_Val.getValue();\
23842378
X##_Major = Y.getMajor();\
2385-
X##_Minor = getOptionalOrZero(Y.getMinor());\
2386-
X##_Subminor = getOptionalOrZero(Y.getSubminor());\
2379+
X##_Minor = Y.getMinor().getValueOr(0);\
2380+
X##_Subminor = Y.getSubminor().getValueOr(0);\
23872381
X##_HasMinor = Y.getMinor().hasValue();\
23882382
X##_HasSubminor = Y.getSubminor().hasValue();\
23892383
}
@@ -2894,8 +2888,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
28942888

28952889
// Reverse the list, and write the parameter lists, from outermost
28962890
// to innermost.
2897-
std::reverse(allGenericParams.begin(), allGenericParams.end());
2898-
for (auto *genericParams : allGenericParams)
2891+
for (auto *genericParams : swift::reversed(allGenericParams))
28992892
writeGenericParams(genericParams);
29002893

29012894
writeMembers(id, extension->getMembers(), isClassExtension);
@@ -4994,7 +4987,7 @@ void swift::serializeToBuffers(
49944987
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
49954988
const SILModule *M) {
49964989

4997-
assert(options.OutputPath && options.OutputPath[0] != '\0');
4990+
assert(!StringRef::withNullAsEmpty(options.OutputPath).empty());
49984991
{
49994992
SharedTimer timer("Serialization, swiftmodule, to buffer");
50004993
llvm::SmallString<1024> buf;
@@ -5013,7 +5006,7 @@ void swift::serializeToBuffers(
50135006
std::move(buf), options.OutputPath);
50145007
}
50155008

5016-
if (options.DocOutputPath && options.DocOutputPath[0] != '\0') {
5009+
if (!StringRef::withNullAsEmpty(options.DocOutputPath).empty()) {
50175010
SharedTimer timer("Serialization, swiftdoc, to buffer");
50185011
llvm::SmallString<1024> buf;
50195012
llvm::raw_svector_ostream stream(buf);
@@ -5033,12 +5026,12 @@ void swift::serializeToBuffers(
50335026
void swift::serialize(ModuleOrSourceFile DC,
50345027
const SerializationOptions &options,
50355028
const SILModule *M) {
5036-
assert(options.OutputPath && options.OutputPath[0] != '\0');
5029+
assert(!StringRef::withNullAsEmpty(options.OutputPath).empty());
50375030

5038-
if (strcmp("-", options.OutputPath) == 0) {
5031+
if (StringRef(options.OutputPath) == "-") {
50395032
// Special-case writing to stdout.
50405033
Serializer::writeToStream(llvm::outs(), DC, M, options);
5041-
assert(!options.DocOutputPath || options.DocOutputPath[0] == '\0');
5034+
assert(StringRef::withNullAsEmpty(options.DocOutputPath).empty());
50425035
return;
50435036
}
50445037

@@ -5052,7 +5045,7 @@ void swift::serialize(ModuleOrSourceFile DC,
50525045
if (hadError)
50535046
return;
50545047

5055-
if (options.DocOutputPath && options.DocOutputPath[0] != '\0') {
5048+
if (!StringRef::withNullAsEmpty(options.DocOutputPath).empty()) {
50565049
(void)withOutputFile(getContext(DC).Diags,
50575050
options.DocOutputPath,
50585051
[&](raw_ostream &out) {

0 commit comments

Comments
 (0)