]> granicus.if.org Git - clang/commitdiff
Warn when trying to call a pure virtual member function in a class from the class...
authorAnders Carlsson <andersca@mac.com>
Fri, 6 May 2011 14:25:31 +0000 (14:25 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 6 May 2011 14:25:31 +0000 (14:25 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130982 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaOverload.cpp
test/SemaCXX/warn-pure-virtual-call-from-ctor-dtor.cpp [new file with mode: 0644]

index 8b73f901265205d110d6b3622217f37eebb4737b..6d90ba17c615a807edfc6b8534792ca6e0ca8a85 100644 (file)
@@ -728,6 +728,10 @@ def err_implicit_object_parameter_init : Error<
 def err_qualified_member_of_unrelated : Error<
   "%q0 is not a member of class %1">;
 
+def warn_call_to_pure_virtual_member_function_from_ctor_dtor : Warning<
+  "call to pure virtual member function %0; overrides of %0 in subclasses are "
+  "not available in the %select{constructor|destructor}1 of %2">;
+
 def note_field_decl : Note<"member is declared here">;
 def note_ivar_decl : Note<"ivar is declared here">;
 def note_bitfield_decl : Note<"bit-field is declared here">;
index 79caecc5a839d8ff1ae7c1a0da93bae6807f3e1d..64f04de87aabbfacf730708c82041950ba00a061 100644 (file)
@@ -8781,6 +8781,19 @@ Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE,
   if (CheckFunctionCall(Method, TheCall))
     return ExprError();
 
+  if ((isa<CXXConstructorDecl>(CurContext) || 
+       isa<CXXDestructorDecl>(CurContext)) && 
+      TheCall->getMethodDecl()->isPure()) {
+    const CXXMethodDecl *MD = TheCall->getMethodDecl();
+
+    if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts()))
+      Diag(MemExpr->getLocStart(), 
+           diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor)
+        << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext)
+        << MD->getParent()->getDeclName();
+
+      Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName();
+  }
   return MaybeBindToTemporary(TheCall);
 }
 
diff --git a/test/SemaCXX/warn-pure-virtual-call-from-ctor-dtor.cpp b/test/SemaCXX/warn-pure-virtual-call-from-ctor-dtor.cpp
new file mode 100644 (file)
index 0000000..698eccd
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+struct A {
+  A() { f(); } // expected-warning {{call to pure virtual member function 'f'; overrides of 'f' in subclasses are not available in the constructor of 'A'}}
+  ~A() { f(); } // expected-warning {{call to pure virtual member function 'f'; overrides of 'f' in subclasses are not available in the destructor of 'A'}}
+
+  virtual void f() = 0; // expected-note 2 {{'f' declared here}}
+};