[clang] [llvm] [Clang] restrict use of attribute names reserved by the C++ standard (PR #106036)
Oleksandr T. via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 16 14:58:30 PST 2025
================
@@ -173,6 +176,20 @@ static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
return false;
}
+static bool isReservedCXXAttributeName(Preprocessor &PP, IdentifierInfo *II) {
+ const LangOptions &Lang = PP.getLangOpts();
+ if (Lang.CPlusPlus &&
+ hasAttribute(AttributeCommonInfo::Syntax::AS_CXX11, /*Scope*/ nullptr, II,
+ PP.getTargetInfo(), Lang) > 0) {
+ AttributeCommonInfo::Kind AttrKind = AttributeCommonInfo::getParsedKind(
+ II, /*Scope*/ nullptr, AttributeCommonInfo::Syntax::AS_CXX11);
+ return !((AttrKind == AttributeCommonInfo::Kind::AT_Likely ||
+ AttrKind == AttributeCommonInfo::Kind::AT_Unlikely) &&
+ PP.isNextPPTokenLParen());
----------------
a-tarasyuk wrote:
@erichkeane thanks for the feedback. `isNextPPTokenLParen` detects whether the definition is function-like, as complete macro information is unavailable at this stage.
https://github.com/llvm/llvm-project/blob/d49a2d2bc9c65c787bfa04ac8ece614da48a8cd5/clang/lib/Lex/PPDirectives.cpp#L2938-L2940
I’ve updated the handling of attribute names as follows: if an attribute has required parameters, a warning will be triggered only for _function-like_ definitions such as `#define assume()`. For attributes with optional parameters like `deprecated`, a warning will be issued for both `#define deprecated` and `#define deprecated()`. In all other cases, a warning will be triggered for _non-function-like_ definitions such as `#define noreturn`.
I decided against using `hasAttribute,` as it involves plugin instance checks that don't seem relevant here. I switched to check attribute implementation
https://github.com/llvm/llvm-project/blob/65dc0d44473481d67d34dcffd1037d2f9f0e574b/clang/lib/Basic/Attributes.cpp#L25-L32
which in this case relies on a single StringSwitch/case with a set of defined attributes, and I believe it shouldn't cause performance regression.
I hope that makes sense., if you have any thoughts or suggestions, I’d be grateful to hear them. thanks
https://github.com/llvm/llvm-project/pull/106036
More information about the llvm-commits
mailing list