Skip to content

Commit dda0a90

Browse files
committed
[Macros] Remove ModuleDecl::isInGeneratedBuffer.
This method was misleading. The majority of callers (all but one!) don't want to unconditionally treat all locations in any macro expansion buffer the same way, because the code also must handle nested macro expansions. There is one part of SourceKit (and possibly others) that really do want to ignore all macro expansions, but those can be handled within SourceKit / IDE code, because I don't believe this utility is useful in the frontend. (cherry picked from commit b958e43)
1 parent 4668824 commit dda0a90

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

include/swift/AST/Attr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,12 +2584,12 @@ class DeclAttributes {
25842584

25852585
/// Predicate used to filter attributes to only the original attributes.
25862586
class OrigDeclAttrFilter {
2587-
ModuleDecl *mod;
2587+
const Decl *decl;
25882588

25892589
public:
2590-
OrigDeclAttrFilter() : mod(nullptr) {}
2590+
OrigDeclAttrFilter() : decl(nullptr) {}
25912591

2592-
OrigDeclAttrFilter(ModuleDecl *mod) : mod(mod) {}
2592+
OrigDeclAttrFilter(const Decl *decl) : decl(decl) {}
25932593

25942594
Optional<const DeclAttribute *>
25952595
operator()(const DeclAttribute *Attr) const;
@@ -2611,7 +2611,7 @@ class OrigDeclAttributes {
26112611
public:
26122612
OrigDeclAttributes() : origRange(make_range(DeclAttributes::const_iterator(nullptr), DeclAttributes::const_iterator(nullptr)), OrigDeclAttrFilter()) {}
26132613

2614-
OrigDeclAttributes(const DeclAttributes &allAttrs, ModuleDecl *mod) : origRange(make_range(allAttrs.begin(), allAttrs.end()), OrigDeclAttrFilter(mod)) {}
2614+
OrigDeclAttributes(const DeclAttributes &allAttrs, const Decl *decl) : origRange(make_range(allAttrs.begin(), allAttrs.end()), OrigDeclAttrFilter(decl)) {}
26152615

26162616
OrigFilteredRange::iterator begin() const { return origRange.begin(); }
26172617
OrigFilteredRange::iterator end() const { return origRange.end(); }

include/swift/AST/Module.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,6 @@ class ModuleDecl
406406
/// \c nullptr if the source location isn't in this module.
407407
SourceFile *getSourceFileContainingLocation(SourceLoc loc);
408408

409-
/// Whether the given location is inside a generated buffer, \c false if
410-
/// the given location isn't in this module.
411-
bool isInGeneratedBuffer(SourceLoc loc);
412-
413409
// Retrieve the buffer ID and source location of the outermost location that
414410
// caused the generation of the buffer containing \p loc. \p loc and its
415411
// buffer if it isn't in a generated buffer or has no original location.

lib/AST/Attr.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,18 @@ SourceLoc DeclAttributes::getStartLoc(bool forModifiers) const {
853853

854854
Optional<const DeclAttribute *>
855855
OrigDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
856-
if (!mod || mod->isInGeneratedBuffer(Attr->AtLoc))
856+
auto declLoc = decl->getStartLoc();
857+
auto *mod = decl->getModuleContext();
858+
auto *declFile = mod->getSourceFileContainingLocation(declLoc);
859+
auto *attrFile = mod->getSourceFileContainingLocation(Attr->AtLoc);
860+
if (!declFile || !attrFile)
861+
return Attr;
862+
863+
// Only attributes in the same buffer as the declaration they're attached to
864+
// are part of the original attribute list.
865+
if (declFile->getBufferID() != attrFile->getBufferID())
857866
return None;
867+
858868
return Attr;
859869
}
860870

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
368368
}
369369

370370
OrigDeclAttributes Decl::getOriginalAttrs() const {
371-
return OrigDeclAttributes(getAttrs(), getModuleContext());
371+
return OrigDeclAttributes(getAttrs(), this);
372372
}
373373

374374
DeclAttributes Decl::getSemanticAttrs() const {

lib/AST/Module.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -816,13 +816,6 @@ SourceFile *ModuleDecl::getSourceFileContainingLocation(SourceLoc loc) {
816816
return foundSourceFile;
817817
}
818818

819-
bool ModuleDecl::isInGeneratedBuffer(SourceLoc loc) {
820-
SourceFile *file = getSourceFileContainingLocation(loc);
821-
if (!file)
822-
return false;
823-
return file->Kind == SourceFileKind::MacroExpansion;
824-
}
825-
826819
std::pair<unsigned, SourceLoc>
827820
ModuleDecl::getOriginalLocation(SourceLoc loc) const {
828821
assert(loc.isValid());

lib/Refactoring/Refactoring.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,20 @@ renameAvailabilityInfo(const ValueDecl *VD, Optional<RenameRefInfo> RefInfo) {
649649
AvailKind = RefactorAvailableKind::Unavailable_has_no_name;
650650
}
651651

652+
auto isInMacroExpansionBuffer = [](const ValueDecl *VD) -> bool {
653+
auto *module = VD->getModuleContext();
654+
auto *file = module->getSourceFileContainingLocation(VD->getLoc());
655+
if (!file)
656+
return false;
657+
658+
return file->getFulfilledMacroRole() != None;
659+
};
660+
652661
if (AvailKind == RefactorAvailableKind::Available) {
653662
SourceLoc Loc = VD->getLoc();
654663
if (!Loc.isValid()) {
655664
AvailKind = RefactorAvailableKind::Unavailable_has_no_location;
656-
} else if (VD->getModuleContext()->isInGeneratedBuffer(Loc)) {
665+
} else if (isInMacroExpansionBuffer(VD)) {
657666
AvailKind = RefactorAvailableKind::Unavailable_decl_in_macro;
658667
}
659668
}

0 commit comments

Comments
 (0)