diff options
| author | Chandler Carruth <chandlerc@gmail.com> | 2011-02-23 18:51:59 +0000 |
|---|---|---|
| committer | Chandler Carruth <chandlerc@gmail.com> | 2011-02-23 18:51:59 +0000 |
| commit | e71b378dc7c0fa9e43532d33827b876008126b10 (patch) | |
| tree | a84e23c2d88461dceda4adba39e1528e119ab4c4 /clang/lib/Sema | |
| parent | 4995b05f56a0993f03f57536129a44d33971dd95 (diff) | |
| download | bcm5719-llvm-e71b378dc7c0fa9e43532d33827b876008126b10.tar.gz bcm5719-llvm-e71b378dc7c0fa9e43532d33827b876008126b10.zip | |
Fix the behavior of -Wignored-qualifiers on return type qualifiers in
several ways. We now warn for more of the return types, and correctly
locate the ignored ones. Also adds fix-it hints to remove the ignored
qualifiers. Fixes much of PR9058, although not all of it.
Patch by Hans Wennborg, a couple of minor style tweaks from me.
llvm-svn: 126321
Diffstat (limited to 'clang/lib/Sema')
| -rw-r--r-- | clang/lib/Sema/SemaType.cpp | 109 |
1 files changed, 72 insertions, 37 deletions
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 7c96a3f2de3..d30ed743106 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -1395,6 +1395,56 @@ QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) { return QT; } +static void DiagnoseIgnoredQualifiers(unsigned Quals, + SourceLocation ConstQualLoc, + SourceLocation VolatileQualLoc, + SourceLocation RestrictQualLoc, + Sema& S) { + std::string QualStr; + unsigned NumQuals = 0; + SourceLocation Loc; + + FixItHint ConstFixIt; + FixItHint VolatileFixIt; + FixItHint RestrictFixIt; + + // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to + // find a range and grow it to encompass all the qualifiers, regardless of + // the order in which they textually appear. + if (Quals & Qualifiers::Const) { + ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc); + Loc = ConstQualLoc; + ++NumQuals; + QualStr = "const"; + } + if (Quals & Qualifiers::Volatile) { + VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc); + if (NumQuals == 0) { + Loc = VolatileQualLoc; + QualStr = "volatile"; + } else { + QualStr += " volatile"; + } + ++NumQuals; + } + if (Quals & Qualifiers::Restrict) { + RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc); + if (NumQuals == 0) { + Loc = RestrictQualLoc; + QualStr = "restrict"; + } else { + QualStr += " restrict"; + } + ++NumQuals; + } + + assert(NumQuals > 0 && "No known qualifiers?"); + + S.Diag(Loc, diag::warn_qual_return_type) + << QualStr << NumQuals + << ConstFixIt << VolatileFixIt << RestrictFixIt; +} + /// GetTypeForDeclarator - Convert the type for the specified /// declarator to Type instances. /// @@ -1678,46 +1728,31 @@ TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S, // cv-qualifiers on return types are pointless except when the type is a // class type in C++. - if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() && + if (T->isPointerType() && T.getCVRQualifiers() && + (!getLangOptions().CPlusPlus || !T->isDependentType())) { + assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?"); + DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1); + assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer); + + DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr; + + DiagnoseIgnoredQualifiers(PTI.TypeQuals, + SourceLocation::getFromRawEncoding(PTI.ConstQualLoc), + SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc), + SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc), + *this); + + } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() && (!getLangOptions().CPlusPlus || (!T->isDependentType() && !T->isRecordType()))) { - unsigned Quals = D.getDeclSpec().getTypeQualifiers(); - std::string QualStr; - unsigned NumQuals = 0; - SourceLocation Loc; - if (Quals & Qualifiers::Const) { - Loc = D.getDeclSpec().getConstSpecLoc(); - ++NumQuals; - QualStr = "const"; - } - if (Quals & Qualifiers::Volatile) { - if (NumQuals == 0) { - Loc = D.getDeclSpec().getVolatileSpecLoc(); - QualStr = "volatile"; - } else - QualStr += " volatile"; - ++NumQuals; - } - if (Quals & Qualifiers::Restrict) { - if (NumQuals == 0) { - Loc = D.getDeclSpec().getRestrictSpecLoc(); - QualStr = "restrict"; - } else - QualStr += " restrict"; - ++NumQuals; - } - assert(NumQuals > 0 && "No known qualifiers?"); - - SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type); - DB << QualStr << NumQuals; - if (Quals & Qualifiers::Const) - DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc()); - if (Quals & Qualifiers::Volatile) - DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc()); - if (Quals & Qualifiers::Restrict) - DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc()); + + DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(), + D.getDeclSpec().getConstSpecLoc(), + D.getDeclSpec().getVolatileSpecLoc(), + D.getDeclSpec().getRestrictSpecLoc(), + *this); } - + if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { // C++ [dcl.fct]p6: // Types shall not be defined in return or parameter types. |

