Extract a function from C code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Schultz

    #1

    Extract a function from C code?

    I have a C/C++ file that I simply want to 'extract' a function from.
    Something like: extract <function name> <c or cpp file>

    I want it to return from the beginning of the function, to the end.

    I tried cscope, and that will find the function, but it won't tell me
    how many lines it is or extract it for me.

    Any ideas on what I could use?

    It would also be awesome if I could choose to extract struct
    definitions, multi-line macro defs, etc.

    sembiance@gmail .com
  • red floyd

    #2
    OT: Extract a function from C code?

    Robert Schultz wrote:[color=blue]
    > I have a C/C++ file that I simply want to 'extract' a function from.
    > Something like: extract <function name> <c or cpp file>
    >
    > I want it to return from the beginning of the function, to the end.
    >
    > I tried cscope, and that will find the function, but it won't tell me
    > how many lines it is or extract it for me.
    >
    > Any ideas on what I could use?
    >
    > It would also be awesome if I could choose to extract struct
    > definitions, multi-line macro defs, etc.
    >
    > sembiance@gmail .com[/color]

    If you're on a *nix system, look into "csplit".

    We're OT now, so I won't say any more.

    Comment

    • Eric Sosman

      #3
      Re: Extract a function from C code?

      Robert Schultz wrote:[color=blue]
      > I have a C/C++ file that I simply want to 'extract' a function from.
      > Something like: extract <function name> <c or cpp file>
      >
      > I want it to return from the beginning of the function, to the end.
      >
      > I tried cscope, and that will find the function, but it won't tell me
      > how many lines it is or extract it for me.
      >
      > Any ideas on what I could use?
      >
      > It would also be awesome if I could choose to extract struct
      > definitions, multi-line macro defs, etc.[/color]

      If by "extract a function" you mean "move the
      function's source lines out of one file and into
      another," an editor is your best bet. Be warned
      that the source lines of a function may not be
      useful when separated from the #include's and other
      declarations of their original file.

      As for "extracting " struct definitions and macros,
      once again an editor seems the best bet. What are
      you trying to do? Break up an everything-thrown-
      together source file into manageable pieces? If so,
      purely mechanical tools -- tools that operate without
      understanding of the relationships between the various
      pieces -- won't do a very good job.

      --
      Eric.Sosman@sun .com

      Comment

      • CBFalconer

        #4
        Re: Extract a function from C code?

        Robert Schultz wrote:[color=blue]
        >
        > I have a C/C++ file that I simply want to 'extract' a function from.
        > Something like: extract <function name> <c or cpp file>
        >
        > I want it to return from the beginning of the function, to the end.
        >
        > I tried cscope, and that will find the function, but it won't tell me
        > how many lines it is or extract it for me.
        >
        > Any ideas on what I could use?
        >
        > It would also be awesome if I could choose to extract struct
        > definitions, multi-line macro defs, etc.
        >
        > sembiance@gmail .com[/color]

        Use an editor. But it won't work on a C/C++ file, because no such
        language exists. There are C source files, and there are C++
        source files, and there are AWK source files, but there are NO
        C/C++ source files, alas.

        --
        Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net> USE worldnet address!

        Comment

        • Arthur J. O'Dwyer

          #5
          Re: Extract a function from C code?


          On Thu, 28 Oct 2004, Robert Schultz wrote:[color=blue]
          >
          > I have a C/C++ file that I simply want to 'extract' a function from.
          > Something like: extract <function name> <c or cpp file>
          >
          > I want it to return from the beginning of the function, to the end.
          >
          > I tried cscope, and that will find the function, but it won't tell me
          > how many lines it is or extract it for me.[/color]

          I don't know what "cscope" does, but if it will tell you where the
          function begins, then you can just take everything from that line,
          to the next '{' character, to the matching '}' character; that will
          give you all the lines you want, barring evil preprocessor tricks
          such as

          #define RBRACE }
          void foo {
          RBRACE
          [color=blue]
          > It would also be awesome if I could choose to extract struct
          > definitions, multi-line macro defs, etc.[/color]

          Sounds like you're looking for a C parser. ;-) In the worst case,
          there are lex/yacc grammars for C90 and maybe C99 available; you can
          learn lex/yacc and use those grammars to extract the stuff you want.

          HTH,
          -Arthur

          Comment

          • Cedric LEMAIRE

            #6
            Re: Extract a function from C code?

            sembiance@gmail .com (Robert Schultz) wrote in message news:<bb82e2.04 10281158.af0f10 c@posting.googl e.com>...[color=blue]
            > I have a C/C++ file that I simply want to 'extract' a function from.
            > Something like: extract <function name> <c or cpp file>
            >
            > I want it to return from the beginning of the function, to the end.
            >
            > I tried cscope, and that will find the function, but it won't tell me
            > how many lines it is or extract it for me.
            >
            > Any ideas on what I could use?[/color]
            You could use CodeWorker, a parsing tool and a source code generator.

            Copy the following script into a file called "function_extra ctor.cwp":
            function_extrac tor ::=
            => if _ARGS.empty() error("function name expected on the command
            line");
            #ignore(C++)
            ->[
            // from here, copy what is parsed to the output file
            #implicitCopy
            // name of the function
            #readIdentifier :sFunctionName
            #nextStep
            // function we are looking for?
            #check(sFunctio nName == _ARGS#front)
            // in C++, the function could be template
            ['<' ignore_template _clause '>']?
            '('
            // read parameters
            ignore_parenthe sis
            ')'
            // in C++, ignore 'throw' or 'const' ...
            [#readIdentifier | '(' ignore_parenthe sis ')']*
            // read the block of instructions
            '{'
            block
            '}'
            ]
            ;

            ignore_template _clause ::= [#readCString | #readCChar | '<'
            template_clause '>' | ~'>']*;
            ignore_parenthe sis ::= [#readCString | #readCChar | '('
            ignore_parenthe sis ')' | ~')']*;
            block ::= [#readCString | #readCChar | '{' block '}' | ~'}']*;

            Then, type the command:
            codeworker -translate function_extrac tor.cwp <your-C-file>
            <your-output-file> -args <function-name>

            It doesn't copy the return type of the function. If you need it, and
            if we can suppose that the return type occupies the space between the
            beginning of the line and the name of the function, it doesn't require
            to parse the type seriously, so I can improve the script very easily.
            [color=blue]
            > It would also be awesome if I could choose to extract struct
            > definitions, multi-line macro defs, etc.[/color]

            It is simpler to extract a macro definition or a 'struct' declaration.
            If you don't want to write them by yourself, because you don't want to
            invest time on CodeWorker, I can write them for you.

            Comment

            • Michael Wojcik

              #7
              Re: Extract a function from C code?


              In article <Pine.LNX.4.6 0-041.04102820421 81.10090@unix45 .andrew.cmu.edu >, "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> writes:[color=blue]
              >
              > I don't know what "cscope" does, but if it will tell you where the
              > function begins, then you can just take everything from that line,
              > to the next '{' character, to the matching '}' character;[/color]

              Provided they're not in comments, or string literals, or character
              literals, or #define directives, or sections of code that are
              conditionally-compiled out...
              [color=blue]
              > that will
              > give you all the lines you want, barring evil preprocessor tricks
              > such as
              >
              > #define RBRACE }
              > void foo {
              > RBRACE[/color]

              Yeah, or those, too.
              [color=blue][color=green]
              > > It would also be awesome if I could choose to extract struct
              > > definitions, multi-line macro defs, etc.[/color]
              >
              > Sounds like you're looking for a C parser.[/color]

              I think so. And it needs to handle preprocessing (and, to be fully
              correct, translations phases 1 (mapping to source character set and
              trigraph translation) and 2 (source line splicing), first), with
              the same environmental conditions (eg predefined macros) in effect
              as when the program is translated by the actual implementation.

              Sure, in most cases, just matching {} characters works fine. Whether
              it sufficies in this case is something the OP will have to judge. It
              isn't the sort of thing I'd be comfortable automating; I'd do it by
              hand, in vim, using brace-matching as a first approximation but
              checking to make sure I had selected the lines I really wanted.

              --
              Michael Wojcik michael.wojcik@ microfocus.com

              You have Sun saying, "Who needs Linux? We have Solaris." You have
              Microsoft saying, "Who needs Linux? We have Windows 2000." Then you
              have IBM saying, "I think we all need Linux." Only the greatest
              sinners know how to really repent. -- John Patrick, IBM VP

              Comment

              • John Bode

                #8
                Re: Extract a function from C code?

                "Arthur J. O'Dwyer" <ajo@nospam.and rew.cmu.edu> wrote in message news:<Pine.LNX. 4.60-041.04102820421 81.10090@unix45 .andrew.cmu.edu >...[color=blue]
                > On Thu, 28 Oct 2004, Robert Schultz wrote:[color=green]
                > >
                > > I have a C/C++ file that I simply want to 'extract' a function from.
                > > Something like: extract <function name> <c or cpp file>
                > >
                > > I want it to return from the beginning of the function, to the end.
                > >
                > > I tried cscope, and that will find the function, but it won't tell me
                > > how many lines it is or extract it for me.[/color]
                >
                > I don't know what "cscope" does, but if it will tell you where the
                > function begins, then you can just take everything from that line,
                > to the next '{' character, to the matching '}' character; that will
                > give you all the lines you want, barring evil preprocessor tricks
                > such as
                >
                > #define RBRACE }
                > void foo {
                > RBRACE
                >[color=green]
                > > It would also be awesome if I could choose to extract struct
                > > definitions, multi-line macro defs, etc.[/color]
                >
                > Sounds like you're looking for a C parser. ;-) In the worst case,
                > there are lex/yacc grammars for C90 and maybe C99 available; you can
                > learn lex/yacc and use those grammars to extract the stuff you want.
                >
                > HTH,
                > -Arthur[/color]

                Yeah, something like this basically screams for a compiler front-end
                to recognize individual translation units, struct and union
                definitions, typedefs, etc. (at least to do it right).

                Comment

                • Cedric LEMAIRE

                  #9
                  Re: Extract a function from C code?

                  sembiance@gmail .com (Robert Schultz) wrote in message news:<bb82e2.04 10290601.161ae7 18@posting.goog le.com>...[color=blue]
                  > Well see, I've written some C functions of my own over the past 10
                  > years.
                  > In numerous projects, on numerous platforms.
                  > The functions have evolved and more have been added, features have
                  > been added, bugs have been fixed.
                  > But silly me, I didn't do all my updates to a single file.
                  >
                  > So now I have about 300 files, all named Utility.c containing more or
                  > less the same functions, but each in a different degree of 'being
                  > right'.
                  >
                  > I wanted to avoid going through each file by hand, looking at each
                  > function by hand.
                  > So I was hoping to be able to automate a process so it would extract
                  > function XYZ from every one of those files.
                  > Then I would use diff to compare the extracted functions to see what
                  > sort of changes are where.
                  > I can't use diff on the original 300 because the files as a whole vary
                  > file to much line by line (functions arranged in different orders).
                  >
                  > So now you see why I wanted an automated method because a little
                  > automation could reduce the amount of manual labor required by a large
                  > large amount.
                  >
                  > Any ideas?[/color]

                  It seems not so hard :
                  * first, to establish the name of all functions automatically,
                  considering the 300 'Utility.c' files,
                  * second, to suppress the function names, which aren't interesting
                  for you, by hand of course,
                  * third, to extract the implementations function by function, from
                  the 300 files:
                  * to retain all different implementations ,
                  * to put them in different output files,
                  * to make a diff on each of them (good luck!),

                  These tasks are easy to implement with CodeWorker (see the extraction
                  further, for instance). If you are interested, I can help you.

                  Comment

                  • Robert Schultz

                    #10
                    Re: Extract a function from C code?

                    codeworker@free .fr (Cedric LEMAIRE) wrote in message news:<dbf873f9. 0410290137.662d 040e@posting.go ogle.com>...[color=blue]
                    > sembiance@gmail .com (Robert Schultz) wrote in message news:<bb82e2.04 10281158.af0f10 c@posting.googl e.com>...[color=green]
                    > > I have a C/C++ file that I simply want to 'extract' a function from.
                    > > Something like: extract <function name> <c or cpp file>
                    > >
                    > > I want it to return from the beginning of the function, to the end.
                    > >
                    > > I tried cscope, and that will find the function, but it won't tell me
                    > > how many lines it is or extract it for me.
                    > >
                    > > Any ideas on what I could use?[/color]
                    > You could use CodeWorker, a parsing tool and a source code generator.
                    >
                    > Copy the following script into a file called "function_extra ctor.cwp":
                    > [CODE]
                    >
                    > Then, type the command:
                    > codeworker -translate function_extrac tor.cwp <your-C-file>
                    > <your-output-file> -args <function-name>
                    >
                    > It doesn't copy the return type of the function. If you need it, and
                    > if we can suppose that the return type occupies the space between the
                    > beginning of the line and the name of the function, it doesn't require
                    > to parse the type seriously, so I can improve the script very easily.
                    >[color=green]
                    > > It would also be awesome if I could choose to extract struct
                    > > definitions, multi-line macro defs, etc.[/color]
                    >
                    > It is simpler to extract a macro definition or a 'struct' declaration.
                    > If you don't want to write them by yourself, because you don't want to
                    > invest time on CodeWorker, I can write them for you.[/color]

                    AWESOME!!!! THANK YOU SO MUCH!
                    This is great.
                    codeworker is EXACTLY what I was looking for!
                    Thanks again!

                    Comment

                    • Cedric LEMAIRE

                      #11
                      Re: Extract a function from C code?

                      sembiance@gmail .com (Robert Schultz) wrote in message news:<bb82e2.04 11031157.632653 c1@posting.goog le.com>...[color=blue]
                      > codeworker@free .fr (Cedric LEMAIRE) wrote in message news:<dbf873f9. 0410290137.662d 040e@posting.go ogle.com>...[color=green][color=darkred]
                      > > > It would also be awesome if I could choose to extract struct
                      > > > definitions, multi-line macro defs, etc.[/color]
                      > >
                      > > It is simpler to extract a macro definition or a 'struct' declaration.
                      > > If you don't want to write them by yourself, because you don't want to
                      > > invest time on CodeWorker, I can write them for you.[/color]
                      >
                      > AWESOME!!!! THANK YOU SO MUCH!
                      > This is great.
                      > codeworker is EXACTLY what I was looking for!
                      > Thanks again![/color]

                      You are welcome.

                      If you need some help to implement the extractor for 'struct' and
                      '#define', do not hesitate to contact me on my email.

                      Comment

                      Working...