Guard system includes for inclusion in a C++20 module purview

imgui.cppm compiles the core sources inside its module purview. System
headers #included there would attach their declarations to the module,
which is ill-formed and clashes with the same declarations in the
global module or in 'import std;'. The module includes them in its
global module fragment instead, so under IMGUI_CXX_MODULE the sources
skip their own includes. Header builds are unchanged.

For the same reason:
- imstb_rectpack.h and imstb_truetype.h skip their extern "C" blocks,
  which would attach their declarations to the global module.
- imgui_draw.cpp defines STBTT_cos/acos/memcpy/memset, so stb_truetype
  doesn't include <math.h> and <string.h> itself.
This commit is contained in:
Sharadh Rajaraman 2026-09-28 16:19:45 +01:00
parent f3dd5566c6
commit da548b83e7
9 changed files with 39 additions and 5 deletions

View File

@ -1257,10 +1257,14 @@ IMPLEMENTING SUPPORT for ImGuiBackendFlags_RendererHasTextures:
#include "imgui_internal.h"
// System includes
#ifndef IMGUI_CXX_MODULE
#include <stdio.h> // vsnprintf, sscanf, printf
#include <stdint.h> // intptr_t
#endif
#ifndef IMGUI_DISABLE_TIME_FUNCTIONS
#ifndef IMGUI_CXX_MODULE
#include <time.h> // time(), localtime_r()/localtime_s()
#endif
#if defined(_WIN32)
static tm* localtime_r(const time_t* timep, tm* result) { return localtime_s(result, timep) == 0 ? result : NULL; }
#endif
@ -1282,11 +1286,13 @@ static tm* localtime_r(const time_t* timep, tm* result) { return localtime_s(res
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef IMGUI_CXX_MODULE
#ifndef __MINGW32__
#include <Windows.h> // _wfopen, OpenClipboard
#else
#include <windows.h>
#endif
#endif
#if defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_APP) && WINAPI_FAMILY == WINAPI_FAMILY_APP) || (defined(WINAPI_FAMILY_GAMES) && WINAPI_FAMILY == WINAPI_FAMILY_GAMES))
// The UWP and GDK Win32 API subsets don't support clipboard nor IME functions
#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS
@ -1296,7 +1302,7 @@ static tm* localtime_r(const time_t* timep, tm* result) { return localtime_s(res
#endif
// [Apple] OS specific includes
#if defined(__APPLE__)
#if defined(__APPLE__) && !defined(IMGUI_CXX_MODULE)
#include <TargetConditionals.h>
#endif
@ -16282,7 +16288,9 @@ static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext*, const char* t
#elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX && defined(IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS)
#ifndef IMGUI_CXX_MODULE
#include <Carbon/Carbon.h> // Use old API to avoid need for separate .mm file
#endif
static PasteboardRef main_clipboard = 0;
// OSX clipboard implementation
@ -16370,7 +16378,9 @@ static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext* ctx, const cha
#ifndef IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS
#ifdef _WIN32
#ifndef IMGUI_CXX_MODULE
#include <shellapi.h> // ShellExecuteA()
#endif
#ifdef _MSC_VER
#pragma comment(lib, "shell32")
#endif
@ -16383,8 +16393,10 @@ static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char* path)
return (INT_PTR)::ShellExecuteW(NULL, L"open", path_wbuf.Data, NULL, NULL, SW_SHOWDEFAULT) > 32;
}
#else
#ifndef IMGUI_CXX_MODULE
#include <sys/wait.h>
#include <unistd.h>
#endif
static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char* path)
{
#if defined(__APPLE__)
@ -16417,7 +16429,9 @@ static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char*) { ret
// Win32 API IME support (for Asian languages, etc.)
#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS)
#ifndef IMGUI_CXX_MODULE
#include <imm.h>
#endif
#ifdef _MSC_VER
#pragma comment(lib, "imm32")
#endif

View File

@ -64,10 +64,12 @@ Index of this file:
//-----------------------------------------------------------------------------
// Includes
#ifndef IMGUI_CXX_MODULE
#include <float.h> // FLT_MIN, FLT_MAX
#include <stdarg.h> // va_list, va_start, va_end
#include <stddef.h> // ptrdiff_t, NULL
#include <string.h> // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp
#endif
// Warnings
#ifdef _MSC_VER

View File

@ -135,6 +135,7 @@ Index of this file:
#ifndef IMGUI_DISABLE
// System includes
#ifndef IMGUI_CXX_MODULE
#include <ctype.h> // toupper
#include <limits.h> // INT_MIN, INT_MAX
#include <math.h> // sqrtf, powf, cosf, sinf, floorf, ceilf
@ -147,6 +148,7 @@ Index of this file:
#ifdef __EMSCRIPTEN__
#include <emscripten/version.h> // __EMSCRIPTEN_MAJOR__ etc.
#endif
#endif
// Visual Studio warnings
#ifdef _MSC_VER

View File

@ -40,8 +40,10 @@ Index of this file:
#include "misc/freetype/imgui_freetype.h"
#endif
#ifndef IMGUI_CXX_MODULE
#include <stdio.h> // vsnprintf, sscanf, printf
#include <stdint.h> // intptr_t
#endif
// Visual Studio warnings
#ifdef _MSC_VER
@ -150,6 +152,10 @@ namespace IMGUI_STB_NAMESPACE
#define STBTT_ifloor(x) ((int)ImFloor(x))
#define STBTT_iceil(x) ((int)ImCeil(x))
#define STBTT_strlen(x) ImStrlen(x)
#define STBTT_cos(x) cos(x) // Same as stb_truetype defaults, but avoid it including <math.h>/<string.h> itself (e.g. in C++20 module purview)
#define STBTT_acos(x) acos(x)
#define STBTT_memcpy memcpy
#define STBTT_memset memset
#define STBTT_STATIC
#define STB_TRUETYPE_IMPLEMENTATION
#else

View File

@ -52,20 +52,26 @@ Index of this file:
#include "imgui.h"
#ifndef IMGUI_CXX_MODULE
#include <stdio.h> // FILE*, sscanf
#include <stdlib.h> // NULL, malloc, free, qsort, atoi, atof
#include <math.h> // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf
#include <limits.h> // INT_MIN, INT_MAX
#endif
// Enable SSE intrinsics if available
#if (defined __SSE__ || defined __x86_64__ || defined _M_X64 || (defined(_M_IX86_FP) && (_M_IX86_FP >= 1))) && !defined(IMGUI_DISABLE_SSE) && !defined(_M_ARM64) && !defined(_M_ARM64EC)
#define IMGUI_ENABLE_SSE
#ifndef IMGUI_CXX_MODULE
#include <immintrin.h>
#endif
#if (defined __AVX__ || defined __SSE4_2__)
#define IMGUI_ENABLE_SSE4_2
#ifndef IMGUI_CXX_MODULE
#include <nmmintrin.h>
#endif
#endif
#endif
// Emscripten has partial SSE 4.2 support where _mm_crc32_u32 is not available. See https://emscripten.org/docs/porting/simd.html#id11 and #8213
#if defined(IMGUI_ENABLE_SSE4_2) && !defined(IMGUI_USE_LEGACY_CRC32_ADLER) && !defined(__EMSCRIPTEN__)
#define IMGUI_ENABLE_SSE4_2_CRC

View File

@ -202,7 +202,9 @@ Index of this file:
#include "imgui_internal.h"
// System includes
#ifndef IMGUI_CXX_MODULE
#include <stdint.h> // intptr_t
#endif
// Visual Studio warnings
#ifdef _MSC_VER

View File

@ -45,7 +45,9 @@ Index of this file:
#include "imgui_internal.h"
// System includes
#ifndef IMGUI_CXX_MODULE
#include <stdint.h> // intptr_t
#endif
//-------------------------------------------------------------------------
// Warnings

View File

@ -78,7 +78,7 @@
#define STBRP_DEF extern
#endif
#ifdef __cplusplus
#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI] extern "C" would attach declarations to the global module
extern "C" {
#endif
@ -195,7 +195,7 @@ struct stbrp_context
stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
};
#ifdef __cplusplus
#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI]
}
#endif

View File

@ -510,7 +510,7 @@ int main(int arg, char **argv)
#define STBTT_DEF extern
#endif
#ifdef __cplusplus
#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI] extern "C" would attach declarations to the global module
extern "C" {
#endif
@ -1095,7 +1095,7 @@ enum { // languageID for STBTT_PLATFORM_ID_MAC
STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19
};
#ifdef __cplusplus
#if defined(__cplusplus) && !defined(IMGUI_CXX_MODULE) // [DEAR IMGUI]
}
#endif