Click here to Skip to main content
Email Password   helpLost your password?

CodeCoverage1

Introduction

This tool parses out your classes and their methods from header and CPP files. It turns out parsing out classes and methods in your C++ projects is actually very easy (if you don't need it to be life or death accurate). I'm not sure this is particularly useful; I had a specific use for this, and I just found it kind of interesting how easy it was. I'm sorry the formatting is wrong here, but CodeProject ate my whole article twice now, so I'm sorry!

Within our loop of headers and the lines in those headers, we simply check for the following; all other lines will be added and parsed later:

if (lines[lineCount].Contains(@"//"))
{
    continue;
}

if (lines[lineCount].Contains(@"friend"))
{
    continue;
}

if (lines[lineCount].Replace(" ", "").Contains(@"template<class"))
{
    continue;
}

if (lines[lineCount].Contains(@"_T("))
{
    continue;
}

To parse the CPP, your loop will exclude only these keywords (I know I'm missing tons of cases here, but surprisingly it seems to work not too shabby :)):

if (lines[lineCount].Contains(@";"))
{
    continue;
}

if (lines[lineCount].Contains(@"//"))
{
    continue;
}

if (lines[lineCount].Contains(@"_T("))
{
    continue;
}

if (lines[lineCount].Contains(@"if"))
{
    continue;
}

if (lines[lineCount].Contains(@"="))
{
    continue;
}

if (lines[lineCount].Contains(@"."))
{
    continue;
}

if (lines[lineCount].Contains(@"."))
{
    continue;
}

if (lines[lineCount].Contains(@"return"))
{
    continue;
}

Obviously, there is a lot missing to make this useful for most people, but it is kind of interesting and perhaps could be used as a learning tool. Also, you could structure this code much cleaner by searching for exclusion keywords and having them in a list, that might be a good exercise for someone.

It also should check for duplicates to handle overloaded methods and many more such issues.

Cheers..

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMy vote of 1
wtwhite
18:37 23 Feb '09  
Although to your credit you do warn that your code doesn't catch every case, there are just too many ways that it is broken. I'm not talking about "pathological" cases such as class definitions that are begun with preprocessor macro #defined to "class", which you might reasonably be forgiven for overlooking. I'm talking about problems handling "normal, everyday" code: e.g. a class name containing the string "if" (e.g. "ifstream", "Difference") will not be found; nor will a class declaration that ends with a single-line comment (as many do). Also, any text string or C-style /* comment */ containing the word "class" will be erroneously interpreted as a class declaration. Finally, for some reason you deliberately ignore derived classes -- why?

I would be willing to forgive minor efficiencies, such as reading in all lines in the file into an array when they could processed one at a time, if the rest of the code was better.

If this code gets the job done for you on your particular codebase, I'm happy for you -- I've often whipped up similar quick and dirty scripts for things like this -- but this is not the sort of code that should be held up as an example for others to replicate. It's nowhere near general enough.
GeneralRe: My vote of 1
rj45
16:09 25 Feb '09  
I do like that you said why you gave this a one and I agree with your points but I'm not sure it deserves a one simply because this is kind of interesting IMHO. Honestly, to write a parser that is real would probably have hundreds if not thousands of nested cases. It wouldn't be as useful at that point either because the inherent complexity

Like I said in the intro, this is just a starting point for an exercise, albeit not a good one for many applications. This might be a good starting point for doing something like opening a .cpp file in visual studio by clicking on a line in your logs or something. Obviously you'll find a bunch of issues along the way no matter what you do and I said that right up front. The task of parsing a directory for all classes/methods is interesting despite how dirty the code is. Hopefully someone will write a better one.


Thanks,
R.J.


Last Updated 19 Feb 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010