From e43aacb1ea03bef88c640b5029fb9b76cce63afd Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Fri, 11 Dec 2015 21:39:12 +0000 Subject: [PATCH] Unify diagnostics for type defintitions in bad contexts The message for a type definition in an "if" condition was different from the other three for no particular reason. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@255372 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 ++-- lib/Sema/SemaType.cpp | 26 +++++++++------------- test/SemaCXX/condition.cpp | 8 +++---- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c008888f74..19dbbc76fc 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1172,6 +1172,8 @@ def err_type_defined_in_param_type : Error< "%0 cannot be defined in a parameter type">; def err_type_defined_in_alias_template : Error< "%0 cannot be defined in a type alias template">; +def err_type_defined_in_condition : Error< + "%0 cannot be defined in a condition">; def note_pure_virtual_function : Note< "unimplemented pure virtual method %0 in %1">; @@ -5961,8 +5963,6 @@ def err_pseudo_dtor_destructor_non_type : Error< def err_invalid_use_of_function_type : Error< "a function type is not allowed here">; def err_invalid_use_of_array_type : Error<"an array type is not allowed here">; -def err_type_defined_in_condition : Error< - "types may not be defined in conditions">; def err_typecheck_bool_condition : Error< "value of type %0 is not contextually convertible to 'bool'">; def err_typecheck_ambiguous_condition : Error< diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 7faa3b3f17..0c22287d4b 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -2749,6 +2749,7 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) { // Check the contexts where C++ forbids the declaration of a new class // or enumeration in a type-specifier-seq. + unsigned DiagID = 0; switch (D.getContext()) { case Declarator::TrailingReturnContext: // Class and enumeration definitions are syntactically not allowed in @@ -2768,10 +2769,7 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, case Declarator::AliasDeclContext: break; case Declarator::AliasTemplateContext: - SemaRef.Diag(OwnedTagDecl->getLocation(), - diag::err_type_defined_in_alias_template) - << SemaRef.Context.getTypeDeclType(OwnedTagDecl); - D.setInvalidType(true); + DiagID = diag::err_type_defined_in_alias_template; break; case Declarator::TypeNameContext: case Declarator::ConversionIdContext: @@ -2780,10 +2778,7 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, case Declarator::CXXCatchContext: case Declarator::ObjCCatchContext: case Declarator::TemplateTypeArgContext: - SemaRef.Diag(OwnedTagDecl->getLocation(), - diag::err_type_defined_in_type_specifier) - << SemaRef.Context.getTypeDeclType(OwnedTagDecl); - D.setInvalidType(true); + DiagID = diag::err_type_defined_in_type_specifier; break; case Declarator::PrototypeContext: case Declarator::LambdaExprParameterContext: @@ -2792,20 +2787,21 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, case Declarator::KNRTypeListContext: // C++ [dcl.fct]p6: // Types shall not be defined in return or parameter types. - SemaRef.Diag(OwnedTagDecl->getLocation(), - diag::err_type_defined_in_param_type) - << SemaRef.Context.getTypeDeclType(OwnedTagDecl); - D.setInvalidType(true); + DiagID = diag::err_type_defined_in_param_type; break; case Declarator::ConditionContext: // C++ 6.4p2: // The type-specifier-seq shall not contain typedef and shall not declare // a new class or enumeration. - SemaRef.Diag(OwnedTagDecl->getLocation(), - diag::err_type_defined_in_condition); - D.setInvalidType(true); + DiagID = diag::err_type_defined_in_condition; break; } + + if (DiagID != 0) { + SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID) + << SemaRef.Context.getTypeDeclType(OwnedTagDecl); + D.setInvalidType(true); + } } assert(!T.isNull() && "This function should not return a null type"); diff --git a/test/SemaCXX/condition.cpp b/test/SemaCXX/condition.cpp index 73f3dceef6..b757fcb8cd 100644 --- a/test/SemaCXX/condition.cpp +++ b/test/SemaCXX/condition.cpp @@ -17,9 +17,9 @@ void test() { switch (s) {} // expected-error {{statement requires expression of integer type ('struct S' invalid)}} while (struct NewS *x=0) ; - while (struct S {} *x=0) ; // expected-error {{types may not be defined in conditions}} - while (struct {} *x=0) ; // expected-error {{types may not be defined in conditions}} - switch (enum {E} x=0) ; // expected-error {{types may not be defined in conditions}} + while (struct S {} *x=0) ; // expected-error {{'S' cannot be defined in a condition}} + while (struct {} *x=0) ; // expected-error-re {{'(anonymous struct at {{.*}})' cannot be defined in a condition}} + switch (enum {E} x=0) ; // expected-error-re {{'(anonymous enum at {{.*}})' cannot be defined in a condition}} if (int x=0) { // expected-note 2 {{previous definition is here}} int x; // expected-error {{redefinition of 'x'}} @@ -59,7 +59,7 @@ void test4(bool (&x)(void)) { template void test5() { - if (struct S {}* p = 0) // expected-error {{types may not be defined in conditions}} + if (struct S {}* p = 0) // expected-error {{'S' cannot be defined in a condition}} ; } void test5_inst() { -- 2.40.0