[Qemu-devel] [PATCH RFC 18/21] qapi/common: Fix guardname() for funny filenames

Markus Armbruster posted 21 patches 8 years ago
There is a newer version of this series
[Qemu-devel] [PATCH RFC 18/21] qapi/common: Fix guardname() for funny filenames
Posted by Markus Armbruster 8 years ago
guardname() fails to return a valid C identifier for arguments
containing anything but [A-Za-z0-9_.-'].  Fix that.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi/common.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index 7ffffc78d9..7d497b5b17 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -1860,7 +1860,7 @@ def mcgen(code, **kwds):
 
 
 def guardname(filename):
-    return c_name(filename, protect=False).upper()
+    return re.sub(r'[^A-Za-z0-9_]', '_', filename).upper()
 
 
 def guardstart(name):
-- 
2.13.6


Re: [Qemu-devel] [PATCH RFC 18/21] qapi/common: Fix guardname() for funny filenames
Posted by Marc-Andre Lureau 8 years ago
On Fri, Feb 2, 2018 at 2:03 PM, Markus Armbruster <armbru@redhat.com> wrote:
> guardname() fails to return a valid C identifier for arguments
> containing anything but [A-Za-z0-9_.-'].  Fix that.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


> ---
>  scripts/qapi/common.py | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
> index 7ffffc78d9..7d497b5b17 100644
> --- a/scripts/qapi/common.py
> +++ b/scripts/qapi/common.py
> @@ -1860,7 +1860,7 @@ def mcgen(code, **kwds):
>
>
>  def guardname(filename):
> -    return c_name(filename, protect=False).upper()
> +    return re.sub(r'[^A-Za-z0-9_]', '_', filename).upper()
>
>
>  def guardstart(name):
> --
> 2.13.6
>

Re: [Qemu-devel] [PATCH RFC 18/21] qapi/common: Fix guardname() for funny filenames
Posted by Eric Blake 8 years ago
On 02/02/2018 07:03 AM, Markus Armbruster wrote:
> guardname() fails to return a valid C identifier for arguments
> containing anything but [A-Za-z0-9_.-'].  Fix that.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   scripts/qapi/common.py | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
> index 7ffffc78d9..7d497b5b17 100644
> --- a/scripts/qapi/common.py
> +++ b/scripts/qapi/common.py
> @@ -1860,7 +1860,7 @@ def mcgen(code, **kwds):
>   
>   
>   def guardname(filename):
> -    return c_name(filename, protect=False).upper()
> +    return re.sub(r'[^A-Za-z0-9_]', '_', filename).upper()

For some choices of filename, the old code prefixes a q_ (via c_name) 
which gets turned into Q_ in the final guard name.  The new code does 
not.  Then again, all of the names protected by c_name() all contain 
lower case, while guard names are all upper case; so we aren't 
protecting ourselves from defining a reserved word; the main use for 
c_name() is to protect ourselves where we are not changing case (for 
example, _BOOL is no better than Q__BOOL as a guard name for a file 
named _Bool).

Might be worth mentioning this design consideration in the commit 
message, but the change itself is reasonable.

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org