diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index cda5aa02caa1866be6073777e6de89a6aa5432c7..78f3eeb70cd865edfd1b27d903ebce96d51c84f7 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -6993,6 +6993,8 @@ def err_filter_expression_integral : Error< def err_non_asm_stmt_in_naked_function : Error< "non-ASM statement in naked function is not supported">; +def err_asm_naked_parm_ref : Error< + "parameter references not allowed in naked functions">; // OpenCL warnings and errors. def err_invalid_astype_of_different_size : Error< diff --git a/lib/Sema/SemaStmtAsm.cpp b/lib/Sema/SemaStmtAsm.cpp index 47a7672ae19bdd8e4a4245d349bec22cbbb9e20f..989999f18d9d44b2cf1ea745b568c507fc411374 100644 --- a/lib/Sema/SemaStmtAsm.cpp +++ b/lib/Sema/SemaStmtAsm.cpp @@ -405,6 +405,19 @@ ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, Result = CheckPlaceholderExpr(Result.get()); if (!Result.isUsable()) return Result; + // Referring to parameters is not allowed in naked functions. + if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Result.get())) { + if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(DRE->getDecl())) { + if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Parm->getDeclContext())) { + if (Func->hasAttr<NakedAttr>()) { + Diag(Id.getLocStart(), diag::err_asm_naked_parm_ref); + Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute); + return ExprError(); + } + } + } + } + QualType T = Result.get()->getType(); // For now, reject dependent types. diff --git a/test/Sema/attr-naked.c b/test/Sema/attr-naked.c index c1ad52c454876a4fde904012f6c462392005c5f8..f6c26b29fc3a2ce74bba65929f8f03cd69d15f30 100644 --- a/test/Sema/attr-naked.c +++ b/test/Sema/attr-naked.c @@ -18,3 +18,8 @@ __attribute__((naked)) int t4() { asm("movl $42, %eax"); asm("retl"); } + +__attribute__((naked)) int t5(int x) { + asm("movl x, %eax"); + asm("retl"); +} diff --git a/test/Sema/ms-inline-asm.c b/test/Sema/ms-inline-asm.c index d4227027d7cb87e85bdf5899d56ef9b4d8d603f2..358b0bd4543ed2adea05f69a6f4b9eefceedef58 100644 --- a/test/Sema/ms-inline-asm.c +++ b/test/Sema/ms-inline-asm.c @@ -103,3 +103,14 @@ void t4() { void test_operand_size() { __asm { call word t4 } // expected-error {{Expected 'PTR' or 'ptr' token!}} } + +__declspec(naked) int t5(int x) { // expected-note {{attribute is here}} + asm { movl eax, x } // expected-error {{parameter references not allowed in naked functions}} + asm { retl } +} + +int y; +__declspec(naked) int t6(int x) { + asm { mov eax, y } // No error. + asm { ret } +}