From d31da60fd47db6b1fc6d6403026951cb6147f65d Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Thu, 12 Apr 2018 16:41:55 +0000 Subject: [PATCH] Correctly diagnose when a conversion function is declared with a type qualifier in the declaration specifiers rather than in the conversion type id. Fixes PR30595. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@329924 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDeclCXX.cpp | 26 ++++++++++++++++++-------- test/SemaCXX/conversion-function.cpp | 10 ++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 08d3bc21a2..8bf4b5a049 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -8319,7 +8319,8 @@ void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId, &ConvTSI); - if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { + const DeclSpec &DS = D.getDeclSpec(); + if (DS.hasTypeSpecifier() && !D.isInvalidType()) { // Conversion functions don't have return types, but the parser will // happily parse something like: // @@ -8329,9 +8330,18 @@ void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, // // The return type will be changed later anyway. Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) - << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) + << SourceRange(DS.getTypeSpecTypeLoc()) << SourceRange(D.getIdentifierLoc()); D.setInvalidType(); + } else if (DS.getTypeQualifiers() && !D.isInvalidType()) { + // It's also plausible that the user writes type qualifiers in the wrong + // place, such as: + // struct S { const operator int(); }; + // FIXME: we could provide a fixit to move the qualifiers onto the + // conversion type. + Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) + << SourceRange(D.getIdentifierLoc()) << 0; + D.setInvalidType(); } const FunctionProtoType *Proto = R->getAs(); @@ -8444,12 +8454,12 @@ void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, R = Context.getFunctionType(ConvType, None, Proto->getExtProtoInfo()); // C++0x explicit conversion operators. - if (D.getDeclSpec().isExplicitSpecified()) - Diag(D.getDeclSpec().getExplicitSpecLoc(), - getLangOpts().CPlusPlus11 ? - diag::warn_cxx98_compat_explicit_conversion_functions : - diag::ext_explicit_conversion_functions) - << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); + if (DS.isExplicitSpecified()) + Diag(DS.getExplicitSpecLoc(), + getLangOpts().CPlusPlus11 + ? diag::warn_cxx98_compat_explicit_conversion_functions + : diag::ext_explicit_conversion_functions) + << SourceRange(DS.getExplicitSpecLoc()); } /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete diff --git a/test/SemaCXX/conversion-function.cpp b/test/SemaCXX/conversion-function.cpp index 531de818b6..1d3593d8ed 100644 --- a/test/SemaCXX/conversion-function.cpp +++ b/test/SemaCXX/conversion-function.cpp @@ -444,3 +444,13 @@ namespace PR18234 { bool k1 = e == A::e; // expected-error {{no member named 'e'}} bool k2 = e.n == 0; } + +namespace PR30595 { +struct S { + const operator int(); // expected-error {{cannot specify any part of a return type in the declaration of a conversion function; put the complete type after 'operator'}} + const operator int() const; // expected-error {{cannot specify any part of a return type}} + volatile const operator int(); // expected-error {{cannot specify any part of a return type}} + + operator const int() const; +}; +} -- 2.40.0