LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep - searching for class references in C++ using regular expression (https://www.linuxquestions.org/questions/linux-newbie-8/grep-searching-for-class-references-in-c-using-regular-expression-810719/)

mapasserby 05-28-2010 09:56 AM

Grep - searching for class references in C++ using regular expression
 
I'm trying to math 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; /* comment*/
ABCZ teste;
ABCZ *teste;

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

paulsm4 05-28-2010 10:50 AM

Hi -

Strong suggestion:
1. Forget about an ad hoc regular expression
<= It's a good thing to know...
... but it's probably not the best tool for this job

2. Instead, use a tool like id-utils:
http://www.gnu.org/software/idutils/manual/idutils.html
http://www.drdobbs.com/184401358;jse...OSKHWATMY32JVN

a) Install GNU id-utils (it might have different names, depending on your distro and package manager. Or you can just download it directly from GNU)

b) Run "mkid" on your source tree

c) Do a "gid" of your header

d) Voila! Problem solved!

IMHO .. PSM

mapasserby 05-31-2010 06:45 AM

Thank you, paulsm4, for your answer!
You had a good idea, I didnt know idutils. I tried it here, and I'm not sure if it is what I need. I forgot to say that what I really want to do it's to search for references in a specific file, to know if its includes are usuless or not. So I use doxygen to generate all files dependences and I get all includes of any files and I see if this include is being used or not.
I dont know if I'm right, but it looks like me that idutils can only search in all files specified with the mkid command, and I already know exactly each file I have to seek. So, I think it doesnt help me, because it would work just like a grep =/ or am I wrong? That's the first time I try idutils.
Any other good idea?
Thanks a lot!


All times are GMT -5. The time now is 12:16 AM.