You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang][sema] Add nonnull attribute to builtin format functions
Annotate printf/scanf and related builtins with the nonnull attribute on
their format string parameters. This enables diagnostics when NULL is
passed, matching GCC behavior. Updated existing Sema tests and added new
one for coverage.
Copy file name to clipboardExpand all lines: clang/test/SemaCXX/format-strings-0x.cpp
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ void f(char **sp, float *fp) {
14
14
printf("%a", 1.0);
15
15
scanf("%afoobar", fp);
16
16
printf(nullptr);
17
+
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
17
18
printf(*sp); // expected-warning {{not a string literal}}
18
19
// expected-note@-1{{treat the string as an argument to avoid this}}
19
20
@@ -32,4 +33,5 @@ void f(char **sp, float *fp) {
32
33
printf("init list: %d", { 0 }); // expected-error {{cannot pass initializer list to variadic function; expected type from format string was 'int'}}
33
34
printf("void: %d", f(sp, fp)); // expected-error {{cannot pass expression of type 'void' to variadic function; expected type from format string was 'int'}}
34
35
printf(0, { 0 }); // expected-error {{cannot pass initializer list to variadic function}}
36
+
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
printf("%d%d%d", 1L, *obj, 1L); // expected-error {{cannot pass object with interface type 'Foo' by value to variadic function; expected type from format string was 'int'}} expected-warning 2 {{format specifies type 'int' but the argument has type 'long'}}
260
260
printf("%!", *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}} expected-warning {{invalid conversion specifier}}
261
261
printf(0, *obj); // expected-error {{cannot pass object with interface type 'Foo' by value through variadic function}}
262
+
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
262
263
263
264
[Bar log2:@"%d", *obj]; // expected-error {{cannot pass object with interface type 'Foo' by value to variadic method; expected type from format string was 'int'}}
0 commit comments