c++code-analysisstatic-code-analysiscqlinqcppdepend

How can we detect all pointer comparisons in source code? c++


We want to find all pointer comparisons from a class type. For example we have a class A and derived classes from A like B, C ect.

A *pa;
A *pa2;
B *pb;

All comparisons like if (pa == pa2) or if (pa != pb) must be found in our source code.

I know that we can use a CLang analyzer to find those comparisons but our source code is not CLang compatible. We are using visual studio 2015.

Please don't give a solution like; remove class A from source code then try compile it so find all usages from class A where it doesn't compiles.

Has anybody a solution to find it? A tool like CppCheck (that checks for possible errors) or Visual Studio extension?

Edit:

Does anybody know, how can i find all comparisons in my code with CppDepend/CQLinq syntax? It could also help me. CppDepend uses CLang but it continue to parse if it has parsing errors.


Solution

  • My solution is: (as @M.M said) wrapping pointers with a template wrapper class, that implements operator overloads like -> * (so less compile problems) and deletes compare operators like == != (so find the comparisons with compile errors). Replacing all pointers can be done with the regular expression. (A * with A_Wrapper)

    What i also found is, using a pointer in a map is like a pointer comparison. If you use pointers in a map, you should also delete <-Operator in your wrapper class.

    Of course, i had compile errors, but those errors were not difficult to solve. And it seems that is certain solution.

    I hope that helps somebody.