Skip to content

[Clang] Permit both gnu and clang prefixes on some attributes #125796

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 2 commits into from
Feb 5, 2025
Merged
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Removed Compiler Flags
Attribute Changes in Clang
--------------------------

- The ``no_sanitize`` attribute now accepts both ``gnu`` and ``clang`` names.

Improvements to Clang's diagnostics
-----------------------------------

Expand Down
9 changes: 8 additions & 1 deletion clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ class Clang<string name, bit allowInC = 1, int version = 1>
bit AllowInC = allowInC;
}

// This spelling combines the spellings of GCC and Clang for cases where the
// spellings are equivalent for compile compatibility.
class ClangGCC<string name, bit allowInC = 1, int version = 1>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Could have probably used this for NoStackProtector, too.

NoSanitizeSpecific was almost a candidate, but the attribute has different spellings between compiler namespaces. Same for AcquireCapability.

: Spelling<name, "ClangGCC", version> {
bit AllowInC = allowInC;
}

// HLSL Annotation spellings
class HLSLAnnotation<string name> : Spelling<name, "HLSLAnnotation">;

Expand Down Expand Up @@ -3677,7 +3684,7 @@ def X86ForceAlignArgPointer : InheritableAttr, TargetSpecificAttr<TargetAnyX86>
}

def NoSanitize : InheritableAttr {
let Spellings = [Clang<"no_sanitize">];
let Spellings = [ClangGCC<"no_sanitize">];
let Args = [VariadicStringArgument<"Sanitizers">];
let Subjects = SubjectList<[Function, ObjCMethod, GlobalVar], ErrorDiag>;
let Documentation = [NoSanitizeDocs];
Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaCXX/attr-no-sanitize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ int f3() __attribute__((no_sanitize("address")));

// DUMP-LABEL: FunctionDecl {{.*}} f4
// DUMP: NoSanitizeAttr {{.*}} hwaddress
// PRINT: {{\[\[}}clang::no_sanitize("hwaddress")]] int f4()
[[clang::no_sanitize("hwaddress")]] int f4();
// PRINT: {{\[\[}}gnu::no_sanitize("hwaddress")]] int f4()
[[gnu::no_sanitize("hwaddress")]] int f4();

// DUMP-LABEL: FunctionDecl {{.*}} f5
// DUMP: NoSanitizeAttr {{.*}} address thread hwaddress
Expand Down
8 changes: 8 additions & 0 deletions clang/utils/TableGen/ClangAttrEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ GetFlattenedSpellings(const Record &Attr) {
Ret.emplace_back("CXX11", Name, "clang", false, *Spelling);
if (Spelling->getValueAsBit("AllowInC"))
Ret.emplace_back("C23", Name, "clang", false, *Spelling);
} else if (Variety == "ClangGCC") {
Ret.emplace_back("GNU", Name, "", false, *Spelling);
Ret.emplace_back("CXX11", Name, "clang", false, *Spelling);
Ret.emplace_back("CXX11", Name, "gnu", false, *Spelling);
if (Spelling->getValueAsBit("AllowInC")) {
Ret.emplace_back("C23", Name, "clang", false, *Spelling);
Ret.emplace_back("C23", Name, "gnu", false, *Spelling);
}
} else {
Ret.push_back(FlattenedSpelling(*Spelling));
}
Expand Down