Skip to content

[Clang importer + macros] Handle name lookup and type checking for expanded macros #77580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 14, 2024

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Nov 13, 2024

Introduce a number of fixes to allow us to fully use declarations that are produced by applying a peer macro to an imported declarations.

These changes include:

  • Treating the source file containing swift_attr macros as one that stores macros, so we don't parse it as top-level code
  • Ensuring that we have the right set of imports in the source file containing the macro expansion, because it depends only on the module it comes from
  • Ensuring that name lookup looks in that file even when the DeclContext hierarchy doesn't contain the source file (because it's based on the Clang module structure)

Expand testing to be sure that we're getting the right calls, diagnostics, printing APIs, and generated IR symbols.

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor enabled auto-merge November 13, 2024 06:42
@DougGregor DougGregor force-pushed the swift-attr-parsing-fixes branch from 39afec7 to 0bcd251 Compare November 13, 2024 18:07
generatedInfo->kind != GeneratedSourceInfo::PrettyPrinted)
generatedInfo->kind != GeneratedSourceInfo::PrettyPrinted &&
generatedInfo->kind != GeneratedSourceInfo::DefaultArgument &&
generatedInfo->kind != GeneratedSourceInfo::Attribute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above says "We only care about macros, so skip everything else", but aren't macros attributes in the grammar? I wouldn't expect us to skip GeneratedSourceInfo::Attribute here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment means "we only care about macro expansion buffers." I'll clarify it

// RUN: %FileCheck -input-file %t/imported.printed.txt %s
// RUN: echo "" >> %t/stderr.log
// RUN: %FileCheck -check-prefix DIAGS -input-file %t/stderr.log %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to instead have swift-ide-test return an error code if the input has errors? That seems like a simpler solution. Not sure what effect they have, but it sounds like --test-input-complete or --typecheck could be useful.

@@ -122,7 +122,8 @@ public func parseSourceFile(
.preambleMacroExpansion,
.replacedFunctionBody,
.prettyPrinted,
.defaultArgument:
.defaultArgument,
.attribute:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move .attribute to . memberAttributeMacroExpansion branch below, it's still not implemented and doing the same thing, but just not to forget to move it later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, done!

@rintaro
Copy link
Member

rintaro commented Nov 13, 2024

While you are here, could you make the parsing use ParseSourceFileRequest? (rdar://139119159)

@@ -364,7 +366,8 @@ SourceFileParsingResult parseSourceFile(SourceFile &SF) {
break;
}

case GeneratedSourceInfo::MemberAttributeMacroExpansion: {
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
case GeneratedSourceInfo::Attribute: {
parser.parseExpandedAttributeList(items);
Copy link
Member

@rintaro rintaro Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh apparently, swift_attr accepts modifiers OR attributes, but parseExpandedAttributeList only accepts attributes? 🤔

if (parser.Tok.is(tok::at_sign)) {
SourceLoc atEndLoc = parser.Tok.getRange().getEnd();
SourceLoc atLoc = parser.consumeToken(tok::at_sign);
hadError = parser
.parseDeclAttribute(MappedDecl->getAttrs(), atLoc,
atEndLoc, initContext,
/*isFromClangAttribute=*/true)
.isError();
} else {
SourceLoc staticLoc;
StaticSpellingKind staticSpelling;
hadError = parser
.parseDeclModifierList(MappedDecl->getAttrs(), staticLoc,
staticSpelling,
/*isFromClangAttribute=*/true)
.isError();
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and we have some things that are permitted but are neither attributes nor modifiers, which is odd. I'm going to have to think about this refactoring more.

… as attributes

Previously, they were being parsed as top-level code, which would cause
errors because there are no definitions. Introduce a new
GeneratedSourceInfo kind to mark the purpose of these buffers so the
parser can handle them appropriately.
…ule-sensitive

We need different buffers for each imported module that has swift_attr attributes,
so cache them appropriately.
Right now, we're creating a bunch of throwaway ErrorExpr nodes in the AST.
…panded macros

Introduce a number of fixes to allow us to fully use declarations that
are produced by applying a peer macro to an imported declarations.
These changes include:
* Ensuring that we have the right set of imports in the source file
containing the macro expansion, because it depends only on the module
it comes from
* Ensuring that name lookup looks in that file even when the
DeclContext hierarchy doesn't contain the source file (because it's
based on the Clang module structure)

Expand testing to be sure that we're getting the right calls,
diagnostics, and generated IR symbols.
@DougGregor DougGregor force-pushed the swift-attr-parsing-fixes branch from c579a63 to 8f5e941 Compare November 14, 2024 05:22
@DougGregor DougGregor requested a review from tshortli as a code owner November 14, 2024 05:22
@DougGregor DougGregor changed the title Ensure that buffers containing Clang swift_attr attributes are parsed as attributes [Clang importer + macros] Handle name lookup and type checking for expanded macros Nov 14, 2024
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit f802b67 into swiftlang:main Nov 14, 2024
3 checks passed
@DougGregor DougGregor deleted the swift-attr-parsing-fixes branch November 14, 2024 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants