Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need -Woverloaded-virtual To Catch Inheritance Errors #759

Closed
llvmbot opened this issue Jun 24, 2004 · 11 comments
Closed

Need -Woverloaded-virtual To Catch Inheritance Errors #759

llvmbot opened this issue Jun 24, 2004 · 11 comments
Labels
bugzilla Issues migrated from bugzilla code-cleanup

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 24, 2004

Bugzilla Link 387
Resolution FIXED
Resolved on Feb 22, 2010 12:41
Version 1.0
OS All
Attachments List Of 158 Overload Hiding Instances
Reporter LLVM Bugzilla Contributor
CC @lattner

Extended Description

We need to start using the -Woverloaded-virtual option in the makefiles to catch
some inheritance bugs. Patches for this bug should first rid all of LLVM of
warnings produced by -Woverloaded-virtual option and then patch the makefiles to
turn the option on.

This idea submitted by Vladimir Prus where he made the following comments on
LLVMdev list:

I've just had some fun, because I wanted to override
FunctionPass::addAnalysisUsage, but forgot "const" after the method name --
so instead of overriding I've just created a new unrelated method.

After spending some time on this, I've decided to add -Woverloaded-virtual
option to compiler to catch such cases. However, it also gives some warnings
on LLVM code:

../../../include/llvm/Pass.h:264: warning: virtual bool llvm::FunctionPass::run(llvm::Module&)' was hidden ../../../include/llvm/Pass.h:326: warning: by bool
llvm::BasicBlockPass::run(llvm::BasicBlock&)'
../../../include/llvm/Pass.h:275: warning: virtual void llvm::FunctionPass::addToPassManager(llvm::PassManagerT<llvm::Module>*, llvm::AnalysisUsage&)' was hidden ../../../include/llvm/Pass.h:332: warning: by virtual void
llvm::BasicBlockPass::addToPassManager(llvm::PassManagerTllvm::BasicBlock*,
llvm::AnalysisUsage&)'

The problem is that "run" method is virtual in Path (with Module& as
argument), but another version (non-virtual), which takes BasicBlock& is
defined in BasicBlockPath.

Do you think this warning is worth fixing? One possible approach is to rename
virtual run(Module&) to runOnModule, but that would require updating all
derived classes :-(

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jun 30, 2004

The attached file was generated from my work on the bytecode analyzer which has
a large number of virtual functions. Several output bugs were caused by
interface changes that caused unintentional overloading to occur. To combat
this, I turned on -Woverloaded-virtual to detect the errors. The attachment
shows all the overloading as of the date of the file. There is likely some bugs
lurking in there.

@lattner
Copy link
Collaborator

lattner commented Jun 30, 2004

Okay, well the first biggest step is to rectify the ones in the Pass class:

virtual void print(std::ostream &O, const Module *M) const { print(O); }
virtual void print(std::ostream &O) const;

There is no reason for the second one. We should just change any who implement
it to implement the first one.

-Chris

@llvmbot
Copy link
Collaborator Author

llvmbot commented Sep 2, 2004

Mine

@llvmbot
Copy link
Collaborator Author

llvmbot commented Nov 25, 2004

Schedule for 1.5

@llvmbot
Copy link
Collaborator Author

llvmbot commented Dec 7, 2004

This patch fixes 1/2 of the warnings, those coming from utils/TableGen/Record.h:

http://mail.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20041206/021811.html

@llvmbot
Copy link
Collaborator Author

llvmbot commented Dec 7, 2004

At this point, the only remaining problem is:

/proj/work/llvm/build/../llvm/include/llvm/Pass.h:292: warning: virtual void llvm::FunctionPass::addToPassManager(llvm::PassManagerT<llvm::Module>*, llvm::AnalysisUsage&)' was hidden /proj/work/llvm/build/../llvm/include/llvm/Pass.h:350: warning: by virtual
void llvm::BasicBlockPass::addToPassManager(llvm::PassManagerTllvm::Function*,
llvm::AnalysisUsage&)'

However, my attempts to fix this one have not succeeded. Doing the obvious thing
and making both overloadings available in both classes doesn't help. The
problem comes with PassManagerT and what kinds of things can call "addPass" on it.

Perhaps someone who's cozy and intimate with the PassManager can take a look at
this?

@llvmbot
Copy link
Collaborator Author

llvmbot commented Dec 9, 2004

A recitation of the problem:

The -Woverloaded-virtual flag is in GCC for a really good reason; becase the
language requires that when a sub-class does not provide ALL overloadings of a
virtual function, that the base class versions are completely hidden from the
subclass. If you have this:

class X { pubic: virtual void nada(int); };
class Y : public X { public: virtual void nada(double); };

then there is no way to call nada(int) from a Y*. The language requires
that nada(double) "hides" X::nada(int). The -Woverloaded-virtual option exposes
these situations and this bug is about making LLVM compile without warning when
-Woverloaded-virtual is used.

Using our example above, there are two ways to solve this problem:
(a) change Y::nada to a new name or
(b) change X to have a nada(double) method.

Unfortunately, the patches so far have chosen (b) as the solution so that the
method names don't have to change and there is no need to visit all the call
sites. This is unfortunate because method (b) is leading to a bloated API for
LLVM and requiring "not implemented" implementations of super-class methods.

Consequently, we're going to switch to option (a) for solution. Its more
changes, but will lead to a better API in the end run.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Dec 9, 2004

Note that there's yet another method: add

using X::nada;

in the declaration of class Y. This is actually pretty clean approach.

However, it does not work when X::nada is private, as is the case with those
AddToPassManager functions.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Aug 28, 2006

With the recent modifications to the PassManager and a few other places, this
bug is (finally!) resolved. We now generate warnings on overloaded virtuals by
turning on the -Woverloaded-virtual option. If you get this warning, please
resolve the overload before committing the code.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla code-cleanup
Projects
None yet
Development

No branches or pull requests

2 participants