Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/AttributeCommonInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class AttributeCommonInfo {
UnknownAttribute,
};
enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
enum class AttrArgsInfo {
None,
Optional,
Required,
};

private:
const IdentifierInfo *AttrName = nullptr;
Expand Down Expand Up @@ -241,6 +246,8 @@ class AttributeCommonInfo {
static Kind getParsedKind(const IdentifierInfo *Name,
const IdentifierInfo *Scope, Syntax SyntaxUsed);

static AttrArgsInfo getCXX11AttrArgsInfo(const IdentifierInfo *Name);

private:
/// Get an index into the attribute spelling list
/// defined in Attr.td. This index is used by an attribute
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ int hasAttribute(AttributeCommonInfo::Syntax Syntax,
const IdentifierInfo *Scope, const IdentifierInfo *Attr,
const TargetInfo &Target, const LangOptions &LangOpts);

int hasAttribute(AttributeCommonInfo::Syntax Syntax,
const IdentifierInfo *Scope, const IdentifierInfo *Attr,
const TargetInfo &Target, const LangOptions &LangOpts,
bool CheckPlugins);

} // end namespace clang

#endif // LLVM_CLANG_BASIC_ATTRIBUTES_H
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ clang_tablegen(AttrHasAttributeImpl.inc -gen-clang-attr-has-attribute-impl
TARGET ClangAttrHasAttributeImpl
)

clang_tablegen(CXX11AttributeInfo.inc -gen-cxx11-attribute-info
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
SOURCE Attr.td
TARGET CXX11AttributeInfo
)

clang_tablegen(Builtins.inc -gen-clang-builtins
SOURCE Builtins.td
TARGET ClangBuiltins)
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,8 @@ def SignedEnumBitfield : DiagGroup<"signed-enum-bitfield">;

def ReservedModuleIdentifier : DiagGroup<"reserved-module-identifier">;
def ReservedIdentifier : DiagGroup<"reserved-identifier",
[ReservedIdAsMacro, ReservedModuleIdentifier, UserDefinedLiterals]>;
[ReservedIdAsMacro, ReservedModuleIdentifier,
UserDefinedLiterals, ReservedAttributeIdentifier]>;

// Unreachable code warning groups.
//
Expand Down
31 changes: 26 additions & 5 deletions clang/lib/Basic/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name,

int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax,
const IdentifierInfo *Scope, const IdentifierInfo *Attr,
const TargetInfo &Target, const LangOptions &LangOpts) {
const TargetInfo &Target, const LangOptions &LangOpts,
bool CheckPlugins) {
StringRef Name = Attr->getName();
// Normalize the attribute name, __foo__ becomes foo.
if (Name.size() >= 4 && Name.starts_with("__") && Name.ends_with("__"))
Expand Down Expand Up @@ -61,14 +62,23 @@ int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax,
if (res)
return res;

// Check if any plugin provides this attribute.
for (auto &Ptr : getAttributePluginInstances())
if (Ptr->hasSpelling(Syntax, Name))
return 1;
if (CheckPlugins) {
// Check if any plugin provides this attribute.
for (auto &Ptr : getAttributePluginInstances())
if (Ptr->hasSpelling(Syntax, Name))
return 1;
}

return 0;
}

int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax,
const IdentifierInfo *Scope, const IdentifierInfo *Attr,
const TargetInfo &Target, const LangOptions &LangOpts) {
return hasAttribute(Syntax, Scope, Attr, Target, LangOpts,
/*CheckPlugins*/ true);
}

const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule) {
switch (Rule) {
#define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
Expand Down Expand Up @@ -151,6 +161,17 @@ AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name,
return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
}

AttributeCommonInfo::AttrArgsInfo
AttributeCommonInfo::getCXX11AttrArgsInfo(const IdentifierInfo *Name) {
StringRef AttrName =
normalizeAttrName(Name, /*NormalizedScopeName*/ "", Syntax::AS_CXX11);
#define CXX11_ATTR_ARGS_INFO
return llvm::StringSwitch<AttributeCommonInfo::AttrArgsInfo>(AttrName)
#include "clang/Basic/CXX11AttributeInfo.inc"
.Default(AttributeCommonInfo::AttrArgsInfo::None);
#undef CXX11_ATTR_ARGS_INFO
}

std::string AttributeCommonInfo::getNormalizedFullName() const {
return static_cast<std::string>(
normalizeName(getAttrName(), getScopeName(), getSyntax()));
Expand Down
16 changes: 9 additions & 7 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
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());
hasAttribute(AttributeCommonInfo::AS_CXX11, /* Scope*/ nullptr, II,
PP.getTargetInfo(), Lang, /*CheckPlugins*/ false) > 0) {
AttributeCommonInfo::AttrArgsInfo AttrArgsInfo =
AttributeCommonInfo::getCXX11AttrArgsInfo(II);
if (AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Required)
return PP.isNextPPTokenLParen();

return !PP.isNextPPTokenLParen() ||
AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Optional;
}
return false;
}
Expand Down
100 changes: 100 additions & 0 deletions clang/test/Preprocessor/macro-reserved-attrs-cxx11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -fsyntax-only -verify -pedantic %s -DTEST1
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -fsyntax-only -verify -pedantic %s -DTEST2
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -fsyntax-only -verify -pedantic %s -DTEST3
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -fsyntax-only -verify -pedantic %s -DTEST4

#ifdef TEST1

#define assume
#undef assume

#define noreturn // expected-warning {{noreturn is a reserved attribute identifier}}
#undef noreturn // expected-warning {{noreturn 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}}

#elif TEST2

#define assume "test"
#undef assume

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

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

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

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

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

#define no_unique_address "test" // 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 "test" // expected-warning {{unlikely is a reserved attribute identifier}}
#undef unlikely // expected-warning {{unlikely is a reserved attribute identifier}}

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

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

#elif TEST3

#define assume() "test" // expected-warning {{assume is a reserved attribute identifier}}
#define deprecated() "test" // expected-warning {{deprecated is a reserved attribute identifier}}
#define nodiscard() "test" // expected-warning {{nodiscard is a reserved attribute identifier}}
#define noreturn() "test"
#define carries_dependency() "test"
#define fallthrough() "test"
#define likely() "test"
#define no_unique_address() "test"
#define unlikely() "test"
#define maybe_unused() "test"

#elif TEST4

#define assume() // expected-warning {{assume is a reserved attribute identifier}}
#define deprecated() // expected-warning {{deprecated is a reserved attribute identifier}}
#define nodiscard() // expected-warning {{nodiscard is a reserved attribute identifier}}
#define noreturn()
#define carries_dependency()
#define fallthrough()
#define likely()
#define no_unique_address()
#define unlikely()
#define maybe_unused()

#else

#error Unknown test

#endif
34 changes: 0 additions & 34 deletions clang/test/Preprocessor/macro-reserved-attrs1.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions clang/test/Preprocessor/macro-reserved-attrs2.cpp

This file was deleted.

30 changes: 30 additions & 0 deletions clang/utils/TableGen/ClangAttrEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3743,6 +3743,36 @@ void EmitClangRegularKeywordAttributeInfo(const RecordKeeper &Records,
OS << "#undef KEYWORD_ATTRIBUTE\n";
}

void EmitCXX11AttributeInfo(const RecordKeeper &Records, raw_ostream &OS) {
OS << "#if defined(CXX11_ATTR_ARGS_INFO)\n";
for (auto *R : Records.getAllDerivedDefinitions("Attr")) {
for (const FlattenedSpelling &SI : GetFlattenedSpellings(*R)) {
if (SI.variety() == "CXX11" && SI.nameSpace().empty()) {
unsigned RequiredArgs = 0;
unsigned OptionalArgs = 0;
for (const auto *Arg : R->getValueAsListOfDefs("Args")) {
if (Arg->getValueAsBit("Fake"))
continue;

if (Arg->getValueAsBit("Optional"))
OptionalArgs++;
else
RequiredArgs++;
}
OS << ".Case(\"" << SI.getSpellingRecord().getValueAsString("Name")
<< "\","
<< "AttributeCommonInfo::AttrArgsInfo::"
<< (RequiredArgs ? "Required"
: OptionalArgs ? "Optional"
: "None")
<< ")"
<< "\n";
}
}
}
OS << "#endif // CXX11_ATTR_ARGS_INFO\n";
}

// Emits the list of spellings for attributes.
void EmitClangAttrHasAttrImpl(const RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Code to implement the __has_attribute logic", OS,
Expand Down
6 changes: 6 additions & 0 deletions clang/utils/TableGen/TableGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum ActionType {
GenClangOpenCLBuiltins,
GenClangOpenCLBuiltinHeader,
GenClangOpenCLBuiltinTests,
GenCXX11AttributeInfo,
GenArmNeon,
GenArmFP16,
GenArmBF16,
Expand Down Expand Up @@ -225,6 +226,8 @@ cl::opt<ActionType> Action(
"Generate OpenCL builtin header"),
clEnumValN(GenClangOpenCLBuiltinTests, "gen-clang-opencl-builtin-tests",
"Generate OpenCL builtin declaration tests"),
clEnumValN(GenCXX11AttributeInfo, "gen-cxx11-attribute-info",
"Generate CXX11 attributes info"),
clEnumValN(GenArmNeon, "gen-arm-neon", "Generate arm_neon.h for clang"),
clEnumValN(GenArmFP16, "gen-arm-fp16", "Generate arm_fp16.h for clang"),
clEnumValN(GenArmBF16, "gen-arm-bf16", "Generate arm_bf16.h for clang"),
Expand Down Expand Up @@ -333,6 +336,9 @@ bool ClangTableGenMain(raw_ostream &OS, const RecordKeeper &Records) {
case GenClangAttrSubjectMatchRulesParserStringSwitches:
EmitClangAttrSubjectMatchRulesParserStringSwitches(Records, OS);
break;
case GenCXX11AttributeInfo:
EmitCXX11AttributeInfo(Records, OS);
break;
case GenClangAttrImpl:
EmitClangAttrImpl(Records, OS);
break;
Expand Down
2 changes: 2 additions & 0 deletions clang/utils/TableGen/TableGenBackends.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ void EmitClangAttrParserStringSwitches(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);
void EmitClangAttrSubjectMatchRulesParserStringSwitches(
const llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
void EmitCXX11AttributeInfo(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);
void EmitClangAttrClass(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);
void EmitClangAttrImpl(const llvm::RecordKeeper &Records,
Expand Down
Loading