Skip to content

Commit 65af274

Browse files
authored
Merge pull request #5729 from tautschnig/attribute-used
Follow-up to 090790a: attribute __used__ requires unique names
2 parents f5525c6 + 9298526 commit 65af274

File tree

7 files changed

+49
-21
lines changed

7 files changed

+49
-21
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
static int foo __attribute__((used)) = 42;
2+
3+
int main()
4+
{
5+
return 0;
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct bar
2+
{
3+
char *ptr;
4+
};
5+
6+
static struct bar foo __attribute__((used))
7+
__attribute__((__section__(".ref.data")));
8+
9+
static struct bar foo __attribute__((used))
10+
__attribute__((__section__(".ref.data"))) = {0};
11+
12+
void use_foo()
13+
{
14+
__CPROVER_assert(foo.ptr == 0, "null");
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE gcc-only
2+
main.c
3+
other.c
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$

regression/goto-instrument/gcc_attribute_used1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ main.c
33

44
^EXIT=0$
55
^SIGNAL=0$
6-
^[[:space:]]*ASSIGN foo := 42$
6+
^[[:space:]]*ASSIGN .*foo := 42$
77
--
88
^warning: ignoring
99
^CONVERSION ERROR$

src/ansi-c/ansi_c_declaration.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ void ansi_c_declarationt::to_symbol(
173173
else if(get_is_extern()) // traditional GCC
174174
symbol.is_file_local=true;
175175
}
176-
177-
// GCC __attribute__((__used__)) - do not treat those as file-local
178-
if(get_is_used())
179-
symbol.is_file_local = false;
180176
}
181177
}
182178
else // non-function
@@ -190,10 +186,8 @@ void ansi_c_declarationt::to_symbol(
190186
(!symbol.is_static_lifetime && !get_is_extern()) ||
191187
get_is_thread_local();
192188

193-
symbol.is_file_local=
194-
symbol.is_macro ||
195-
(!get_is_global() && !get_is_extern()) ||
196-
(get_is_global() && get_is_static() && !get_is_used()) ||
197-
symbol.is_parameter;
189+
symbol.is_file_local =
190+
symbol.is_macro || (!get_is_global() && !get_is_extern()) ||
191+
(get_is_global() && get_is_static()) || symbol.is_parameter;
198192
}
199193
}

src/ansi-c/ansi_c_declaration.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,6 @@ class ansi_c_declarationt:public exprt
195195
set(ID_is_weak, is_weak);
196196
}
197197

198-
bool get_is_used() const
199-
{
200-
return get_bool(ID_is_used);
201-
}
202-
203-
void set_is_used(bool is_used)
204-
{
205-
set(ID_is_used, is_used);
206-
}
207-
208198
void to_symbol(
209199
const ansi_c_declaratort &,
210200
symbolt &symbol) const;

src/ansi-c/c_typecheck_base.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Author: Daniel Kroening, [email protected]
1919
#include <util/std_types.h>
2020
#include <util/symbol_table_base.h>
2121

22+
#include <goto-programs/name_mangler.h>
23+
2224
#include "ansi_c_declaration.h"
2325
#include "c_storage_spec.h"
2426
#include "expr2c.h"
@@ -760,7 +762,6 @@ void c_typecheck_baset::typecheck_declaration(
760762
declaration.set_is_register(full_spec.is_register);
761763
declaration.set_is_typedef(full_spec.is_typedef);
762764
declaration.set_is_weak(full_spec.is_weak);
763-
declaration.set_is_used(full_spec.is_used);
764765

765766
symbolt symbol;
766767
declaration.to_symbol(declarator, symbol);
@@ -790,6 +791,20 @@ void c_typecheck_baset::typecheck_declaration(
790791
symbol.is_macro=true;
791792
}
792793

794+
if(full_spec.is_used && symbol.is_file_local)
795+
{
796+
// GCC __attribute__((__used__)) - do not treat those as file-local, but
797+
// make sure the name is unique
798+
symbol.is_file_local = false;
799+
800+
symbolt symbol_for_renaming = symbol;
801+
if(!full_spec.asm_label.empty())
802+
symbol_for_renaming.name = full_spec.asm_label;
803+
full_spec.asm_label = djb_manglert{}(
804+
symbol_for_renaming,
805+
id2string(symbol_for_renaming.location.get_file()));
806+
}
807+
793808
if(full_spec.section.empty())
794809
apply_asm_label(full_spec.asm_label, symbol);
795810
else

0 commit comments

Comments
 (0)