For Programmers: Free Programming Magazines  


Home > Archive > Compilers > September 2004 > detect hardcodings in C/C++









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author detect hardcodings in C/C++
Zombie

2004-08-25, 3:59 pm

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;

I don't have the slighest idea about how to do it. (I have never
worked with compiler stuff)
Any idea what options I have?

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

2004-09-03, 4:03 pm

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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com