]> granicus.if.org Git - clang/commitdiff
Don't allow non-ASM statements in naked functions
authorHans Wennborg <hans@hanshq.net>
Thu, 4 Sep 2014 22:16:40 +0000 (22:16 +0000)
committerHans Wennborg <hans@hanshq.net>
Thu, 4 Sep 2014 22:16:40 +0000 (22:16 +0000)
Naked functions don't have prologues or epilogues, so doing
codegen for anything other than inline assembly would be completely
hit or miss.

Differential Revision: http://reviews.llvm.org/D5183

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/Sema/attr-naked.c

index e26374c8af0a6ef7157efbe5523497ef41291e36..cda5aa02caa1866be6073777e6de89a6aa5432c7 100644 (file)
@@ -6991,6 +6991,9 @@ def err_unknown_any_function : Error<
 def err_filter_expression_integral : Error<
   "filter expression type should be an integral value not %0">;
 
+def err_non_asm_stmt_in_naked_function : Error<
+  "non-ASM statement in naked function is not supported">;
+
 // OpenCL warnings and errors.
 def err_invalid_astype_of_different_size : Error<
   "invalid reinterpretation: sizes of %0 and %1 must match">;
index 9f49e68c07290778c4a2f827128ece84b55cf202..f5ba0ea90327585b0dca7ed37ee54764af75cff2 100644 (file)
@@ -10376,6 +10376,17 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
          !CheckConstexprFunctionBody(FD, Body)))
       FD->setInvalidDecl();
 
+    if (FD && FD->hasAttr<NakedAttr>()) {
+      for (const Stmt *S : Body->children()) {
+        if (!isa<AsmStmt>(S)) {
+          Diag(S->getLocStart(), diag::err_non_asm_stmt_in_naked_function);
+          Diag(FD->getAttr<NakedAttr>()->getLocation(), diag::note_attribute);
+          FD->setInvalidDecl();
+          break;
+        }
+      }
+    }
+
     assert(ExprCleanupObjects.empty() && "Leftover temporaries in function");
     assert(!ExprNeedsCleanups && "Unaccounted cleanups in function");
     assert(MaybeODRUseExprs.empty() &&
index 55c6b326ef156821187709e028badbe0ca8b2653..c1ad52c454876a4fde904012f6c462392005c5f8 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -verify -fsyntax-only
+// RUN: %clang_cc1 %s -verify -fsyntax-only -triple i686-pc-linux
 
 int a __attribute__((naked)); // expected-warning {{'naked' attribute only applies to functions}}
 
@@ -10,3 +10,11 @@ void t1() __attribute__((naked));
 
 void t2() __attribute__((naked(2))); // expected-error {{'naked' attribute takes no arguments}}
 
+__attribute__((naked)) int t3() { // expected-note{{attribute is here}}
+  return 42; // expected-error{{non-ASM statement in naked function is not supported}}
+}
+
+__attribute__((naked)) int t4() {
+  asm("movl $42, %eax");
+  asm("retl");
+}