diff options
| author | Nicolai Stange <nicstange@gmail.com> | 2016-02-01 03:38:24 +0100 |
|---|---|---|
| committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2017-03-31 02:27:12 +0200 |
| commit | 8880427d5596d48c9b55b13b87b595e4b1763e14 (patch) | |
| tree | 4b3793e44429031a9be9bc76bf60c022cdb7eb2e /validation | |
| parent | 080bef90f57599975b9a29a170d548038f01c59e (diff) | |
| download | sparse-dev-8880427d5596d48c9b55b13b87b595e4b1763e14.tar.gz | |
constexpr: recognize static objects as address constants
Introduce support for recognizing address constants created either
- explicitly by referencing a static storage duration object by means
of the unary & operator,
- implicitly by the use of an expression of array or function type.
Initially tag an expression as being an address constant at the primary
expression level, i.e. upon encountering a symbol designating an object of
static storage duration in primary_expression().
Carry the address constant flag over to the *-preop wrapped expression
created by evaluate_symbol_expression().
When dereferencing such a *-preop wrapped expression, make
evaluate_addressof() keep any address constant flag for the unwrapped
expression.
Signed-off-by: Nicolai Stange <nicstange@gmail.com>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Diffstat (limited to 'validation')
| -rw-r--r-- | validation/constexpr-addr-of-static.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/validation/constexpr-addr-of-static.c b/validation/constexpr-addr-of-static.c new file mode 100644 index 00000000..a3af99ae --- /dev/null +++ b/validation/constexpr-addr-of-static.c @@ -0,0 +1,36 @@ +static int a = 1; +static int b[2] = {1, 1}; +static void c(void) {} + +static int *d = &a; // OK +static int *e = d; // KO +static int *f = b; // OK + +static void (*g)(void) = c; // OK +static void (*h)(void) = &c; // OK + +static int *i = &*&a; // OK +static int *j = &*b; // OK +static int *k = &*d; // KO + + +static void l(void) { + int a = 1; + static int *b = &a; // KO +} + +static void m(void) { + static int a = 1; + static int *b = &a; // OK +} + +/* + * check-name: address of static object constness verification. + * check-command: sparse -Wconstexpr-not-const $file + * + * check-error-start +constexpr-addr-of-static.c:6:17: warning: non-constant initializer for static object +constexpr-addr-of-static.c:14:19: warning: non-constant initializer for static object +constexpr-addr-of-static.c:19:26: warning: non-constant initializer for static object + * check-error-end + */ |
