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

Possible to apply two opposite definitions at the same time for one single C source file? #1834

Open
JadenLiu0103 opened this issue Nov 24, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@JadenLiu0103
Copy link

JadenLiu0103 commented Nov 24, 2023

I have an Embedded C project to be run on a multiple-core hardware platform.
In a single source file (main.c), there are codes for both CPU0 and CPU1. The code of different CPUs is separated by different definitions.
image

When making the project, code of both CPUs is compiled. Use Bear to generate the compile_commands.json, there are two build commands with different -DCPU_ID of the single main.c. One is for CPU0, and the other is CPU1.
image

It seems Clangd only use the first -DCPU_ID listed in the compile_commands.json, so only the code of CPU0 is available when reading the source file. If I want to read the code of CPU1, I need to put the build commands of CPU1 at the first place.

Is it any methods to make the codes of both CPUs available at the same time? I understand this requirement maybe not intuitive or logical because the CPU_ID mustn't be two different values at the same time, but it really helps a lot to read the code if it is available. Thanks.

@JadenLiu0103 JadenLiu0103 added the enhancement New feature or request label Nov 24, 2023
@HighCommander4
Copy link

This would be very challenging to implement.

If we tried to make the compiler process both branches of an #if during a single compilation, it would run into errors in many cases. For example, both branches could define a function with the same name, in which case processing both would lead to a "multiple definitions" error. Or the branch could be placed in the middle of a declaration such that the contents of the two branches in sequence isn't even syntactically valid.

I think the only way to make this work in general would be to have the compiler build two different ASTs (one for each value of CPU_ID), and then teach every editor feature to base its results on multiple ASTs. (For example, for semantic tokens, the tokens from the two ASTs could be merged into a single set of tokens. For go-to-definition, it would be performed on each AST, and results from both would be offered). It's possible in theory, but it would take an enormous amount of work.

@JadenLiu0103
Copy link
Author

Thanks for the quick reply. I agree it is a challenge to implement after your explanation.

I think I can change the order of the source file in the compile_commands.json as a workaround.

@i-ky
Copy link

i-ky commented Nov 27, 2023

Sounds related to #681.

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

No branches or pull requests

3 participants