From a08fcb8105bf53f3640ad17f61bdcde2d8ace78a Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Fri, 3 Jun 2011 17:37:26 +0000 Subject: [PATCH] Make -Wignored-qualifiers point to the first ignored qualifier. In code such as "char* volatile const j()", Clang warns that "volatile const" will be ignored. Make it point to the first ignored qualifier, and simplify the code a bit. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132563 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaType.cpp | 28 ++++++++++++---------------- test/SemaCXX/return.cpp | 5 +++++ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 3c4e09ae86..e7f9885698 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -1480,41 +1480,37 @@ static void DiagnoseIgnoredQualifiers(unsigned Quals, FixItHint VolatileFixIt; FixItHint RestrictFixIt; + const SourceManager &SM = S.getSourceManager(); + // 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"; + ++NumQuals; + if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc)) + Loc = ConstQualLoc; } if (Quals & Qualifiers::Volatile) { VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc); - if (NumQuals == 0) { - Loc = VolatileQualLoc; - QualStr = "volatile"; - } else { - QualStr += " volatile"; - } + QualStr += (NumQuals == 0 ? "volatile" : " volatile"); ++NumQuals; + if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc)) + Loc = VolatileQualLoc; } if (Quals & Qualifiers::Restrict) { RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc); - if (NumQuals == 0) { - Loc = RestrictQualLoc; - QualStr = "restrict"; - } else { - QualStr += " restrict"; - } + QualStr += (NumQuals == 0 ? "restrict" : " restrict"); ++NumQuals; + if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc)) + Loc = RestrictQualLoc; } assert(NumQuals > 0 && "No known qualifiers?"); S.Diag(Loc, diag::warn_qual_return_type) - << QualStr << NumQuals - << ConstFixIt << VolatileFixIt << RestrictFixIt; + << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt; } /// GetTypeForDeclarator - Convert the type for the specified diff --git a/test/SemaCXX/return.cpp b/test/SemaCXX/return.cpp index f83b55a6ab..b457f6ace5 100644 --- a/test/SemaCXX/return.cpp +++ b/test/SemaCXX/return.cpp @@ -39,6 +39,11 @@ g(); char* const h(); // expected-warning{{'const' type qualifier on return type has no effect}} char* volatile i(); // expected-warning{{'volatile' type qualifier on return type has no effect}} +char* +volatile // expected-warning{{'const volatile' type qualifiers on return type have no effect}} +const +j(); + const volatile int scalar_cv(); // expected-warning{{'const volatile' type qualifiers on return type have no effect}} } -- 2.40.0