Find class refences in a C++ file

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
passerby
Forum Newbie
Posts: 2
Joined: Mon May 31, 2010 7:26 am

Find class refences in a C++ file

Post by passerby »

I'm trying to match all class references in a C++ file using grep with regular expression. I'm trying to know if a specific include is usuless or not, so I have to know if there is a refence in cpp.
I wrote this RE that searches for a reference from class ABCZ, but unfortunately it isn't working as I espected:
grep -E '^[^(\/\*)(\/\/)].*[^a-zA-Z]ABCZ[]*[\*\(\<:;,{& ]'
^[^(\/\*)(\/\/)] don't math comments in the begging of the line ( // or /* )
.* followed by any character
[^a-zA-Z] do not accept any caracter before the one I'm searching (like defABCZ)
[]* any white space (I can have something like ABCZ var;)
[\*\(\<:;,{& ] followed by ( * < : ; , & { (I cant get #define "ABCZ.h" or ABCZdef for example)

Well, I can get patterns like this:
class Test: public ABCZ{
class Test: public ABCZ {
class Test :public ABCZ<T>
class NameSpace::Test :public ABCZ<T>
ABCZ var = ABCZ();
ABCZ* var = new ABCZ();
ABCZ* var = new ABCZ<T>();
teste = ABCZ(var);
teste = ABCZ(var, otherVar);
teste = ABCZ(var,otherVar);
teste = ABCZ();
funct(*ABCZ av);
struct(ABCZ, BBBB, CCCC);
const ABCZ& getNot_before(void) const;
That's all right, they are all acceptable patterns. But these I'm missing, and they are all acceptable too:
ABCZ teste;
ABCZ ¨¨¨¨ teste; //comment
ABCZ ¨¨ teste; /* comment*/
ABCZ teste;
ABCZ *teste;
¨¨ mean space...

And all these are not acceptable:
#include "ABCZ.h"
//ABCZ var = ABCZ();
ABCZdef a = ABCZdef();
DefABCZ a = DefABCZ();
ABCZdef a = ABCZdef();
/*ABCZ var = ABCZ();
I don't know if I'm missing any other patterns, if I am, please tell me =p
What am I doing wrong?
I'll be very grateful if someone answer me :)
Thanks in advance
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Find class refences in a C++ file

Post by Weirdan »

Parsing ctags output would be much easier.
passerby
Forum Newbie
Posts: 2
Joined: Mon May 31, 2010 7:26 am

Re: Find class refences in a C++ file

Post by passerby »

Thank you for your answer, I will try to use ctags
Post Reply