Susanth
Susanth

Reputation: 29

how to search for all constructors in c++ code?

I have to find out all the constructors in my code base (which is huge) , is there any easy way to do it (without opening each file , reading it and finding all classes)? Any language specific feature that I can use in my grep?

To find destructors it is easy , I can search for "~". I can write some code to find "::" and match right and left words , if they are equal then I can print that line. But if constructor is inside the class (with in H/HPP file), the above logic is missing.

Upvotes: 0

Views: 687

Answers (3)

David V. Corbin
David V. Corbin

Reputation: 397

Finding the constructors is fairly simple (as other had said).... Finding all calls to constructors and destructors is non-trivial and so far has eluded me....

Upvotes: 0

hogo
hogo

Reputation: 11

Since you're thinking of using grep, I'm assuming you want to do it programmaticly, and not in an IDE. It also depend if you're parsing the header or the code, again I'm assuming you want to parse the header.

I did it using python:

inClass=False
className=""
motifClass=re.compile("class [a-zA-Z][a-zA-Z1-9_]*)")#to get the class name
motifEndClass=re.compile("};")#Not sure that'll work for every file
motifConstructor=re.compile("~?"+className+"\(.*\)")
res=[]
#assuming you already got the file loaded
for line in lines:
    if not inClass:#we're searching to be in one
        temp=line.match(class)
        if temp:
            className=res.group(1)
            inClass=True
    else:
        temp=line.match(motifEndClass)
        if temp:#doesn't end at the end of the class, since multiple class can be in a file
            inClass=False
            continue
        temp=line.match(motifConstructor)
        if temp:
            res.append(line)#we're adding the line that matched
#do whatever you want with res here!

I didn't test it,I did it rather quickly, and tried to simplify an old piece of code, so numerous things are not supported, like nested classes. From that, you can do a script looking for every header in a directory, and use the result how you like !

Upvotes: 1

Salman
Salman

Reputation: 333

Search all classes names and then find the function has same name like class name. And second option is that as we know that the constructor is always be public so search word public and find the constructor.

Upvotes: 0

Related Questions