diff options
author | Jean-Daniel Dupas <devlists@shadowlab.org> | 2012-02-21 20:00:53 +0000 |
---|---|---|
committer | Jean-Daniel Dupas <devlists@shadowlab.org> | 2012-02-21 20:00:53 +0000 |
commit | f57c413e444b441fa75ae8911d183c19b53bdd56 (patch) | |
tree | 8242e39f4ac230f2c3348e89f45d48354439df33 /lib/Sema/SemaChecking.cpp | |
parent | f4b7de1cef3007cc0479775638198287384d9af1 (diff) |
When calling a non variadic format function(vprintf, vscanf, NSLogv, …), warn if the format string argument is a parameter that is not itself declared as a format string with compatible format.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151080 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaChecking.cpp')
-rw-r--r-- | lib/Sema/SemaChecking.cpp | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 1d75ef6e6f..9454a75155 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -1447,13 +1447,27 @@ bool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args, // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". // ... // - // - // FIXME: We don't have full attribute support yet, so just check to see - // if the argument is a DeclRefExpr that references a parameter. We'll - // add proper support for checking the attribute later. - if (HasVAListArg) - if (isa<ParmVarDecl>(VD)) - return true; + if (HasVAListArg) { + if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { + if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { + int PVIndex = PV->getFunctionScopeIndex() + 1; + for (specific_attr_iterator<FormatAttr> + i = ND->specific_attr_begin<FormatAttr>(), + e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) { + FormatAttr *PVFormat = *i; + // adjust for implicit parameter + if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) + if (MD->isInstance()) + ++PVIndex; + // We also check if the formats are compatible. + // We can't pass a 'scanf' string to a 'printf' function. + if (PVIndex == PVFormat->getFormatIdx() && + Type == GetFormatStringType(PVFormat)) + return true; + } + } + } + } } return false; |