Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2847,53 +2847,62 @@ class DeclAttributes {
SourceLoc getStartLoc(bool forModifiers = false) const;
};

/// Predicate used to filter attributes to only the original attributes.
class OrigDeclAttrFilter {
/// Predicate used to filter attributes to only the parsed attributes.
class ParsedDeclAttrFilter {
const Decl *decl;

public:
OrigDeclAttrFilter() : decl(nullptr) {}
ParsedDeclAttrFilter() : decl(nullptr) {}

OrigDeclAttrFilter(const Decl *decl) : decl(decl) {}
ParsedDeclAttrFilter(const Decl *decl) : decl(decl) {}

llvm::Optional<const DeclAttribute *>
operator()(const DeclAttribute *Attr) const;
};

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

private:
OrigFilteredRange origRange;
ParsedFilteredRange parsedRange;

public:
OrigDeclAttributes() : origRange(make_range(DeclAttributes::const_iterator(nullptr), DeclAttributes::const_iterator(nullptr)), OrigDeclAttrFilter()) {}
ParsedDeclAttributes()
: parsedRange(make_range(DeclAttributes::const_iterator(nullptr),
DeclAttributes::const_iterator(nullptr)),
ParsedDeclAttrFilter()) {}

OrigDeclAttributes(const DeclAttributes &allAttrs, const Decl *decl) : origRange(make_range(allAttrs.begin(), allAttrs.end()), OrigDeclAttrFilter(decl)) {}
ParsedDeclAttributes(const DeclAttributes &allAttrs, const Decl *decl)
: parsedRange(make_range(allAttrs.begin(), allAttrs.end()),
ParsedDeclAttrFilter(decl)) {}

OrigFilteredRange::iterator begin() const { return origRange.begin(); }
OrigFilteredRange::iterator end() const { return origRange.end(); }
ParsedFilteredRange::iterator begin() const { return parsedRange.begin(); }
ParsedFilteredRange::iterator end() const { return parsedRange.end(); }

template <typename AttrType, bool AllowInvalid>
using AttributeKindRange =
OptionalTransformRange<OrigFilteredRange, ToAttributeKind<AttrType, AllowInvalid>>;
OptionalTransformRange<ParsedFilteredRange,
ToAttributeKind<AttrType, AllowInvalid>>;

template <typename AttrType, bool AllowInvalid = false>
AttributeKindRange<AttrType, AllowInvalid> getAttributes() const {
return AttributeKindRange<AttrType, AllowInvalid>(origRange, ToAttributeKind<AttrType, AllowInvalid>());
return AttributeKindRange<AttrType, AllowInvalid>(
parsedRange, ToAttributeKind<AttrType, AllowInvalid>());
}

/// Retrieve the first attribute of the given attribute class.
template <typename AttrType>
const AttrType *getAttribute(bool allowInvalid = false) const {
for (auto *attr : origRange) {
for (auto *attr : parsedRange) {
if (auto *specificAttr = dyn_cast<AttrType>(attr)) {
if (specificAttr->isValid() || allowInvalid)
return specificAttr;
Expand Down
6 changes: 3 additions & 3 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,9 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
}

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

/// Returns the attributes attached to this declaration,
/// including attributes that are generated as the result of member
Expand Down
2 changes: 1 addition & 1 deletion include/swift/IDE/SyntaxModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ struct SyntaxStructureElement {
struct SyntaxStructureNode {
const Decl *Dcl = nullptr;
SyntaxStructureKind Kind;
OrigDeclAttributes Attrs;
ParsedDeclAttributes Attrs;
CharSourceRange Range;
CharSourceRange BodyRange;
CharSourceRange NameRange;
Expand Down
7 changes: 5 additions & 2 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,16 @@ SourceLoc DeclAttributes::getStartLoc(bool forModifiers) const {
}

llvm::Optional<const DeclAttribute *>
OrigDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
ParsedDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
if (Attr->isImplicit())
return llvm::None;

auto declLoc = decl->getStartLoc();
auto *mod = decl->getModuleContext();
auto *declFile = mod->getSourceFileContainingLocation(declLoc);
auto *attrFile = mod->getSourceFileContainingLocation(Attr->getLocation());
if (!declFile || !attrFile)
return Attr;
return llvm::None;

// Only attributes in the same buffer as the declaration they're attached to
// are part of the original attribute list.
Expand Down
10 changes: 5 additions & 5 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
llvm_unreachable("bad DescriptiveDeclKind");
}

OrigDeclAttributes Decl::getOriginalAttrs() const {
return OrigDeclAttributes(getAttrs(), this);
ParsedDeclAttributes Decl::getParsedAttrs() const {
return ParsedDeclAttributes(getAttrs(), this);
}

DeclAttributes Decl::getExpandedAttrs() const {
Expand Down Expand Up @@ -777,7 +777,7 @@ SourceRange Decl::getSourceRangeIncludingAttrs() const {

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

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

for (auto *Attr : getOriginalAttrs()) {
for (auto *Attr : getParsedAttrs()) {
if (Attr->getRange().isValid())
Range.widen(Attr->getRangeWithAt());
}
Expand Down
6 changes: 4 additions & 2 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ class RangeWalker: protected ASTWalker {
return true;
}
}
for (auto *customAttr : D->getOriginalAttrs().getAttributes<CustomAttr, true>()) {
for (auto *customAttr :
D->getParsedAttrs().getAttributes<CustomAttr, true>()) {
if (auto *Repr = customAttr->getTypeRepr()) {
if (!Repr->walk(*this))
return false;
Expand Down Expand Up @@ -1341,7 +1342,8 @@ class FormatWalker : public ASTWalker {
return true;
}
}
for (auto *customAttr : D->getOriginalAttrs().getAttributes<CustomAttr, true>()) {
for (auto *customAttr :
D->getParsedAttrs().getAttributes<CustomAttr, true>()) {
if (auto *Repr = customAttr->getTypeRepr()) {
if (!Repr->walk(*this))
return false;
Expand Down
3 changes: 2 additions & 1 deletion lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ bool NameMatcher::handleCustomAttrs(Decl *D) {
}
}

for (auto *customAttr : D->getOriginalAttrs().getAttributes<CustomAttr, true>()) {
for (auto *customAttr :
D->getParsedAttrs().getAttributes<CustomAttr, true>()) {
if (shouldSkip(customAttr->getRangeWithAt()))
continue;
auto *Args = customAttr->getArgs();
Expand Down
14 changes: 7 additions & 7 deletions lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class ModelASTWalker : public ASTWalker {
static bool findUrlStartingLoc(StringRef Text, unsigned &Start,
std::regex& Regex);
bool annotateIfConfigConditionIdentifiers(Expr *Cond);
bool handleAttrs(const OrigDeclAttributes &Attrs);
bool handleAttrs(const ParsedDeclAttributes &Attrs);
bool handleAttrs(const TypeAttributes &Attrs);

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

static void setDecl(SyntaxStructureNode &N, Decl *D) {
N.Dcl = D;
N.Attrs = D->getOriginalAttrs();
N.Attrs = D->getParsedAttrs();
N.DocRange = D->getRawComment().getCharSourceRange();
}

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

Expand Down Expand Up @@ -956,7 +956,7 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
passTokenNodesUntil(ArgStart, PassNodesBehavior::ExcludeNodeAtLocation);
}
SN.Range = charSourceRangeFromSourceRange(SM, PD->getSourceRange());
SN.Attrs = PD->getOriginalAttrs();
SN.Attrs = PD->getParsedAttrs();
SN.TypeRange = charSourceRangeFromSourceRange(SM,
PD->getTypeSourceRangeForDiagnostics());
pushStructureNode(SN, PD);
Expand All @@ -970,7 +970,7 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
Contained = VD;
});
if (Contained) {
if (!handleAttrs(Contained->getOriginalAttrs()))
if (!handleAttrs(Contained->getParsedAttrs()))
return Action::SkipChildren();
break;
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
// We need to handle the special case where attributes semantically
// attach to enum element decls while syntactically locate before enum case decl.
if (auto *element = EnumCaseD->getFirstElement()) {
if (!handleAttrs(element->getOriginalAttrs()))
if (!handleAttrs(element->getParsedAttrs()))
return Action::SkipChildren();
}
if (pushStructureNode(SN, D)) {
Expand Down Expand Up @@ -1240,7 +1240,7 @@ bool ModelASTWalker::handleSpecialDeclAttribute(const DeclAttribute *D,
return false;
}

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