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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions clang/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ if(CLANG_PLUGIN_SUPPORT)
add_subdirectory(Attribute)
add_subdirectory(CallSuperAttribute)
add_subdirectory(PluginsOrder)
add_subdirectory(MicrosoftAttributes)
endif()
11 changes: 11 additions & 0 deletions clang/examples/MicrosoftAttributes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
61 changes: 61 additions & 0 deletions clang/examples/MicrosoftAttributes/MicrosoftAttributes.cpp
Original file line number Diff line number Diff line change
@@ -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 --------------------------------------------===//

//
// 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", "");
7 changes: 4 additions & 3 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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;
Expand Down
3 changes: 2 additions & 1 deletion clang/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ list(APPEND CLANG_TEST_DEPS
diagtool
hmaptool
)

if(CLANG_ENABLE_STATIC_ANALYZER)
list(APPEND CLANG_TEST_DEPS
clang-check
Expand All @@ -98,6 +98,7 @@ if(CLANG_BUILD_EXAMPLES AND CLANG_PLUGIN_SUPPORT)
list(APPEND CLANG_TEST_DEPS
Attribute
AnnotateFunctions
MicrosoftAttributes
CallSuperAttr
PluginsOrder
PrintFunctionNames
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Frontend/ms-attributes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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: AnnotateAttr{{.*}} "ms_example"