diff options
author | Jordan Rose <jordan_rose@apple.com> | 2013-01-17 18:47:16 +0000 |
---|---|---|
committer | Jordan Rose <jordan_rose@apple.com> | 2013-01-17 18:47:16 +0000 |
commit | 242ae3d6805185fcb4fd45e96af5beba93e3532c (patch) | |
tree | ca28f50c979f0895ee2fa893654c8de51e32069f /lib/Analysis | |
parent | c8145bbab03caba4b7ef5540727487529c7effeb (diff) |
Format strings: correct signedness if already correcting width (%d,%u).
It is valid to do this:
printf("%u", (int)x);
But if we see this:
printf("%lu", (int)x);
...our fixit should suggest %d, not %u.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172739 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis')
-rw-r--r-- | lib/Analysis/PrintfFormatString.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/lib/Analysis/PrintfFormatString.cpp b/lib/Analysis/PrintfFormatString.cpp index b52433a0a4..5d37de362a 100644 --- a/lib/Analysis/PrintfFormatString.cpp +++ b/lib/Analysis/PrintfFormatString.cpp @@ -499,8 +499,26 @@ bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus11)) namedTypeToLengthModifier(QT, LM); - // If fixing the length modifier was enough, we are done. + // If fixing the length modifier was enough, we might be done. if (hasValidLengthModifier(Ctx.getTargetInfo())) { + // If we're going to offer a fix anyway, make sure the sign matches. + switch (CS.getKind()) { + case ConversionSpecifier::uArg: + case ConversionSpecifier::UArg: + if (QT->isSignedIntegerType()) + CS.setKind(clang::analyze_format_string::ConversionSpecifier::dArg); + break; + case ConversionSpecifier::dArg: + case ConversionSpecifier::DArg: + case ConversionSpecifier::iArg: + if (QT->isUnsignedIntegerType()) + CS.setKind(clang::analyze_format_string::ConversionSpecifier::uArg); + break; + default: + // Other specifiers do not have signed/unsigned variants. + break; + } + const analyze_printf::ArgType &ATR = getArgType(Ctx, IsObjCLiteral); if (ATR.isValid() && ATR.matchesType(Ctx, QT)) return true; |