Skip to content

Commit d226b08

Browse files
committed
[ASTScope] Add APIs for determining whether a given source location is inside
a macro argument. (cherry picked from commit cd79cf9)
1 parent 5dbe6ac commit d226b08

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

include/swift/AST/ASTScope.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
278278
static std::pair<CaseStmt *, CaseStmt *>
279279
lookupFallthroughSourceAndDest(SourceFile *sourceFile, SourceLoc loc);
280280

281+
/// Returns \c true if the given source location is inside an attached
282+
/// or freestanding macro argument.
283+
static bool isInMacroArgument(SourceFile *sourceFile, SourceLoc loc);
284+
285+
/// Returns \c true if this scope contains macro arguments.
286+
///
287+
/// This is always true within macro expansion decl scopes, and it's
288+
/// true within custom attribute scopes if the attribute name is a
289+
/// potential macro reference.
290+
virtual bool isMacroArgumentScope() const { return false; }
291+
281292
/// Scopes that cannot bind variables may set this to true to create more
282293
/// compact scope tree in the debug info.
283294
virtual bool ignoreInDebugInfo() const { return false; }
@@ -867,7 +878,15 @@ class CustomAttributeScope final : public ASTScopeImpl {
867878
NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
868879
return attr;
869880
}
870-
bool ignoreInDebugInfo() const override { return true; }
881+
bool ignoreInDebugInfo() const override { return true; }
882+
883+
bool isMacroArgumentScope() const override {
884+
// FIXME: This should check whether the attribute name is
885+
// a macro. Otherwise, macro expansion will be suppressed
886+
// for property wrapper arguments.
887+
return true;
888+
}
889+
871890
private:
872891
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
873892
};
@@ -1266,6 +1285,10 @@ class MacroExpansionDeclScope final : public ASTScopeImpl {
12661285
SourceRange
12671286
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
12681287

1288+
bool isMacroArgumentScope() const override {
1289+
return true;
1290+
}
1291+
12691292
protected:
12701293
void printSpecifics(llvm::raw_ostream &out) const override;
12711294

include/swift/AST/NameLookup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,10 @@ class ASTScope : public ASTAllocated<ASTScope> {
815815
static std::pair<CaseStmt *, CaseStmt *>
816816
lookupFallthroughSourceAndDest(SourceFile *sourceFile, SourceLoc loc);
817817

818+
/// Returns \c true if the given source location is inside an attached
819+
/// or freestanding macro argument.
820+
static bool isInMacroArgument(SourceFile *sourceFile, SourceLoc loc);
821+
818822
SWIFT_DEBUG_DUMP;
819823
void print(llvm::raw_ostream &) const;
820824
void dumpOneScopeMapLocation(std::pair<unsigned, unsigned>);

lib/AST/ASTScope.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ std::pair<CaseStmt *, CaseStmt *> ASTScope::lookupFallthroughSourceAndDest(
6060
return ASTScopeImpl::lookupFallthroughSourceAndDest(sourceFile, loc);
6161
}
6262

63+
bool ASTScope::isInMacroArgument(SourceFile *sourceFile,
64+
SourceLoc loc) {
65+
return ASTScopeImpl::isInMacroArgument(sourceFile, loc);
66+
}
67+
6368
#if SWIFT_COMPILER_IS_MSVC
6469
#pragma warning(push)
6570
#pragma warning(disable : 4996)

lib/AST/ASTScopeLookup.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,21 @@ std::pair<CaseStmt *, CaseStmt *> ASTScopeImpl::lookupFallthroughSourceAndDest(
675675

676676
return { nullptr, nullptr };
677677
}
678+
679+
bool ASTScopeImpl::isInMacroArgument(SourceFile *sourceFile,
680+
SourceLoc loc) {
681+
if (!sourceFile || sourceFile->Kind == SourceFileKind::Interface)
682+
return false;
683+
684+
if (loc.isInvalid())
685+
return false;
686+
687+
auto *fileScope = sourceFile->getScope().impl;
688+
auto *scope = fileScope->findInnermostEnclosingScope(loc, nullptr);
689+
do {
690+
if (scope->isMacroArgumentScope())
691+
return true;
692+
} while ((scope = scope->getParent().getPtrOrNull()));
693+
694+
return false;
695+
}

0 commit comments

Comments
 (0)