Skip to content

Match against all plugins when parsing microsoft attributes #86426

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

apache-hb
Copy link

Fixes #86422

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2024

@llvm/pr-subscribers-clang

Author: Elliot (apache-hb)

Changes

Fixes #86422


Full diff: https://github.com/llvm/llvm-project/pull/86426.diff

1 Files Affected:

  • (modified) clang/lib/Parse/ParseDeclCXX.cpp (+4-3)
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 63fe678cbb29e2..d05b3a455f7f63 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -5061,11 +5061,12 @@ void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
         IdentifierInfo *II = Tok.getIdentifierInfo();
         SourceLocation NameLoc = Tok.getLocation();
         ConsumeToken();
-        ParsedAttr::Kind AttrKind =
-            ParsedAttr::getParsedKind(II, nullptr, ParsedAttr::AS_Microsoft);
+
         // For HLSL we want to handle all attributes, but for MSVC compat, we
         // silently ignore unknown Microsoft attributes.
-        if (getLangOpts().HLSL || AttrKind != ParsedAttr::UnknownAttribute) {
+        AttributeCommonInfo Info{II, NameLoc, AttributeCommonInfo::Form::Microsoft()};
+        const ParsedAttrInfo& AttrInfo = ParsedAttrInfo::get(Info);
+        if (getLangOpts().HLSL || AttrInfo.hasSpelling(AttributeCommonInfo::AS_Microsoft, II->getName())) {
           bool AttrParsed = false;
           if (Tok.is(tok::l_paren)) {
             CachedTokens OpenMPTokens;

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

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

In addition to the comment below, this also needs a release note as well as some tests.

Also, as the bot has already pointed out, you need to set your email address to public.

@to268
Copy link
Contributor

to268 commented Apr 5, 2024

CC: @erichkeane, since it's a PR about attributes.
I can't add the extension::microsoftlabel to the PR myself.

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

This has enough to do with parsing that i think Aaron should take a look too.

Also, this needs a parser test, not just the plugin test. The plugins/examples aren't built often enough, so this needs to be a full lit test.

Copy link

github-actions bot commented Apr 8, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 5d7fd6a04a6748936dece9d90481b2ba4ec97e53 6ba913fb0d3efdb17ae481ccad63ccde2170d4e2 -- clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp clang/test/Frontend/ms-attributes.cpp clang/lib/Parse/ParseDeclCXX.cpp
View the diff from clang-format here.
diff --git a/clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp b/clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp
index 571d6f8624..dd252f7d5b 100644
--- a/clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp
+++ b/clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp
@@ -25,34 +25,35 @@ using namespace clang;
 namespace {
 
 struct ExampleAttrInfo : public ParsedAttrInfo {
-    ExampleAttrInfo() {
-        // Can take up to 1 optional argument.
-        OptArgs = 1;
-
-        // Only Microsoft-style [ms_example] supported.
-        // e.g.:
-        // [ms_example] class C {};
-        // [ms_example] void f() {}
-        static constexpr Spelling S[] = {
-            {ParsedAttr::AS_Microsoft, "ms_example"}
-        };
-        Spellings = S;
-    }
-
-    bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr, const Decl *D) const override {
-        // This attribute can be applied to any declaration.
-        if (!isa<CXXRecordDecl>(D)) {
-            S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
-                << Attr << Attr.isRegularKeywordAttribute() << "classes";
-        }
-        return true;
-    }
-
-    AttrHandling handleDeclAttribute(Sema &S, Decl *D, const ParsedAttr &Attr) const override {
-        // Add an annotation to the declaration.
-        D->addAttr(AnnotateAttr::Create(S.Context, "ms_example", nullptr, 0, Attr.getRange()));
-        return AttributeApplied;
+  ExampleAttrInfo() {
+    // Can take up to 1 optional argument.
+    OptArgs = 1;
+
+    // Only Microsoft-style [ms_example] supported.
+    // e.g.:
+    // [ms_example] class C {};
+    // [ms_example] void f() {}
+    static constexpr Spelling S[] = {{ParsedAttr::AS_Microsoft, "ms_example"}};
+    Spellings = S;
+  }
+
+  bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
+                            const Decl *D) const override {
+    // This attribute can be applied to any declaration.
+    if (!isa<CXXRecordDecl>(D)) {
+      S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
+          << Attr << Attr.isRegularKeywordAttribute() << "classes";
     }
+    return true;
+  }
+
+  AttrHandling handleDeclAttribute(Sema &S, Decl *D,
+                                   const ParsedAttr &Attr) const override {
+    // Add an annotation to the declaration.
+    D->addAttr(AnnotateAttr::Create(S.Context, "ms_example", nullptr, 0,
+                                    Attr.getRange()));
+    return AttributeApplied;
+  }
 };
 
 } // namespace
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 619f7f88bf..b323f053c4 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -5064,8 +5064,8 @@ void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
 
         // For HLSL we want to handle all attributes, but for MSVC compat, we
         // silently ignore unknown Microsoft attributes.
-        int Attr = hasAttribute(AttributeCommonInfo::Syntax::AS_Microsoft, nullptr,
-                                II, getTargetInfo(), getLangOpts());
+        int Attr = hasAttribute(AttributeCommonInfo::Syntax::AS_Microsoft,
+                                nullptr, II, getTargetInfo(), getLangOpts());
         if (getLangOpts().HLSL || Attr != 0) {
           bool AttrParsed = false;
           if (Tok.is(tok::l_paren)) {

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

I'm really sorry for not realizing this was held up on me -- mea culpa!

This LGTM (modulo the request for a parser test @erichkeane suggested) with a caveat: Clang has ignored Microsoft style attributes for a long time, so it's entirely possible that we don't correctly implement their parsing in all cases.

@@ -0,0 +1,61 @@
//===- Attribute.cpp ------------------------------------------------------===//
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
//===- Attribute.cpp ------------------------------------------------------===//
//===- MicrosoftAttributes.cpp --------------------------------------------===//

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category extension:microsoft
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ParsedAttr plugins with AS_Microsoft spellings are never matched.
6 participants