]> granicus.if.org Git - clang/commitdiff
Diagnose CXX 'this' pointer reference in funcs with naked attr
authorWeiming Zhao <weimingz@codeaurora.org>
Tue, 3 Feb 2015 22:35:58 +0000 (22:35 +0000)
committerWeiming Zhao <weimingz@codeaurora.org>
Tue, 3 Feb 2015 22:35:58 +0000 (22:35 +0000)
Clang asserts for this pointer reference in asms of naked functions.
This patch diagnoses if this pointer reference is used.

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

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaStmtAsm.cpp
test/Sema/attr-naked.cpp [new file with mode: 0644]

index 1283bafd61af5bae4e913d67430b6b3d43f628d6..9bbdab4918eb848686daa0d2915c0617836f00a9 100644 (file)
@@ -7211,6 +7211,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_this_ref : Error<
+  "'this' pointer references not allowed in naked functions">;
 def err_asm_naked_parm_ref : Error<
   "parameter references not allowed in naked functions">;
 
index 0d32581e8daa8adf63b0f8f2482b311b485dc52f..1beac366c5081adadb02bdab966b1532d933c38f 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Sema/SemaInternal.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/TargetInfo.h"
@@ -86,6 +87,11 @@ static bool CheckNakedParmReference(Expr *E, Sema &S) {
   WorkList.push_back(E);
   while (WorkList.size()) {
     Expr *E = WorkList.pop_back_val();
+    if (isa<CXXThisExpr>(E)) {
+      S.Diag(E->getLocStart(), diag::err_asm_naked_this_ref);
+      S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute);
+      return true;
+    }
     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
       if (isa<ParmVarDecl>(DRE->getDecl())) {
         S.Diag(DRE->getLocStart(), diag::err_asm_naked_parm_ref);
diff --git a/test/Sema/attr-naked.cpp b/test/Sema/attr-naked.cpp
new file mode 100644 (file)
index 0000000..eaa8e22
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only -triple arm-none-linux
+class Foo {
+  void bar();
+  static void bar2();
+  unsigned v;
+  static unsigned s;
+};
+
+void __attribute__((naked)) Foo::bar() { // expected-note{{attribute is here}}
+  asm("mov r2, %0" : : "r"(v)); // expected-error{{'this' pointer references not allowed in naked functions}}
+}
+
+void __attribute__((naked)) Foo::bar2() {
+  asm("mov r2, %0" : : "r"(s)); // static member reference is OK
+}