gdbus-codegen: Add support for pragma inclusion guard

The #pragma once is widely supported preprocessor directive that can
be used instead of include guards.

This adds support for using optionally this directive instead of
include guards.

https://bugzilla.gnome.org/show_bug.cgi?id=791015
This commit is contained in:
Iñigo Martínez 2018-01-03 17:01:30 +01:00
parent e59bce3c74
commit c658d03b76
3 changed files with 32 additions and 8 deletions

View File

@ -35,6 +35,7 @@
<arg><option>--c-generate-autocleanup</option> none|objects|all</arg> <arg><option>--c-generate-autocleanup</option> none|objects|all</arg>
<arg><option>--output-directory</option> <replaceable>OUTDIR</replaceable></arg> <arg><option>--output-directory</option> <replaceable>OUTDIR</replaceable></arg>
<arg><option>--generate-docbook</option> <replaceable>OUTFILES</replaceable></arg> <arg><option>--generate-docbook</option> <replaceable>OUTFILES</replaceable></arg>
<arg><option>--pragma-once</option></arg>
<arg><option>--xml-files</option> <replaceable>FILE</replaceable></arg> <arg><option>--xml-files</option> <replaceable>FILE</replaceable></arg>
<group choice="plain" rep="repeat"> <group choice="plain" rep="repeat">
<arg> <arg>
@ -215,6 +216,17 @@
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--pragma-once</option></term>
<listitem>
<para>
If this option is passed, the
<ulink url="https://en.wikipedia.org/wiki/Pragma_once">&#35;pragma once</ulink>
preprocessor directive is used instead of include guards.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>--c-generate-object-manager</option></term> <term><option>--c-generate-object-manager</option></term>
<listitem> <listitem>

View File

@ -37,7 +37,8 @@ LICENSE_STR = '''/*
class CodeGenerator: class CodeGenerator:
def __init__(self, ifaces, namespace, interface_prefix, generate_objmanager, def __init__(self, ifaces, namespace, interface_prefix, generate_objmanager,
generate_autocleanup, docbook_gen, h, c, header_name): generate_autocleanup, docbook_gen, h, c, header_name,
use_pragma):
self.docbook_gen = docbook_gen self.docbook_gen = docbook_gen
self.generate_objmanager = generate_objmanager self.generate_objmanager = generate_objmanager
self.generate_autocleanup = generate_autocleanup self.generate_autocleanup = generate_autocleanup
@ -59,6 +60,7 @@ class CodeGenerator:
self.ns_lower = '' self.ns_lower = ''
self.interface_prefix = interface_prefix self.interface_prefix = interface_prefix
self.header_guard = header_name.upper().replace('.', '_').replace('-', '_').replace('/', '_').replace(':', '_') self.header_guard = header_name.upper().replace('.', '_').replace('-', '_').replace('/', '_').replace(':', '_')
self.use_pragma = use_pragma
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
@ -224,8 +226,13 @@ class CodeGenerator:
self.h.write(LICENSE_STR.format(config.VERSION)) self.h.write(LICENSE_STR.format(config.VERSION))
self.h.write('\n') self.h.write('\n')
if self.use_pragma:
self.h.write('#pragma once\n')
else:
self.h.write('#ifndef __{!s}__\n'.format(self.header_guard)) self.h.write('#ifndef __{!s}__\n'.format(self.header_guard))
self.h.write('#define __{!s}__\n'.format(self.header_guard)) self.h.write('#define __{!s}__\n'.format(self.header_guard))
self.h.write('\n') self.h.write('\n')
self.h.write('#include <gio/gio.h>\n') self.h.write('#include <gio/gio.h>\n')
self.h.write('\n') self.h.write('\n')
@ -746,10 +753,12 @@ class CodeGenerator:
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
def generate_outro(self): def generate_outro(self):
self.h.write('\n' self.h.write('\n')
'G_END_DECLS\n' self.h.write('G_END_DECLS\n')
'\n'
'#endif /* __%s__ */\n'%(self.header_guard)) if not self.use_pragma:
self.h.write('\n')
self.h.write('#endif /* __{!s}__ */\n'.format(self.header_guard))
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------

View File

@ -163,6 +163,8 @@ def codegen_main():
help='Generate autocleanup support') help='Generate autocleanup support')
arg_parser.add_argument('--generate-docbook', metavar='OUTFILES', arg_parser.add_argument('--generate-docbook', metavar='OUTFILES',
help='Generate Docbook in OUTFILES-org.Project.IFace.xml') help='Generate Docbook in OUTFILES-org.Project.IFace.xml')
arg_parser.add_argument('--pragma-once', action='store_true',
help='Use "pragma once" as the inclusion guard')
arg_parser.add_argument('--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE', arg_parser.add_argument('--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE',
help='Add annotation (may be used several times)') help='Add annotation (may be used several times)')
arg_parser.add_argument('--output-directory', metavar='OUTDIR', default='', arg_parser.add_argument('--output-directory', metavar='OUTDIR', default='',
@ -204,7 +206,8 @@ def codegen_main():
args.c_generate_autocleanup, args.c_generate_autocleanup,
docbook_gen, docbook_gen,
h, c, h, c,
header_name) header_name,
args.pragma_once)
ret = gen.generate() ret = gen.generate()
h.close() h.close()
c.close() c.close()