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 | 58dab6829a970b798e04daed5ce750b8023483f2 (patch) | |
| tree | 3c1cd90743467683615976f6921dbed174b687c7 /clang/lib/Sema | |
| parent | b41b407f3d54d5ab0bc7342f723fda2dbef59955 (diff) | |
| download | bcm5719-llvm-58dab6829a970b798e04daed5ce750b8023483f2.tar.gz bcm5719-llvm-58dab6829a970b798e04daed5ce750b8023483f2.zip | |
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.
llvm-svn: 151080
Diffstat (limited to 'clang/lib/Sema')
| -rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 1d75ef6e6f5..9454a751550 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/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; |

