Using C::Scan : How to ignore #includes ?

Discussion in 'Perl Misc' started by Abhinav, Sep 23, 2004.

  1. Abhinav

    Abhinav Guest

    Hi,

    I have a piece of code which parses a file using C::Scan, and takes in the
    declarations :

    use strict;
    use warnings;
    use C::Scan;

    my $c = new C::Scan 'filename' => $name;
    my $fdec = $c->get('fdecls');

    I am able to get the declarations in $fdec, but I also get the following
    warnings:

    In file included from :1:
    sample_main.oc:4:8: macro names must be identifiers
    sample_main.oc:4:15: s.h: No such file or directory
    sample_main.oc:8:20: adcore.h: No such file or directory
    sample_main.oc:12:20: adxstf.h: No such file or directory

    I basically am interested *only* in the *prototypes* of the functions.[1]

    How can I remove the warnings given below ? I tried looking through Scan.pm
    and Data/Flow.pm, but without success.

    Thanks
    Abhinav

    [1] : $fdec contains info for functions with only prototypes, as well as
    definitions. I need only the prototypes. I am currently checking if the
    result matches with m/[)]\s*[;]\s*$/ to get what I need. Anyway to do it
    through C::Scan itself?
     
    Abhinav, Sep 23, 2004
    #1
    1. Advertising

  2. [A complimentary Cc of this posting was sent to
    Abhinav
    <>], who wrote in article <_iA4d.39$>:
    > my $fdec = $c->get('fdecls');
    >
    > I am able to get the declarations in $fdec, but I also get the following
    > warnings:
    >
    > In file included from :1:
    > sample_main.oc:4:8: macro names must be identifiers
    > sample_main.oc:4:15: s.h: No such file or directory
    > sample_main.oc:8:20: adcore.h: No such file or directory
    > sample_main.oc:12:20: adxstf.h: No such file or directory
    >
    > I basically am interested *only* in the *prototypes* of the functions.[1]


    You did not explain why the headers are missing. Without headers,
    what makes you think the prototypes make sense?

    > How can I remove the warnings given below ? I tried looking through Scan.pm
    > and Data/Flow.pm, but without success.


    Install the headers? What do you mean by "removing"?

    > [1] : $fdec contains info for functions with only prototypes, as well as
    > definitions. I need only the prototypes. I am currently checking if the
    > result matches with m/[)]\s*[;]\s*$/ to get what I need. Anyway to do it
    > through C::Scan itself?


    Sorry, I cannot understand what you are talking about...

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Sep 24, 2004
    #2
    1. Advertising

  3. Abhinav

    Abhinav Guest

    Ilya Zakharevich wrote:
    > [A complimentary Cc of this posting was sent to
    > Abhinav
    > <>], who wrote in article <_iA4d.39$>:
    >
    >> my $fdec = $c->get('fdecls');
    >>
    >>I am able to get the declarations in $fdec, but I also get the following
    >>warnings:
    >>
    >>In file included from :1:
    >>sample_main.oc:4:8: macro names must be identifiers
    >>sample_main.oc:4:15: s.h: No such file or directory
    >>sample_main.oc:8:20: adcore.h: No such file or directory
    >>sample_main.oc:12:20: adxstf.h: No such file or directory
    >>
    >>I basically am interested *only* in the *prototypes* of the functions.[1]

    >
    >
    > You did not explain why the headers are missing. Without headers,
    > what makes you think the prototypes make sense?
    >


    I am checking the C File to ensure that all function definitions have
    proper documentation. I do this using doxygen (http://www.doxygen.org).

    I do not need to check whether any function prototypes are having any
    documentation.

    I do the checking on a per-file basis, and do not care about included files
    - I will check them individually.


    I want to "skip" all the #defines, #includes, etc ..
    Hence,

    C:Scan should (for my case) ignore any preprocessor directives. (It would
    be as if I am removing these before running C::Scan on the file.

    This includes #ifdef as well as #include

    >
    >>How can I remove the warnings given below ? I tried looking through Scan.pm
    >>and Data/Flow.pm, but without success.

    >
    >
    > Install the headers? What do you mean by "removing"?


    As I said above, I do not want to.

    >
    >
    >>[1] : $fdec contains info for functions with only prototypes, as well as
    >>definitions. I need only the prototypes. I am currently checking if the
    >>result matches with m/[)]\s*[;]\s*$/ to get what I need. Anyway to do it
    >>through C::Scan itself?

    >
    >
    > Sorry, I cannot understand what you are talking about...


    I do not need to check if prototypes are having comments, but doxygen
    apparently has no way to differentiate this, so I used C::Scan to get all
    the declarations. C::Scan returns declarations for prototypes as well as
    definitions*, and I see if it really is a prototype by iterating over the
    returned arrayref and checking whether it ends with a ';'.

    I then comment out the prototypes before passing the file to doxygen.

    >
    > Hope this helps,
    > Ilya
    >
    >
    >
     
    Abhinav, Sep 24, 2004
    #3
  4. [A complimentary Cc of this posting was sent to
    Abhinav
    <>], who wrote in article <QXP4d.2$>:
    > > You did not explain why the headers are missing. Without headers,
    > > what makes you think the prototypes make sense?


    > I am checking the C File to ensure that all function definitions have
    > proper documentation. I do this using doxygen (http://www.doxygen.org).


    I still do not have a slightest idea what you want to achieve, and why
    do you want to achieve it in the way you do it.

    > I want to "skip" all the #defines, #includes, etc ..
    > Hence,
    >
    > C:Scan should (for my case) ignore any preprocessor directives. (It would
    > be as if I am removing these before running C::Scan on the file.


    I have no idea what "ignore" means here. If you want a broken
    pre-processor, write one, and set up C::Scan to use it. However, I'm
    not sure that a C preprocessor is a completely trivial program (well,
    probably to write one is trivial, but to *understand* what it must do
    from the C standard may be a non-trivial task).

    > > Sorry, I cannot understand what you are talking about...

    >
    > I do not need to check if prototypes are having comments, but doxygen
    > apparently has no way to differentiate this, so I used C::Scan to get all
    > the declarations. C::Scan returns declarations for prototypes as well as
    > definitions*, and I see if it really is a prototype by iterating over the
    > returned arrayref and checking whether it ends with a ';'.


    This is still not a full enough description to understand what you
    want to do. However, why not just redirect STDERR to /dev/null?

    Yours,
    Ilya
     
    Ilya Zakharevich, Sep 26, 2004
    #4
  5. Abhinav

    Abhinav Guest

    Ilya Zakharevich wrote:
    > [A complimentary Cc of this posting was sent to
    > Abhinav
    > <>], who wrote in article <QXP4d.2$>:
    >
    >>>You did not explain why the headers are missing. Without headers,
    >>>what makes you think the prototypes make sense?

    >
    >
    >>I am checking the C File to ensure that all function definitions have
    >>proper documentation. I do this using doxygen (http://www.doxygen.org).

    >
    >
    > I still do not have a slightest idea what you want to achieve, and why
    > do you want to achieve it in the way you do it.
    >
    >
    >>I want to "skip" all the #defines, #includes, etc ..
    >>Hence,
    >>
    >>C:Scan should (for my case) ignore any preprocessor directives. (It would
    >>be as if I am removing these before running C::Scan on the file.

    >
    >
    > I have no idea what "ignore" means here. If you want a broken
    > pre-processor, write one, and set up C::Scan to use it. However, I'm
    > not sure that a C preprocessor is a completely trivial program (well,
    > probably to write one is trivial, but to *understand* what it must do
    > from the C standard may be a non-trivial task).
    >
    >
    >>>Sorry, I cannot understand what you are talking about...

    >>
    >>I do not need to check if prototypes are having comments, but doxygen
    >>apparently has no way to differentiate this, so I used C::Scan to get all
    >>the declarations. C::Scan returns declarations for prototypes as well as
    >>definitions*, and I see if it really is a prototype by iterating over the
    >>returned arrayref and checking whether it ends with a ';'.

    >
    >
    > This is still not a full enough description to understand what you
    > want to do. However, why not just redirect STDERR to /dev/null?



    I have no doubt that C::Scan is an excellent module. Maybe I am the one wh
    is expecting to do things with it which it isn;t meant for..

    I have a C source file, sample.c :

    #include <stdio.h>

    int func1( int x,
    int y, /*This is a comment*/
    int z);

    int main(void){
    print ("Hello, World\n");
    }

    int func1( int x,
    int y, /* This is a comment*/
    int x)
    {
    return 0;
    }

    I used C::Scan (fdecls) to read this files and get a list of the function
    declarations.

    For the above file, it rightly gives

    int func1( int x,
    int y,
    int z);

    I am using this string, returned from C::Scan, to match the protoype in the
    file, and comment it out. Thus, the modified file I have should have the
    prototype for func1 commented out.

    However, since C::Scan->get('fdecls') strips of comments, I am having a
    problem. Thus, In the above case. the comment "/* This is a comment*? is
    not present in the searcj expression.

    I thought using C::Scan's fdecl would help me out here, as it already
    provides me with the list of prototypes..

    Is there any way I could get the whole prototype text as it is ? Or should
    I just stop using C::Scan ?

    Thanks for your help ..

    Abhinav
     
    Abhinav, Sep 27, 2004
    #5
  6. [A complimentary Cc of this posting was sent to
    Abhinav
    <>], who wrote in article <9qP5d.3$>:
    > For the above file, it rightly gives
    >
    > int func1( int x,
    > int y,
    > int z);
    >
    > I am using this string, returned from C::Scan, to match the protoype in the
    > file, and comment it out. Thus, the modified file I have should have the
    > prototype for func1 commented out.
    >
    > However, since C::Scan->get('fdecls') strips of comments, I am having a
    > problem.


    C::Scan needs to write statement/declaration boundaries. To do this,
    it needs to deal with whatever is "not C code": preprocessor directives,
    comments, literal strings.

    The first thing C::Scan does is running the input through
    preprocessor. Then the only (?) thing it needs to work with is literal
    strings (it needs to ignore C code which is embedded in literal
    strings, right?).

    As I said, you can use `cat' as a preprocessor; but C::Scan may get
    confused by the unpreprocessed code.

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Sep 27, 2004
    #6
  7. Abhinav <> writes:

    [...]

    > I want to "skip" all the #defines, #includes, etc ..
    > Hence,
    >
    > C:Scan should (for my case) ignore any preprocessor directives. (It
    > would be as if I am removing these before running C::Scan on the file.
    >
    > This includes #ifdef as well as #include


    An extremely simplistic solution is to use as your preprocessor:

    grep -v '^#'

    or perhaps a script containing that.

    I'm not familiar with C::Scan at all, so that may be off base, but
    from following this discussion it looks like it might be useful.

    ----ScottG.
     
    Scott W Gifford, Sep 27, 2004
    #7
  8. Abhinav

    Abhinav Guest

    Ilya Zakharevich wrote:
    > [A complimentary Cc of this posting was sent to
    > Abhinav
    > <>], who wrote in article <9qP5d.3$>:
    >
    >>For the above file, it rightly gives
    >>
    >>int func1( int x,
    >> int y,
    >> int z);
    >>
    >>I am using this string, returned from C::Scan, to match the protoype in the
    >>file, and comment it out. Thus, the modified file I have should have the
    >>prototype for func1 commented out.
    >>
    >>However, since C::Scan->get('fdecls') strips of comments, I am having a
    >>problem.

    >
    >
    > C::Scan needs to write statement/declaration boundaries. To do this,
    > it needs to deal with whatever is "not C code": preprocessor directives,
    > comments, literal strings.
    >
    > The first thing C::Scan does is running the input through
    > preprocessor. Then the only (?) thing it needs to work with is literal
    > strings (it needs to ignore C code which is embedded in literal
    > strings, right?).
    >
    > As I said, you can use `cat' as a preprocessor; but C::Scan may get
    > confused by the unpreprocessed code.
    >


    I was not aware of the Preprocessing step and thought that C::Scan was
    doing everything itself. Thanks to some help from Hugo, we invoked the
    preprocessor with the -C switch, which retained the comments in the file.
    Its now working like a charm. :)

    Thanks for your help

    Abhinav
     
    Abhinav, Sep 29, 2004
    #8
  9. [A complimentary Cc of this posting was sent to
    Abhinav
    <>], who wrote in article <Hbs6d.58$>:

    > I was not aware of the Preprocessing step and thought that C::Scan was
    > doing everything itself.


    All one needs to do to implement this is to implement cpp in Perl.
    Thanks, but no thanks. ;-) I prefer to delegate the work to a tool
    which is known to work, and work well...

    > Thanks to some help from Hugo, we invoked the
    > preprocessor with the -C switch, which retained the comments in the file.
    > Its now working like a charm. :)


    I'm very glad that you managed to solve your problem. I think the
    principal problem was communication one; it was very hard to
    understand what you wanted to do, and why...

    Yours,
    Ilya
     
    Ilya Zakharevich, Sep 30, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    355
    Richard
    May 7, 2007
  2. Mike D
    Replies:
    2
    Views:
    337
    Mike D
    Oct 27, 2008
  3. Rob Meade
    Replies:
    6
    Views:
    281
    Rob Meade
    Mar 1, 2004
  4. Kevin Stolt
    Replies:
    9
    Views:
    92
    Bernard Kenik
    Nov 6, 2006
  5. Philip Primak
    Replies:
    3
    Views:
    102
    Veli-Pekka Tätilä
    Aug 7, 2005
Loading...

Share This Page