2

I'm trying to figure out if it is possible to print headers hierarchy using just the C language itself (like in quines). The goal is to get a set of trees where root is a .c file and leafs are the basement headers which doesn't include any other headers. The possible output:

start: header1.h->header2.h->header3.h;header1.h->header4.h; end

which means for example that header1.h includes two headers: header2.h and header4.h.

It looks like my attempt to do this Print headers hierarchy doesn't work at all. But is there some way using preprocessor or any other language feature to print headers hierarchy? Or should I just use Python script?

EDIT:

The solution proposed by @stijn in comments to use compiler option -H actually doesn't do the full job since it doesn't print headers twice. So for headers included later the hierarchy will be partial.

The question How to tell where a header file is included from? is about absolute paths of headers and where compiler actually finds on disk the particular header. My question is about headers inclusion hierarchy - which header does include which headers? So it doesn't matter where on disk headers lie.

Community
  • 1
  • 1
Link42
  • 43
  • 4
  • 1
    Can't you use the compiler? E.g. g++ -H ? – stijn Nov 23 '16 at 15:32
  • You could try to parse the `line` directives in the preprocessor output, because the hierarchy depends on preprocessing anyway. –  Nov 23 '16 at 15:35
  • @stijn many thanks, that what I wanted! I just didn't know that there is such feature. – Link42 Nov 23 '16 at 15:37
  • @Olaf I don't understand why my question is too broad or opinionated. I knew that I could do this by a script or some other tool. Question was about possibility to do it with the help of language itself. Help of the compiler is also OK since I don't need to use third-party tools. – Link42 Nov 23 '16 at 15:49
  • @stijn I didn't ask where a header file is included from, I asked about their hierarchy. In other words what is the chain of inclusions between c file and particular header file. – Link42 Nov 23 '16 at 16:05
  • You have to know where the headers are included from because you have to read the included headers to determine the hierarchy. If you don't think you have to worry about `#ifdef SOME_OBSCURE_CONDITION` / `#define HEADER_FILE "header_001.h"` / `#else` / `#define HEADER_FILE "sys/pink-grey.h"` / `#endif` / `#include HEADER_FILE` then you may be able to get away with a crude parsing of the C file to find `#include` lines and then find the headers and apply the same scan to them in turn, etc. However, there's apt to be conditional code (especially in system headers) which makes it hard to do well. – Jonathan Leffler Nov 23 '16 at 17:01
  • 1
    Using the C compiler is probably best. Failing that, you'll probably need to go to something like the LLVM project and use their tools. It is 100% non-trivial — see you in a few months (6-36?) with the completed project. – Jonathan Leffler Nov 23 '16 at 17:02
  • Yeah, someone has to generate entire inclusion graph, and do the topological sorting to provide the required output. So, GCC & Python will do I think. – g-217 Nov 23 '16 at 17:32

0 Answers0