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

Support for using iwyu under various sets of compiler flags #1020

Open
KierenP opened this issue Mar 28, 2022 · 3 comments
Open

Support for using iwyu under various sets of compiler flags #1020

KierenP opened this issue Mar 28, 2022 · 3 comments

Comments

@KierenP
Copy link

KierenP commented Mar 28, 2022

Hi, I'm using IWYU currently at my workplace and I've run into an issue. The software I am building supports various mutually exclusive targets set by compiler flags. For example, I might build the software with -DTARGET_A or -DTARGET_B.

Currently, I apply the iwyu_tool.py tool with a compilation database.

The issue is, if I am building the software with -DTARGET_A, iwyu suggests the removal of #include's that are only required by -DTARGET_B. Is there any support for combining the results of iwyu when build under multiple compiler flag sets and only removing includes if they are needed for none of the options?

As a mini example, take the following file:

#include <array>
#include <vector>

void fn()
{
#ifdef TARGET_A
    std::vector<int> v;
#endif

#ifdef TARGET_B
    std::array<int, 10> a;
#endif
}

After invoking <path to binary>/include-what-you-use test_file.cpp -DTARGET_A=1 it gives the following output:

test_file.cpp should add these lines:

test_file.cpp should remove these lines:
- #include <array>  // lines 2-2

The full include-list for test_file.cpp:
#include <vector>  // for vector
---

After invoking <path to binary>/include-what-you-use test_file.cpp -DTARGET_B=1 it gives the following output:

test_file.cpp should add these lines:

test_file.cpp should remove these lines:
- #include <vector>  // lines 3-3

The full include-list for test_file.cpp:
#include <array>  // for array
---

Is there any support or tooling to support applying iwyu recommendations across multiple runs with various compiler flag settings?

@i-ky
Copy link
Contributor

i-ky commented Mar 28, 2022

As a mini example, take the following file:

#include <array>
#include <vector>

void fn()
{
#ifdef TARGET_A
    std::vector<int> v;
#endif

#ifdef TARGET_B
    std::array<int, 10> a;
#endif
}

This is my personal opinion, but I would suggest wrapping #include directives with appropriate #ifdef directives. Some people think it's ugly, I don't mind. You probably need to do a similar thing with uses of v and a to avoid "unused variable" warnings. Unless, of course, if their scope is very limited and fits inside one #ifdef with a declaration.

@kimgr
Copy link
Contributor

kimgr commented Mar 28, 2022

How about running IWYU with both -DTARGET_A=1 -DTARGET_B=1 defined?

The suggestion from @i-ky also makes sense to me, and allows you to IWYU one configuration at a time.

@KierenP
Copy link
Author

KierenP commented Mar 29, 2022

Thanks for getting back to me on this.

Without going into specifics I can't build the software with more than one target enabled at a time so @kimgr that idea is sadly not possible.

@i-ky's idea is interesting. Sadly, it would require a major refactor of our codebase to guard target specific includes with preproccessor directives so I don't think that's possible for me either.

Thank you both for your help.

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

3 participants