Skip to main content
gamedev.net
🔒 Locked

C++ Validation Tools?

Started by MENTAL 12 replies 2.2k views
Original Post
MENTAL
MENTAL
Does anyone know of any good (and free) C++ source code validation programs, that can scan a load of source files and give reports on the following: 1) Unused functions/classes (i.e. static code coverage). 2) Missing pure-virtual function implementations. 3) Err... that's it :). (2) is probably the most important at the moment (just fixed a few nasty errors due to forgetfulness), and 1) is just to make sure I don't write code I never use. thanks in advance
chollida1
chollida1
what's a "missing pure virtual function implementation"??

The compiler wont' let you instantiate a pure virtual class so if your code compiles aren't you always are guarenteed to have a concrete version of any method you need to call???

As for one. vs.net's linker in release mode will let you know about any unused functions it can discard.

If you can afford, and wait for, vs.net then you may be inluck as it has tools for 1) built in!!

Cheers
Chris
CheersChris
MENTAL
MENTAL
by missing pure virtual function, I mean a derived class that has failed to implement a pure virtual function in it's base class. As I am using an abstract factory and only accessing the classes through a pointer to the base class, calling the missing function will throw a nasty run-time exception.
CraZeE
CraZeE
afaik, if u use pure virtual functions.. a derived class MUST provide an implementation, else the compiler will give you an error. You theoretically never get to a runtime error, unless the implementation itself is erroneous. That's a whole different problem altogether :P
To learn, we share... Give some to take some -
MENTAL
MENTAL
yes I know the derived class must provide an implementation - what I was looking for was a tool that would alert me when I have forgotten to write one :).

An example:
class Base{public:    virtual void SomeFunc = 0;};class Derived : public Base{};Base *base = new Derived;base->SomeFunc ();    <--- runtime error.
Squirm
Squirm
new Derived; is a compile time error. The tool which detects this error is the compiler. If your compiler doesn't detect this error, get a real compiler! (VC, gcc, cw ...)

edit1: unless your base class is in a separate library to your derived class, in which case I would expect a link time error to an undefined v-table.

edit2: what compiler are you using, anyway?
c2_0
c2_0
The compiler will tell you... it will give you an error on trying to instantiate 'new Derived'.

The only case you'd be able to call a pure virtual function would be in constructors and destructors (since the state of the object is that of the type you're contructing/deconstructing). The compiler would probably give you a warning or even error here as well, unless you call through some proxy function.
blizzard999
blizzard999
You can get a "pure virtual functions not implemented run time error" if you call them in the "wrong place".
The wrong place can be the base class ctor; in this case your derived class will not override the function and you get a null pointer!
This is the standard C++ behaviour (in fact when you enter the base ctor the derived part is not created yet)
I dont know if are there compilers that stop you to do this; VC6 compiler does not report any error for example.
But this is an error equal to use a pointer without initialize it!!!
chollida1
chollida1
Quote:
Original post by MENTAL
yes I know the derived class must provide an implementation - what I was looking for was a tool that would alert me when I have forgotten to write one :).

An example:
*** Source Snippet Removed ***


Ahh MENTAL, my vs.net compiler gives an error

C2259 'Derived': cannot instantiate abstract class.


Do an msdn search for C2259. Compilers might be smarter than you think:)

Cheers
Chris
CheersChris
smart_idiot
smart_idiot
Quote:
Original post by blizzard999
You can get a "pure virtual functions not implemented run time error" if you call them in the "wrong place".
The wrong place can be the base class ctor; in this case your derived class will not override the function and you get a null pointer!


You can't expect virtual funtions to work in a constructor anyway. Since the base class has it's constructor called before the derived classes, you're basically trying to use functions on an object that doesn't even exist yet.

#include <iostream>struct Base {  Base() { doSomething(); }  virtual void doSomething() const = 0; };struct Derived: public Base {  virtual void doSomething() const {} };int main() {  Derived foo; }


g++ was nice enough to give me this error:
foo.cpp: In constructor `Base::Base()':foo.cpp:5: error: abstract virtual `virtual void Base::doSomething() const'   called from constructor

Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
antareus
antareus
I've hit this error before, but I cannot recall the specifics. IIRC, I had the base class in a DLL that was outdated.

Getting back on topic, I don't know of many. C++ is a complex language to parse, unfortunately, so you don't see very much in the way of lint-like tools, for instance.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
blizzard999
blizzard999
Quote:
Original post by smart_idiot
g++ was nice enough to give me this error:
foo.cpp: In constructor `Base::Base()':foo.cpp:5: error: abstract virtual `virtual void Base::doSomething() const'   called from constructor



No doubt about gcc is a good compiler. [smile]
The same is in the dtor in which the derived part of the object is already destroyed.

dalleboy
dalleboy
Ok, back to topic:

Gimpel: PC-lint, a static code analysis software for C++, not free though, but pretty cheap ($239 a pop). Not perhaps the 'bestest' either.

Parasoft: CodeWizard is a very nice piece of software. With customizable coding guidelines. More expensive (don't know the exact figure) than PC-lint, but you don't get all the noice.

502 standard coding rules, including:
* 26 Effective C++ Items by Scott Meyers
* 7 More Effective C++ Items 26 by Scott Meyers
* 22 Built-in MISRA Guidelines
* 2 Meyers-Klaus Items
* 36 Universal Coding Standard Items
* 194 User Items
* 88 Ellemtel (Telecom Industry) Items
* 127 User MISRA Guidelines
MENTAL
MENTAL
ignore this thread - you don't want to know what I was doing to get those PVC errors :).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.