summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema
diff options
context:
space:
mode:
authorFariborz Jahanian <fjahanian@apple.com>2014-09-11 19:13:23 +0000
committerFariborz Jahanian <fjahanian@apple.com>2014-09-11 19:13:23 +0000
commitfba4fe6717364ea12835a34e59e45c162fdf2b9e (patch)
treeecb13af7e17e3d4e60d110fb06866f44151706f7 /clang/lib/Sema
parentc1599157902e8deea660e22b396c111bb0bea69b (diff)
downloadbcm5719-llvm-fba4fe6717364ea12835a34e59e45c162fdf2b9e.tar.gz
bcm5719-llvm-fba4fe6717364ea12835a34e59e45c162fdf2b9e.zip
Objective-C. Under a special flag, -Wcstring-format-directive,
off by default, issue a warning if %s directive is used in formart argument of a function/method declared as __attribute__((format(CF/NSString, ...))) To complete rdar://18182443 llvm-svn: 217619
Diffstat (limited to 'clang/lib/Sema')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp60
-rw-r--r--clang/lib/Sema/SemaExprObjC.cpp35
2 files changed, 64 insertions, 31 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 0c3a3df1f18..ac5b68fb233 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -767,6 +767,15 @@ static void CheckNonNullArgument(Sema &S,
S.Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
}
+bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) {
+ FormatStringInfo FSI;
+ if ((GetFormatStringType(Format) == FST_NSString) &&
+ getFormatStringInfo(Format, false, &FSI)) {
+ Idx = FSI.FormatIdx;
+ return true;
+ }
+ return false;
+}
/// \brief Diagnose use of %s directive in an NSString which is being passed
/// as formatting string to formatting method.
static void
@@ -774,27 +783,38 @@ DiagnoseCStringFormatDirectiveInCFAPI(Sema &S,
const NamedDecl *FDecl,
Expr **Args,
unsigned NumArgs) {
- if (NumArgs < 3)
- return;
+ unsigned Idx = 0;
+ bool Format = false;
ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily();
if (SFFamily == ObjCStringFormatFamily::SFF_CFString) {
- const Expr *FormatExpr = Args[2];
- if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr))
- FormatExpr = CSCE->getSubExpr();
- const StringLiteral *FormatString;
- if (const ObjCStringLiteral *OSL =
- dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts()))
- FormatString = OSL->getString();
- else
- FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts());
- if (!FormatString)
- return;
- if (S.FormatStringHasSArg(FormatString)) {
- S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
- << "%s" << 1 << 1;
- S.Diag(FDecl->getLocation(), diag::note_entity_declared_at)
- << FDecl->getDeclName();
+ Idx = 2;
+ Format = true;
+ }
+ else
+ for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
+ if (S.GetFormatNSStringIdx(I, Idx)) {
+ Format = true;
+ break;
+ }
}
+ if (!Format || NumArgs <= Idx)
+ return;
+ const Expr *FormatExpr = Args[Idx];
+ if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr))
+ FormatExpr = CSCE->getSubExpr();
+ const StringLiteral *FormatString;
+ if (const ObjCStringLiteral *OSL =
+ dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts()))
+ FormatString = OSL->getString();
+ else
+ FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts());
+ if (!FormatString)
+ return;
+ if (S.FormatStringHasSArg(FormatString)) {
+ S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
+ << "%s" << 1 << 1;
+ S.Diag(FDecl->getLocation(), diag::note_entity_declared_at)
+ << FDecl->getDeclName();
}
}
@@ -930,8 +950,8 @@ bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
return false;
CheckAbsoluteValueFunction(TheCall, FDecl, FnInfo);
-
- DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs);
+ if (getLangOpts().ObjC1)
+ DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs);
unsigned CMId = FDecl->getMemoryFunctionKind();
if (CMId == 0)
diff --git a/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp
index d1c7aa6a435..ac28d833823 100644
--- a/clang/lib/Sema/SemaExprObjC.cpp
+++ b/clang/lib/Sema/SemaExprObjC.cpp
@@ -2121,21 +2121,34 @@ DiagnoseCStringFormatDirectiveInObjCAPI(Sema &S,
ObjCMethodDecl *Method,
Selector Sel,
Expr **Args, unsigned NumArgs) {
- if (NumArgs == 0)
- return;
+ unsigned Idx = 0;
+ bool Format = false;
ObjCStringFormatFamily SFFamily = Sel.getStringFormatFamily();
if (SFFamily == ObjCStringFormatFamily::SFF_NSString) {
- Expr *FormatExpr = Args[0];
- if (ObjCStringLiteral *OSL =
- dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) {
- StringLiteral *FormatString = OSL->getString();
- if (S.FormatStringHasSArg(FormatString)) {
- S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
+ Idx = 0;
+ Format = true;
+ }
+ else if (Method) {
+ for (const auto *I : Method->specific_attrs<FormatAttr>()) {
+ if (S.GetFormatNSStringIdx(I, Idx)) {
+ Format = true;
+ break;
+ }
+ }
+ }
+ if (!Format || NumArgs <= Idx)
+ return;
+
+ Expr *FormatExpr = Args[Idx];
+ if (ObjCStringLiteral *OSL =
+ dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) {
+ StringLiteral *FormatString = OSL->getString();
+ if (S.FormatStringHasSArg(FormatString)) {
+ S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
<< "%s" << 0 << 0;
- if (Method)
- S.Diag(Method->getLocation(), diag::note_method_declared_at)
+ if (Method)
+ S.Diag(Method->getLocation(), diag::note_method_declared_at)
<< Method->getDeclName();
- }
}
}
}
OpenPOWER on IntegriCloud