public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Tom Tromey <tom@tromey.com>
Cc: Simon Marchi via Gdb-patches <gdb-patches@sourceware.org>
Subject: Re: The defs.h / common-defs.h / server.h rule
Date: Fri, 23 Feb 2024 15:35:30 -0500	[thread overview]
Message-ID: <88f15917-7989-4e47-a7cf-2cb62faad7ad@polymtl.ca> (raw)
In-Reply-To: <87zfvv2non.fsf@tromey.com>



On 2024-02-20 09:52, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:
> 
> Simon> We currently have a rule [1] saying that all .c/.cc file must include
> Simon> one of defs.h, common-defs.h or server.h, depending on where they are,
> Simon> and that no header should include them.
> 
> Simon> I was wondering about why that rule exists, and if it is still relevant
> Simon> today.
> 
> Every source file has to include config.h first.  In other GNU-ish
> projects this is done in a straightforward way, but gdb (like gcc) used
> a kind of prologue header file instead.

Ok, so through defs.h, every source file in gdb/ for instance ends up
including:

 - gdbsupport/config.h
 - gnulib/config.h
 - gdb/config.h

Plus, in gdbsupport/common-defs.h (or transitively included) are some
definitions that we want all source files to see all the time.  For
instance, the stuff in poison.h, or our override of ATTRIBUTE_PRINTF.

I suppose the other classic way to do this is with an -include flag,
passed to the compiler?  Actually that might be a better way to solve my
problem.  In order to correctly analyze some headers in a standalone
way, it is indeed necessary for clangd to include the various config.h
files first (and perhaps other stuff from defs.h & co).  If we used
-include to include whatever we want all code to see all the time, then
clangd would see the -include flag in the compilation flags and include
those "prologue" headers when analyzing any other header.

I gave it a try just for fun, it seems to work nicely:

  - include config.h files with -include
    https://review.lttng.org/c/binutils-gdb/+/11857/1

  - move more things to gdbsupport.inc.h
    https://review.lttng.org/c/binutils-gdb/+/11858/2

I like this solution because (a) it helps my problem and (b) it makes
defs.h / common-defs.h / server.h no longer special.  All things that we
want all source files to see all the time are included by that -include
flag, so enforced by the build system, and for the rest, source files /
header files include what they need (e.g. if they don't need defs.h,
they don't have to include it).

defs.h & co pull in a ton of stuff that is not needed everywhere, so I
think it would be nice to gradually remove some includes from them, and
include things where they are really needed.  This could help build
times and avoid recompiling files unnecessarily.

I'm not sure my argument for such a change is very strong, but I would
be happy to see a change like that.

> Simon> Could we switch to having headers include what they use?
> 
> Do you mean just adding defs.h to every header, or something else?

That's what I thought at first (either defs.h, or whatever include files
they use).  But it's not possible to include defs.h / common-defs.h /
server.h in the header files that defs.h / common-defs.h / server.h
themselves include.  So for those include files, I would be stuck.

> Simon> Before someome says "it's your tools' fault, your tools are bad": I
> Simon> know.
> 
> In addition to that, I wonder how to enforce this rule.
> For example I'm not likely to start using clangd.
> Or if clangd is changed, will the rule change?

I don't think what I propose is really clangd specific.  I think it's
just to design headers that can be analyzed in a self-contained way, to
the extent possible, which I think is just good software design.

Do people use other editors / IDEs that give accurate compilation errors
in real time like clangd does?  How do they do for header files that
assume they've been included in some context?

Simon

  parent reply	other threads:[~2024-02-23 20:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <2f2f552a-0028-49cc-adbe-ca038c1e9acd@polymtl.ca>
2024-02-20 12:20 ` Andrew Burgess
2024-02-20 14:52 ` Tom Tromey
2024-02-20 15:14   ` Tom Tromey
2024-02-23 20:35   ` Simon Marchi [this message]
2024-02-23 21:06     ` Tom Tromey
2024-02-23 21:55       ` Simon Marchi
2024-02-27 14:54         ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=88f15917-7989-4e47-a7cf-2cb62faad7ad@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).