Re: detect hardcodings in C/C++ (Cedric LEMAIRE)
3 Sep 2004 12:33:03 -0400

          From comp.compilers

Related articles
detect hardcodings in C/C++ (2004-08-25)
Re: detect hardcodings in C/C++ (2004-09-03)
| List of all articles for this month |

From: (Cedric LEMAIRE)
Newsgroups: comp.compilers
Date: 3 Sep 2004 12:33:03 -0400
References: 04-08-144
Keywords: C, lex
Posted-Date: 03 Sep 2004 12:33:03 EDT (Zombie) wrote
> Hi,
> I wish to scan C/C++ code and detect any hard coded statements in it,
> like the '1000' in:
> char str[1000];
> or the '10' in:
> int i = 10;

> [You could practically do that with grep, with a prepass to get rid
> of the comments. -John]

If you want to extract integer constants from your C/C++ file, you can
type this piece of code, written in CodeWorker, and save it in
"integers.cwp" :
    integers ::=
            // ignore C++-like comments between BNF symbols,
            // so that it doesn't read integers into comments
                // jump to the next integer, and consume it
                        // trace the integer on the console
                        => traceLine(iValue);
                        // if a string is encountered, consume it
                        // to not take into account the integers,
                        // which are perhaps into
            ]* // search again

Then, download CodeWorker at "" and type the
command line:
    codeworker -parsebnf integers.cwp <your-C-file.c>

If you have more than one file to process, write the following lines
in the file "iterateSources.cws":
    forfile i in "*.c" {
        parseAsBNF("integers.cwp", project, i);

and execute :
    codeworker iterateSources.cws

As CodeWorker is also a source code generator, you can choose to save
the integers in a file, following a template-based script, instead of
writing them on the console. You can also write the filename
(getInputFilename()), the line (countInputLines()) and column
(countInputCols()) where they were located.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.