>From 860b827f7c6f284bc4109c2925aae49da75ccbc6 Mon Sep 17 00:00:00 2001
From: Erik Verbruggen <erikjv@me.com>
Date: Tue, 15 Nov 2011 17:43:43 +0100
Subject: [PATCH] Warn on @interface declaration inside an @implementation.

Fixes PR2709.
---
 include/clang/Basic/DiagnosticParseKinds.td |    2 ++
 lib/Parse/ParseObjc.cpp                     |    6 +++++-
 test/SemaObjC/missing-atend-metadata.m      |   12 ++++++++++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td
index e0b10fa..2b0d859 100644
--- a/include/clang/Basic/DiagnosticParseKinds.td
+++ b/include/clang/Basic/DiagnosticParseKinds.td
@@ -359,6 +359,8 @@ def err_synthesized_property_name : Error<
 def warn_semicolon_before_method_body : Warning<
   "semicolon before method body is ignored">,
   InGroup<DiagGroup<"semicolon-before-method-body">>, DefaultIgnore;
+def warn_objc_nested_container_decl : Warning<
+  "cannot nest '@%0' inside an Objective-C container context">;
 
 def err_expected_field_designator : Error<
   "expected a field designator, such as '.field = 4'">;
diff --git a/lib/Parse/ParseObjc.cpp b/lib/Parse/ParseObjc.cpp
index 88044d1..364cc99 100644
--- a/lib/Parse/ParseObjc.cpp
+++ b/lib/Parse/ParseObjc.cpp
@@ -145,7 +145,11 @@ Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
                                               ParsedAttributes &attrs) {
   assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
          "ParseObjCAtInterfaceDeclaration(): Expected @interface");
-  ConsumeToken(); // the "interface" identifier
+  const IdentifierInfo *IFaceII = Tok.getIdentifierInfo();
+  const SourceLocation IFaceTok = ConsumeToken(); // the "interface" identifier
+  if (Actions.CurContext->isObjCContainer())
+      Diag(IFaceTok, diag::warn_objc_nested_container_decl)
+              << IFaceII->getName();
 
   // Code completion after '@interface'.
   if (Tok.is(tok::code_completion)) {
diff --git a/test/SemaObjC/missing-atend-metadata.m b/test/SemaObjC/missing-atend-metadata.m
index 9b79c52..6f1c84d 100644
--- a/test/SemaObjC/missing-atend-metadata.m
+++ b/test/SemaObjC/missing-atend-metadata.m
@@ -1,18 +1,25 @@
 // RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -verify %s 
 
+@interface X
+@end
+@implementation X
+@interface Y  // expected-warning {{cannot nest '@interface' inside an Objective-C container context}}
+@end
+@end
+
 @interface I0 
 @end
 
 @implementation I0 // expected-error {{'@end' is missing in implementation context}}
 - meth { return 0; }
 
-@interface I1 : I0 
+@interface I1 : I0 // expected-warning {{cannot nest '@interface' inside an Objective-C container context}}
 @end
 
 @implementation I1 // expected-error {{'@end' is missing in implementation context}}
 -(void) im0 { self = [super init]; }
 
-@interface I2 : I0
+@interface I2 : I0 // expected-warning {{cannot nest '@interface' inside an Objective-C container context}}
 - I2meth;
 @end
 
@@ -20,3 +27,4 @@
 - I2meth { return 0; }
 
 @implementation  I2(CAT) // expected-error {{'@end' is missing in implementation context}}
+
-- 
1.7.5.4

