Skip to content

Commit e7a0ba8

Browse files
authored
Merge pull request #9979 from ahoppen/adjust-declname-printing
Adjust printing to take into account special DeclNames
2 parents b8d2b08 + 949968a commit e7a0ba8

23 files changed

+83
-51
lines changed

include/swift/AST/Decl.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,13 +2123,6 @@ class ValueDecl : public Decl {
21232123
bool hasName() const { return bool(Name); }
21242124
bool isOperator() const { return Name.isOperator(); }
21252125

2126-
/// Returns the string for the base name, or "_" if this is unnamed.
2127-
StringRef getNameStr() const {
2128-
// TODO: Check if this function is called for special names
2129-
assert(!Name.isSpecial() && "Cannot get string for special names");
2130-
return hasName() ? Name.getBaseName().getIdentifier().str() : "_";
2131-
}
2132-
21332126
/// Retrieve the full name of the declaration.
21342127
/// TODO: Rename to getName?
21352128
DeclName getFullName() const { return Name; }
@@ -2358,6 +2351,12 @@ class TypeDecl : public ValueDecl {
23582351
public:
23592352
Identifier getName() const { return getFullName().getBaseIdentifier(); }
23602353

2354+
/// Returns the string for the base name, or "_" if this is unnamed.
2355+
StringRef getNameStr() const {
2356+
assert(!getFullName().isSpecial() && "Cannot get string for special names");
2357+
return hasName() ? getBaseName().getIdentifier().str() : "_";
2358+
}
2359+
23612360
/// The type of this declaration's values. For the type of the
23622361
/// declaration itself, use getInterfaceType(), which returns a
23632362
/// metatype.
@@ -4356,6 +4355,12 @@ class VarDecl : public AbstractStorageDecl {
43564355

43574356
Identifier getName() const { return getFullName().getBaseIdentifier(); }
43584357

4358+
/// Returns the string for the base name, or "_" if this is unnamed.
4359+
StringRef getNameStr() const {
4360+
assert(!getFullName().isSpecial() && "Cannot get string for special names");
4361+
return hasName() ? getBaseName().getIdentifier().str() : "_";
4362+
}
4363+
43594364
TypeLoc &getTypeLoc() { return typeLoc; }
43604365
TypeLoc getTypeLoc() const { return typeLoc; }
43614366

@@ -4827,6 +4832,12 @@ class AbstractFunctionDecl : public ValueDecl, public GenericContext {
48274832
public:
48284833
Identifier getName() const { return getFullName().getBaseIdentifier(); }
48294834

4835+
/// Returns the string for the base name, or "_" if this is unnamed.
4836+
StringRef getNameStr() const {
4837+
assert(!getFullName().isSpecial() && "Cannot get string for special names");
4838+
return hasName() ? getBaseName().getIdentifier().str() : "_";
4839+
}
4840+
48304841
/// \brief Should this declaration be treated as if annotated with transparent
48314842
/// attribute.
48324843
bool isTransparent() const;
@@ -5458,6 +5469,12 @@ class EnumElementDecl : public ValueDecl {
54585469

54595470
Identifier getName() const { return getFullName().getBaseIdentifier(); }
54605471

5472+
/// Returns the string for the base name, or "_" if this is unnamed.
5473+
StringRef getNameStr() const {
5474+
assert(!getFullName().isSpecial() && "Cannot get string for special names");
5475+
return hasName() ? getBaseName().getIdentifier().str() : "_";
5476+
}
5477+
54615478
/// \returns false if there was an error during the computation rendering the
54625479
/// EnumElementDecl invalid, true otherwise.
54635480
bool computeType();

include/swift/AST/Identifier.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ class DeclBaseName {
233233
return !isSpecial() && getIdentifier().isEditorPlaceholder();
234234
}
235235

236+
/// A representation of the name to be displayed to users. May be ambiguous
237+
/// between identifiers and special names.
238+
StringRef userFacingName() const {
239+
if (empty())
240+
return "_";
241+
return getIdentifier().str();
242+
}
243+
236244
int compare(DeclBaseName other) const {
237245
// TODO: Sort special names cleverly
238246
return getIdentifier().compare(other.getIdentifier());

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3259,7 +3259,7 @@ bool swift::shouldVerify(const Decl *D, const ASTContext &Context) {
32593259
return true;
32603260
}
32613261

3262-
size_t Hash = llvm::hash_value(VD->getNameStr());
3262+
size_t Hash = llvm::hash_value(VD->getBaseName().userFacingName());
32633263
return Hash % ProcessCount == ProcessId;
32643264
#else
32653265
return false;

lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ bool Decl::isPrivateStdlibDecl(bool whitelistProtocols) const {
457457
return false;
458458

459459
// If the name has leading underscore then it's a private symbol.
460-
if (VD->getNameStr().startswith("_"))
460+
if (!VD->getBaseName().isSpecial() &&
461+
VD->getBaseName().getIdentifier().str().startswith("_"))
461462
return true;
462463

463464
return false;

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
752752
}
753753

754754
if (auto value = dyn_cast<ValueDecl>(ppDecl)) {
755-
bufferName += value->getNameStr();
755+
bufferName += value->getBaseName().userFacingName();
756756
} else if (auto ext = dyn_cast<ExtensionDecl>(ppDecl)) {
757757
bufferName += ext->getExtendedType().getString();
758758
}

lib/AST/Identifier.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, Identifier I) {
2727
return OS << I.get();
2828
}
2929

30-
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclBaseName I) {
31-
// TODO: Handle special names
32-
return OS << I.getIdentifier();
30+
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclBaseName D) {
31+
return OS << D.userFacingName();
3332
}
3433

3534
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclName I) {

lib/AST/SourceEntityWalker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ bool SemaAnnotator::walkToDeclPre(Decl *D) {
9595
bool IsExtension = false;
9696

9797
if (auto *VD = dyn_cast<ValueDecl>(D)) {
98-
// TODO: Handle special names
9998
if (VD->hasName() && !VD->isImplicit())
100-
NameLen = VD->getBaseName().getIdentifier().getLength();
99+
NameLen = VD->getBaseName().userFacingName().size();
101100

102101
auto ReportParamList = [&](ParameterList *PL) {
103102
for (auto *PD : *PL) {

lib/AST/USRGeneration.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
215215
auto &Importer = *D->getASTContext().getClangModuleLoader();
216216

217217
auto ClangMacroInfo = ClangN.getAsMacro();
218-
bool Ignore = clang::index::generateUSRForMacro(D->getNameStr(),
218+
bool Ignore = clang::index::generateUSRForMacro(
219+
D->getBaseName().getIdentifier().str(),
219220
ClangMacroInfo->getDefinitionLoc(),
220221
Importer.getClangASTContext().getSourceManager(), Buf);
221222
if (!Ignore)

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,9 @@ class DarwinBlacklistDeclConsumer : public swift::VisibleDeclConsumer {
20262026
return false;
20272027

20282028
if (clangModule->Name == "MacTypes") {
2029-
return llvm::StringSwitch<bool>(VD->getNameStr())
2029+
if (!VD->hasName() || VD->getBaseName().isSpecial())
2030+
return true;
2031+
return llvm::StringSwitch<bool>(VD->getBaseName().getIdentifier().str())
20302032
.Cases("OSErr", "OSStatus", "OptionBits", false)
20312033
.Cases("FourCharCode", "OSType", false)
20322034
.Case("Boolean", false)

lib/FrontendTool/ReferenceDependencies.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
150150
}
151151

152152
auto escape = [](DeclBaseName name) -> std::string {
153-
// TODO: Handle special names
154-
return llvm::yaml::escape(name.getIdentifier().str());
153+
return llvm::yaml::escape(name.userFacingName());
155154
};
156155

157156
out << "### Swift dependencies file v0 ###\n";

lib/IDE/CodeCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3727,7 +3727,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
37273727
for (auto Ex : NTD->getExtensions()) {
37283728
handleDeclRange(Ex->getMembers(), Reason);
37293729
}
3730-
} else if (isNameHit(VD->getNameStr())) {
3730+
} else if (!VD->getBaseName().isSpecial() &&
3731+
isNameHit(VD->getBaseName().getIdentifier().str())) {
37313732
if (VD->hasInterfaceType())
37323733
unboxType(VD->getInterfaceType());
37333734
}

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) {
291291
}
292292

293293
for (auto &VD : DeclaredDecls) {
294-
OS << "<Declared>" << VD.VD->getNameStr() << "</Declared>";
294+
OS << "<Declared>" << VD.VD->getBaseName() << "</Declared>";
295295
OS << "<OutscopeReference>";
296296
if (VD.ReferredAfterRange)
297297
OS << "true";
@@ -300,7 +300,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) {
300300
OS << "</OutscopeReference>\n";
301301
}
302302
for (auto &RD : ReferencedDecls) {
303-
OS << "<Referenced>" << RD.VD->getNameStr() << "</Referenced>";
303+
OS << "<Referenced>" << RD.VD->getBaseName() << "</Referenced>";
304304
OS << "<Type>";
305305
RD.Ty->print(OS);
306306
OS << "</Type>\n";
@@ -1023,8 +1023,7 @@ void swift::ide::getLocationInfo(const ValueDecl *VD,
10231023
NameLen = getCharLength(SM, R);
10241024
} else {
10251025
if (VD->hasName()) {
1026-
// TODO: Handle special names
1027-
NameLen = VD->getBaseName().getIdentifier().getLength();
1026+
NameLen = VD->getBaseName().userFacingName().size();
10281027
} else {
10291028
NameLen = getCharLength(SM, VD->getLoc());
10301029
}

lib/IDE/SyntaxModel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,9 +1021,9 @@ class IdRefWalker : public ASTWalker {
10211021
return { true, E };
10221022
if (DRE->getRefKind() != DeclRefKind::Ordinary)
10231023
return { true, E };
1024-
// TODO: Handle special names
1025-
if (!Fn(CharSourceRange(DRE->getSourceRange().Start,
1026-
DRE->getName().getBaseIdentifier().getLength())))
1024+
if (!Fn(CharSourceRange(
1025+
DRE->getSourceRange().Start,
1026+
DRE->getName().getBaseName().userFacingName().size())))
10271027
return { false, nullptr };
10281028
}
10291029
return { true, E };

lib/IRGen/GenReflection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
340340
}
341341

342342
if (IGM.IRGen.Opts.EnableReflectionNames) {
343-
auto fieldName = IGM.getAddrOfFieldName(value->getNameStr());
343+
auto name = value->getBaseName().getIdentifier().str();
344+
auto fieldName = IGM.getAddrOfFieldName(name);
344345
B.addRelativeAddress(fieldName);
345346
} else {
346347
B.addInt32(0);

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,9 +2605,8 @@ class ModuleWriter {
26052605
assert(*lhs != *rhs && "duplicate top-level decl");
26062606

26072607
auto getSortName = [](const Decl *D) -> StringRef {
2608-
// TODO: Handle special names
26092608
if (auto VD = dyn_cast<ValueDecl>(D))
2610-
return VD->getBaseName().getIdentifier().str();
2609+
return VD->getBaseName().userFacingName();
26112610

26122611
if (auto ED = dyn_cast<ExtensionDecl>(D)) {
26132612
auto baseClass = ED->getExtendedType()->getClassOrBoundGenericClass();

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,11 @@ ValueDecl *DIMemoryObjectInfo::
257257
getPathStringToElement(unsigned Element, std::string &Result) const {
258258
auto &Module = MemoryInst->getModule();
259259

260-
// TODO: Handle special names
261260
if (isAnyInitSelf())
262261
Result = "self";
263262
else if (ValueDecl *VD =
264263
dyn_cast_or_null<ValueDecl>(getLoc().getAsASTNode<Decl>()))
265-
Result = VD->getBaseName().getIdentifier().str();
264+
Result = VD->getBaseName().userFacingName();
266265
else
267266
Result = "<unknown>";
268267

lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
15281528
if (auto declRefExpr = dyn_cast<DeclRefExpr>(fn)) {
15291529
auto decl = declRefExpr->getDecl();
15301530
candidates.push_back({ decl, getCalleeLevel(decl) });
1531-
declName = decl->getNameStr().str();
1531+
declName = decl->getBaseName().userFacingName();
15321532
return;
15331533
}
15341534

@@ -1549,7 +1549,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
15491549
}
15501550

15511551
if (!candidates.empty())
1552-
declName = candidates[0].getDecl()->getNameStr().str();
1552+
declName = candidates[0].getDecl()->getBaseName().userFacingName();
15531553
return;
15541554
}
15551555

@@ -1682,7 +1682,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
16821682
if (candidates.empty()) continue;
16831683

16841684
if (declName.empty())
1685-
declName = candidates[0].getDecl()->getNameStr().str();
1685+
declName = candidates[0].getDecl()->getBaseName().userFacingName();
16861686
return;
16871687
}
16881688

@@ -1813,7 +1813,7 @@ CalleeCandidateInfo::CalleeCandidateInfo(Type baseType,
18131813
}
18141814

18151815
if (!candidates.empty())
1816-
declName = candidates[0].getDecl()->getNameStr().str();
1816+
declName = candidates[0].getDecl()->getBaseName().userFacingName();
18171817
}
18181818

18191819

lib/Sema/MiscDiagnostics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,7 +2639,7 @@ static Expr *endConditionValueForConvertingCStyleForLoop(const ForStmt *FS,
26392639
return nullptr;
26402640

26412641
// Verify that the condition is a simple != or < comparison to the loop variable.
2642-
auto comparisonOpName = binaryFuncExpr->getDecl()->getNameStr();
2642+
auto comparisonOpName = binaryFuncExpr->getDecl()->getBaseName();
26432643
if (comparisonOpName == "!=")
26442644
OpKind = OperatorKind::NotEqual;
26452645
else if (comparisonOpName == "<")
@@ -2681,7 +2681,7 @@ static bool unaryOperatorCheckForConvertingCStyleForLoop(const ForStmt *FS,
26812681
auto unaryFuncExpr = dyn_cast<DeclRefExpr>(unaryExpr->getFn());
26822682
if (!unaryFuncExpr)
26832683
return false;
2684-
if (unaryFuncExpr->getDecl()->getNameStr() != OpName)
2684+
if (unaryFuncExpr->getDecl()->getBaseName() != OpName)
26852685
return false;
26862686
return incrementDeclRefExpr->getDecl() == loopVar;
26872687
}
@@ -2710,7 +2710,7 @@ static bool binaryOperatorCheckForConvertingCStyleForLoop(TypeChecker &TC,
27102710
auto binaryFuncExpr = dyn_cast<DeclRefExpr>(binaryExpr->getFn());
27112711
if (!binaryFuncExpr)
27122712
return false;
2713-
if (binaryFuncExpr->getDecl()->getNameStr() != OpName)
2713+
if (binaryFuncExpr->getDecl()->getBaseName() != OpName)
27142714
return false;
27152715
auto argTupleExpr = dyn_cast<TupleExpr>(binaryExpr->getArg());
27162716
if (!argTupleExpr)

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,8 +2442,8 @@ bool AvailabilityWalker::diagnoseIncDecRemoval(const ValueDecl *D,
24422442
SourceRange R,
24432443
const AvailableAttr *Attr) {
24442444
// We can only produce a fixit if we're talking about ++ or --.
2445-
bool isInc = D->getNameStr() == "++";
2446-
if (!isInc && D->getNameStr() != "--")
2445+
bool isInc = D->getBaseName() == "++";
2446+
if (!isInc && D->getBaseName() != "--")
24472447
return false;
24482448

24492449
// We can only handle the simple cases of lvalue++ and ++lvalue. This is
@@ -2503,11 +2503,15 @@ bool AvailabilityWalker::diagnoseMemoryLayoutMigration(const ValueDecl *D,
25032503
if (!D->getModuleContext()->isStdlibModule())
25042504
return false;
25052505

2506-
StringRef Property = llvm::StringSwitch<StringRef>(D->getNameStr())
2507-
.Case("sizeof", "size")
2508-
.Case("alignof", "alignment")
2509-
.Case("strideof", "stride")
2510-
.Default(StringRef());
2506+
StringRef Property;
2507+
if (D->getBaseName() == "sizeof") {
2508+
Property = "size";
2509+
} else if (D->getBaseName() == "alignof") {
2510+
Property = "alignment";
2511+
} else if (D->getBaseName() == "strideof") {
2512+
Property = "stride";
2513+
}
2514+
25112515
if (Property.empty())
25122516
return false;
25132517

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class FindCapturedVars : public ASTWalker {
356356
isNested = f->getDeclContext()->isLocalContext();
357357

358358
if (isInOut && !AFR.isKnownNoEscape() && !isNested) {
359-
if (D->getNameStr() == "self") {
359+
if (D->getBaseName() == D->getASTContext().Id_self) {
360360
TC.diagnose(DRE->getLoc(),
361361
diag::closure_implicit_capture_mutating_self);
362362
} else {

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3899,9 +3899,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
38993899
VD->getNameLoc().isValid() &&
39003900
Context.SourceMgr.extractText({VD->getNameLoc(), 1}) != "`") {
39013901
TC.diagnose(VD->getNameLoc(), diag::reserved_member_name,
3902-
VD->getFullName(), VD->getNameStr());
3902+
VD->getFullName(), VD->getBaseName().getIdentifier().str());
39033903
TC.diagnose(VD->getNameLoc(), diag::backticks_to_escape)
3904-
.fixItReplace(VD->getNameLoc(), "`"+VD->getNameStr().str()+"`");
3904+
.fixItReplace(VD->getNameLoc(),
3905+
"`" + VD->getBaseName().userFacingName().str() + "`");
39053906
}
39063907
}
39073908

lib/Sema/TypeChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ void TypeChecker::checkForForbiddenPrefix(const Decl *D) {
983983
if (!hasEnabledForbiddenTypecheckPrefix())
984984
return;
985985
if (auto VD = dyn_cast<ValueDecl>(D)) {
986-
checkForForbiddenPrefix(VD->getNameStr());
986+
if (!VD->getBaseName().isSpecial())
987+
checkForForbiddenPrefix(VD->getBaseName().getIdentifier().str());
987988
}
988989
}
989990

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,8 @@ class SwiftDeclCollector : public VisibleDeclConsumer {
13991399
llvm::array_pod_sort(ClangMacros.begin(), ClangMacros.end(),
14001400
[](ValueDecl * const *lhs,
14011401
ValueDecl * const *rhs) -> int {
1402-
return (*lhs)->getNameStr().compare((*rhs)->getNameStr());
1402+
return (*lhs)->getBaseName().userFacingName().compare(
1403+
(*rhs)->getBaseName().userFacingName());
14031404
});
14041405

14051406
for (auto *VD : ClangMacros)

0 commit comments

Comments
 (0)