Issue 971 in include-what-you-use: Export a forward declaration (e.g., for an opaque type)?

0 views
Skip to first unread message

notifi...@include-what-you-use.org

unread,
Nov 10, 2021, 4:57:43 AM11/10/21
to include-wh...@googlegroups.com
New issue 971 by osandov: Export a forward declaration (e.g., for an opaque type)?
https://github.com/include-what-you-use/include-what-you-use/issues/971

Let's say the public header `public.h` for my library defines some interfaces, including an opaque pointer type:

```c
struct opaque;

struct opaque *create_opaque(void);

enum my_enum {
FOO,
BAR,
BAZ,
};
```

And I have an internal header that uses those interfaces:

```c
#include "public.h"

void use_opaque(struct opaque *);
void use_enum(enum my_enum);
```

Ideally, IWYU would realize that `public.h` "exports" a declaration of `struct opaque`, so "private.h" doesn't need a forward declaration. However, this is not the case:

```console
$ include-what-you-use private.h

private.h should add these lines:
struct opaque;

private.h should remove these lines:

The full include-list for private.h:
#include "public.h" // for my_enum
struct opaque;
---
```

Is there any way to avoid this? The declaration of `struct opaque` really is a part of the interface provided by `public.h`, so requiring forward declarations seems silly.


notifi...@include-what-you-use.org

unread,
Nov 10, 2021, 3:51:28 PM11/10/21
to include-wh...@googlegroups.com
Comment #0 on issue 971 by kimgr: Export a forward declaration (e.g., for an opaque type)?
https://github.com/include-what-you-use/include-what-you-use/issues/971

Yes, it does seem annoying, but unfortunately, I don't think there's a knob to work around it. You could try with `struct opaque; // IWYU pragma: export` in the public header, but I suspect IWYU is more trigger-happy with suggesting fwd-decls.


notifi...@include-what-you-use.org

unread,
Nov 10, 2021, 4:41:38 PM11/10/21
to include-wh...@googlegroups.com
Comment #1 on issue 971 by osandov: Export a forward declaration (e.g., for an opaque type)?
https://github.com/include-what-you-use/include-what-you-use/issues/971

The pragma didn't help. I could add a mapping that `public.h` provides `struct opaque`, but then that would miss full uses.

Would it be possible to make the pragma work for this?


Reply all
Reply to author
Forward
0 new messages