]> granicus.if.org Git - clang/commitdiff
Sema: prevent __declspec(naked) use on x64
authorSaleem Abdulrasool <compnerd@compnerd.org>
Fri, 7 Apr 2017 15:13:47 +0000 (15:13 +0000)
committerSaleem Abdulrasool <compnerd@compnerd.org>
Fri, 7 Apr 2017 15:13:47 +0000 (15:13 +0000)
MSDN (https://msdn.microsoft.com/en-us/library/h5w10wxs.aspx) indicates
that `__declspec(naked)` is only permitted on x86 and ARM targets.
Testing with cl does confirm this behaviour.  Provide a warning for use
of `__declspec(naked)` on x64.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299774 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/Sema/declspec-naked.c [new file with mode: 0644]
test/Sema/ms-inline-asm.c

index 79a8fd57ee33db53be8942f0667ef505dbdfe81d..9b2cfe495ce223bbba8ba03802677d04d3be8475 100644 (file)
@@ -3231,7 +3231,8 @@ def err_attribute_regparm_invalid_number : Error<
   "'regparm' parameter must be between 0 and %0 inclusive">;
 def err_attribute_not_supported_in_lang : Error<
   "%0 attribute is not supported in %select{C|C++|Objective-C}1">;
-
+def err_attribute_not_supported_on_arch
+    : Error<"%0 attribute is not supported on '%1'">;
 
 // Clang-Specific Attributes
 def warn_attribute_iboutlet : Warning<
index c2bd9f939b163eac84f8479d282c8a6da7a9d718..a1ba9de368dbf12529682a9938c61f79832edf62 100644 (file)
@@ -1923,6 +1923,17 @@ static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
                                                      Attr.getName()))
     return;
 
+  if (Attr.isDeclspecAttribute()) {
+    const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
+    const auto &Arch = Triple.getArch();
+    if (Arch != llvm::Triple::x86 &&
+        (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
+      S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_on_arch)
+          << Attr.getName() << Triple.getArchName();
+      return;
+    }
+  }
+
   D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
                                          Attr.getAttributeSpellingListIndex()));
 }
diff --git a/test/Sema/declspec-naked.c b/test/Sema/declspec-naked.c
new file mode 100644 (file)
index 0000000..ad40941
--- /dev/null
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple i686-unknown-windows-msvc -fsyntax-only -fdeclspec -verify %s
+// RUN: %clang_cc1 -triple thumbv7-unknown-windows-msvc -fsyntax-only -fdeclspec -verify %s
+// RUN: %clang_cc1 -triple x86_64-unknown-windows-msvc -fsyntax-only -fdeclspec -verify %s
+#if defined(_M_IX86) || defined(_M_ARM)
+// CHECK: expected-no-diagnostics
+#endif
+
+void __declspec(naked) f(void) {}
+#if !defined(_M_IX86) && !defined(_M_ARM)
+// expected-error@-2{{'naked' attribute is not supported on 'x86_64'}}
+#endif
index abf10b67cce1846f5747dac3d86483ee2342cdc4..952112ea8cf88c1cf2c06386f2322585d2dffe66 100644 (file)
@@ -1,5 +1,5 @@
 // REQUIRES: x86-registered-target
-// RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -fms-extensions -fasm-blocks -Wno-microsoft -Wunused-label -verify -fsyntax-only
+// RUN: %clang_cc1 %s -triple i386-apple-darwin10 -fms-extensions -fasm-blocks -Wno-microsoft -Wunused-label -verify -fsyntax-only
 
 void t1(void) { 
  __asm __asm // expected-error {{__asm used with no assembly instructions}}