Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-U incorrectly undefines source-level define #387

Open
firewave opened this issue Nov 22, 2024 · 4 comments
Open

-U incorrectly undefines source-level define #387

firewave opened this issue Nov 22, 2024 · 4 comments
Assignees

Comments

@firewave
Copy link
Collaborator

#define DEF_1

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


void f ( )
{



}
@firewave firewave self-assigned this Nov 22, 2024
@firewave
Copy link
Collaborator Author

$ 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 added a commit to firewave/simplecpp that referenced this issue Nov 22, 2024
firewave added a commit to firewave/simplecpp that referenced this issue Nov 22, 2024
firewave added a commit to firewave/simplecpp that referenced this issue Nov 22, 2024
firewave added a commit to firewave/simplecpp that referenced this issue Nov 22, 2024
@firewave
Copy link
Collaborator Author

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.

@Tal500
Copy link
Contributor

Tal500 commented Dec 1, 2024

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

@firewave
Copy link
Collaborator Author

firewave commented Dec 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants