ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

PHNT #if directives causes "token is not a valid binary operator in a preprocessor subexpression"

Open dongle-the-gadget opened this issue 1 year ago • 4 comments

PHNT includes some #if directives that look like below, and that causes ClangSharpPInvokeGenerator to output "token is not a valid binary operator in a preprocessor subexpression":

#define PHNT_MODE_KERNEL 0

#ifndef PHNT_MODE
#define PHNT_MODE 1
#endif

#if (PHNT_MODE != PHNT_MODE_KERNEL)
// Some real code here
#endif

dongle-the-gadget avatar Aug 25 '24 06:08 dongle-the-gadget

Can you provide some more details about the options you're passing into ClangSharp, the version you're using, and possibly a minimal repro?

ClangSharp simply passes the files down to Clang. So if Clang is reporting an error then it's likely caused by some other mismatch.

tannergooding avatar Aug 25 '24 18:08 tannergooding

I'm using ClangSharpPInvokeGenerator version 18.1.0.1.

RSP file:

--file
test.h
--output
Test
--namespace
Test
--define-macro
PHNT_MODE PHNT_MODE_KERNEL

As for the test.h file, the code in the original comment should work as-is to reproduce the error.

dongle-the-gadget avatar Aug 26 '24 11:08 dongle-the-gadget

You've defined PHNT_MODE with no value, so it compiles down to #if ( != 0)

You need to either not do --define-macro because it's automatically included in the file, or explicitly give it a value such as PHNT_MODE=1

tannergooding avatar Aug 26 '24 14:08 tannergooding

You can reproduce this in isolation on godbolt using something like:

#define PHNT_MODE
#define PHNT_MODE_KERNEL

#define PHNT_MODE_KERNEL 0

#ifndef PHNT_MODE
#define PHNT_MODE 1
#endif

#if (PHNT_MODE != PHNT_MODE_KERNEL)
// Some real code here
#endif

tannergooding avatar Aug 26 '24 14:08 tannergooding