@@ -689,9 +689,12 @@ redo:
static struct expression *constant_symbol_value(struct symbol *sym, int offset)
{
struct expression *value;
+ unsigned long mods = sym->ctype.modifiers;
- if (sym->ctype.modifiers & MOD_ACCESS)
- return NULL;
+ if (mods & MOD_ACCESS) {
+ if ((mods & (MOD_STATIC | MOD_CONST)) != (MOD_STATIC | MOD_CONST))
+ return NULL;
+ }
value = sym->initializer;
if (!value)
return NULL;
@@ -10,6 +10,6 @@ int test_array(int i)
* check-command: test-linearize -Wno-decl -fdump-ir $file
*
* check-output-ignore
- * check-output-excludes: phisrc\\..*return.*\\$2
- * check-output-contains: load\\.
+ * check-output-contains: phisrc\\..*return.*\\$2
+ * check-output-excludes: load\\.
*/
new file mode 100644
@@ -0,0 +1,16 @@
+static const int a = 3;
+
+static int foo(void)
+{
+ int *p = (int*) &a;
+ return a;
+}
+
+/*
+ * check-name: constant-static
+ * check-command: test-linearize $file
+ *
+ * check-output-ignore
+ * check-output-contains: ret\\.32 *\\$3
+ * check-output-excludes: load\\.
+ */
@@ -8,7 +8,6 @@ int test_array(int i)
/*
* check-name: default-init-array
* check-command: test-linearize -Wno-decl -fdump-ir $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-contains: phisrc\\..*return.*\\$0
Currently, constant_symbol_value() safely refuses to convert a symbol that is accessed. But accessing a 'static const' object should be fine since it should never be modified. So, allow to also expand symbol marked as accessed but being 'static const'. Note: a simple 'const' should be enough and 'static' doesn't make it 'more const'. In both cases, the object can be modified via a cast to a non-const pointer (which is UB). The only thing that 'static' changes is that *if* the object's address is not taken (so all accesses are const-expanded) then it's not needed to allocate memory for thsi object. OTOH, GCC also seems to do some these expansions only if the object is static const. So, I dunno. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> --- expand.c | 7 +++++-- validation/expand/constant-init-array.c | 4 ++-- validation/expand/constant-static.c | 16 ++++++++++++++++ validation/expand/default-init-array.c | 1 - 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 validation/expand/constant-static.c