Skip to content

Commit 1958928

Browse files
committed
Some fixes and cleanups
tcc.h, tccgen.c: Add and use IS_BT_ARRAY tccpp.c: free memory when size is 0 in realloc code tccdbg.c: move common code to seperate function remove_type_info
1 parent edcd228 commit 1958928

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

tcc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,7 @@ struct filespec {
10981098

10991099
/* base type is array (from typedef/typeof) */
11001100
#define VT_BT_ARRAY (6 << VT_STRUCT_SHIFT)
1101+
#define IS_BT_ARRAY(t) ((t & VT_STRUCT_MASK) == VT_BT_ARRAY)
11011102

11021103
/* general: set/get the pseudo-bitfield value for bit-mask M */
11031104
#define BFVAL(M,N) ((unsigned)((M) & ~((M) << 1)) * (N))

tccdbg.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,16 @@ static int stabs_struct_find(TCCState *s1, Sym *t, int *p_id)
17691769
return 1;
17701770
}
17711771

1772+
static int remove_type_info(int type)
1773+
{
1774+
type &= ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1775+
if ((type & VT_BTYPE) != VT_BYTE)
1776+
type &= ~VT_DEFSIGN;
1777+
if (!(type & VT_BITFIELD) && (type & VT_STRUCT_MASK) > VT_ENUM)
1778+
type &= ~VT_STRUCT_MASK;
1779+
return type;
1780+
}
1781+
17721782
static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result)
17731783
{
17741784
int type;
@@ -1778,11 +1788,7 @@ static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result)
17781788
CString str;
17791789

17801790
for (;;) {
1781-
type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1782-
if ((type & VT_BTYPE) != VT_BYTE)
1783-
type &= ~VT_DEFSIGN;
1784-
if (!(type & VT_BITFIELD) && (type & VT_STRUCT_MASK) > VT_ENUM)
1785-
type &= ~VT_STRUCT_MASK;
1791+
type = remove_type_info (t->type.t);
17861792
if (type == VT_PTR || type == (VT_PTR | VT_ARRAY))
17871793
n++, t = t->type.ref;
17881794
else
@@ -1861,9 +1867,7 @@ static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result)
18611867
cstr_printf (result, "%d=", ++debug_next_type);
18621868
t = s;
18631869
for (;;) {
1864-
type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1865-
if ((type & VT_BTYPE) != VT_BYTE)
1866-
type &= ~VT_DEFSIGN;
1870+
type = remove_type_info (t->type.t);
18671871
if (type == VT_PTR)
18681872
cstr_printf (result, "%d=*", ++debug_next_type);
18691873
else if (type == (VT_PTR | VT_ARRAY))
@@ -1901,11 +1905,7 @@ static int tcc_get_dwarf_info(TCCState *s1, Sym *s)
19011905
if (new_file)
19021906
put_new_file(s1);
19031907
for (;;) {
1904-
type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
1905-
if ((type & VT_BTYPE) != VT_BYTE)
1906-
type &= ~VT_DEFSIGN;
1907-
if (!(type & VT_BITFIELD) && (type & VT_STRUCT_MASK) > VT_ENUM)
1908-
type &= ~VT_STRUCT_MASK;
1908+
type = remove_type_info (t->type.t);
19091909
if (type == VT_PTR || type == (VT_PTR | VT_ARRAY))
19101910
t = t->type.ref;
19111911
else
@@ -2052,9 +2052,7 @@ static int tcc_get_dwarf_info(TCCState *s1, Sym *s)
20522052
e = NULL;
20532053
t = s;
20542054
for (;;) {
2055-
type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE | VT_VLA);
2056-
if ((type & VT_BTYPE) != VT_BYTE)
2057-
type &= ~VT_DEFSIGN;
2055+
type = remove_type_info (t->type.t);
20582056
if (type == VT_PTR) {
20592057
i = dwarf_info_section->data_offset;
20602058
if (retval == debug_type)

tccgen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8204,7 +8204,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
82048204
we will overwrite the unknown size by the real one for
82058205
this decl. We need to unshare the ref symbol holding
82068206
that size. */
8207-
if (type->t & VT_BT_ARRAY)
8207+
if (IS_BT_ARRAY(type->t))
82088208
type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
82098209
p.flex_array_ref = type->ref;
82108210

tccpp.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ static void *tcc_realloc_impl(void *p, unsigned size)
159159
PP_ALLOC_INSERT(alloc);
160160
return alloc + 1;
161161
}
162+
tcc_free(alloc);
162163
return NULL;
163164
}
164165
#else
@@ -372,8 +373,10 @@ static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_
372373
PP_ALLOC_INSERT(alloc);
373374
ret = alloc + 1;
374375
}
375-
else
376+
else {
377+
tcc_free(alloc);
376378
ret = NULL;
379+
}
377380
}
378381
#ifdef TAL_INFO
379382
al->nb_missed++;

0 commit comments

Comments
 (0)