diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-07-27 04:46:02 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-07-27 04:46:02 +0000 |
commit | 96827eb52405a71c65c200949f3e644368e86454 (patch) | |
tree | 3e7254b9dc5eaa4bacd765500a57e4773daca8bb /lib | |
parent | 5e04bdde8e74d991feffe9cf95d731f7e473dbb7 (diff) |
Revert r109428 "Hoist argument type checking into CheckFormatHandler. This is prep for scanf format"
Got errors about ASTContext being undefined with Visual Studio 2010.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@109491 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Analysis/FormatString.cpp | 4 | ||||
-rw-r--r-- | lib/Analysis/ScanfFormatString.cpp | 6 | ||||
-rw-r--r-- | lib/Sema/SemaChecking.cpp | 94 |
3 files changed, 42 insertions, 62 deletions
diff --git a/lib/Analysis/FormatString.cpp b/lib/Analysis/FormatString.cpp index 2c012d3506..0fbe54353e 100644 --- a/lib/Analysis/FormatString.cpp +++ b/lib/Analysis/FormatString.cpp @@ -379,11 +379,9 @@ void OptionalAmount::toString(llvm::raw_ostream &os) const { } //===----------------------------------------------------------------------===// -// Methods on FormatSpecifier. +// Methods on ConversionSpecifier. //===----------------------------------------------------------------------===// -FormatSpecifier::~FormatSpecifier() {} - bool FormatSpecifier::hasValidLengthModifier() const { switch (LM.getKind()) { case LengthModifier::None: diff --git a/lib/Analysis/ScanfFormatString.cpp b/lib/Analysis/ScanfFormatString.cpp index 4c2c4c6fb9..6a8673ab55 100644 --- a/lib/Analysis/ScanfFormatString.cpp +++ b/lib/Analysis/ScanfFormatString.cpp @@ -218,10 +218,4 @@ bool clang::analyze_format_string::ParseScanfString(FormatStringHandler &H, return false; } -ArgTypeResult ScanfSpecifier::getArgType(ASTContext &Ctx) const { - // FIXME: Fill in. - return ArgTypeResult(); -} - - diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 1b689499ed..03ca084dd8 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -1174,11 +1174,6 @@ protected: const analyze_format_string::ConversionSpecifier &CS, const char *startSpecifier, unsigned specifierLen, unsigned argIndex); - - void CheckArgType(const analyze_format_string::FormatSpecifier &FS, - const analyze_format_string::ConversionSpecifier &CS, - const char *startSpecifier, unsigned specifierLen, - unsigned argIndex); }; } @@ -1304,52 +1299,6 @@ CheckFormatHandler::CheckNumArgs( return true; } -void CheckFormatHandler::CheckArgType( - const analyze_format_string::FormatSpecifier &FS, - const analyze_format_string::ConversionSpecifier &CS, - const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { - - const Expr *Ex = getDataArg(argIndex); - const analyze_format_string::ArgTypeResult &ATR = FS.getArgType(S.Context); - - if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { - // Check if we didn't match because of an implicit cast from a 'char' - // or 'short' to an 'int'. This is done because scanf/printf are varargs - // functions. - if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) - if (ICE->getType() == S.Context.IntTy) - if (ATR.matchesType(S.Context, ICE->getSubExpr()->getType())) - return; - - if (const analyze_printf::PrintfSpecifier *PFS = - dyn_cast<analyze_printf::PrintfSpecifier>(&FS)) { - // We may be able to offer a FixItHint if it is a supported type. - analyze_printf::PrintfSpecifier fixedFS(*PFS); - if (fixedFS.fixType(Ex->getType())) { - // Get the fix string from the fixed format specifier - llvm::SmallString<128> buf; - llvm::raw_svector_ostream os(buf); - fixedFS.toString(os); - - S.Diag(getLocationOfByte(CS.getStart()), - diag::warn_printf_conversion_argument_type_mismatch) - << ATR.getRepresentativeType(S.Context) << Ex->getType() - << getSpecifierRange(startSpecifier, specifierLen) - << Ex->getSourceRange() - << FixItHint::CreateReplacement( - getSpecifierRange(startSpecifier, specifierLen), os.str()); - } - else { - S.Diag(getLocationOfByte(CS.getStart()), - diag::warn_printf_conversion_argument_type_mismatch) - << ATR.getRepresentativeType(S.Context) << Ex->getType() - << getSpecifierRange(startSpecifier, specifierLen) - << Ex->getSourceRange(); - } - } - } -} - //===--- CHECK: Printf format string checking ------------------------------===// namespace { @@ -1621,8 +1570,47 @@ CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) return false; - CheckArgType(FS, CS, startSpecifier, specifierLen, argIndex); - + // Now type check the data expression that matches the + // format specifier. + const Expr *Ex = getDataArg(argIndex); + const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context); + if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { + // Check if we didn't match because of an implicit cast from a 'char' + // or 'short' to an 'int'. This is done because printf is a varargs + // function. + if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) + if (ICE->getType() == S.Context.IntTy) + if (ATR.matchesType(S.Context, ICE->getSubExpr()->getType())) + return true; + + // We may be able to offer a FixItHint if it is a supported type. + PrintfSpecifier fixedFS = FS; + bool success = fixedFS.fixType(Ex->getType()); + + if (success) { + // Get the fix string from the fixed format specifier + llvm::SmallString<128> buf; + llvm::raw_svector_ostream os(buf); + fixedFS.toString(os); + + S.Diag(getLocationOfByte(CS.getStart()), + diag::warn_printf_conversion_argument_type_mismatch) + << ATR.getRepresentativeType(S.Context) << Ex->getType() + << getSpecifierRange(startSpecifier, specifierLen) + << Ex->getSourceRange() + << FixItHint::CreateReplacement( + getSpecifierRange(startSpecifier, specifierLen), + os.str()); + } + else { + S.Diag(getLocationOfByte(CS.getStart()), + diag::warn_printf_conversion_argument_type_mismatch) + << ATR.getRepresentativeType(S.Context) << Ex->getType() + << getSpecifierRange(startSpecifier, specifierLen) + << Ex->getSourceRange(); + } + } + return true; } |