diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -146,7 +146,8 @@ ``[[clang::guard(nocf)]]``, which is equivalent to ``__declspec(guard(nocf))`` when using the MSVC environment. This is to support enabling Windows Control Flow Guard checks with the ability to disable them for specific functions when - using the MinGW environment. + using the MinGW environment. This attribute is only available for Windows + targets. Windows Support --------------- diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -399,6 +399,9 @@ def TargetX86 : TargetArch<["x86"]>; def TargetAnyX86 : TargetArch<["x86", "x86_64"]>; def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>; +def TargetWindows : TargetSpec { + let OSes = ["Win32"]; +} def TargetHasDLLImportExport : TargetSpec { let CustomCode = [{ Target.getTriple().hasDLLImportExport() }]; } @@ -3494,7 +3497,7 @@ let Documentation = [MSAllocatorDocs]; } -def CFGuard : InheritableAttr { +def CFGuard : InheritableAttr, TargetSpecificAttr { // Currently only the __declspec(guard(nocf)) modifier is supported. In future // we might also want to support __declspec(guard(suppress)). let Spellings = [Declspec<"guard">, Clang<"guard">]; diff --git a/clang/test/Sema/attr-guard_nocf.c b/clang/test/Sema/attr-guard_nocf.c --- a/clang/test/Sema/attr-guard_nocf.c +++ b/clang/test/Sema/attr-guard_nocf.c @@ -2,10 +2,14 @@ // RUN: %clang_cc1 -triple %ms_abi_triple -fms-extensions -verify -std=c++11 -fsyntax-only -x c++ %s // RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -verify -fsyntax-only %s // RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -verify -std=c++11 -fsyntax-only -x c++ %s +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -verify -fsyntax-only %s +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -verify -std=c++11 -fsyntax-only -x c++ %s // The x86_64-w64-windows-gnu version tests mingw target, which relies on // __declspec(...) being defined as __attribute__((...)) by compiler built-in. +#if defined(_WIN32) + // Function definition. __declspec(guard(nocf)) void testGuardNoCF(void) { // no warning } @@ -35,3 +39,9 @@ // 'guard' Attribute argument must be a supported identifier. __declspec(guard(cf)) void testGuardNoCFInvalidParam(void) { // expected-warning {{'guard' attribute argument not supported: 'cf'}} } + +#else + +__attribute((guard(nocf))) void testGNUStyleGuardNoCF(void) {} // expected-warning {{unknown attribute 'guard' ignored}} + +#endif