[cfe-commits] r58705 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/Sema.h lib/Sema/SemaDeclObjC.cpp test/SemaObjCXX/objc-decls-inside-namespace.mm

Anders Carlsson andersca at mac.com
Tue Nov 4 08:57:32 PST 2008


Author: andersca
Date: Tue Nov  4 10:57:32 2008
New Revision: 58705

URL: http://llvm.org/viewvc/llvm-project?rev=58705&view=rev
Log:
Make it an error if an Objective-C declaration is not in the global scope.

Added:
    cfe/trunk/test/SemaObjCXX/objc-decls-inside-namespace.mm
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=58705&r1=58704&r2=58705&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Tue Nov  4 10:57:32 2008
@@ -696,6 +696,10 @@
 DIAG(err_const_var_requires_init, ERROR,
      "declaration of const variable '%0' requires an initializer")
 
+// Objective-C++
+DIAG(err_objc_decls_may_only_appear_in_global_scope, ERROR,
+     "Objective-C declarations may only appear in global scope")
+
 // Attributes
 DIAG(err_attribute_wrong_number_arguments, ERROR,
      "attribute requires %0 argument(s)")

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=58705&r1=58704&r2=58705&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Nov  4 10:57:32 2008
@@ -1209,7 +1209,11 @@
   bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS,
                                          bool ForCompare);
 
-  
+  /// Checks that the Objective-C declaration is declared in the global scope.
+  /// Emits an error and marks the declaration as invalid if it's not declared
+  /// in the global scope.
+  bool CheckObjCDeclScope(Decl *D);
+
   void InitBuiltinVaListType();
 
   // Helper method to turn variable array types into

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=58705&r1=58704&r2=58705&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Tue Nov  4 10:57:32 2008
@@ -124,6 +124,8 @@
     IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
     IDecl->setLocEnd(EndProtoLoc);
   }
+  
+  CheckObjCDeclScope(IDecl);
   return IDecl;
 }
 
@@ -163,7 +165,10 @@
     ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
   
   ObjCAliasDecls[AliasName] = AliasDecl;
-  TUScope->AddDecl(AliasDecl);
+  
+  if (!CheckObjCDeclScope(AliasDecl))
+    TUScope->AddDecl(AliasDecl);
+  
   return AliasDecl;
 }
 
@@ -201,6 +206,8 @@
     PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
     PDecl->setLocEnd(EndProtoLoc);
   }
+  
+  CheckObjCDeclScope(PDecl);  
   return PDecl;
 }
 
@@ -370,8 +377,13 @@
     
     Protocols.push_back(PDecl);
   }
-  return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
-                                         &Protocols[0], Protocols.size());
+  
+  ObjCForwardProtocolDecl *PDecl = 
+    ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
+                                    &Protocols[0], Protocols.size());
+  
+  CheckObjCDeclScope(PDecl);
+  return PDecl;
 }
 
 Sema::DeclTy *Sema::
@@ -410,6 +422,8 @@
     CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
     CDecl->setLocEnd(EndProtoLoc);
   }
+  
+  CheckObjCDeclScope(CDecl);
   return CDecl;
 }
 
@@ -430,6 +444,8 @@
   /// TODO: Check that CatName, category name, is not used in another
   // implementation.
   ObjCCategoryImpls.push_back(CDecl);
+  
+  CheckObjCDeclScope(CDecl);
   return CDecl;
 }
 
@@ -498,6 +514,9 @@
     ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, 
                                    IDecl, SDecl);
   
+  if (CheckObjCDeclScope(IMPDecl))
+    return IMPDecl;
+  
   // Check that there is no duplicate implementation of this class.
   if (ObjCImplementations[ClassName])
     // FIXME: Don't leak everything!
@@ -730,8 +749,12 @@
     Interfaces.push_back(IDecl);
   }
   
-  return ObjCClassDecl::Create(Context, AtClassLoc,
-                               &Interfaces[0], Interfaces.size());
+  ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
+                                               &Interfaces[0],
+                                               Interfaces.size());
+  
+  CheckObjCDeclScope(CDecl);
+  return CDecl;  
 }
 
 
@@ -1327,3 +1350,14 @@
     
   return PIDecl;
 }
+
+bool Sema::CheckObjCDeclScope(Decl *D)
+{
+  if (isa<TranslationUnitDecl>(CurContext))
+    return false;
+  
+  Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
+  D->setInvalidDecl();
+  
+  return true;
+}

Added: cfe/trunk/test/SemaObjCXX/objc-decls-inside-namespace.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/objc-decls-inside-namespace.mm?rev=58705&view=auto

==============================================================================
--- cfe/trunk/test/SemaObjCXX/objc-decls-inside-namespace.mm (added)
+++ cfe/trunk/test/SemaObjCXX/objc-decls-inside-namespace.mm Tue Nov  4 10:57:32 2008
@@ -0,0 +1,27 @@
+// RUN: clang -fsyntax-only -verify %s
+
+namespace C {
+
+ at protocol P; //expected-error{{Objective-C declarations may only appear in global scope}}
+
+ at class Bar; //expected-error{{Objective-C declarations may only appear in global scope}}
+
+ at compatibility_alias Foo Bar; //expected-error{{Objective-C declarations may only appear in global scope}}
+
+ at interface A //expected-error{{Objective-C declarations may only appear in global scope}}
+ at end
+
+ at implementation A //expected-error{{Objective-C declarations may only appear in global scope}}
+ at end
+
+ at protocol P //expected-error{{Objective-C declarations may only appear in global scope}}
+ at end
+
+ at interface A(C) //expected-error{{Objective-C declarations may only appear in global scope}}
+ at end
+
+ at implementation A(C) //expected-error{{Objective-C declarations may only appear in global scope}}
+ at end
+
+}
+





More information about the cfe-commits mailing list