[Macros] Don't include attr range when checking macro definition

For macro definition checking, we use the range of the `macro`
declaration and re-parse it with `SwiftParser`. Previously it uses the
range including the attributes, but that can result invalid code because
the attribute can be in a `#if ... #endif` region.

Since we don't use attributes for checking the definition, just use the
range without the attributes instead.

rdar://150805795
This commit is contained in:
Rintaro Ishizaki
2025-05-06 16:42:19 -07:00
parent 4517a6e26b
commit 8519e71602
2 changed files with 8 additions and 1 deletions

View File

@@ -136,7 +136,7 @@ MacroDefinition MacroDefinitionRequest::evaluate(
SM.getEntireTextForBuffer(sourceFile->getBufferID());
StringRef macroDeclText =
SM.extractText(Lexer::getCharSourceRangeFromSourceRange(
SM, macro->getSourceRangeIncludingAttrs()));
SM, macro->getSourceRange()));
auto checkResult = swift_Macros_checkMacroDefinition(
&ctx.Diags, sourceFileText, macroDeclText, &externalMacroName,

View File

@@ -234,3 +234,10 @@ func someGlobalNext(
) async throws {
fatalError()
}
// This is testing if the definition is actually checked. The error means the '#externalMacro' was correctly parsed and checked.
#if true
@available(*, unavailable)
#endif
@freestanding(expression) public macro MacroWithIfConfigAttr() = #externalMacro(module: "ThisMacroModuleDoesNotExist", type: "ThisMacroTypeDoesNotExist")
// expected-warning@-1{{external macro implementation type 'ThisMacroModuleDoesNotExist.ThisMacroTypeDoesNotExist'}}