Skip to content

Commit 79e2f49

Browse files
committed
Fixed some issues with #defines in the header (#114, #115)
1 parent e854f70 commit 79e2f49

26 files changed

Lines changed: 132 additions & 5 deletions

dear_bindings.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,20 +632,36 @@ def convert_header(
632632
# Remove these #defines as they don't make sense in C
633633
mod_remove_defines.apply(dom_root, ["IM_PLACEMENT_NEW(_PTR)", "IM_NEW(_TYPE)"])
634634
# Rewrite these defines to reference the new function names
635-
# This could be done more generically but there are only three at present and there's a limit to how generic
635+
# This could be done more generically but there are only four at present and there's a limit to how generic
636636
# we can get (as there's all sorts of #define trickery that could break the general case), so for now we'll
637637
# just do it the easy way
638638
mod_rewrite_defines.apply(dom_root, [
639639
"IM_ALLOC(_SIZE)",
640640
"IM_FREE(_PTR)",
641-
"IMGUI_CHECKVERSION()"
641+
"IMGUI_CHECKVERSION()",
642+
"IMGUI_DEBUG_LOG(...)"
642643
], {"ImGui::": "ImGui_"})
644+
# Also rewrite IMGUI_DEBUG_LOG_FONT to use ImGui_GetCurrentContext() instead of GImGui as the latter isn't
645+
# accessible from C
646+
mod_rewrite_defines.apply(dom_root, [
647+
"IMGUI_DEBUG_LOG_FONT(...)"
648+
], {"GImGui": "ImGui_GetCurrentContext()"})
643649
# Rename these to stop them generating compile warnings as they clash with those in imgui.h
644650
mod_rename_defines.apply(dom_root, {
645651
'IM_ALLOC(_SIZE)': 'CIM_ALLOC(_SIZE)',
646652
'IM_FREE(_PTR)': 'CIM_FREE(_PTR)',
647653
'IMGUI_CHECKVERSION()': 'CIMGUI_CHECKVERSION()'
648654
})
655+
# Put these behind a guard to avoid spurious warnings
656+
mod_add_define_guards.apply(dom_root, [
657+
"IMGUI_DEBUG_LOG(...)",
658+
"IMGUI_DEBUG_LOG_FONT(...)",
659+
# We guard these specifically because although they are in theory the same, if the user has a slightly
660+
# mismatched version of the headers (i.e. C++ headers from one version but generated headers from another),
661+
# they will generate a warning.
662+
"IMGUI_VERSION_NUM",
663+
"IMGUI_VERSION"
664+
], "DEAR_BINDINGS_INTERNAL_GLUE_CODE")
649665

650666
mod_forward_declare_structs.apply(dom_root)
651667
mod_wrap_with_extern_c.apply(main_src_root) # main_src_root here to avoid wrapping the config headers

docs/Changelog.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
--- v0.18 WIP
1+
--- v0.18
22

33
* Added support for SDLGPU3 backend + SDL3+SDLGPU3 example.
44
* Added support for NULL backend. (#110)
55
* Added all generated backends to GitHub release packages. (#109)
6+
* Fixed IMGUI_DEBUG_LOG() macros using only-accessible-from-C++ symbols. (#115)
7+
* Added define guards to prevent redefinition warnings in some situations. (#114)
68

79
--- v0.17
810

src/modifiers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@
6565
from . import mod_mark_structs_as_single_line_definition
6666
from . import mod_add_field_comment
6767
from . import mod_add_defines
68+
from . import mod_add_define_guards
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from src import code_dom
2+
from src import utils
3+
4+
5+
# This modifier adds the specified guard to a list of #defines.
6+
# This is primarily intended as a way to avoid duplicate definitions when both the C and C++ headers are included
7+
# in the same file.
8+
def apply(dom_root, define_names, guard_name):
9+
for define in dom_root.list_all_children_of_type(code_dom.DOMDefine):
10+
if define.name in define_names:
11+
# Add #ifndef check
12+
ifguard = code_dom.DOMPreprocessorIf()
13+
ifguard.is_ifdef = True
14+
ifguard.is_negated = True
15+
ifguard.expression_tokens = [utils.create_token(guard_name)]
16+
17+
# Insert at the same point as the original define
18+
define.parent.insert_after_child(define, [ifguard])
19+
20+
# Move the define into the guard
21+
ifguard.add_child(define)

src/templates/imgui-header-template.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
#include <stdio.h>
55

66
// Wrap this in a namespace to keep it separate from the C++ API
7+
8+
// This define prevents #defines in the header getting defined again (as they are already in the normal header above),
9+
// and thus generating redefinition warnings
10+
#define DEAR_BINDINGS_INTERNAL_GLUE_CODE
711
namespace cimgui
812
{
913
#include "%OUTPUT_HEADER_NAME%"
1014
}
15+
#undef DEAR_BINDINGS_INTERNAL_GLUE_CODE
1116

1217
// Manual helpers
1318
// These implement functionality that isn't in the original C++ API, but is useful to callers from other languages

src/templates/imgui_impl_allegro5-header-template.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
#include <stdio.h>
55

66
// Wrap this in a namespace to keep it separate from the C++ API
7+
// This define prevents #defines in the header getting defined again (as they are already in the normal header above),
8+
// and thus generating redefinition warnings
9+
#define DEAR_BINDINGS_INTERNAL_GLUE_CODE
710
namespace cimgui
811
{
912
#include "%OUTPUT_HEADER_NAME%"
1013
}
11-
14+
#undef DEAR_BINDINGS_INTERNAL_GLUE_CODE

src/templates/imgui_impl_android-header-template.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
#include <stdio.h>
55

66
// Wrap this in a namespace to keep it separate from the C++ API
7+
// This define prevents #defines in the header getting defined again (as they are already in the normal header above),
8+
// and thus generating redefinition warnings
9+
#define DEAR_BINDINGS_INTERNAL_GLUE_CODE
710
namespace cimgui
811
{
912
#include "%OUTPUT_HEADER_NAME%"
1013
}
11-
14+
#undef DEAR_BINDINGS_INTERNAL_GLUE_CODE

src/templates/imgui_impl_dx10-header-template.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include <stdio.h>
55

66
// Wrap this in a namespace to keep it separate from the C++ API
7+
// This define prevents #defines in the header getting defined again (as they are already in the normal header above),
8+
// and thus generating redefinition warnings
9+
#define DEAR_BINDINGS_INTERNAL_GLUE_CODE
710
namespace cimgui
811
{
912
#include "%OUTPUT_HEADER_NAME%"
1013
}
14+
#undef DEAR_BINDINGS_INTERNAL_GLUE_CODE
1115

src/templates/imgui_impl_dx11-header-template.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include <stdio.h>
55

66
// Wrap this in a namespace to keep it separate from the C++ API
7+
// This define prevents #defines in the header getting defined again (as they are already in the normal header above),
8+
// and thus generating redefinition warnings
9+
#define DEAR_BINDINGS_INTERNAL_GLUE_CODE
710
namespace cimgui
811
{
912
#include "%OUTPUT_HEADER_NAME%"
1013
}
14+
#undef DEAR_BINDINGS_INTERNAL_GLUE_CODE
1115

src/templates/imgui_impl_dx12-header-template.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
#include <d3d12.h>
66

77
// Wrap this in a namespace to keep it separate from the C++ API
8+
// This define prevents #defines in the header getting defined again (as they are already in the normal header above),
9+
// and thus generating redefinition warnings
10+
#define DEAR_BINDINGS_INTERNAL_GLUE_CODE
811
namespace cimgui
912
{
1013
#include "%OUTPUT_HEADER_NAME%"
1114
}
15+
#undef DEAR_BINDINGS_INTERNAL_GLUE_CODE
1216

0 commit comments

Comments
 (0)