How to use pragma push_macro/pop_macro with include-guarded PCH?


I’d like to use #pragma push_macro in an include-guarded precompiled header and then use #pragma pop_macro in a source file to restore the macro. Example:


#define TestMacro
#pragma push_macro("TestMacro")
#undef TestMacro


#include "TestHeader.h"

#pragma pop_macro("TestMacro")

#ifdef TestMacro
    #warning TestMacro is defined.
    #warning TestMacro is not defined.

int main(void) {}

Compile (as described in Clang Compiler User’s Manual — Clang 16.0.0git documentation):

clang -x c-header TestHeader.h -o TestHeader.h.pch
clang -include-pch TestHeader.h.pch TestSource.c

I expected the output to be

TestSource.c:6:6: warning: TestMacro is defined. [-W#warnings]

However, the actual output is

TestSource.c:3:9: warning: pragma pop_macro could not pop 'TestMacro', no matching push_macro [-Wignored-pragmas]
TestSource.c:8:6: warning: TestMacro is not defined. [-W#warnings]

Replacing the include guard with #pragma once results in the same output. Using gcc (gcc TestHeader.h && gcc TestSource.c), removing the include guard or not using PCH shows the expected output.

Is there a way to make Clang keep the macros pushed by an include-guarded precompiled header so they can be popped by a source file?