/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// Find uses of dynamic_cast that cast between unrelated classes, which is suspicious and might // indicate a bug. The heuristic used to consider two classes unrelated is that neither derives // from the other (directly or indirectly) and they do not both virtually derive (directly or // indirectly) from a common third class. Additionally, class definitions can be attributed with // SAL_LOPLUGIN_ANNOTATE("crosscast") (from sal/types.h) to suppress false warnings about known-good // cases casting from or to such a class.
#ifndef LO_CLANG_SHARED_PLUGINS
#include <cassert> #include <set>
#include"compat.hxx" #include"plugin.hxx"
namespace
{ void computeVirtualBases(CXXRecordDecl const* decl, std::set<CXXRecordDecl const*>* vbases)
{
assert(vbases != nullptr); for (autoconst& i : decl->bases())
{ autoconst d = i.getType()->getAsCXXRecordDecl(); if (d == nullptr)
{ continue;
} if (i.isVirtual())
{ if (!vbases->insert(d->getCanonicalDecl()).second)
{ // As we track the already computed virtual bases in vbases anyway, we can cheaply // optimize the case where we see a virtual base again, even though we don't bother // to optimize the case where we see a non-virtual base multiple times: continue;
}
}
computeVirtualBases(d, vbases);
}
}
bool compareVirtualBases(CXXRecordDecl const* decl, std::set<CXXRecordDecl const*>& vbases)
{ for (autoconst& i : decl->bases())
{ autoconst d = i.getType()->getAsCXXRecordDecl(); if (d == nullptr)
{ continue;
} if (i.isVirtual() && vbases.count(d->getCanonicalDecl()) == 1)
{ returntrue;
} if (compareVirtualBases(d, vbases))
{ returntrue;
}
} returnfalse;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.