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

forward declaration not suggested for type returned from function #1578

Open
firewave opened this issue Jul 27, 2024 · 5 comments
Open

forward declaration not suggested for type returned from function #1578

firewave opened this issue Jul 27, 2024 · 5 comments

Comments

@firewave
Copy link
Contributor

test.h

#include "s.h"

S f();

s.h

class S {};

Analyzing a file including test.h will not suggest a forward declaration which would be sufficient.

But if you are already using a forward declaration instead of the include it will report that everything is correct.

If the type is used as a function parameter it will suggest the forward declaration.

I am quite surprised but apparently it is legal: https://stackoverflow.com/questions/47817012/incomplete-types-as-function-parameters-and-return-values.

I am using include-what-you-use 0.22 on Manjaro Linux.

@bolshakov-a
Copy link
Contributor

This is the default IWYU policy. Please read the docs: https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/WhatIsAUse.md

I think it would be nice to add command-line flags to switch these policies off.

@firewave
Copy link
Contributor Author

This is the default IWYU policy.

Interesting. This is basically the "mission statement" I was referring to in #1568 (comment).

I think it would be nice to add command-line flags to switch these policies off.

I second that.

I doubt that each author will properly express their intend. And even if that is covered in code reviews there might still be cases slipping through.

And with the function parameter case suggesting the forward declaration, depending on when you started to use include-what-you-use and the evolution of the code, you might end with either of the cases.

I think it would be nice to be able to enforce a certain policy for consistency across the code.

@firewave
Copy link
Contributor Author

I think it would be nice to be able to enforce a certain policy for consistency across the code.

I think there should be three different policies: permissive (current / default), incomplete and full.

@firewave
Copy link
Contributor Author

firewave commented Aug 12, 2024

I think there should be three different policies: permissive (current / default), incomplete and full.

It could be more complex than that though as currently there are two different policies in place. They allow for either full or incomplete types if they are already being used and suggests the full type if a change is necessary.

So there would be more cases:

  • incomplete - enforce usage of incomplete types
  • full - enforce usage of full types
  • suggest-incomplete - do not enforce and use incomplete types for suggestions (if necessary)
  • suggest-full - do not enforce and use full types for suggestions (if necessary / current default)

@kimgr
Copy link
Contributor

kimgr commented Oct 13, 2024

If you're interested in short term local solution, simply replace the include with a forward decl, and iwyu should interpret that according to its policy.

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