[cfe-commits] r78276 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaChecking.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/format-attribute-printf0.c
Ryan Flynn
pizza at parseerror.com
Wed Aug 5 20:01:12 PDT 2009
Author: pizza
Date: Wed Aug 5 22:00:50 2009
New Revision: 78276
URL: http://llvm.org/viewvc/llvm-project?rev=78276&view=rev
Log:
add support for FreeBSD's format(printf0,x,y) attribute; allows null format string.
Added:
cfe/trunk/test/Sema/format-attribute-printf0.c
Modified:
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/lib/Sema/SemaDeclAttr.cpp
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=78276&r1=78275&r2=78276&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Aug 5 22:00:50 2009
@@ -3308,6 +3308,7 @@
Action::OwningExprResult CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall);
SourceLocation getLocationOfStringLiteralByte(const StringLiteral *SL,
unsigned ByteNo) const;
+ bool CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall);
bool CheckObjCString(Expr *Arg);
bool SemaBuiltinVAStart(CallExpr *TheCall);
bool SemaBuiltinUnorderedCompare(CallExpr *TheCall);
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=78276&r1=78275&r2=78276&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Aug 5 22:00:50 2009
@@ -84,6 +84,21 @@
}
}
+/// CheckablePrintfAttr - does a function call have a "printf" attribute
+/// and arguments that merit checking?
+bool Sema::CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall) {
+ if (Format->getType() == "printf") return true;
+ if (Format->getType() == "printf0") {
+ // printf0 allows null "format" string; if so don't check format/args
+ unsigned format_idx = Format->getFormatIdx() - 1;
+ if (format_idx < TheCall->getNumArgs()) {
+ Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts();
+ if (!Format->isNullPointerConstant(Context))
+ return true;
+ }
+ }
+ return false;
+}
/// CheckFunctionCall - Check a direct function call for various correctness
/// and safety properties not strictly enforced by the C type system.
@@ -167,7 +182,7 @@
// Printf checking.
if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) {
- if (Format->getType() == "printf") {
+ if (CheckablePrintfAttr(Format, TheCall)) {
bool HasVAListArg = Format->getFirstArg() == 0;
if (!HasVAListArg) {
if (const FunctionProtoType *Proto
@@ -201,7 +216,7 @@
QualType Ty = V->getType();
if (!Ty->isBlockPointerType())
return move(TheCallResult);
- if (Format->getType() == "printf") {
+ if (CheckablePrintfAttr(Format, TheCall)) {
bool HasVAListArg = Format->getFirstArg() == 0;
if (!HasVAListArg) {
const FunctionType *FT =
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=78276&r1=78275&r2=78276&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Wed Aug 5 22:00:50 2009
@@ -1212,7 +1212,8 @@
default: break;
case 5: Supported = !memcmp(Format, "scanf", 5); break;
case 6: Supported = !memcmp(Format, "printf", 6); break;
- case 7: Supported = !memcmp(Format, "strfmon", 7); break;
+ case 7: Supported = !memcmp(Format, "printf0", 7) ||
+ !memcmp(Format, "strfmon", 7); break;
case 8:
Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
(is_NSString = !memcmp(Format, "NSString", 8)) ||
Added: cfe/trunk/test/Sema/format-attribute-printf0.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-attribute-printf0.c?rev=78276&view=auto
==============================================================================
--- cfe/trunk/test/Sema/format-attribute-printf0.c (added)
+++ cfe/trunk/test/Sema/format-attribute-printf0.c Wed Aug 5 22:00:50 2009
@@ -0,0 +1,26 @@
+//RUN: clang-cc -fsyntax-only -verify %s
+
+#include <stdarg.h>
+
+// same as format(printf(...))...
+void a2(const char *a, ...) __attribute__((format(printf0, 1,2))); // no-error
+void b2(const char *a, ...) __attribute__((format(printf0, 1,1))); // expected-error {{'format' attribute parameter 3 is out of bounds}}
+void c2(const char *a, ...) __attribute__((format(printf0, 0,2))); // expected-error {{'format' attribute parameter 2 is out of bounds}}
+void d2(const char *a, int c) __attribute__((format(printf0, 1,2))); // expected-error {{format attribute requires variadic function}}
+void e2(char *str, int c, ...) __attribute__((format(printf0, 2,3))); // expected-error {{format argument not a string type}}
+
+// FreeBSD usage
+#define __printf0like(fmt,va) __attribute__((__format__(__printf0__,fmt,va)))
+void null(int i, const char *a, ...) __printf0like(2,0); // no-error
+void null(int i, const char *a, ...) {
+ if (a)
+ (void)0/* vprintf(...) would go here */;
+}
+
+void callnull(void){
+ null(0, 0); // no error
+ null(0, (char*)0); // no error
+ null(0, (void*)0); // no error
+ null(0, (int*)0); // expected-warning {{incompatible pointer types}}
+}
+
More information about the cfe-commits
mailing list