loplugin:unreffun: also warn about redundant redeclarations

Change-Id: I9a812220b58cf6da00d854e65794f7c673ab239d
This commit is contained in:
Stephan Bergmann
2014-06-27 15:25:46 +02:00
parent b05b970daa
commit e48a233960
31 changed files with 38 additions and 131 deletions

View File

@@ -45,6 +45,19 @@ bool hasCLanguageLinkageType(FunctionDecl const * decl) {
return false;
}
bool isFriendDecl(Decl const * decl) {
return decl->getFriendObjectKind() != Decl::FOK_None;
}
Decl const * getPreviousNonFriendDecl(Decl const * decl) {
for (;;) {
decl = decl->getPreviousDecl();
if (decl == nullptr || !isFriendDecl(decl)) {
return decl;
}
}
}
class UnrefFun: public RecursiveASTVisitor<UnrefFun>, public loplugin::Plugin {
public:
explicit UnrefFun(InstantiationData const & data): Plugin(data) {}
@@ -69,6 +82,26 @@ bool UnrefFun::VisitFunctionDecl(FunctionDecl const * decl) {
return true;
}
if (!(decl->isThisDeclarationADefinition() || isFriendDecl(decl)
|| decl->isFunctionTemplateSpecialization()))
{
Decl const * prev = getPreviousNonFriendDecl(decl);
if (prev != nullptr/* && prev != decl->getPrimaryTemplate()*/) {
report(
DiagnosticsEngine::Warning,
"redundant function%0 redeclaration", decl->getLocation())
<< ((decl->getTemplatedKind()
== FunctionDecl::TK_FunctionTemplate)
? " template" : "")
<< decl->getSourceRange();
report(
DiagnosticsEngine::Note, "previous declaration is here",
prev->getLocation())
<< prev->getSourceRange();
return true;
}
}
FunctionDecl const * canon = decl->getCanonicalDecl();
//TODO: is that the first?
if (canon->isDeleted() || canon->isReferenced()