]> granicus.if.org Git - clang/commitdiff
Namespaces can only be defined at global or namespace scope. Fixes PR6596.
authorDouglas Gregor <dgregor@apple.com>
Fri, 14 May 2010 05:08:22 +0000 (05:08 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 14 May 2010 05:08:22 +0000 (05:08 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103767 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticParseKinds.td
lib/Parse/ParseDeclCXX.cpp
test/Parser/namespaces.cpp [new file with mode: 0644]

index 27958515462af89d13daf5d6d849f2ebd2986167..162398dfd2cb5186f166657fbed019d813d3d59e 100644 (file)
@@ -119,6 +119,8 @@ def err_expected_semi_after_namespace_name : Error<
   "expected ';' after namespace name">;
 def err_unexpected_namespace_attributes_alias : Error<
   "attributes can not be specified on namespace alias">;
+def err_namespace_nonnamespace_scope : Error<
+  "namespaces can only be defined in global or namespace scope">;
 def err_expected_semi_after_attribute_list : Error<
   "expected ';' after attribute list">;
 def err_expected_semi_after_static_assert : Error<
index 015ac5b5dddddbbc6924e072b4d5bd178fd974ae..149efda8551d3ed11c7a7b208470d23cddfe6e69 100644 (file)
@@ -87,6 +87,14 @@ Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
 
   SourceLocation LBrace = ConsumeBrace();
 
+  if (CurScope->isClassScope() || CurScope->isTemplateParamScope() || 
+      CurScope->isInObjcMethodScope() || CurScope->getBlockParent() || 
+      CurScope->getFnParent()) {
+    Diag(LBrace, diag::err_namespace_nonnamespace_scope);
+    SkipUntil(tok::r_brace, false);
+    return DeclPtrTy();
+  }
+
   // Enter a scope for the namespace.
   ParseScope NamespaceScope(this, Scope::DeclScope);
 
diff --git a/test/Parser/namespaces.cpp b/test/Parser/namespaces.cpp
new file mode 100644 (file)
index 0000000..b8c7819
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// PR6596
+namespace g { enum { o = 0 }; }
+
+void foo() {
+  namespace a { typedef g::o o; } // expected-error{{namespaces can only be defined in global or namespace scope}}
+}