Skip to content

Commit 00d648b

Browse files
alvinhochunmstorsjo
authored andcommitted
[clang] Make guard(nocf) attribute available only for Windows
Control Flow Guard is only supported on Windows target, therefore there is no point to make it an accepted attribute for other targets. Reviewed By: rnk, aaron.ballman Differential Revision: https://reviews.llvm.org/D132661
1 parent a845d8f commit 00d648b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

clang/docs/ReleaseNotes.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ Attribute Changes in Clang
146146
``[[clang::guard(nocf)]]``, which is equivalent to ``__declspec(guard(nocf))``
147147
when using the MSVC environment. This is to support enabling Windows Control
148148
Flow Guard checks with the ability to disable them for specific functions when
149-
using the MinGW environment.
149+
using the MinGW environment. This attribute is only available for Windows
150+
targets.
150151

151152
Windows Support
152153
---------------

clang/include/clang/Basic/Attr.td

+4-1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ def TargetRISCV : TargetArch<["riscv32", "riscv64"]>;
399399
def TargetX86 : TargetArch<["x86"]>;
400400
def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
401401
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
402+
def TargetWindows : TargetSpec {
403+
let OSes = ["Win32"];
404+
}
402405
def TargetHasDLLImportExport : TargetSpec {
403406
let CustomCode = [{ Target.getTriple().hasDLLImportExport() }];
404407
}
@@ -3494,7 +3497,7 @@ def MSAllocator : InheritableAttr {
34943497
let Documentation = [MSAllocatorDocs];
34953498
}
34963499

3497-
def CFGuard : InheritableAttr {
3500+
def CFGuard : InheritableAttr, TargetSpecificAttr<TargetWindows> {
34983501
// Currently only the __declspec(guard(nocf)) modifier is supported. In future
34993502
// we might also want to support __declspec(guard(suppress)).
35003503
let Spellings = [Declspec<"guard">, Clang<"guard">];

clang/test/Sema/attr-guard_nocf.c

+10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
// RUN: %clang_cc1 -triple %ms_abi_triple -fms-extensions -verify -std=c++11 -fsyntax-only -x c++ %s
33
// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -verify -fsyntax-only %s
44
// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -verify -std=c++11 -fsyntax-only -x c++ %s
5+
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -verify -fsyntax-only %s
6+
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -verify -std=c++11 -fsyntax-only -x c++ %s
57

68
// The x86_64-w64-windows-gnu version tests mingw target, which relies on
79
// __declspec(...) being defined as __attribute__((...)) by compiler built-in.
810

11+
#if defined(_WIN32)
12+
913
// Function definition.
1014
__declspec(guard(nocf)) void testGuardNoCF(void) { // no warning
1115
}
@@ -35,3 +39,9 @@ __declspec(guard(nocf, nocf)) void testGuardNoCFTooManyParams(void) { // expecte
3539
// 'guard' Attribute argument must be a supported identifier.
3640
__declspec(guard(cf)) void testGuardNoCFInvalidParam(void) { // expected-warning {{'guard' attribute argument not supported: 'cf'}}
3741
}
42+
43+
#else
44+
45+
__attribute((guard(nocf))) void testGNUStyleGuardNoCF(void) {} // expected-warning {{unknown attribute 'guard' ignored}}
46+
47+
#endif

0 commit comments

Comments
 (0)