simplecpp icon indicating copy to clipboard operation
simplecpp copied to clipboard

`-U` incorrectly undefines source-level define

Open firewave opened this issue 1 year ago • 4 comments

#define DEF_1

void f()
{
#ifdef DEF_1
    int i;
#endif
}
$ ./simplecpp -UDEF_1 test.cpp


void f ( )
{



}

firewave avatar Nov 22 '24 16:11 firewave

$ gcc -UDEF_1 -save-temps input.cpp | cat a-input.ii
# 0 "input.cpp"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "input.cpp"


void f()
{

    int i;

}

firewave avatar Nov 22 '24 16:11 firewave

From https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Preprocessor-Options.html:

-U name

    Cancel any previous definition of name, either built in or provided with a -D option.

So built-in defines needs to be handled differently. Might be outside the scope of this issue though.

firewave avatar Nov 23 '24 18:11 firewave

From https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Preprocessor-Options.html:

-U name

    Cancel any previous definition of name, either built in or provided with a -D option.

So built-in defines needs to be handled differently. Might be outside the scope of this issue though.

Built-in do not need to be handled differently in my opinion. Your initial understanding is correct - i.e. manual #define in the translation unit cancel any -U or -D flag - since code-level pragmas are "stronger" than compiler flags.

What the docs you specify says is that the compiler have some builtin definitions(not defined in the header files!), so their way of being disabled manually is by the -U flag.

But for the user definitions, they can only come either from -D or from the source code. So what they tell you in this case is that -Us cancel previous -Ds - but not the source code #define pragmas.

So in conclusion, the mental model is that the order of definitions, from top to bottom, is:

  1. Builtin definition
  2. User flags by -D and -U - the latter ones in the command line override the previous
  3. Source code #define and #undef- the latter ones in the next lines of the source code override the previous

Tal500 avatar Dec 01 '24 16:12 Tal500

Built-in do not need to be handled differently in my opinion.

We do. simplecpp and Cppcheck both provide different built-in defines which are currently not treated as such. With this change -U no longer has an effect on those.

Since we could (and we actually do) provide more built-in defines externally this needs to be reflected in the DUI configuration. That requires some breaking changes and I would also like to avoid logic and data structures to be duplicated downstream in Cppcheck so I need to model it all through to see what is feasible. I already have it hacked into the downstream code but need to see how to properly pass it through (main issue is to prevent the simplecpp include to spill into the whole code).

Coincidentally I need those changes anyways to address some other define-related issues.

firewave avatar Dec 01 '24 16:12 firewave