Skip to content

Commit 4aac43f

Browse files
committed
Do not allocate macro_scope on the heap
I noticed that there's no particular reason to allocate the macro_scope objects on the heap. They can be passed around by value just as easily. Approved-By: Simon Marchi <[email protected]>
1 parent 408984e commit 4aac43f

File tree

6 files changed

+56
-59
lines changed

6 files changed

+56
-59
lines changed

gdb/c-exp.y

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,18 +3392,18 @@ c_parse (struct parser_state *par_state)
33923392
c_parse_state cstate;
33933393
scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
33943394

3395-
gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3395+
macro_scope macro_scope;
33963396

33973397
if (par_state->expression_context_block)
33983398
macro_scope
33993399
= sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
34003400
else
34013401
macro_scope = default_macro_scope ();
3402-
if (! macro_scope)
3402+
if (!macro_scope.is_valid ())
34033403
macro_scope = user_macro_scope ();
34043404

34053405
scoped_restore restore_macro_scope
3406-
= make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3406+
= make_scoped_restore (&expression_macro_scope, &macro_scope);
34073407

34083408
scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
34093409
par_state->debug);

gdb/compile/compile-c-support.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,18 @@ static void
185185
write_macro_definitions (const struct block *block, CORE_ADDR pc,
186186
struct ui_file *file)
187187
{
188-
gdb::unique_xmalloc_ptr<struct macro_scope> scope;
188+
macro_scope scope;
189189

190190
if (block != NULL)
191191
scope = sal_macro_scope (find_pc_line (pc, 0));
192192
else
193193
scope = default_macro_scope ();
194-
if (scope == NULL)
194+
if (!scope.is_valid ())
195195
scope = user_macro_scope ();
196196

197-
if (scope != NULL && scope->file != NULL && scope->file->table != NULL)
197+
if (scope.is_valid () && scope.file->table != nullptr)
198198
{
199-
macro_for_each_in_scope (scope->file, scope->line,
199+
macro_for_each_in_scope (scope.file, scope.line,
200200
[&] (const char *name,
201201
const macro_definition *macro,
202202
macro_source_file *source,

gdb/macrocmd.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ macro_expand_command (const char *exp, int from_tty)
5757
" expression you\n"
5858
"want to expand."));
5959

60-
gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
60+
macro_scope ms = default_macro_scope ();
6161

62-
if (ms != nullptr)
62+
if (ms.is_valid ())
6363
{
64-
gdb::unique_xmalloc_ptr<char> expanded = macro_expand (exp, *ms);
64+
gdb::unique_xmalloc_ptr<char> expanded = macro_expand (exp, ms);
6565

6666
gdb_puts ("expands to: ");
6767
gdb_puts (expanded.get ());
@@ -85,11 +85,11 @@ macro_expand_once_command (const char *exp, int from_tty)
8585
" the expression\n"
8686
"you want to expand."));
8787

88-
gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
88+
macro_scope ms = default_macro_scope ();
8989

90-
if (ms != nullptr)
90+
if (ms.is_valid ())
9191
{
92-
gdb::unique_xmalloc_ptr<char> expanded = macro_expand_once (exp, *ms);
92+
gdb::unique_xmalloc_ptr<char> expanded = macro_expand_once (exp, ms);
9393

9494
gdb_puts ("expands to: ");
9595
gdb_puts (expanded.get ());
@@ -169,7 +169,6 @@ print_macro_definition (const char *name,
169169
static void
170170
info_macro_command (const char *args, int from_tty)
171171
{
172-
gdb::unique_xmalloc_ptr<struct macro_scope> ms;
173172
const char *name;
174173
int show_all_macros_named = 0;
175174
const char *arg_start = args;
@@ -201,15 +200,15 @@ info_macro_command (const char *args, int from_tty)
201200
" of the macro\n"
202201
"whose definition you want to see."));
203202

204-
ms = default_macro_scope ();
203+
macro_scope ms = default_macro_scope ();
205204

206-
if (! ms)
205+
if (!ms.is_valid ())
207206
macro_inform_no_debuginfo ();
208207
else if (show_all_macros_named)
209-
macro_for_each (ms->file->table, [&] (const char *macro_name,
210-
const macro_definition *macro,
211-
macro_source_file *source,
212-
int line)
208+
macro_for_each (ms.file->table, [&] (const char *macro_name,
209+
const macro_definition *macro,
210+
macro_source_file *source,
211+
int line)
213212
{
214213
if (strcmp (name, macro_name) == 0)
215214
print_macro_definition (name, macro, source, line);
@@ -218,12 +217,12 @@ info_macro_command (const char *args, int from_tty)
218217
{
219218
struct macro_definition *d;
220219

221-
d = macro_lookup_definition (ms->file, ms->line, name);
220+
d = macro_lookup_definition (ms.file, ms.line, name);
222221
if (d)
223222
{
224223
int line;
225224
struct macro_source_file *file
226-
= macro_definition_location (ms->file, ms->line, name, &line);
225+
= macro_definition_location (ms.file, ms.line, name, &line);
227226

228227
print_macro_definition (name, d, file, line);
229228
}
@@ -232,7 +231,7 @@ info_macro_command (const char *args, int from_tty)
232231
gdb_printf ("The symbol `%s' has no definition as a C/C++"
233232
" preprocessor macro\n"
234233
"at ", name);
235-
show_pp_source_pos (gdb_stdout, ms->file, ms->line);
234+
show_pp_source_pos (gdb_stdout, ms.file, ms.line);
236235
}
237236
}
238237
}
@@ -241,7 +240,7 @@ info_macro_command (const char *args, int from_tty)
241240
static void
242241
info_macros_command (const char *args, int from_tty)
243242
{
244-
gdb::unique_xmalloc_ptr<struct macro_scope> ms;
243+
macro_scope ms;
245244

246245
if (args == NULL)
247246
ms = default_macro_scope ();
@@ -254,10 +253,10 @@ info_macros_command (const char *args, int from_tty)
254253
ms = sal_macro_scope (sals[0]);
255254
}
256255

257-
if (! ms || ! ms->file || ! ms->file->table)
256+
if (!ms.is_valid () || ms.file->table == nullptr)
258257
macro_inform_no_debuginfo ();
259258
else
260-
macro_for_each_in_scope (ms->file, ms->line, print_macro_definition);
259+
macro_for_each_in_scope (ms.file, ms.line, print_macro_definition);
261260
}
262261

263262

gdb/macroscope.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,29 @@
3434
struct macro_table *macro_user_macros;
3535

3636

37-
gdb::unique_xmalloc_ptr<struct macro_scope>
37+
macro_scope
3838
sal_macro_scope (struct symtab_and_line sal)
3939
{
40+
macro_scope result;
4041
struct macro_source_file *main_file, *inclusion;
4142
struct compunit_symtab *cust;
4243

4344
if (sal.symtab == NULL)
44-
return NULL;
45+
return result;
4546

4647
cust = sal.symtab->compunit ();
4748
if (cust->macro_table () == NULL)
48-
return NULL;
49+
return result;
4950

50-
gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
51+
macro_scope ms;
5152

5253
main_file = macro_main (cust->macro_table ());
5354
inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id);
5455

5556
if (inclusion)
5657
{
57-
ms->file = inclusion;
58-
ms->line = sal.line;
58+
ms.file = inclusion;
59+
ms.line = sal.line;
5960
}
6061
else
6162
{
@@ -73,8 +74,8 @@ sal_macro_scope (struct symtab_and_line sal)
7374
7475
For the time being, though, we'll just treat these as
7576
occurring at the end of the main source file. */
76-
ms->file = main_file;
77-
ms->line = -1;
77+
ms.file = main_file;
78+
ms.line = -1;
7879

7980
complaint (_("symtab found for `%s', but that file\n"
8081
"is not covered in the compilation unit's macro information"),
@@ -85,20 +86,16 @@ sal_macro_scope (struct symtab_and_line sal)
8586
}
8687

8788

88-
gdb::unique_xmalloc_ptr<struct macro_scope>
89-
user_macro_scope (void)
89+
macro_scope
90+
user_macro_scope ()
9091
{
91-
gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
92-
ms->file = macro_main (macro_user_macros);
93-
ms->line = -1;
94-
return ms;
92+
return { macro_main (macro_user_macros), -1 };
9593
}
9694

97-
gdb::unique_xmalloc_ptr<struct macro_scope>
98-
default_macro_scope (void)
95+
macro_scope
96+
default_macro_scope ()
9997
{
10098
struct symtab_and_line sal;
101-
gdb::unique_xmalloc_ptr<struct macro_scope> ms;
10299
frame_info_ptr frame;
103100
CORE_ADDR pc;
104101

@@ -128,8 +125,8 @@ default_macro_scope (void)
128125
sal.line = cursal.line;
129126
}
130127

131-
ms = sal_macro_scope (sal);
132-
if (! ms)
128+
macro_scope ms = sal_macro_scope (sal);
129+
if (!ms.is_valid ())
133130
ms = user_macro_scope ();
134131

135132
return ms;

gdb/macroscope.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,25 @@ extern struct macro_table *macro_user_macros;
3131
in scope: a source file (either a main source file or an
3232
#inclusion), and a line number in that file. */
3333
struct macro_scope {
34-
struct macro_source_file *file;
35-
int line;
34+
struct macro_source_file *file = nullptr;
35+
int line = 0;
36+
37+
/* Return true if this scope is valid. */
38+
bool is_valid () const
39+
{
40+
return file != nullptr;
41+
}
3642
};
3743

3844

3945
/* Return a `struct macro_scope' object corresponding to the symtab
4046
and line given in SAL. If we have no macro information for that
41-
location, or if SAL's pc is zero, return zero. */
42-
gdb::unique_xmalloc_ptr<struct macro_scope> sal_macro_scope
43-
(struct symtab_and_line sal);
44-
47+
location, or if SAL's pc is zero, return an invalid scope. */
48+
macro_scope sal_macro_scope (struct symtab_and_line sal);
4549

4650
/* Return a `struct macro_scope' object representing just the
4751
user-defined macros. */
48-
gdb::unique_xmalloc_ptr<struct macro_scope> user_macro_scope (void);
52+
macro_scope user_macro_scope ();
4953

5054
/* Return a `struct macro_scope' object describing the scope the `macro
5155
expand' and `macro expand-once' commands should use for looking up
@@ -54,7 +58,7 @@ gdb::unique_xmalloc_ptr<struct macro_scope> user_macro_scope (void);
5458
5559
If we have no macro information for the current location, return
5660
the user macro scope. */
57-
gdb::unique_xmalloc_ptr<struct macro_scope> default_macro_scope (void);
61+
macro_scope default_macro_scope ();
5862

5963
/* Look up the definition of the macro named NAME in scope at the source
6064
location given by MS. */

gdb/symtab.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6198,8 +6198,6 @@ default_collect_symbol_completion_matches_break_on
61986198
if (current_language->macro_expansion () == macro_expansion_c
61996199
&& code == TYPE_CODE_UNDEF)
62006200
{
6201-
gdb::unique_xmalloc_ptr<struct macro_scope> scope;
6202-
62036201
/* This adds a macro's name to the current completion list. */
62046202
auto add_macro_name = [&] (const char *macro_name,
62056203
const macro_definition *,
@@ -6217,10 +6215,9 @@ default_collect_symbol_completion_matches_break_on
62176215
resulting expression will be evaluated at "file:line" -- but
62186216
at there does not seem to be a way to detect this at
62196217
completion time. */
6220-
scope = default_macro_scope ();
6221-
if (scope)
6222-
macro_for_each_in_scope (scope->file, scope->line,
6223-
add_macro_name);
6218+
macro_scope scope = default_macro_scope ();
6219+
if (scope.is_valid ())
6220+
macro_for_each_in_scope (scope.file, scope.line, add_macro_name);
62246221

62256222
/* User-defined macros are always visible. */
62266223
macro_for_each (macro_user_macros, add_macro_name);

0 commit comments

Comments
 (0)