-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sema] Add check for bitfield assignments to larger integral types (#…
…68276) We noticed that clang does not check for bitfield assignment widths, while gcc does check this. gcc produced a warning like so for it's -Wconversion flag: ``` $ gcc -Wconversion -c test.c test.c: In function 'foo': test.c:10:15: warning: conversion from 'int' to 'signed char:7' may change value [-Wconversion] 10 | vxx.bf = x; // no warning | ^ ``` This change simply adds this check for integral types under the -Wbitfield-conversion compiler option.
- Loading branch information
1 parent
4c6cba3
commit dd0f642
Showing
5 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// RUN: %clang_cc1 -Wconversion -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -Wbitfield-conversion -fsyntax-only -verify %s | ||
|
||
typedef struct _xx { | ||
int bf:9; // expected-note 4{{declared here}} | ||
} xx, *pxx; | ||
|
||
xx vxx; | ||
|
||
void foo1(int x) { | ||
vxx.bf = x; // expected-warning{{conversion from 'int' (32 bits) to bit-field 'bf' (9 bits) may change value}} | ||
} | ||
void foo2(short x) { | ||
vxx.bf = x; // expected-warning{{conversion from 'short' (16 bits) to bit-field 'bf' (9 bits) may change value}} | ||
} | ||
void foo3(char x) { | ||
vxx.bf = x; // no warning expected | ||
} | ||
void foo5(void * x) { | ||
vxx.bf = (int)x; // expected-warning{{cast to smaller integer type 'int' from 'void *'}} | ||
// expected-warning@-1{{conversion from 'int' (32 bits) to bit-field 'bf' (9 bits) may change value}} | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
void foo6(short x) { | ||
vxx.bf = 0xff & x; // no warning expected | ||
} | ||
void foo7(short x) { | ||
vxx.bf = 0x1ff & x; // no warning expected | ||
} | ||
void foo8(short x) { | ||
vxx.bf = 0x3ff & x; // expected-warning{{conversion from 'int' (10 bits) to bit-field 'bf' (9 bits) may change value}} | ||
} | ||
int fee(void) { | ||
return 0; | ||
} |
This test line fails on a 32 bit system, https://lab.llvm.org/buildbot/#/builders/245/builds/15263. As sizeof(void*) == sizeof(int).
If I get time today I'll see if I can fix it.