[clang] Match against all plugins when parsing microsoft attributes (PR #86426)

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 1 11:54:47 PDT 2024


https://github.com/apache-hb updated https://github.com/llvm/llvm-project/pull/86426

>From 245a21512d8658225b17b91b8af4764f54084e01 Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Sun, 24 Mar 2024 03:03:47 -0400
Subject: [PATCH 1/6] Match against all plugins when parsing microsoft
 attributes

fixes #86422
---
 clang/lib/Parse/ParseDeclCXX.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

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;

>From 4e47899a9fb17dec5931007b5025f6ce97dbe3eb Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Sun, 24 Mar 2024 03:05:54 -0400
Subject: [PATCH 2/6] Apply formatting

---
 clang/lib/Parse/ParseDeclCXX.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index d05b3a455f7f63..856ac11f7bb839 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -5064,9 +5064,12 @@ void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
 
         // For HLSL we want to handle all attributes, but for MSVC compat, we
         // silently ignore unknown Microsoft attributes.
-        AttributeCommonInfo Info{II, NameLoc, AttributeCommonInfo::Form::Microsoft()};
-        const ParsedAttrInfo& AttrInfo = ParsedAttrInfo::get(Info);
-        if (getLangOpts().HLSL || AttrInfo.hasSpelling(AttributeCommonInfo::AS_Microsoft, II->getName())) {
+        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;

>From 8adb5bc07dd23afac4f66f6658a4197fb97f058f Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Mon, 25 Mar 2024 13:37:00 -0400
Subject: [PATCH 3/6] use hasAttribute instead of ParsedAttrInfo::get

---
 clang/lib/Parse/ParseDeclCXX.cpp | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 856ac11f7bb839..619f7f88bfdbfe 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -5064,12 +5064,9 @@ void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
 
         // For HLSL we want to handle all attributes, but for MSVC compat, we
         // silently ignore unknown Microsoft attributes.
-        AttributeCommonInfo Info{II, NameLoc,
-                                 AttributeCommonInfo::Form::Microsoft()};
-        const ParsedAttrInfo &AttrInfo = ParsedAttrInfo::get(Info);
-        if (getLangOpts().HLSL ||
-            AttrInfo.hasSpelling(AttributeCommonInfo::AS_Microsoft,
-                                 II->getName())) {
+        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)) {
             CachedTokens OpenMPTokens;

>From b3f297eec02643452a8815d7d62b1b5a0f07760b Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Mon, 25 Mar 2024 13:54:27 -0400
Subject: [PATCH 4/6] Update ReleaseNotes.rst

---
 clang/docs/ReleaseNotes.rst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8054d90fc70f93..df73abb12e89fa 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -345,6 +345,9 @@ Bug Fixes to Compiler Builtins
 Bug Fixes to Attribute Support
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Clang now correctly matches plugin attributes with microsoft ``[attribute]`` syntax.
+  (#GH86422)
+
 Bug Fixes to C++ Support
 ^^^^^^^^^^^^^^^^^^^^^^^^
 

>From 1b4823b3bd6c679fce7d74f2c361a50ad4ad0776 Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Mon, 25 Mar 2024 18:54:54 -0400
Subject: [PATCH 5/6] add test for custom microsoft attributes

---
 clang/examples/CMakeLists.txt                 |  1 +
 .../MicrosoftAttributes/CMakeLists.txt        | 11 ++++
 .../MicrosoftAttributes.cpp                   | 60 +++++++++++++++++++
 clang/test/CMakeLists.txt                     |  3 +-
 clang/test/Frontend/ms-attributes.cpp         |  6 ++
 5 files changed, 80 insertions(+), 1 deletion(-)
 create mode 100644 clang/examples/MicrosoftAttributes/CMakeLists.txt
 create mode 100644 clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp
 create mode 100644 clang/test/Frontend/ms-attributes.cpp

diff --git a/clang/examples/CMakeLists.txt b/clang/examples/CMakeLists.txt
index 2396ecac16b2dc..58f11bc51ff697 100644
--- a/clang/examples/CMakeLists.txt
+++ b/clang/examples/CMakeLists.txt
@@ -10,4 +10,5 @@ if(CLANG_PLUGIN_SUPPORT)
   add_subdirectory(Attribute)
   add_subdirectory(CallSuperAttribute)
   add_subdirectory(PluginsOrder)
+  add_subdirectory(MicrosoftAttributes)
 endif()
diff --git a/clang/examples/MicrosoftAttributes/CMakeLists.txt b/clang/examples/MicrosoftAttributes/CMakeLists.txt
new file mode 100644
index 00000000000000..e4532974084edd
--- /dev/null
+++ b/clang/examples/MicrosoftAttributes/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_llvm_library(MicrosoftAttributes MODULE MicrosoftAttributes.cpp PLUGIN_TOOL clang)
+
+if(WIN32 OR CYGWIN)
+  target_link_libraries(MicrosoftAttributes PRIVATE
+    clangAST
+    clangBasic
+    clangFrontend
+    clangLex
+    LLVMSupport
+    )
+endif()
diff --git a/clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp b/clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp
new file mode 100644
index 00000000000000..571d6f8624d81e
--- /dev/null
+++ b/clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp
@@ -0,0 +1,60 @@
+//===- Attribute.cpp ------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Example clang plugin which adds an an annotation to class declarations
+// with a microsoft style '[example]' attribute.
+//
+// This plugin is used by clang/test/Frontend/ms-attributes tests.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Attr.h"
+#include "clang/Sema/ParsedAttr.h"
+#include "clang/Sema/Sema.h"
+#include "clang/Sema/SemaDiagnostic.h"
+#include "llvm/IR/Attributes.h"
+
+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;
+    }
+};
+
+} // namespace
+
+static ParsedAttrInfoRegistry::Add<ExampleAttrInfo> Y("microsoft-example", "");
diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt
index fcfca354f4a75f..78c07b4be390d6 100644
--- a/clang/test/CMakeLists.txt
+++ b/clang/test/CMakeLists.txt
@@ -79,7 +79,7 @@ list(APPEND CLANG_TEST_DEPS
   diagtool
   hmaptool
   )
-  
+
 if(CLANG_ENABLE_STATIC_ANALYZER)
   list(APPEND CLANG_TEST_DEPS
     clang-check
@@ -98,6 +98,7 @@ if(CLANG_BUILD_EXAMPLES AND CLANG_PLUGIN_SUPPORT)
   list(APPEND CLANG_TEST_DEPS
     Attribute
     AnnotateFunctions
+    MicrosoftAttributes
     CallSuperAttr
     PluginsOrder
     PrintFunctionNames
diff --git a/clang/test/Frontend/ms-attributes.cpp b/clang/test/Frontend/ms-attributes.cpp
new file mode 100644
index 00000000000000..ed15a31cd87a69
--- /dev/null
+++ b/clang/test/Frontend/ms-attributes.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang -fplugin=%llvmshlibdir/MicrosoftAttributes%pluginext -fms-extensions -E %s | FileCheck %s --check-prefix=MS
+// REQUIRE: plugins, examples
+// expected-no-diagnostics
+[ms_example]
+class C {};
+// CHECK-NEXT: AnnotateAttr{{.*}} "ms_example"

>From 82ef047d9aae3d1d58bc5127cb44601e3f7a3e69 Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Mon, 1 Apr 2024 14:54:36 -0400
Subject: [PATCH 6/6] fix failing test

---
 clang/test/Frontend/ms-attributes.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/test/Frontend/ms-attributes.cpp b/clang/test/Frontend/ms-attributes.cpp
index ed15a31cd87a69..1dfcd76727e7a8 100644
--- a/clang/test/Frontend/ms-attributes.cpp
+++ b/clang/test/Frontend/ms-attributes.cpp
@@ -1,6 +1,6 @@
-// RUN: %clang -fplugin=%llvmshlibdir/MicrosoftAttributes%pluginext -fms-extensions -E %s | FileCheck %s --check-prefix=MS
-// REQUIRE: plugins, examples
+// RUN: %clang -fplugin=%llvmshlibdir/MicrosoftAttributes%pluginext -fsyntax-only -fms-extensions -E %s | FileCheck %s
+// REQUIRES: plugins, examples
 // expected-no-diagnostics
 [ms_example]
 class C {};
-// CHECK-NEXT: AnnotateAttr{{.*}} "ms_example"
+// CHECK: AnnotateAttr{{.*}} "ms_example"



More information about the cfe-commits mailing list