Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 52aabaf

Browse files
author
Jean-Daniel Dupas
committed
Implements support of format_arg attribute on C++ member.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149998 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7fb8630 commit 52aabaf

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

lib/Sema/SemaChecking.cpp

+14-19
Original file line numberDiff line numberDiff line change
@@ -1459,21 +1459,20 @@ bool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args,
14591459
return false;
14601460
}
14611461

1462-
case Stmt::CallExprClass: {
1462+
case Stmt::CallExprClass:
1463+
case Stmt::CXXMemberCallExprClass: {
14631464
const CallExpr *CE = cast<CallExpr>(E);
1464-
if (const ImplicitCastExpr *ICE
1465-
= dyn_cast<ImplicitCastExpr>(CE->getCallee())) {
1466-
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1467-
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
1468-
if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) {
1469-
unsigned ArgIndex = FA->getFormatIdx();
1470-
const Expr *Arg = CE->getArg(ArgIndex - 1);
1471-
1472-
return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg,
1473-
format_idx, firstDataArg, Type,
1474-
inFunctionCall);
1475-
}
1476-
}
1465+
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
1466+
if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
1467+
unsigned ArgIndex = FA->getFormatIdx();
1468+
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1469+
if (MD->isInstance())
1470+
--ArgIndex;
1471+
const Expr *Arg = CE->getArg(ArgIndex - 1);
1472+
1473+
return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg,
1474+
format_idx, firstDataArg, Type,
1475+
inFunctionCall);
14771476
}
14781477
}
14791478

@@ -1534,11 +1533,7 @@ void Sema::CheckFormatArguments(const FormatAttr *Format, CallExpr *TheCall) {
15341533
// The way the format attribute works in GCC, the implicit this argument
15351534
// of member functions is counted. However, it doesn't appear in our own
15361535
// lists, so decrement format_idx in that case.
1537-
if (isa<CXXMemberCallExpr>(TheCall)) {
1538-
const CXXMethodDecl *method_decl =
1539-
dyn_cast<CXXMethodDecl>(TheCall->getCalleeDecl());
1540-
IsCXXMember = method_decl && method_decl->isInstance();
1541-
}
1536+
IsCXXMember = isa<CXXMemberCallExpr>(TheCall);
15421537
CheckFormatArguments(Format, TheCall->getArgs(), TheCall->getNumArgs(),
15431538
IsCXXMember, TheCall->getRParenLoc(),
15441539
TheCall->getCallee()->getSourceRange());

test/SemaCXX/format-strings.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -pedantic %s
22

33
extern "C" {
44
extern int scanf(const char *restrict, ...);
@@ -17,3 +17,25 @@ void f(char **sp, float *fp) {
1717
void g() {
1818
printf("%ls", "foo"); // expected-warning{{format specifies type 'wchar_t *' but the argument has type 'const char *'}}
1919
}
20+
21+
// Test that we properly handle format_idx on C++ members.
22+
class Foo {
23+
public:
24+
const char *gettext(const char *fmt) __attribute__((format_arg(2)));
25+
26+
int scanf(const char *restrict, ...) __attribute__((format(scanf, 2, 3)));
27+
int printf(const char *restrict, ...) __attribute__((format(printf, 2, 3)));
28+
29+
static const char *gettext_static(const char *fmt) __attribute__((format_arg(1)));
30+
static int printf_static(const char *restrict, ...) __attribute__((format(printf, 1, 2)));
31+
};
32+
33+
void h(int *i) {
34+
Foo foo;
35+
foo.scanf("%d"); // expected-warning{{more '%' conversions than data arguments}}
36+
foo.printf("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
37+
Foo::printf_static("%d", i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
38+
39+
printf(foo.gettext("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
40+
printf(Foo::gettext_static("%d"), i); // expected-warning{{format specifies type 'int' but the argument has type 'int *'}}
41+
}

0 commit comments

Comments
 (0)