Skip to content

[Clang] restrict use of attribute names reserved by the C++ standard #106036

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 13 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,8 @@ Improvements to Clang's diagnostics
require(scope); // Warning! Requires mu1.
}

- Clang now diagnoses the use of attribute names reserved by the C++ standard (#GH92196).

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/AttributeCommonInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AttributeCommonInfo {
};
enum Kind {
#define PARSED_ATTR(NAME) AT_##NAME,
#include "clang/Sema/AttrParsedAttrList.inc"
#include "clang/Basic/AttrParsedAttrList.inc"
#undef PARSED_ATTR
NoSemaHandlerAttribute,
IgnoredAttribute,
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ clang_tablegen(AttrList.inc -gen-clang-attr-list
SOURCE Attr.td
TARGET ClangAttrList)

clang_tablegen(AttrParsedAttrList.inc -gen-clang-attr-parsed-attr-list
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
SOURCE Attr.td
TARGET ClangAttrParsedAttrList)

clang_tablegen(AttrSubMatchRulesList.inc -gen-clang-attr-subject-match-rule-list
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
SOURCE Attr.td
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ def AmbiguousMacro : DiagGroup<"ambiguous-macro">;
def KeywordAsMacro : DiagGroup<"keyword-macro">;
def ReservedIdAsMacro : DiagGroup<"reserved-macro-identifier">;
def ReservedIdAsMacroAlias : DiagGroup<"reserved-id-macro", [ReservedIdAsMacro]>;
def ReservedAttributeIdentifier : DiagGroup<"reserved-attribute-identifier">;
def RestrictExpansionMacro : DiagGroup<"restrict-expansion">;
def FinalMacro : DiagGroup<"final-macro">;

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticLexKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ def warn_pp_macro_hides_keyword : Extension<
def warn_pp_macro_is_reserved_id : Warning<
"macro name is a reserved identifier">, DefaultIgnore,
InGroup<ReservedIdAsMacro>;
def warn_pp_macro_is_reserved_attribute_id : Warning<
"%0 is a reserved attribute identifier">, InGroup<ReservedAttributeIdentifier>;
def warn_pp_objc_macro_redef_ignored : Warning<
"ignoring redefinition of Objective-C qualifier macro">,
InGroup<DiagGroup<"objc-macro-redefinition">>;
Expand Down
10 changes: 5 additions & 5 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,11 @@ class Preprocessor {
}
}

/// Determine whether the next preprocessor token to be
/// lexed is a '('. If so, consume the token and return true, if not, this
/// method should have no observable side-effect on the lexed tokens.
bool isNextPPTokenLParen();

private:
/// Identifiers used for SEH handling in Borland. These are only
/// allowed in particular circumstances
Expand Down Expand Up @@ -2648,11 +2653,6 @@ class Preprocessor {

void removeCachedMacroExpandedTokensOfLastLexer();

/// Determine whether the next preprocessor token to be
/// lexed is a '('. If so, consume the token and return true, if not, this
/// method should have no observable side-effect on the lexed tokens.
bool isNextPPTokenLParen();

/// After reading "MACRO(", this method is invoked to read all of the formal
/// arguments specified for the macro invocation. Returns null on error.
MacroArgs *ReadMacroCallArgumentList(Token &MacroName, MacroInfo *MI,
Expand Down
5 changes: 0 additions & 5 deletions clang/include/clang/Sema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ clang_tablegen(AttrTemplateInstantiate.inc -gen-clang-attr-template-instantiate
SOURCE ../Basic/Attr.td
TARGET ClangAttrTemplateInstantiate)

clang_tablegen(AttrParsedAttrList.inc -gen-clang-attr-parsed-attr-list
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
SOURCE ../Basic/Attr.td
TARGET ClangAttrParsedAttrList)

clang_tablegen(AttrParsedAttrKinds.inc -gen-clang-attr-parsed-attr-kinds
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
SOURCE ../Basic/Attr.td
Expand Down
26 changes: 25 additions & 1 deletion clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
///
//===----------------------------------------------------------------------===//

#include "clang/Basic/AttributeCommonInfo.h"
#include "clang/Basic/Attributes.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/DirectoryEntry.h"
#include "clang/Basic/FileManager.h"
Expand Down Expand Up @@ -97,7 +99,8 @@ SourceRange Preprocessor::DiscardUntilEndOfDirective(Token &Tmp) {
enum MacroDiag {
MD_NoWarn, //> Not a reserved identifier
MD_KeywordDef, //> Macro hides keyword, enabled by default
MD_ReservedMacro //> #define of #undef reserved id, disabled by default
MD_ReservedMacro, //> #define of #undef reserved id, disabled by default
MD_ReservedAttributeIdentifier
};

/// Enumerates possible %select values for the pp_err_elif_after_else and
Expand Down Expand Up @@ -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());
}
return false;
}

static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
const LangOptions &Lang = PP.getLangOpts();
StringRef Text = II->getName();
Expand All @@ -182,6 +199,8 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
return MD_KeywordDef;
if (Lang.CPlusPlus11 && (Text == "override" || Text == "final"))
return MD_KeywordDef;
if (isReservedCXXAttributeName(PP, II))
return MD_ReservedAttributeIdentifier;
return MD_NoWarn;
}

Expand All @@ -190,6 +209,8 @@ static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
// Do not warn on keyword undef. It is generally harmless and widely used.
if (isReservedInAllContexts(II->isReserved(Lang)))
return MD_ReservedMacro;
if (isReservedCXXAttributeName(PP, II))
return MD_ReservedAttributeIdentifier;
return MD_NoWarn;
}

Expand Down Expand Up @@ -365,6 +386,9 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
}
if (D == MD_ReservedMacro)
Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
if (D == MD_ReservedAttributeIdentifier)
Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_attribute_id)
<< II->getName();
}

// Okay, we got a good identifier.
Expand Down
34 changes: 34 additions & 0 deletions clang/test/Preprocessor/macro-reserved-attrs1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -fsyntax-only -verify -pedantic %s

#define noreturn 1 // expected-warning {{noreturn is a reserved attribute identifier}}
#undef noreturn // expected-warning {{noreturn is a reserved attribute identifier}}

#define assume 1 // expected-warning {{assume is a reserved attribute identifier}}
#undef assume // expected-warning {{assume is a reserved attribute identifier}}

#define carries_dependency 1 // expected-warning {{carries_dependency is a reserved attribute identifier}}
#undef carries_dependency // expected-warning {{carries_dependency is a reserved attribute identifier}}

#define deprecated 1 // expected-warning {{deprecated is a reserved attribute identifier}}
#undef deprecated // expected-warning {{deprecated is a reserved attribute identifier}}

#define fallthrough 1 // expected-warning {{fallthrough is a reserved attribute identifier}}
#undef fallthrough // expected-warning {{fallthrough is a reserved attribute identifier}}

#define likely 1 // expected-warning {{likely is a reserved attribute identifier}}
#undef likely // expected-warning {{likely is a reserved attribute identifier}}

#define no_unique_address 1 // expected-warning {{no_unique_address is a reserved attribute identifier}}
#undef no_unique_address // expected-warning {{no_unique_address is a reserved attribute identifier}}

#define unlikely 1 // expected-warning {{unlikely is a reserved attribute identifier}}
#undef unlikely // expected-warning {{unlikely is a reserved attribute identifier}}

#define maybe_unused 1 // expected-warning {{maybe_unused is a reserved attribute identifier}}
#undef maybe_unused // expected-warning {{maybe_unused is a reserved attribute identifier}}

#define nodiscard 1 // expected-warning {{nodiscard is a reserved attribute identifier}}
#undef nodiscard // expected-warning {{nodiscard is a reserved attribute identifier}}

#define likely() 1 // ok
#define unlikely() 1 // ok
34 changes: 34 additions & 0 deletions clang/test/Preprocessor/macro-reserved-attrs2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -fsyntax-only -verify -pedantic %s

#define noreturn // expected-warning {{noreturn is a reserved attribute identifier}}
#undef noreturn // expected-warning {{noreturn is a reserved attribute identifier}}

#define assume // expected-warning {{assume is a reserved attribute identifier}}
#undef assume // expected-warning {{assume is a reserved attribute identifier}}

#define carries_dependency // expected-warning {{carries_dependency is a reserved attribute identifier}}
#undef carries_dependency // expected-warning {{carries_dependency is a reserved attribute identifier}}

#define deprecated // expected-warning {{deprecated is a reserved attribute identifier}}
#undef deprecated // expected-warning {{deprecated is a reserved attribute identifier}}

#define fallthrough // expected-warning {{fallthrough is a reserved attribute identifier}}
#undef fallthrough // expected-warning {{fallthrough is a reserved attribute identifier}}

#define likely // expected-warning {{likely is a reserved attribute identifier}}
#undef likely // expected-warning {{likely is a reserved attribute identifier}}

#define no_unique_address // expected-warning {{no_unique_address is a reserved attribute identifier}}
#undef no_unique_address // expected-warning {{no_unique_address is a reserved attribute identifier}}

#define unlikely // expected-warning {{unlikely is a reserved attribute identifier}}
#undef unlikely // expected-warning {{unlikely is a reserved attribute identifier}}

#define maybe_unused // expected-warning {{maybe_unused is a reserved attribute identifier}}
#undef maybe_unused // expected-warning {{maybe_unused is a reserved attribute identifier}}

#define nodiscard // expected-warning {{nodiscard is a reserved attribute identifier}}
#undef nodiscard // expected-warning {{nodiscard is a reserved attribute identifier}}

#define likely() // ok
#define unlikely() // ok
5 changes: 1 addition & 4 deletions llvm/utils/gn/secondary/clang/lib/Basic/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static_library("Basic") {
public_deps = [
# public_dep because public header Version.h includes generated Version.inc.
"//clang/include/clang/Basic:AttrList",
"//clang/include/clang/Basic:AttrParsedAttrList",
"//clang/include/clang/Basic:AttrSubMatchRulesList",
"//clang/include/clang/Basic:Builtins",
"//clang/include/clang/Basic:BuiltinsBPF",
Expand All @@ -42,10 +43,6 @@ static_library("Basic") {
"//clang/include/clang/Basic:riscv_vector_builtins",
"//clang/include/clang/Basic:version",

# public_dep because public header AttributeCommonInfo.h includes generated
# AttrParsedAttrList.inc.
"//clang/include/clang/Sema:AttrParsedAttrList",

# public_dep because public header OpenMPKinds.h includes generated
# OMP.h.inc.
"//llvm/include/llvm/Frontend/OpenMP:public_tablegen",
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/gn/secondary/clang/lib/Sema/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ static_library("Sema") {
configs += [ "//llvm/utils/gn/build:clang_code" ]
deps = [
":OpenCLBuiltins",
"//clang/include/clang/Basic:AttrParsedAttrList",
"//clang/include/clang/Basic:arm_cde_builtin_aliases",
"//clang/include/clang/Basic:arm_cde_builtin_sema",
"//clang/include/clang/Basic:arm_mve_builtin_aliases",
Expand All @@ -22,7 +23,6 @@ static_library("Sema") {
"//clang/include/clang/Basic:riscv_vector_builtin_sema",
"//clang/include/clang/Sema:AttrParsedAttrImpl",
"//clang/include/clang/Sema:AttrParsedAttrKinds",
"//clang/include/clang/Sema:AttrParsedAttrList",
"//clang/include/clang/Sema:AttrSpellingListIndex",
"//clang/include/clang/Sema:AttrTemplateInstantiate",
"//clang/lib/APINotes",
Expand Down
9 changes: 4 additions & 5 deletions utils/bazel/llvm-project-overlay/clang/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ gentbl(
"-gen-clang-attr-list",
"include/clang/Basic/AttrList.inc",
),
(
"-gen-clang-attr-parsed-attr-list",
"include/clang/Basic/AttrParsedAttrList.inc",
),
(
"-gen-clang-attr-subject-match-rule-list",
"include/clang/Basic/AttrSubMatchRulesList.inc",
Expand Down Expand Up @@ -1139,10 +1143,6 @@ gentbl(
"-gen-clang-attr-parsed-attr-kinds",
"include/clang/Sema/AttrParsedAttrKinds.inc",
),
(
"-gen-clang-attr-parsed-attr-list",
"include/clang/Sema/AttrParsedAttrList.inc",
),
(
"-gen-clang-attr-spelling-index",
"include/clang/Sema/AttrSpellingListIndex.inc",
Expand Down Expand Up @@ -1178,7 +1178,6 @@ cc_library(
textual_hdrs = [
"include/clang/Sema/AttrParsedAttrImpl.inc",
"include/clang/Sema/AttrParsedAttrKinds.inc",
"include/clang/Sema/AttrParsedAttrList.inc",
"include/clang/Sema/AttrSpellingListIndex.inc",
"include/clang/Sema/AttrTemplateInstantiate.inc",
"lib/Sema/OpenCLBuiltins.inc",
Expand Down
Loading