detect necessary #includes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Woody

    detect necessary #includes

    hi,

    after some weeks of development of a project, there likely are many
    "#include" in source files which was added before but are now
    unnecessary. is there any tool which can find out those "#include"s
    and tell me it's safe to remove them? manual work is so time consuming.
    thanks.

    -
    woody

  • Logan Shaw

    #2
    Re: detect necessary #includes

    Steven Woody wrote:[color=blue]
    > hi,
    >
    > after some weeks of development of a project, there likely are many
    > "#include" in source files which was added before but are now
    > unnecessary. is there any tool which can find out those "#include"s
    > and tell me it's safe to remove them? manual work is so time consuming.[/color]

    Only a standards document or other API documentation can really tell
    you whether it's safe to remove a #include.

    Consider what happens if some documentation says this:

    "foo() is available in foo.h, and bar() is available in bar.h".

    Then suppose that foo.h and bar.h both contain this:

    #include <foobar.h>

    and that foobar.h contains the prototypes for both foo() and bar().

    Now let's suppose that your code uses both foo() and bar(), and you,
    by trial and error, determine you can get away by only including
    foo.h, and you remove the include for bar.h. It will compile OK
    right now, but what happens later on when the foobar library that
    you're using changes its implementation so that foo()'s prototype
    is really in foo.h and bar()'s prototype is really in bar.h? You
    will have created for yourself a horrible mess because your #includes
    are depending on quirks of the .h files which aren't guaranteed
    rather than depending on what is guaranteed by the documentation.

    More to the point, how can any automated program that just looks
    at header files tell which header files are the ones that the
    documentation guarantees will work?

    I suppose it might be possible in some limited cases to write such
    a tool, such as if no .h file every includes another .h file, but
    that's not how most .h files usually work...

    - Logan

    Comment

    • Steven Woody

      #3
      Re: detect necessary #includes


      Logan Shaw wrote:[color=blue]
      > Steven Woody wrote:[color=green]
      > > hi,
      > >
      > > after some weeks of development of a project, there likely are many
      > > "#include" in source files which was added before but are now
      > > unnecessary. is there any tool which can find out those "#include"s
      > > and tell me it's safe to remove them? manual work is so time consuming.[/color]
      >
      > Only a standards document or other API documentation can really tell
      > you whether it's safe to remove a #include.
      >
      > Consider what happens if some documentation says this:
      >
      > "foo() is available in foo.h, and bar() is available in bar.h".
      >
      > Then suppose that foo.h and bar.h both contain this:
      >
      > #include <foobar.h>
      >
      > and that foobar.h contains the prototypes for both foo() and bar().
      >
      > Now let's suppose that your code uses both foo() and bar(), and you,
      > by trial and error, determine you can get away by only including
      > foo.h, and you remove the include for bar.h. It will compile OK
      > right now, but what happens later on when the foobar library that
      > you're using changes its implementation so that foo()'s prototype
      > is really in foo.h and bar()'s prototype is really in bar.h? You
      > will have created for yourself a horrible mess because your #includes
      > are depending on quirks of the .h files which aren't guaranteed
      > rather than depending on what is guaranteed by the documentation.
      >
      > More to the point, how can any automated program that just looks
      > at header files tell which header files are the ones that the
      > documentation guarantees will work?
      >
      > I suppose it might be possible in some limited cases to write such
      > a tool, such as if no .h file every includes another .h file, but
      > that's not how most .h files usually work...
      >
      > - Logan[/color]

      i am not going to remove any standard or system wide #includes, i am
      going to remove those #includes which include my own headers.

      Comment

      • Bjørn Augestad

        #4
        Re: detect necessary #includes

        Steven Woody wrote:[color=blue]
        > hi,
        >
        > after some weeks of development of a project, there likely are many
        > "#include" in source files which was added before but are now
        > unnecessary. is there any tool which can find out those "#include"s
        > and tell me it's safe to remove them? manual work is so time consuming.
        > thanks.
        >
        > -
        > woody
        >[/color]

        PC-lint(www.gimpel.com) does that, IIRC.
        Bjørn

        Comment

        • Richard Heathfield

          #5
          Re: detect necessary #includes

          Steven Woody said:
          [color=blue]
          > hi,
          >
          > after some weeks of development of a project, there likely are many
          > "#include" in source files which was added before but are now
          > unnecessary. is there any tool which can find out those "#include"s
          > and tell me it's safe to remove them? manual work is so time consuming.
          > thanks.[/color]

          Remove all your headers, and recompile. Then see what the compiler moans
          about. This may also help you to refactor your headers in a more sensible
          way.

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)

          Comment

          • Mark McIntyre

            #6
            Re: detect necessary #includes

            On 13 Jan 2006 23:47:03 -0800, in comp.lang.c , "Steven Woody"
            <narkewoody@gma il.com> wrote:
            [color=blue]
            >hi,
            >
            >after some weeks of development of a project, there likely are many
            >"#include" in source files which was added before but are now
            >unnecessary. is there any tool which can find out those "#include"s
            >and tell me it's safe to remove them? manual work is so time consuming.[/color]

            Well, you could us an cross-ref tool to build up a list of which
            functions are used and which #include they come from, then by process
            of elimination work out which ones you don't need. If you have a many
            MLOC legacy project split into dozens of libs, thats how I'd do it.

            However for a project you've been working on for only weeks, it would
            be quicker to just comment out any header you don't think you need,
            recompile and watch the warnings.
            Mark McIntyre
            --

            ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
            http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
            ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

            Comment

            • Steven Woody

              #7
              Re: detect necessary #includes

              Richard Heathfield wrote:[color=blue]
              > Steven Woody said:
              >[color=green]
              > > hi,
              > >
              > > after some weeks of development of a project, there likely are many
              > > "#include" in source files which was added before but are now
              > > unnecessary. is there any tool which can find out those "#include"s
              > > and tell me it's safe to remove them? manual work is so time consuming.
              > > thanks.[/color]
              >
              > Remove all your headers, and recompile. Then see what the compiler moans
              > about. This may also help you to refactor your headers in a more sensible
              > way.
              >[/color]

              thanks for all your advices. commenting out all headers then add one by
              one seems the only solution i can adapt thought i wish there exists a
              more time saving method. pc-lint is not suite to me since i am working
              on Linux. thanks again.

              Comment

              • Emmanuel Delahaye

                #8
                Re: detect necessary #includes

                Steven Woody a écrit :[color=blue]
                > after some weeks of development of a project, there likely are many
                > "#include" in source files which was added before but are now
                > unnecessary.[/color]

                Who knows ? Things can change and the guards are doing their job. Don't
                touch anything. You'll gain peanuts.

                --
                A+

                Emmanuel Delahaye

                Comment

                • Richard Heathfield

                  #9
                  Re: detect necessary #includes

                  Steven Woody said:
                  [color=blue]
                  > thanks for all your advices. commenting out all headers[/color]

                  Please, please, please don't do this. If you want to rip them out
                  temporarily, do it like so:

                  #if 0
                  #include "foo.h"
                  #include "bar.h"
                  #include "baz.h"
                  #include "quux.h"
                  #endif

                  Then, as you discover you need them, fish them out of the #if and into the
                  code proper.

                  Comment syntax is for adding something extra to the code, not for taking
                  something away.

                  --
                  Richard Heathfield
                  "Usenet is a strange place" - dmr 29/7/1999

                  email: rjh at above domain (but drop the www, obviously)

                  Comment

                  • Steven Woody

                    #10
                    Re: detect necessary #includes


                    Richard Heathfield wrote:[color=blue]
                    > Steven Woody said:
                    >[color=green]
                    > > thanks for all your advices. commenting out all headers[/color]
                    >
                    > Please, please, please don't do this. If you want to rip them out
                    > temporarily, do it like so:
                    >
                    > #if 0
                    > #include "foo.h"
                    > #include "bar.h"
                    > #include "baz.h"
                    > #include "quux.h"
                    > #endif[/color]

                    i've not seen big difference between this method and that of commting.
                    [color=blue]
                    >
                    > Then, as you discover you need them, fish them out of the #if and into the
                    > code proper.
                    >
                    > Comment syntax is for adding something extra to the code, not for taking
                    > something away.[/color]

                    how to understand this ? i am so interesting

                    Comment

                    • Emmanuel Delahaye

                      #11
                      Re: detect necessary #includes

                      Richard Heathfield a écrit :[color=blue]
                      > #if 0
                      > #include "foo.h"
                      > #include "bar.h"
                      > #include "baz.h"
                      > #include "quux.h"
                      > #endif
                      >
                      > Then, as you discover you need them, fish them out of the #if and into the
                      > code proper.[/color]

                      "fish them out"

                      I like it !

                      --
                      A+

                      Emmanuel Delahaye

                      Comment

                      • Richard Heathfield

                        #12
                        Re: detect necessary #includes

                        Steven Woody said:
                        [color=blue]
                        >
                        > Richard Heathfield wrote:[color=green]
                        >> Steven Woody said:
                        >>[color=darkred]
                        >> > thanks for all your advices. commenting out all headers[/color]
                        >>
                        >> Please, please, please don't do this. If you want to rip them out
                        >> temporarily, do it like so:
                        >>
                        >> #if 0
                        >> #include "foo.h"
                        >> #include "bar.h"
                        >> #include "baz.h"
                        >> #include "quux.h"
                        >> #endif[/color]
                        >
                        > i've not seen big difference between this method and that of commting.[/color]

                        The obvious difference is that "commenting out" code is an abuse of the
                        syntax. But perhaps you're not so interested in clarity, and want a more
                        practical motivation. Okay, here it is:

                        foo(); /* do the foo thing */
                        if(condition) /* we only want to bar if there's an 'r' in the month */
                        {
                        bar(); /* this will use the default directory, C:\bar\ */
                        baz(); /* don't forget to baz everything back to normal */
                        }

                        Observe the problem with "commenting out" this code:

                        /*
                        foo(); /* do the foo thing */
                        if(condition) /* we only want to bar if there's an 'r' in the month */
                        {
                        bar(); /* this will use the default directory, C:\bar\ */
                        baz(); /* don't forget to baz everything back to normal */
                        }
                        */

                        See the difficulty? Only the foo() call has been "commented out". And now
                        you have a syntax error on the last line - "unmatched closing comment" or
                        similar.

                        Now let's do it properly:

                        #if 0
                        foo(); /* do the foo thing */
                        if(condition) /* we only want to bar if there's an 'r' in the month */
                        {
                        bar(); /* this will use the default directory, C:\bar\ */
                        baz(); /* don't forget to baz everything back to normal */
                        }
                        #endif

                        No syntax error. No problem with existing comments. And to undo it, you need
                        only change a single character (change 0 to 1) - and of course it's just as
                        easy to re-do it.
                        [color=blue][color=green]
                        >> Then, as you discover you need them, fish them out of the #if and into
                        >> the code proper.
                        >>
                        >> Comment syntax is for adding something extra to the code, not for taking
                        >> something away.[/color]
                        >
                        > how to understand this ? i am so interesting[/color]

                        Unmatched closing comment.

                        --
                        Richard Heathfield
                        "Usenet is a strange place" - dmr 29/7/1999

                        email: rjh at above domain (but drop the www, obviously)

                        Comment

                        • Keith Thompson

                          #13
                          Re: detect necessary #includes

                          Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                          > On 13 Jan 2006 23:47:03 -0800, in comp.lang.c , "Steven Woody"
                          > <narkewoody@gma il.com> wrote:[color=green]
                          >>after some weeks of development of a project, there likely are many
                          >>"#include" in source files which was added before but are now
                          >>unnecessary . is there any tool which can find out those "#include"s
                          >>and tell me it's safe to remove them? manual work is so time consuming.[/color]
                          >
                          > Well, you could us an cross-ref tool to build up a list of which
                          > functions are used and which #include they come from, then by process
                          > of elimination work out which ones you don't need. If you have a many
                          > MLOC legacy project split into dozens of libs, thats how I'd do it.[/color]

                          The cross-ref tool would only tell you about headers that provide
                          function declarations.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • Keith Thompson

                            #14
                            Re: detect necessary #includes

                            Richard Heathfield <invalid@invali d.invalid> writes:[color=blue]
                            > Steven Woody said:[color=green]
                            >> thanks for all your advices. commenting out all headers[/color]
                            >
                            > Please, please, please don't do this. If you want to rip them out
                            > temporarily, do it like so:
                            >
                            > #if 0
                            > #include "foo.h"
                            > #include "bar.h"
                            > #include "baz.h"
                            > #include "quux.h"
                            > #endif
                            >
                            > Then, as you discover you need them, fish them out of the #if and into the
                            > code proper.
                            >
                            > Comment syntax is for adding something extra to the code, not for taking
                            > something away.[/color]

                            Uncomfortable as it is to disagree with Richard, I have to say I don't
                            think this is a big deal. Commenting out substantial blocks of code
                            is a bad idea, partly because comments don't nest, but I don't see a
                            problem with commenting out #include directives one at a time:

                            /* #include "foo.h" */
                            /* #include "bar.h" */
                            /* #include "baz.h" */
                            /* #include "quux.h" */

                            With the "#if 0", re-adding "bar.h" results in:

                            #if 0
                            #include "foo.h"
                            #endif
                            #include "bar.h"
                            #if 0
                            #include "baz.h"
                            #include "quux.h"
                            #endif

                            as opposed to:

                            /* #include "foo.h" */
                            #include "bar.h"
                            /* #include "baz.h" */
                            /* #include "quux.h" */

                            which makes it much easier to tell at a glance which lines are active
                            and which ones aren't.

                            Furthermore, the whole idea any #includes that are commented out will
                            eventually be removed altogether, so the comments are only temporary.

                            There is a risk if you have comments on the #include lines, but then
                            you just have to be careful to comment out only the directive, not the
                            entire line.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • Richard Heathfield

                              #15
                              Re: detect necessary #includes

                              Keith Thompson said:
                              [color=blue]
                              > Uncomfortable as it is to disagree with Richard,[/color]

                              That should tell you something right there. :-)
                              [color=blue]
                              > I have to say I don't
                              > think this is a big deal. Commenting out substantial blocks of code
                              > is a bad idea, partly because comments don't nest, but I don't see a
                              > problem with commenting out #include directives one at a time:
                              >
                              > /* #include "foo.h" */
                              > /* #include "bar.h" */
                              > /* #include "baz.h" */
                              > /* #include "quux.h" */
                              >
                              > With the "#if 0", re-adding "bar.h" results in:
                              >
                              > #if 0
                              > #include "foo.h"
                              > #endif
                              > #include "bar.h"
                              > #if 0
                              > #include "baz.h"
                              > #include "quux.h"
                              > #endif[/color]

                              Huh? What are you talking about?

                              Before:

                              #if 0
                              #include "foo.h"
                              #include "bar.h"
                              #include "baz.h"
                              #include "quux.h"
                              #endif

                              Edit:

                              /bar
                              dd2jp

                              After:

                              #if 0
                              #include "foo.h"
                              #include "baz.h"
                              #include "quux.h"
                              #endif
                              #include "bar.h"

                              The quickest way I can find to remove the comments you used is:

                              /bar
                              03x$2Xx

                              which is actually slightly more work.

                              --
                              Richard Heathfield
                              "Usenet is a strange place" - dmr 29/7/1999

                              email: rjh at above domain (but drop the www, obviously)

                              Comment

                              Working...