Related articles |
---|
detect hardcodings in C/C++ natkhatbandar@yahoo.com (2004-08-25) |
Re: detect hardcodings in C/C++ codeworker@free.fr (2004-09-03) |
From: | codeworker@free.fr (Cedric LEMAIRE) |
Newsgroups: | comp.compilers |
Date: | 3 Sep 2004 12:33:03 -0400 |
Organization: | http://groups.google.com |
References: | 04-08-144 |
Keywords: | C, lex |
Posted-Date: | 03 Sep 2004 12:33:03 EDT |
natkhatbandar@yahoo.com (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
#ignore(C++)
[
// jump to the next integer, and consume it
->[
#readInteger:iValue
// 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
#readCString
]
]* // search again
;
Then, download CodeWorker at "http://www.codeworker.org" 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.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.