swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.55k stars 10.35k forks source link

[SR-13953] Give Identifier a hasUnderscoredNaming() helper #56350

Open beccadax opened 3 years ago

beccadax commented 3 years ago
Previous ID SR-13953
Radar None
Original Reporter @beccadax
Type Improvement
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler, Source Tooling | |Labels | Improvement, Refactoring, StarterBug | |Assignee | mohit.athwani (JIRA) | |Priority | Medium | md5: 2f01bbed82012d22d725137279f99626

Issue Description:

Various parts of the compiler and SourceKit treat names with leading underscores a little bit specially. For example, SourceKit keeps these names out of code completion results.

To help with this, Decl has a hasUnderscoredNaming() method which checks if the method's name starts with an underscore. However, there are half a dozen places in the compiler where we need to check if plain old identifiers are underscored, and in those places, we usually write something like:

  if (ModuleName.str().startswith("_") ||

It would be better if Identifier, and possibly some other name-related types like DeclName, had a helper like Decl's to standardize this check.

typesanitizer commented 3 years ago

@beccadax should this be closed? I don't see any such uses, but maybe I'm not looking carefully enough.

flexlixrup commented 2 months ago

@beccadax I guess this should be closed.

bnbarham commented 2 months ago

There's still a few:

include/swift/AST/ASTPrinter.h
258:    if (Opts.SkipUnderscoredKeywords && name.starts_with("_"))

lib/AST/Attr.cpp
161:    return StringRef(#SPELLING).starts_with("_");

lib/Sema/TypeCheckNameLookup.cpp
556:  if (correctedBase.starts_with("_") && !writtenBase.starts_with("_")) {

Though of those, it's really only the TypeCheckNameLookup.cpp uses that could be replaced with a helper on DeclName.