From be6126a2a784e1446460b8d15c2b26f880c871fc Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Wed, 15 Feb 2012 09:59:46 +0000 Subject: Make -Wformat fix-its preserve original conversion specifiers. This commit makes PrintfSpecifier::fixType() and ScanfSpecifier::fixType() only fix a conversion specification enough that Clang wouldn't warn about it, as opposed to always changing it to use the "canonical" conversion specifier. (PR11975) This preserves the user's choice of conversion specifier in cases like: printf("%a", (long double)1); where we previously suggested "%Lf", we now suggest "%La" printf("%x", (long)1); where we previously suggested "%ld", we now suggest "%lx". git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150578 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ScanfFormatString.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/Analysis/ScanfFormatString.cpp') diff --git a/lib/Analysis/ScanfFormatString.cpp b/lib/Analysis/ScanfFormatString.cpp index c1cdef8632..5990a56c35 100644 --- a/lib/Analysis/ScanfFormatString.cpp +++ b/lib/Analysis/ScanfFormatString.cpp @@ -307,8 +307,8 @@ ScanfArgTypeResult ScanfSpecifier::getArgType(ASTContext &Ctx) const { return ScanfArgTypeResult(); } -bool ScanfSpecifier::fixType(QualType QT, const LangOptions &LangOpt) -{ +bool ScanfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, + ASTContext &Ctx) { if (!QT->isPointerType()) return false; @@ -390,17 +390,19 @@ bool ScanfSpecifier::fixType(QualType QT, const LangOptions &LangOpt) } } + // If fixing the length modifier was enough, we are done. + const analyze_scanf::ScanfArgTypeResult &ATR = getArgType(Ctx); + if (hasValidLengthModifier() && ATR.isValid() && ATR.matchesType(Ctx, QT)) + return true; + // Figure out the conversion specifier. if (PT->isRealFloatingType()) CS.setKind(ConversionSpecifier::fArg); else if (PT->isSignedIntegerType()) CS.setKind(ConversionSpecifier::dArg); - else if (PT->isUnsignedIntegerType()) { - // Preserve the original formatting, e.g. 'X', 'o'. - if (!CS.isUIntArg()) { - CS.setKind(ConversionSpecifier::uArg); - } - } else + else if (PT->isUnsignedIntegerType()) + CS.setKind(ConversionSpecifier::uArg); + else llvm_unreachable("Unexpected type"); return true; -- cgit v1.2.3