From 2c281208decf86074354dc6a70de68c6687b91d0 Mon Sep 17 00:00:00 2001 From: James Brown Date: Sat, 2 Mar 2024 17:09:55 -0500 Subject: [PATCH] 56350 - Give Identifier a hasUnderscoredNaming() helper and in the places currently using str().startsWith, change it to use the new helper. --- include/swift/AST/Identifier.h | 4 ++++ include/swift/IDE/CompletionLookup.h | 2 +- include/swift/Sema/CSFix.h | 3 +-- lib/AST/Decl.cpp | 2 +- lib/AST/Module.cpp | 2 +- lib/AST/NameLookup.cpp | 2 +- lib/PrintAsClang/ModuleContentsWriter.cpp | 2 +- 7 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/swift/AST/Identifier.h b/include/swift/AST/Identifier.h index 5560cffaf21a4..733063b64647e 100644 --- a/include/swift/AST/Identifier.h +++ b/include/swift/AST/Identifier.h @@ -177,6 +177,10 @@ class Identifier { return str().startswith("$") && !(getLength() == 1); } + bool hasUnderscoredNaming() const { + return str().startswith("_"); + } + const void *getAsOpaquePointer() const { return static_cast(Pointer); } diff --git a/include/swift/IDE/CompletionLookup.h b/include/swift/IDE/CompletionLookup.h index f61df202dfe10..2dae850717e1f 100644 --- a/include/swift/IDE/CompletionLookup.h +++ b/include/swift/IDE/CompletionLookup.h @@ -304,7 +304,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer { void includeInstanceMembers() { IncludeInstanceMembers = true; } bool isHiddenModuleName(Identifier Name) { - return (Name.str().startswith("_") || Name == Ctx.SwiftShimsModuleName || + return (Name.hasUnderscoredNaming() || Name == Ctx.SwiftShimsModuleName || Name.str() == SWIFT_ONONE_SUPPORT); } diff --git a/include/swift/Sema/CSFix.h b/include/swift/Sema/CSFix.h index 106ef09ba4acf..5d686e39f1d0d 100644 --- a/include/swift/Sema/CSFix.h +++ b/include/swift/Sema/CSFix.h @@ -1335,8 +1335,7 @@ class UseWrappedValue final : public ConstraintFix { PropertyWrapper(propertyWrapper), Base(base), Wrapper(wrapper) {} bool usingProjection() const { - auto nameStr = PropertyWrapper->getName().str(); - return !nameStr.startswith("_"); + return !PropertyWrapper->getName().hasUnderscoredNaming(); } public: diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index cd3824e4482e7..48ff0b3542d92 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -1083,7 +1083,7 @@ bool Decl::hasUnderscoredNaming() const { } if (!VD->getBaseName().isSpecial() && - VD->getBaseIdentifier().str().startswith("_")) { + VD->getBaseIdentifier().hasUnderscoredNaming()) { return true; } diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index 805aef8fa2121..a7a42693b6035 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -2296,7 +2296,7 @@ void ModuleDecl::findDeclaredCrossImportOverlaysTransitive( for (Identifier overlay: overlays) { // We don't present non-underscored overlays as part of the underlying // module, so ignore them. - if (!overlay.str().startswith("_")) + if (!overlay.hasUnderscoredNaming()) continue; ModuleDecl *overlayMod = getASTContext().getModuleByName(overlay.str()); diff --git a/lib/AST/NameLookup.cpp b/lib/AST/NameLookup.cpp index 6af8249d2e9d7..31e1fe20b2d04 100644 --- a/lib/AST/NameLookup.cpp +++ b/lib/AST/NameLookup.cpp @@ -2422,7 +2422,7 @@ static void installPropertyWrapperMembersIfNeeded(NominalTypeDecl *target, return; if ((!baseName.getIdentifier().str().startswith("$") && - !baseName.getIdentifier().str().startswith("_")) || + !baseName.getIdentifier().hasUnderscoredNaming()) || baseName.getIdentifier().str().size() <= 1) { return; } diff --git a/lib/PrintAsClang/ModuleContentsWriter.cpp b/lib/PrintAsClang/ModuleContentsWriter.cpp index 02764e4083c23..e9a3208d01063 100644 --- a/lib/PrintAsClang/ModuleContentsWriter.cpp +++ b/lib/PrintAsClang/ModuleContentsWriter.cpp @@ -834,7 +834,7 @@ class ModuleWriter { [&](const ValueDecl *vd) { return !printer.isVisible(vd) || vd->isObjC() || (vd->isStdlibDecl() && !vd->getName().isSpecial() && - vd->getBaseIdentifier().str().startswith("_")) || + vd->getBaseIdentifier().hasUnderscoredNaming()) || (vd->isStdlibDecl() && isa(vd)) || (vd->isStdlibDecl() && vd->getASTContext().getErrorDecl() == vd);