Discussion on the repeated introduction of ".h" in some files in Chromium

483 views
Skip to first unread message

连瑞龙

unread,
Oct 16, 2022, 9:51:19 PM10/16/22
to c...@chromium.org, pb...@chromium.org
hi,

I am a developer of Chromium, I have a confusion about the IWYU style guide, that is :
"a.h" is included in both foo.h and foo.cc, can it be considered a duplicate inclusion, should the "a.h" included in foo.cc be deleted?

Such an include would be considered a duplicate since I wrote the code, but
inclusion is not repeated, and these inclusions do not need to be deleted.

If so, how exactly do I know which header files should be included in both "foo.h" and "foo.cc",
if it's not clear, we developers tend to include most of the header files in "foo.h" and include them again in "foo.cc",
This is bound to cause a lot of duplication.

I hope I can get a more reasonable explanation.

Our previous discussion can be seen in this MR: https://chromium-review.googlesource.com/c/chromium/src/+/3948303

Hope to get your reply.

Thank you very much.



Gabriel Charette

unread,
Oct 17, 2022, 8:51:59 AM10/17/22
to 连瑞龙, cxx, Peter Boström
The latest rule is indeed to IWYU in foo.cc even if foo.h is already including it.

But the style in Chromium has allowed relying on the include from foo.h for a long time before this recent change so you'll see most files adhere to the old style (and some devs sticking to it out of habit).

(I personally don't care much to enforce either way..)

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/b8d0c1b86776e54a32562f3f117ade5d571fe394.7b17bc48.135c.4d81.bccf.8cbb8795ea1b%40feishu.cn.
Message has been deleted

Peter Kasting

unread,
Oct 17, 2022, 12:52:34 PM10/17/22
to Bruce Dawson, cxx, g...@chromium.org, pb...@chromium.org, 连瑞龙
On Mon, Oct 17, 2022 at 9:10 AM 'Bruce Dawson' via cxx <c...@chromium.org> wrote:
The IWYU algorithm in cpplint (which is run on some but not all Chromium code) allows relying on the include from foo.h. If we want to enforce that we should be including from foo.cc as well then we should consider reconfiguring cpplint.

We haven't aimed to enforce this programmatically, but yes, including from both files as necessary is the letter of the law.

I imagine we won't do much about this until such time as we start trying to run IWYU or IWYS ("Include What You Spell") on Chromium.

PK

连瑞龙

unread,
Mar 9, 2023, 10:20:00 PM3/9/23
to Peter Kasting, Bruce Dawson, cxx, g...@chromium.org, pb...@chromium.org
Hi, Peter,
The IWYU description is not appropriate.

Include What You Use
If a source or header file refers to a symbol defined elsewhere, the file should directly include a header file which properly intends to provide a declaration or definition of that symbol. It should not include header files for any other reason.

Do not rely on transitive inclusions. This allows people to remove no-longer-needed #include statements from their headers without breaking clients. This also applies to related headers - foo.cc should include bar.h if it uses a symbol from it even if foo.h includes bar.h.

This part
Do not rely on transitive inclusions. This allows people to remove no-longer-needed #include statements from their headers without breaking clients. This also applies to related headers - foo.cc should include bar.h if it uses a symbol from it even if foo.h includes bar.h.
seems better to change it to
Do not rely on transitive inclusions. This allows people to remove no-longer-needed #include statements from their headers without breaking clients. This also applies to related headers - foo.cc should include bar.h if it uses a symbol from it even if the .h included by foo.cc includes bar.h.
Try not to include the same .h in foo.cc and foo.h unless it is really necessary.

This will avoid many unnecessary .h being included in foo.h to be included in foo.cc, and the Chromium code will become more elegant.
From: "Peter Kasting"<pkas...@google.com>
Date: Tue, Oct 18, 2022, 00:52
Subject: Re: Discussion on the repeated introduction of ".h" in some files in Chromium
To: "Bruce Dawson"<bruce...@google.com>

Peter Kasting

unread,
Mar 9, 2023, 11:18:46 PM3/9/23
to 连瑞龙, Bruce Dawson, cxx, g...@chromium.org, pb...@chromium.org
No, that just adds confusion. It tells you to include the heat from both places, then it tells you to try not to.

Not including a header from both .h and .cc does not make chromium more elegant and preventing people from doing that is not a goal.

PK

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
Reply all
Reply to author
Forward
0 new messages