]> granicus.if.org Git - clang/commitdiff
[GCC] Support for __final specifier
authorAndrey Bokhanko <andreybokhanko@gmail.com>
Fri, 29 Jul 2016 10:42:48 +0000 (10:42 +0000)
committerAndrey Bokhanko <andreybokhanko@gmail.com>
Fri, 29 Jul 2016 10:42:48 +0000 (10:42 +0000)
As reported in bug 28473, GCC supports "final" functionality in pre-C++11 code using the __final keyword. Clang currently supports the "final" keyword in accordance with the C++11 specification, however it ALSO supports it in pre-C++11 mode, with a warning.

This patch adds the "__final" keyword for compatibility with GCC in GCC Keywords mode (so it is enabled with existing flags), and issues a warning on its usage (suggesting switching to the C++11 keyword). This patch also adds a regression test for the functionality described. I believe this patch has minimal impact, as it simply adds a new keyword for existing behavior.

This has been validated with check-clang to avoid regressions. Patch is created in reference to revisions 276665.

Patch by Erich Keane.

Differential Revision: https://reviews.llvm.org/D22919

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

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Parse/Parser.h
include/clang/Sema/DeclSpec.h
lib/Parse/ParseDeclCXX.cpp
lib/Sema/DeclSpec.cpp
test/Parser/gcc-__final-compatibility.cpp [new file with mode: 0644]

index 37b6daec6615ad33af69638dae2653dc3c144a38..1fcb0fd9c496ac8050c72f419f9e2d2f9cee98bc 100644 (file)
@@ -8652,4 +8652,8 @@ def warn_block_literal_qualifiers_on_omitted_return_type : Warning<
   "'%0' qualifier on omitted return type %1 has no effect">,
   InGroup<IgnoredQualifiers>;
 
+def ext_warn_gnu_final : ExtWarn<
+  "__final is a GNU extension, consider using C++11 final">,
+  InGroup<GccCompat>;
+
 } // end of sema component.
index 2e5390a5c0fbea6625bd403b2ead138b001ff697..2ba9f71e59753d11b486807c50cedcc2e9e1082e 100644 (file)
@@ -143,6 +143,7 @@ class Parser : public CodeCompletionHandler {
 
   /// C++0x contextual keywords.
   mutable IdentifierInfo *Ident_final;
+  mutable IdentifierInfo *Ident_GNU_final;
   mutable IdentifierInfo *Ident_override;
 
   // C++ type trait keywords that can be reverted to identifiers and still be
index 0c6514fc69422645c58fd631b6205a1a4d7d75cd..df434ec778ff6382de12c7f754be0f5689b103aa 100644 (file)
@@ -2399,7 +2399,9 @@ public:
     VS_None = 0,
     VS_Override = 1,
     VS_Final = 2,
-    VS_Sealed = 4
+    VS_Sealed = 4,
+    // Represents the __final keyword, which is legal for gcc in pre-C++11 mode.
+    VS_GNU_Final = 8
   };
 
   VirtSpecifiers() : Specifiers(0), LastSpecifier(VS_None) { }
@@ -2412,7 +2414,7 @@ public:
   bool isOverrideSpecified() const { return Specifiers & VS_Override; }
   SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
 
-  bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed); }
+  bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed | VS_GNU_Final); }
   bool isFinalSpelledSealed() const { return Specifiers & VS_Sealed; }
   SourceLocation getFinalLoc() const { return VS_finalLoc; }
 
index 6436e3dfc7631791a16aa9832688211f79c2fbfc..5448d08ff4afa3a6bf3c6862ce68401d35285619 100644 (file)
@@ -2005,6 +2005,7 @@ void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
 ///       virt-specifier:
 ///         override
 ///         final
+///         __final
 VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
   if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
     return VirtSpecifiers::VS_None;
@@ -2014,6 +2015,8 @@ VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
   // Initialize the contextual keywords.
   if (!Ident_final) {
     Ident_final = &PP.getIdentifierTable().get("final");
+    if (getLangOpts().GNUKeywords)
+      Ident_GNU_final = &PP.getIdentifierTable().get("__final");
     if (getLangOpts().MicrosoftExt)
       Ident_sealed = &PP.getIdentifierTable().get("sealed");
     Ident_override = &PP.getIdentifierTable().get("override");
@@ -2028,6 +2031,9 @@ VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
   if (II == Ident_final)
     return VirtSpecifiers::VS_Final;
 
+  if (II == Ident_GNU_final)
+    return VirtSpecifiers::VS_GNU_Final;
+
   return VirtSpecifiers::VS_None;
 }
 
@@ -2067,6 +2073,8 @@ void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
         << VirtSpecifiers::getSpecifierName(Specifier);
     } else if (Specifier == VirtSpecifiers::VS_Sealed) {
       Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
+    } else if (Specifier == VirtSpecifiers::VS_GNU_Final) {
+      Diag(Tok.getLocation(), diag::ext_warn_gnu_final);
     } else {
       Diag(Tok.getLocation(),
            getLangOpts().CPlusPlus11
@@ -2083,6 +2091,7 @@ void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
 bool Parser::isCXX11FinalKeyword() const {
   VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
   return Specifier == VirtSpecifiers::VS_Final ||
+         Specifier == VirtSpecifiers::VS_GNU_Final || 
          Specifier == VirtSpecifiers::VS_Sealed;
 }
 
@@ -2996,6 +3005,7 @@ void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
   if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
     VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
     assert((Specifier == VirtSpecifiers::VS_Final ||
+            Specifier == VirtSpecifiers::VS_GNU_Final || 
             Specifier == VirtSpecifiers::VS_Sealed) &&
            "not a class definition");
     FinalLoc = ConsumeToken();
@@ -3011,6 +3021,8 @@ void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
         << VirtSpecifiers::getSpecifierName(Specifier);
     else if (Specifier == VirtSpecifiers::VS_Sealed)
       Diag(FinalLoc, diag::ext_ms_sealed_keyword);
+    else if (Specifier == VirtSpecifiers::VS_GNU_Final)
+      Diag(FinalLoc, diag::ext_warn_gnu_final);
 
     // Parse any C++11 attributes after 'final' keyword.
     // These attributes are not allowed to appear here,
index c294658c031ded1662353d724b92bb0dc0fee925..42d4633dd8c1dd3e29adf26cf2bfc9bd7ea0ce27 100644 (file)
@@ -1299,6 +1299,7 @@ bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
   switch (VS) {
   default: llvm_unreachable("Unknown specifier!");
   case VS_Override: VS_overrideLoc = Loc; break;
+  case VS_GNU_Final:
   case VS_Sealed:
   case VS_Final:    VS_finalLoc = Loc; break;
   }
@@ -1311,6 +1312,7 @@ const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
   default: llvm_unreachable("Unknown specifier");
   case VS_Override: return "override";
   case VS_Final: return "final";
+  case VS_GNU_Final: return "__final";
   case VS_Sealed: return "sealed";
   }
 }
diff --git a/test/Parser/gcc-__final-compatibility.cpp b/test/Parser/gcc-__final-compatibility.cpp
new file mode 100644 (file)
index 0000000..ddd14ba
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -std=c++98 -fgnu-keywords -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++11 -fgnu-keywords -fsyntax-only -verify %s
+
+struct B {
+  virtual void g();
+};
+struct D __final : B { // expected-warning {{__final is a GNU extension, consider using C++11 final}}
+  virtual void g() __final; // expected-warning {{__final is a GNU extension, consider using C++11 final}}
+};