git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCHv3 1/6] Add missing includes and forward declares

From
Elijah Newren <newren@gmail.com>
Date
Aug 15, 2018, 05:50 UTC
Message-ID
<CABPp-BFk2X5TApYzs3QtdokBs3Hqz9uX737M6RGMtaU+wYUikw@mail.gmail.com>
In-Reply-To
<20180815051011.GC32543@aiede.svl.corp.google.com>
On Tue, Aug 14, 2018 at 10:10 PM Jonathan Nieder <jrnieder@gmail.com> wrote:
Show 6 quoted lines
>
> Elijah Newren wrote:
>
> > Subject: Add missing includes and forward declares
>
> nit: s/declares/declarations/
Thanks.
> This is a huge patch.  Was it autogenerated or generated manually?
> Can the commit message say something about methodology?

Mostly manually. I had a simple program that would create a dummy.c file that included git-compat-util.h then exactly one header, compile it, and spit any compile errors at me. I repeated that through the top-level headers.

I didn't want to repeat that description in all 6 patches, since all six came from that, so I put it in the cover letter. Since patch #1 has most that changes though, I guess it makes sense to include it at least in that one?

> Is there an easy way to review it?  (Keep in mind that I'm super lazy.
> ;-))

I guess I could send you my hacky python script that loops through the top-level header files and creates the dummy two-line c file, and you could inspect it and run it. But that only verifies that it compiles, not that the changes I choose are "correct".

Show 21 quoted lines
>
> > Signed-off-by: Elijah Newren <newren@gmail.com>
> > ---
> [...]
> > --- a/alloc.h
> > +++ b/alloc.h
> > @@ -1,9 +1,11 @@
> >  #ifndef ALLOC_H
> >  #define ALLOC_H
> >
> > +struct alloc_state;
> >  struct tree;
> >  struct commit;
> >  struct tag;
> > +struct repository;
> >
> >  void *alloc_blob_node(struct repository *r);
>
> That's reasonable.  Going forward, is there a way to tell if some of
> these forward declarations are no longer needed at some point in the
> future (e.g. can clang be convinced to warn us about it)?

I'm not aware of anything currently; while I could have easily missed things, projects like https://github.com/include-what-you-use/include-what-you-use (which look active and have a July 2018 date on them) make me suspect there isn't a good answer currently.

Show 15 quoted lines
> [...]
> > --- a/apply.h
> > +++ b/apply.h
> > @@ -1,6 +1,9 @@
> >  #ifndef APPLY_H
> >  #define APPLY_H
> >
> > +#include "lockfile.h"
> > +#include "string-list.h"
> > +
> >  enum apply_ws_error_action {
>
> Here, to avoid strange behavior, we have to be careful to make sure
> the headers don't #include apply.h back.  It's a pretty high-level
> header so there's no risk of that *phew*.
:-)
Show 12 quoted lines
> [...]
> > --- a/archive.h
> > +++ b/archive.h
> > @@ -3,6 +3,9 @@
> >
> >  #include "pathspec.h"
> >
> > +struct object_id;
> > +enum object_type;
>
> enums are of unknown size, so forward declarations don't work for
> them.  See bb/pedantic for some examples.

structs are also of unknown size; the size is irrelevant when the function signature merely uses a pointer to the struct or enum. The enum forward declaration fixes a compilation bug.

> enum object_type is defined in cache.h, so should this #include that?
We could, but we don't need the definition; a forward declaration is sufficient.
Show 11 quoted lines
> [...]
> > --- a/commit-graph.h
> > +++ b/commit-graph.h
> > @@ -4,6 +4,7 @@
> >  #include "git-compat-util.h"
> >  #include "repository.h"
> >  #include "string-list.h"
> > +#include "cache.h"
>
> We can skip the #include of git-compat-util.h since all .c files
> include it.

Good point. Should I go through and remove all the inclusions of git-compat-util.h in header files?

Show 16 quoted lines
> [...]
> > --- a/fsmonitor.h
> > +++ b/fsmonitor.h
> > @@ -1,6 +1,13 @@
> >  #ifndef FSMONITOR_H
> >  #define FSMONITOR_H
> >
> > +#include "cache.h"
> > +#include "dir.h"
> > +
> > +struct cache_entry;
> > +struct index_state;
> > +struct strbuf;
>
> cache_entry et al are defined in cache.h, right?  Are these forward
> decls needed?

Good catch; they can be removed. I'm pretty sure I added the forward declarations first, then noticed it wasn't enough, added the cache.h include, and forgot to clean up.

Show 12 quoted lines
> [...]
> > --- a/merge-recursive.h
> > +++ b/merge-recursive.h
> > @@ -1,8 +1,10 @@
> >  #ifndef MERGE_RECURSIVE_H
> >  #define MERGE_RECURSIVE_H
> >
> > -#include "unpack-trees.h"
> >  #include "string-list.h"
> > +#include "unpack-trees.h"
>
> just curious, no need to change: where does this reordering come from?

Well, since I was manually editing anyway, I saw these and decided to alphabetize it since it's a file I deal with a lot. *shrug*

Show 11 quoted lines
> [...]
> > --- a/pathspec.h
> > +++ b/pathspec.h
> > @@ -1,6 +1,11 @@
> >  #ifndef PATHSPEC_H
> >  #define PATHSPEC_H
> >
> > +#include "string.h"
> > +#include "strings.h"
>
> What are these headers?
The original patch[1] had explanations of why I added them:

+#include "string.h" /* For str[n]cmp */ +#include "strings.h" /* For str[n]casecmp */

But Peff requested that I remove the comments.
[1] https://public-inbox.org/git/20180811043218.31456-2-newren@gmail.com/
> The rest looks good.
>
> Thanks and hope that helps,
> Jonathan
Thanks for taking a look!
Previous: Jonathan NiederNext: Jonathan Nieder
Message 46 of 49 in “Add missing includes and forward declares”
  1. 0/9 Add missing includes and forward declaresElijah Newren, Aug 11, 2018
  2. 5/9 compat/precompose_utf8.h: use more common include guard styleElijah Newren, Aug 11, 2018
  3. 3/9 Move definition of enum branch_track from cache.h to branch.hElijah Newren, Aug 11, 2018
  4. 9/9 Add missing includes and forward declaresElijah Newren, Aug 11, 2018
  5. 1/9 Add missing includes and forward declaresElijah Newren, Aug 11, 2018
  6. Jeff KingAug 11, 2018
  7. 4/9 urlmatch.h: fix include guardElijah Newren, Aug 11, 2018
  8. 2/9 alloc: make allocate_alloc_state and clear_alloc_state more consistentElijah Newren, Aug 11, 2018
  9. 6/9 ewah/ewok.h: add missing includeElijah Newren, Aug 11, 2018
  10. 8/9 xdiff/xdiff.h: add missing includeElijah Newren, Aug 11, 2018
  11. 7/9 sha1dc/sha1.h: add missing includeElijah Newren, Aug 11, 2018
  12. Ævar Arnfjörð BjarmasonAug 11, 2018
  13. Elijah NewrenAug 11, 2018
  14. Jeff KingAug 11, 2018
  15. Junio C HamanoAug 13, 2018
  16. Elijah NewrenAug 14, 2018
  17. Junio C HamanoAug 15, 2018
  18. Elijah NewrenAug 15, 2018
  19. Junio C HamanoAug 15, 2018
  20. Elijah NewrenAug 15, 2018
  21. Junio C HamanoAug 15, 2018
  22. Jeff KingAug 15, 2018
  23. Jonathan NiederAug 15, 2018
  24. Jonathan NiederAug 15, 2018
  25. 0/6 Add missing includes and forward declaresElijah Newren, Aug 11, 2018
  26. 6/6 Add missing includes and forward declaresElijah Newren, Aug 11, 2018
  27. 2/6 alloc: make allocate_alloc_state and clear_alloc_state more consistentElijah Newren, Aug 11, 2018
  28. 4/6 urlmatch.h: fix include guardElijah Newren, Aug 11, 2018
  29. 3/6 Move definition of enum branch_track from cache.h to branch.hElijah Newren, Aug 11, 2018
  30. Ramsay JonesAug 11, 2018
  31. 1/6 Add missing includes and forward declaresElijah Newren, Aug 11, 2018
  32. 5/6 compat/precompose_utf8.h: use more common include guard styleElijah Newren, Aug 11, 2018
  33. 0/6 Add missing includes and forward declaresElijah Newren, Aug 13, 2018
  34. 4/6 urlmatch.h: fix include guardElijah Newren, Aug 13, 2018
  35. Jonathan NiederAug 15, 2018
  36. 3/6 Move definition of enum branch_track from cache.h to branch.hElijah Newren, Aug 13, 2018
  37. Jonathan NiederAug 15, 2018
  38. 5/6 compat/precompose_utf8.h: use more common include guard styleElijah Newren, Aug 13, 2018
  39. Jonathan NiederAug 15, 2018
  40. 2/6 alloc: make allocate_alloc_state and clear_alloc_state more consistentElijah Newren, Aug 13, 2018
  41. Jonathan NiederAug 15, 2018
  42. 6/6 Add missing includes and forward declaresElijah Newren, Aug 13, 2018
  43. Jonathan NiederAug 15, 2018
  44. 1/6 Add missing includes and forward declaresElijah Newren, Aug 13, 2018
  45. Jonathan NiederAug 15, 2018
  46. Elijah NewrenAug 15, 2018
  47. Jonathan NiederAug 15, 2018
  48. Elijah NewrenAug 15, 2018
  49. Elijah NewrenAug 15, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.