mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-26 08:56:06 +00:00

We have a new policy in place making links to private resources something we try to avoid in source and test files. Normally, we'd organically switch to the new policy rather than make a sweeping change across a project. However, Clang is in a somewhat special circumstance currently: recently, I've had several new contributors run into rdar links around test code which their patch was changing the behavior of. This turns out to be a surprisingly bad experience, especially for newer folks, for a handful of reasons: not understanding what the link is and feeling intimidated by it, wondering whether their changes are actually breaking something important to a downstream in some way, having to hunt down strangers not involved with the patch to impose on them for help, accidental pressure from asking for potentially private IP to be made public, etc. Because folks run into these links entirely by chance (through fixing bugs or working on new features), there's not really a set of problematic links to focus on -- all of the links have basically the same potential for causing these problems. As a result, this is an omnibus patch to remove all such links. This was not a mechanical change; it was done by manually searching for rdar, radar, radr, and other variants to find all the various problematic links. From there, I tried to retain or reword the surrounding comments so that we would lose as little context as possible. However, because most links were just a plain link with no supporting context, the majority of the changes are simple removals. Differential Review: https://reviews.llvm.org/D158071
146 lines
4.9 KiB
C
146 lines
4.9 KiB
C
// RUN: %clang_cc1 -fsyntax-only -ffreestanding -verify=expected,one-bit -triple x86_64-apple-darwin %s
|
|
// RUN: %clang_cc1 -fsyntax-only -ffreestanding -Wno-single-bit-bitfield-constant-conversion -verify -triple x86_64-apple-darwin %s
|
|
|
|
#include <stdbool.h>
|
|
|
|
// This file tests -Wconstant-conversion, a subcategory of -Wconversion
|
|
// which is on by default.
|
|
|
|
void test_6792488(void) {
|
|
int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}}
|
|
}
|
|
|
|
void test_7809123(void) {
|
|
struct { int i5 : 5; } a;
|
|
|
|
a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 36 to 4}}
|
|
}
|
|
|
|
void test(void) {
|
|
struct S {
|
|
int b : 1; // The only valid values are 0 and -1.
|
|
} s;
|
|
|
|
s.b = -3; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to -1}}
|
|
s.b = -2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -2 to 0}}
|
|
s.b = -1; // no-warning
|
|
s.b = 0; // no-warning
|
|
s.b = 1; // one-bit-warning {{implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1}}
|
|
s.b = true; // no-warning (we suppress it manually to reduce false positives)
|
|
s.b = false; // no-warning
|
|
s.b = 2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 2 to 0}}
|
|
}
|
|
|
|
enum Test2 { K_zero, K_one };
|
|
enum Test2 test2(enum Test2 *t) {
|
|
*t = 20;
|
|
return 10; // shouldn't warn
|
|
}
|
|
|
|
void test3(void) {
|
|
struct A {
|
|
unsigned int foo : 2;
|
|
int bar : 2;
|
|
};
|
|
|
|
struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to -2}}
|
|
struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to -2}}
|
|
struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
|
|
struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
|
|
struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
|
|
}
|
|
|
|
void test4(void) {
|
|
struct A {
|
|
char c : 2;
|
|
} a;
|
|
|
|
a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 257 to 1}}
|
|
}
|
|
|
|
void test5(void) {
|
|
struct A {
|
|
_Bool b : 1;
|
|
} a;
|
|
|
|
// Don't warn about this implicit conversion to bool, or at least
|
|
// don't warn about it just because it's a bit-field.
|
|
a.b = 100;
|
|
}
|
|
|
|
void test6(void) {
|
|
// Test that unreachable code doesn't trigger the truncation warning.
|
|
unsigned char x = 0 ? 65535 : 1; // no-warning
|
|
unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
|
|
}
|
|
|
|
void test7(void) {
|
|
struct {
|
|
unsigned int twoBits1:2;
|
|
unsigned int twoBits2:2;
|
|
unsigned int reserved:28;
|
|
} f;
|
|
|
|
f.twoBits1 = ~0; // no-warning
|
|
f.twoBits1 = ~1; // no-warning
|
|
f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to 1}}
|
|
f.twoBits1 &= ~1; // no-warning
|
|
f.twoBits2 &= ~2; // no-warning
|
|
}
|
|
|
|
void test8(void) {
|
|
enum E { A, B, C };
|
|
struct { enum E x : 1; } f;
|
|
f.x = C; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 2 to 0}}
|
|
}
|
|
|
|
void test9(void) {
|
|
const char max_char = 0x7F;
|
|
const short max_short = 0x7FFF;
|
|
const int max_int = 0x7FFFFFFF;
|
|
|
|
const short max_char_plus_one = (short)max_char + 1;
|
|
const int max_short_plus_one = (int)max_short + 1;
|
|
const long max_int_plus_one = (long)max_int + 1;
|
|
|
|
char new_char = max_char_plus_one; // expected-warning {{implicit conversion from 'const short' to 'char' changes value from 128 to -128}}
|
|
short new_short = max_short_plus_one; // expected-warning {{implicit conversion from 'const int' to 'short' changes value from 32768 to -32768}}
|
|
int new_int = max_int_plus_one; // expected-warning {{implicit conversion from 'const long' to 'int' changes value from 2147483648 to -2147483648}}
|
|
|
|
char hex_char = 0x80;
|
|
short hex_short = 0x8000;
|
|
int hex_int = 0x80000000;
|
|
|
|
char oct_char = 0200;
|
|
short oct_short = 0100000;
|
|
int oct_int = 020000000000;
|
|
|
|
char bin_char = 0b10000000;
|
|
short bin_short = 0b1000000000000000;
|
|
int bin_int = 0b10000000000000000000000000000000;
|
|
|
|
#define CHAR_MACRO_HEX 0xff
|
|
char macro_char_hex = CHAR_MACRO_HEX;
|
|
#define CHAR_MACRO_DEC 255
|
|
char macro_char_dec = CHAR_MACRO_DEC; // expected-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}}
|
|
|
|
char array_init[] = { 255, 127, 128, 129, 0 };
|
|
}
|
|
|
|
#define A 1
|
|
|
|
void test10(void) {
|
|
struct S {
|
|
unsigned a : 4;
|
|
} s;
|
|
s.a = -1;
|
|
s.a = 15;
|
|
s.a = -8;
|
|
s.a = ~0;
|
|
s.a = ~0U;
|
|
s.a = ~(1<<A);
|
|
|
|
s.a = -9; // expected-warning{{implicit truncation from 'int' to bit-field changes value from -9 to 7}}
|
|
s.a = 16; // expected-warning{{implicit truncation from 'int' to bit-field changes value from 16 to 0}}
|
|
}
|