Skip to content

Commit b74e4a2

Browse files
authored
Merge pull request #69599 from tshortli/rename-orig-attrs-to-parsed-attrs
2 parents b129d92 + e879c07 commit b74e4a2

File tree

8 files changed

+51
-36
lines changed

8 files changed

+51
-36
lines changed

include/swift/AST/Attr.h

+24-15
Original file line numberDiff line numberDiff line change
@@ -2847,53 +2847,62 @@ class DeclAttributes {
28472847
SourceLoc getStartLoc(bool forModifiers = false) const;
28482848
};
28492849

2850-
/// Predicate used to filter attributes to only the original attributes.
2851-
class OrigDeclAttrFilter {
2850+
/// Predicate used to filter attributes to only the parsed attributes.
2851+
class ParsedDeclAttrFilter {
28522852
const Decl *decl;
28532853

28542854
public:
2855-
OrigDeclAttrFilter() : decl(nullptr) {}
2855+
ParsedDeclAttrFilter() : decl(nullptr) {}
28562856

2857-
OrigDeclAttrFilter(const Decl *decl) : decl(decl) {}
2857+
ParsedDeclAttrFilter(const Decl *decl) : decl(decl) {}
28582858

28592859
llvm::Optional<const DeclAttribute *>
28602860
operator()(const DeclAttribute *Attr) const;
28612861
};
28622862

2863-
/// Attributes applied directly to the declaration.
2863+
/// Attributes written in source on a declaration.
28642864
///
28652865
/// We should really just have \c DeclAttributes and \c SemanticDeclAttributes,
28662866
/// but currently almost all callers expect the latter. Instead of changing all
28672867
/// callers of \c getAttrs, instead provide a way to retrieve the original
28682868
/// attributes.
2869-
class OrigDeclAttributes {
2869+
class ParsedDeclAttributes {
28702870
public:
2871-
using OrigFilteredRange = OptionalTransformRange<iterator_range<DeclAttributes::const_iterator>, OrigDeclAttrFilter>;
2871+
using ParsedFilteredRange =
2872+
OptionalTransformRange<iterator_range<DeclAttributes::const_iterator>,
2873+
ParsedDeclAttrFilter>;
28722874

28732875
private:
2874-
OrigFilteredRange origRange;
2876+
ParsedFilteredRange parsedRange;
28752877

28762878
public:
2877-
OrigDeclAttributes() : origRange(make_range(DeclAttributes::const_iterator(nullptr), DeclAttributes::const_iterator(nullptr)), OrigDeclAttrFilter()) {}
2879+
ParsedDeclAttributes()
2880+
: parsedRange(make_range(DeclAttributes::const_iterator(nullptr),
2881+
DeclAttributes::const_iterator(nullptr)),
2882+
ParsedDeclAttrFilter()) {}
28782883

2879-
OrigDeclAttributes(const DeclAttributes &allAttrs, const Decl *decl) : origRange(make_range(allAttrs.begin(), allAttrs.end()), OrigDeclAttrFilter(decl)) {}
2884+
ParsedDeclAttributes(const DeclAttributes &allAttrs, const Decl *decl)
2885+
: parsedRange(make_range(allAttrs.begin(), allAttrs.end()),
2886+
ParsedDeclAttrFilter(decl)) {}
28802887

2881-
OrigFilteredRange::iterator begin() const { return origRange.begin(); }
2882-
OrigFilteredRange::iterator end() const { return origRange.end(); }
2888+
ParsedFilteredRange::iterator begin() const { return parsedRange.begin(); }
2889+
ParsedFilteredRange::iterator end() const { return parsedRange.end(); }
28832890

28842891
template <typename AttrType, bool AllowInvalid>
28852892
using AttributeKindRange =
2886-
OptionalTransformRange<OrigFilteredRange, ToAttributeKind<AttrType, AllowInvalid>>;
2893+
OptionalTransformRange<ParsedFilteredRange,
2894+
ToAttributeKind<AttrType, AllowInvalid>>;
28872895

28882896
template <typename AttrType, bool AllowInvalid = false>
28892897
AttributeKindRange<AttrType, AllowInvalid> getAttributes() const {
2890-
return AttributeKindRange<AttrType, AllowInvalid>(origRange, ToAttributeKind<AttrType, AllowInvalid>());
2898+
return AttributeKindRange<AttrType, AllowInvalid>(
2899+
parsedRange, ToAttributeKind<AttrType, AllowInvalid>());
28912900
}
28922901

28932902
/// Retrieve the first attribute of the given attribute class.
28942903
template <typename AttrType>
28952904
const AttrType *getAttribute(bool allowInvalid = false) const {
2896-
for (auto *attr : origRange) {
2905+
for (auto *attr : parsedRange) {
28972906
if (auto *specificAttr = dyn_cast<AttrType>(attr)) {
28982907
if (specificAttr->isValid() || allowInvalid)
28992908
return specificAttr;

include/swift/AST/Decl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,9 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
920920
}
921921

922922
/// Returns the attributes that were directly attached to this declaration
923-
/// as written in source, ie. does not include attributes generated by macro
924-
/// expansions.
925-
OrigDeclAttributes getOriginalAttrs() const;
923+
/// as written in source, ie. does not include semantic attributes or ones
924+
/// generated by macro expansions.
925+
ParsedDeclAttributes getParsedAttrs() const;
926926

927927
/// Returns the attributes attached to this declaration,
928928
/// including attributes that are generated as the result of member

include/swift/IDE/SyntaxModel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ struct SyntaxStructureElement {
139139
struct SyntaxStructureNode {
140140
const Decl *Dcl = nullptr;
141141
SyntaxStructureKind Kind;
142-
OrigDeclAttributes Attrs;
142+
ParsedDeclAttributes Attrs;
143143
CharSourceRange Range;
144144
CharSourceRange BodyRange;
145145
CharSourceRange NameRange;

lib/AST/Attr.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -860,13 +860,16 @@ SourceLoc DeclAttributes::getStartLoc(bool forModifiers) const {
860860
}
861861

862862
llvm::Optional<const DeclAttribute *>
863-
OrigDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
863+
ParsedDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
864+
if (Attr->isImplicit())
865+
return llvm::None;
866+
864867
auto declLoc = decl->getStartLoc();
865868
auto *mod = decl->getModuleContext();
866869
auto *declFile = mod->getSourceFileContainingLocation(declLoc);
867870
auto *attrFile = mod->getSourceFileContainingLocation(Attr->getLocation());
868871
if (!declFile || !attrFile)
869-
return Attr;
872+
return llvm::None;
870873

871874
// Only attributes in the same buffer as the declaration they're attached to
872875
// are part of the original attribute list.

lib/AST/Decl.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
371371
llvm_unreachable("bad DescriptiveDeclKind");
372372
}
373373

374-
OrigDeclAttributes Decl::getOriginalAttrs() const {
375-
return OrigDeclAttributes(getAttrs(), this);
374+
ParsedDeclAttributes Decl::getParsedAttrs() const {
375+
return ParsedDeclAttributes(getAttrs(), this);
376376
}
377377

378378
DeclAttributes Decl::getExpandedAttrs() const {
@@ -777,7 +777,7 @@ SourceRange Decl::getSourceRangeIncludingAttrs() const {
777777

778778
// Otherwise, include attributes directly attached to the accessor.
779779
SourceLoc VarLoc = AD->getStorage()->getStartLoc();
780-
for (auto *Attr : getOriginalAttrs()) {
780+
for (auto *Attr : getParsedAttrs()) {
781781
if (!Attr->getRange().isValid())
782782
continue;
783783

@@ -796,13 +796,13 @@ SourceRange Decl::getSourceRangeIncludingAttrs() const {
796796
if (auto *PBD = dyn_cast<PatternBindingDecl>(this)) {
797797
for (auto i : range(PBD->getNumPatternEntries()))
798798
PBD->getPattern(i)->forEachVariable([&](VarDecl *VD) {
799-
for (auto *Attr : VD->getOriginalAttrs())
799+
for (auto *Attr : VD->getParsedAttrs())
800800
if (Attr->getRange().isValid())
801801
Range.widen(Attr->getRangeWithAt());
802802
});
803803
}
804804

805-
for (auto *Attr : getOriginalAttrs()) {
805+
for (auto *Attr : getParsedAttrs()) {
806806
if (Attr->getRange().isValid())
807807
Range.widen(Attr->getRangeWithAt());
808808
}

lib/IDE/Formatting.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ class RangeWalker: protected ASTWalker {
461461
return true;
462462
}
463463
}
464-
for (auto *customAttr : D->getOriginalAttrs().getAttributes<CustomAttr, true>()) {
464+
for (auto *customAttr :
465+
D->getParsedAttrs().getAttributes<CustomAttr, true>()) {
465466
if (auto *Repr = customAttr->getTypeRepr()) {
466467
if (!Repr->walk(*this))
467468
return false;
@@ -1341,7 +1342,8 @@ class FormatWalker : public ASTWalker {
13411342
return true;
13421343
}
13431344
}
1344-
for (auto *customAttr : D->getOriginalAttrs().getAttributes<CustomAttr, true>()) {
1345+
for (auto *customAttr :
1346+
D->getParsedAttrs().getAttributes<CustomAttr, true>()) {
13451347
if (auto *Repr = customAttr->getTypeRepr()) {
13461348
if (!Repr->walk(*this))
13471349
return false;

lib/IDE/SwiftSourceDocInfo.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ bool NameMatcher::handleCustomAttrs(Decl *D) {
179179
}
180180
}
181181

182-
for (auto *customAttr : D->getOriginalAttrs().getAttributes<CustomAttr, true>()) {
182+
for (auto *customAttr :
183+
D->getParsedAttrs().getAttributes<CustomAttr, true>()) {
183184
if (shouldSkip(customAttr->getRangeWithAt()))
184185
continue;
185186
auto *Args = customAttr->getArgs();

lib/IDE/SyntaxModel.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ class ModelASTWalker : public ASTWalker {
409409
static bool findUrlStartingLoc(StringRef Text, unsigned &Start,
410410
std::regex& Regex);
411411
bool annotateIfConfigConditionIdentifiers(Expr *Cond);
412-
bool handleAttrs(const OrigDeclAttributes &Attrs);
412+
bool handleAttrs(const ParsedDeclAttributes &Attrs);
413413
bool handleAttrs(const TypeAttributes &Attrs);
414414

415415
using DeclAttributeAndRange = std::pair<const DeclAttribute *, SourceRange>;
@@ -503,7 +503,7 @@ CharSourceRange innerCharSourceRangeFromSourceRange(const SourceManager &SM,
503503

504504
static void setDecl(SyntaxStructureNode &N, Decl *D) {
505505
N.Dcl = D;
506-
N.Attrs = D->getOriginalAttrs();
506+
N.Attrs = D->getParsedAttrs();
507507
N.DocRange = D->getRawComment().getCharSourceRange();
508508
}
509509

@@ -873,7 +873,7 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
873873
// attached to syntactically).
874874
if (!isa<EnumElementDecl>(D) &&
875875
!(isa<VarDecl>(D) && cast<VarDecl>(D)->getParentPatternBinding())) {
876-
if (!handleAttrs(D->getOriginalAttrs()))
876+
if (!handleAttrs(D->getParsedAttrs()))
877877
return Action::SkipChildren();
878878
}
879879

@@ -956,7 +956,7 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
956956
passTokenNodesUntil(ArgStart, PassNodesBehavior::ExcludeNodeAtLocation);
957957
}
958958
SN.Range = charSourceRangeFromSourceRange(SM, PD->getSourceRange());
959-
SN.Attrs = PD->getOriginalAttrs();
959+
SN.Attrs = PD->getParsedAttrs();
960960
SN.TypeRange = charSourceRangeFromSourceRange(SM,
961961
PD->getTypeSourceRangeForDiagnostics());
962962
pushStructureNode(SN, PD);
@@ -970,7 +970,7 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
970970
Contained = VD;
971971
});
972972
if (Contained) {
973-
if (!handleAttrs(Contained->getOriginalAttrs()))
973+
if (!handleAttrs(Contained->getParsedAttrs()))
974974
return Action::SkipChildren();
975975
break;
976976
}
@@ -1042,7 +1042,7 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
10421042
// We need to handle the special case where attributes semantically
10431043
// attach to enum element decls while syntactically locate before enum case decl.
10441044
if (auto *element = EnumCaseD->getFirstElement()) {
1045-
if (!handleAttrs(element->getOriginalAttrs()))
1045+
if (!handleAttrs(element->getParsedAttrs()))
10461046
return Action::SkipChildren();
10471047
}
10481048
if (pushStructureNode(SN, D)) {
@@ -1240,7 +1240,7 @@ bool ModelASTWalker::handleSpecialDeclAttribute(const DeclAttribute *D,
12401240
return false;
12411241
}
12421242

1243-
bool ModelASTWalker::handleAttrs(const OrigDeclAttributes &Attrs) {
1243+
bool ModelASTWalker::handleAttrs(const ParsedDeclAttributes &Attrs) {
12441244
SmallVector<DeclAttributeAndRange, 4> DeclRanges;
12451245
for (auto *At : Attrs) {
12461246
if (At->getRangeWithAt().isValid())

0 commit comments

Comments
 (0)