Skip to content

Commit cd754af

Browse files
authored
[Clang] Permit both gnu and clang prefixes on some attributes (#125796)
Summary: Some attributes have gnu extensions that share names with clang attributes. If these imply the same thing, we can specially declare this to be an alternate but equivalent spelling. This patch enables this for `no_sanitize` and provides the infrastructure for more to be added if needed. Discussions welcome on whether or not we want to bind ourselves to GNU behavior, since theoretically it's possible for GNU to silently change the semantics away from our implementation, but I'm not an expert. Fixes: #125760
1 parent ccb08b9 commit cd754af

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Removed Compiler Flags
112112
Attribute Changes in Clang
113113
--------------------------
114114

115+
- The ``no_sanitize`` attribute now accepts both ``gnu`` and ``clang`` names.
116+
115117
Improvements to Clang's diagnostics
116118
-----------------------------------
117119

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ class Clang<string name, bit allowInC = 1, int version = 1>
380380
bit AllowInC = allowInC;
381381
}
382382

383+
// This spelling combines the spellings of GCC and Clang for cases where the
384+
// spellings are equivalent for compile compatibility.
385+
class ClangGCC<string name, bit allowInC = 1, int version = 1>
386+
: Spelling<name, "ClangGCC", version> {
387+
bit AllowInC = allowInC;
388+
}
389+
383390
// HLSL Annotation spellings
384391
class HLSLAnnotation<string name> : Spelling<name, "HLSLAnnotation">;
385392

@@ -3677,7 +3684,7 @@ def X86ForceAlignArgPointer : InheritableAttr, TargetSpecificAttr<TargetAnyX86>
36773684
}
36783685

36793686
def NoSanitize : InheritableAttr {
3680-
let Spellings = [Clang<"no_sanitize">];
3687+
let Spellings = [ClangGCC<"no_sanitize">];
36813688
let Args = [VariadicStringArgument<"Sanitizers">];
36823689
let Subjects = SubjectList<[Function, ObjCMethod, GlobalVar], ErrorDiag>;
36833690
let Documentation = [NoSanitizeDocs];

clang/test/SemaCXX/attr-no-sanitize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ int f3() __attribute__((no_sanitize("address")));
2121

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

2727
// DUMP-LABEL: FunctionDecl {{.*}} f5
2828
// DUMP: NoSanitizeAttr {{.*}} address thread hwaddress

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ GetFlattenedSpellings(const Record &Attr) {
108108
Ret.emplace_back("CXX11", Name, "clang", false, *Spelling);
109109
if (Spelling->getValueAsBit("AllowInC"))
110110
Ret.emplace_back("C23", Name, "clang", false, *Spelling);
111+
} else if (Variety == "ClangGCC") {
112+
Ret.emplace_back("GNU", Name, "", false, *Spelling);
113+
Ret.emplace_back("CXX11", Name, "clang", false, *Spelling);
114+
Ret.emplace_back("CXX11", Name, "gnu", false, *Spelling);
115+
if (Spelling->getValueAsBit("AllowInC")) {
116+
Ret.emplace_back("C23", Name, "clang", false, *Spelling);
117+
Ret.emplace_back("C23", Name, "gnu", false, *Spelling);
118+
}
111119
} else {
112120
Ret.push_back(FlattenedSpelling(*Spelling));
113121
}

0 commit comments

Comments
 (0)