]> granicus.if.org Git - clang/commitdiff
Mark classes as final or explicit. Diagnose when a class marked 'final' is used as...
authorAnders Carlsson <andersca@mac.com>
Sat, 22 Jan 2011 17:51:53 +0000 (17:51 +0000)
committerAnders Carlsson <andersca@mac.com>
Sat, 22 Jan 2011 17:51:53 +0000 (17:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124039 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Sema/Sema.h
lib/Parse/ParseDeclCXX.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclCXX.cpp
test/CXX/class/p2-0x.cpp [new file with mode: 0644]

index 0743d161248b83e023b2d3bd42e19c6b59e16cd2..7d3c3c82c4236afaa1b2cfddcbdb0ce217ef667e 100644 (file)
@@ -873,16 +873,17 @@ def err_auto_missing_trailing_return : Error<
   "'auto' return without trailing return type">;
 def err_trailing_return_without_auto : Error<
   "trailing return type without 'auto' return">;
-  
-// C++0x attributes
-def err_repeat_attribute : Error<"'%0' attribute cannot be repeated">;
 
 // C++0x override control
 def override_keyword_only_allowed_on_virtual_member_functions : Error<
   "only virtual member functions can be marked '%0'">;
-
 def err_function_marked_override_not_overriding : Error<
   "%0 marked 'override' but does not override any member functions">;
+def err_class_marked_final_used_as_base : Error<
+  "base %0 is marked 'final'">;
+
+// C++0x attributes
+def err_repeat_attribute : Error<"'%0' attribute cannot be repeated">;
 
 // C++0x [[final]]
 def err_final_function_overridden : Error<
index 6e989ac0069a75eb976831cdad546da2202aed79..90242e6192528a4cc341cd39ca97da19854b217c 100644 (file)
@@ -884,6 +884,7 @@ public:
   /// C++ record definition's base-specifiers clause and are starting its
   /// member declarations.
   void ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagDecl,
+                                       ClassVirtSpecifiers &CVS,
                                        SourceLocation LBraceLoc);
 
   /// ActOnTagFinishDefinition - Invoked once we have finished parsing
index e2bb751b872560f542c7e946580df37fef8e13cf..9a23cf1be5cf44af4e84ce0411e32c06280c8d68 100644 (file)
@@ -1772,7 +1772,8 @@ void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
   SourceLocation LBraceLoc = ConsumeBrace();
 
   if (TagDecl)
-    Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
+    Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, CVS,
+                                            LBraceLoc);
 
   // C++ 11p3: Members of a class defined with the keyword class are private
   // by default. Members of a class defined with the keywords struct or union
index a266b60ac386fdcb85091237aa8f92040e2a97d7..7c6d9d9142f5e77a6c033bb2c8d48319473454c0 100644 (file)
@@ -6423,6 +6423,7 @@ void Sema::ActOnTagStartDefinition(Scope *S, Decl *TagD) {
 }
 
 void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD,
+                                           ClassVirtSpecifiers &CVS,
                                            SourceLocation LBraceLoc) {
   AdjustDeclIfTemplate(TagD);
   CXXRecordDecl *Record = cast<CXXRecordDecl>(TagD);
@@ -6432,6 +6433,9 @@ void Sema::ActOnStartCXXMemberDeclarations(Scope *S, Decl *TagD,
   if (!Record->getIdentifier())
     return;
 
+  Record->setIsMarkedFinal(CVS.isFinalSpecified());
+  Record->setIsMarkedExplicit(CVS.isExplicitSpecified());
+    
   // C++ [class]p2:
   //   [...] The class-name is also inserted into the scope of the
   //   class itself; this is known as the injected-class-name. For
index 2f7640cd612068ee5c3bd232c55a133af80f4c55..bf5addc34d56bc19ba5c003f4892efea54b909c7 100644 (file)
@@ -521,6 +521,19 @@ Sema::CheckBaseSpecifier(CXXRecordDecl *Class,
   CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl);
   assert(CXXBaseDecl && "Base type is not a C++ type");
 
+  // C++ [class.derived]p2:
+  //   If a class is marked with the class-virt-specifier final and it appears
+  //   as a base-type-specifier in a base-clause (10 class.derived), the program
+  //   is ill-formed.
+  if (CXXBaseDecl->isMarkedFinal()) {
+    Diag(BaseLoc, diag::err_class_marked_final_used_as_base) 
+      << CXXBaseDecl->getDeclName();
+    Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl)
+      << CXXBaseDecl->getDeclName();
+    return 0;
+  }
+
+  // FIXME: Get rid of this.
   // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases.
   if (CXXBaseDecl->hasAttr<FinalAttr>()) {
     Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString();
diff --git a/test/CXX/class/p2-0x.cpp b/test/CXX/class/p2-0x.cpp
new file mode 100644 (file)
index 0000000..1164b8f
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++0x
+namespace Test1 {
+
+class A final { }; // expected-note {{'A' declared here}}
+class B : A { }; // expected-error {{base 'A' is marked 'final'}}
+
+}
+