llvm-project/clang/test/SemaObjC/objc-literal-fixit.m
Aaron Ballman 7068aa9841 Strengthen -Wint-conversion to default to an error
Clang has traditionally allowed C programs to implicitly convert
integers to pointers and pointers to integers, despite it not being
valid to do so except under special circumstances (like converting the
integer 0, which is the null pointer constant, to a pointer). In C89,
this would result in undefined behavior per 3.3.4, and in C99 this rule
was strengthened to be a constraint violation instead. Constraint
violations are most often handled as an error.

This patch changes the warning to default to an error in all C modes
(it is already an error in C++). This gives us better security posture
by calling out potential programmer mistakes in code but still allows
users who need this behavior to use -Wno-error=int-conversion to retain
the warning behavior, or -Wno-int-conversion to silence the diagnostic
entirely.

Differential Revision: https://reviews.llvm.org/D129881
2022-07-22 15:24:54 -04:00

40 lines
1.8 KiB
Objective-C

// RUN: %clang_cc1 -triple x86_64-apple-macos10.10 %s -verify=c,expected
// RUN: %clang_cc1 -triple x86_64-apple-macos10.10 %s -xobjective-c++ -verify=cxx,expected
// RUN: %clang_cc1 -triple x86_64-apple-macos10.10 %s -fobjc-arc -verify=c,arc,expected
typedef signed char BOOL;
#define YES __objc_yes
#define NO __objc_no
@interface NSNumber
+(instancetype)numberWithChar:(char)value;
+(instancetype)numberWithInt:(int)value;
+(instancetype)numberWithDouble:(double)value;
+(instancetype)numberWithBool:(BOOL)value;
@end
void test(void) {
NSNumber *n = YES; // expected-error{{numeric literal must be prefixed by '@'}}
NSNumber *n1 = 1; // expected-error{{numeric literal must be prefixed by '@'}}
NSNumber *n2 = NO; // c-warning{{expression which evaluates to zero treated as a null pointer constant}}
// cxx-error@-1{{numeric literal must be prefixed by '@'}}
NSNumber *n3 = 0;
NSNumber *n4 = 0.0; // expected-error{{numeric literal must be prefixed by '@'}}
NSNumber *n5 = '\0'; // c-warning{{expression which evaluates to zero treated as a null pointer constant}}
// cxx-error@-1{{numeric literal must be prefixed by '@'}}
NSNumber *n6 = '1'; // expected-error{{numeric literal must be prefixed by '@'}}
int i;
NSNumber *n7 = i; // c-error{{incompatible integer to pointer conversion}}
// arc-error@-1{{implicit conversion of 'int' to 'NSNumber *' is disallowed with ARC}}
// cxx-error@-2{{cannot initialize a variable of type 'NSNumber *' with an lvalue of type 'int'}}
id n8 = 1; // c-error{{incompatible integer to pointer conversion}}
// arc-error@-1{{implicit conversion of 'int' to 'id' is disallowed with ARC}}
// cxx-error@-2{{cannot initialize a variable of type 'id' with an rvalue of type 'int'}}
}