From: Hans Wennborg Date: Thu, 4 Sep 2014 22:16:40 +0000 (+0000) Subject: Don't allow non-ASM statements in naked functions X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5417edc777ae1e5bc9ab5a6e459888a7e21a2a00;p=clang Don't allow non-ASM statements in naked functions 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 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index e26374c8af..cda5aa02ca 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 9f49e68c07..f5ba0ea903 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -10376,6 +10376,17 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, !CheckConstexprFunctionBody(FD, Body))) FD->setInvalidDecl(); + if (FD && FD->hasAttr()) { + for (const Stmt *S : Body->children()) { + if (!isa(S)) { + Diag(S->getLocStart(), diag::err_non_asm_stmt_in_naked_function); + Diag(FD->getAttr()->getLocation(), diag::note_attribute); + FD->setInvalidDecl(); + break; + } + } + } + assert(ExprCleanupObjects.empty() && "Leftover temporaries in function"); assert(!ExprNeedsCleanups && "Unaccounted cleanups in function"); assert(MaybeODRUseExprs.empty() && diff --git a/test/Sema/attr-naked.c b/test/Sema/attr-naked.c index 55c6b326ef..c1ad52c454 100644 --- a/test/Sema/attr-naked.c +++ b/test/Sema/attr-naked.c @@ -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"); +}