[PATCH 2/2] gdb: remove TYPE_NAME macro

Simon Marchi simon.marchi@efficios.com
Sat May 16 16:35:50 GMT 2020


On 2020-05-16 12:00 p.m., Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Simon> Remove `TYPE_NAME`, changing all the call sites to use `type::name`
> Simon> directly.  This is quite a big diff, but this was mostly done using sed
> Simon> and coccinelle.  A few call sites were done by hand.
> 
> Looks good.  I didn't realize coccinelle worked on C++.

Thanks, I'll push them in a moment.

No, coccinelle doesn't work on C++ unfortunately.  When there's a C++
construct in a function, it just skips it it seems.  But our codebase
still looks largely like C in a lot of places that it can still do
a significant number of changes.  But what this means is that it will
become less and less effective for us as time goes by :(.

Also, coccinelle doesn't really allow to specify the output code style,
so it just never puts a space before the parenthesis.  I worked around
that by making it use `a_very_unique_string` as the function name in its
output, so I could easily replace it with the desired value with sed:

-  thetype->a_very_unique_string(...)
+  thetype->set_name (...)

The above can also leave out a space in other function calls in the modified
code, for example turning

  TYPE_NAME (thetype) = a_function ();

into

  thetype->set_name (a_function());

So the space after `a_function` needs to be added back by hand, or some sed
to turn `a_function(` into `a_function (`.

While at it, here's the complete method I used, maybe it will help you or somebody
else when making similar changes:

- Add getter / setters, change the TYPE_NAME macro to use the getter.  The compiler
  will point out any spots that are using the macro to set the field.

- A first sed pass to change the obvious spots to use the setter (not essential, but it's
  quick):

  sed -ri 's|TYPE_NAME \(([a-z0-9_]+)\) = ([a-z0-9_"]+)|\1->set_name (\2)|g' *.c */*.c

- Run this coccinelle rule to change some less obvious spots to use the setter

  ---
  @@
  expression type;
  expression value;
  @@
  - TYPE_NAME(type) = value
  + type->a_very_unique_string(value)
  ---

  $ spatch --sp-file type-set-name.cocci --in-place <FILES>

  Followed by this sed to replace `a_very_unique_string`:

  $ sed -i "s|a_very_unique_string|set_name |g" <FILES>

- Fix the remaining spots to use the setter by hand.

That should be enough for the first patch.  For the second patch:

- Remove the TYPE_NAME macro.

- A first sed pass:

  sed -ri 's|TYPE_NAME \(([a-z0-9_]+)\)|\1->name ()|g'

- Run this coccinelle rule followed by sed, as mentioned above:

  @@
  expression type;
  @@
  - TYPE_NAME(type)
  + type->a_very_unique_string()

Simon


More information about the Gdb-patches mailing list