Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -5236,6 +5236,9 @@ def err_standalone_class_nested_name_specifier : Error< "forward declaration of %select{class|struct|interface|union|enum}0 cannot " "have a nested name specifier">; +def err_standalone_class_specifier : Error< + "forward declaration of qualified %select{class|struct|interface|union|enum}0 " + "not allowed">; def err_typecheck_sclass_func : Error<"illegal storage class on function">; def err_static_block_func : Error< "function declared in block scope cannot have 'static' storage class">; Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -3789,6 +3789,17 @@ if (Tag && SS.isNotEmpty() && !Tag->isCompleteDefinition() && !IsExplicitInstantiation && !IsExplicitSpecialization && !isa(Tag)) { + + // Per C++ standard [n3485] 3.4.4 Elaborated type specifiers, section 3: + // "Cannot introduce an qualified". + // A clang::NestedNameSpecifier can represent many kinds of specifiers. + // A global-specifier with no nested-name-specifier requires a different + // diagnostic from a nested-name specifier. + unsigned diagId = ( SS.getScopeRep()->getKind() == NestedNameSpecifier::Global && + !SS.getScopeRep()->getPrefix() ) + ? diag::err_standalone_class_specifier + : diagId = diag::err_standalone_class_nested_name_specifier; + // Per C++ [dcl.type.elab]p1, a class declaration cannot have a // nested-name-specifier unless it is an explicit instantiation // or an explicit specialization. @@ -3797,8 +3808,9 @@ // obvious intent of DR1819. // // Per C++ [dcl.enum]p1, an opaque-enum-declaration can't either. - Diag(SS.getBeginLoc(), diag::err_standalone_class_nested_name_specifier) - << GetDiagnosticTypeSpecifierID(DS.getTypeSpecType()) << SS.getRange(); + Diag(SS.getBeginLoc(), diagId) + << GetDiagnosticTypeSpecifierID(DS.getTypeSpecType()) << SS.getRange(); + return nullptr; } Index: test/Parser/forward-declaration.cpp =================================================================== --- /dev/null +++ test/Parser/forward-declaration.cpp @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 %s -fsyntax-only -verify + +class Outer {class Inner;}; + +class ::Outer; // expected-error {{forward declaration of qualified class not allowed}} expected-warning {{extra qualification on member 'Outer'}} + +class Outer; + +// specializations of qualified type introduction? +class Outer::Inner; // expected-error {{forward declaration of class cannot have a nested name specifier}} +class ::Outer::Inner; // expected-error {{forward declaration of class cannot have a nested name specifier}}