...<https://github.com/llvm/llvm-project/commit/91cdd35008e9ab32dffb7e401cdd7313b3461892> "[clang] Improve nested name specifier AST representation (#147835)". For one, clang::TypeDecl::getTypeForDecl no longer works for TagDecls, so the loplugin::TypeCheck ctor taking a TypeDecl started to fail at runtime. So heed the advice given in the comment to clang::TypeDecl::getTypeForDecl and instead "ckeck out ASTContext::getTypeDeclType or one of...", which means that we now need to transport a clang::ASTContext to all those places that want to create such a loplugin::TypeCheck. For another, clang::TagType::getDecl ("renamed to `getOriginalDecl`" for now) no longer returns one dedicated Decl per type (so its results could be compared for equality), so wrap that in compat::getDecl to do an additional getCanonicalDecl. (But note how that requires an additional compat::getDefinitionOrSelf across the various loplugins' isWarnUnusedType implementations, to check for any attributes on the actual definitions rather than on whatever first declarations happen to be considered the canonical decls.) And the visitor's TraverseDecltypeTypeLoc takes an additional bool TraverseQualifier parameter now, so adapt the sharedvisitor machinery accordingly. Plus some bespoke fixes across various loplugins. loplugin:indentation is hit by <https://github.com/llvm/llvm-project/issues/153540> "Clang getBeginLoc of some CXXConstructExpr has moved past namespace prefix" (which would cause false > core/dbaccess/source/core/recovery/subcomponentrecovery.cxx:546:18: error: statement mis-aligned compared to neighbours [loplugin:indentation] > 546 | rtl::Reference( new SubComponentLoader( xController, xDocDefinition ) ); > | ^ > core/dbaccess/source/core/recovery/subcomponentrecovery.cxx:545:13: note: measured against this one [loplugin:indentation] > 545 | Reference< XController > xController( m_xDocumentUI, UNO_QUERY_THROW ); > | ^ etc.), so is disabled for Clang 22 for now, until that bug is fixed. (Various diagnostics are slightly different now, leading to some more expected-error-re variation in the tests. And loplugin::refcounting no longer duplicates its warnings, which is probably a feature and not a bug.) Change-Id: I9fe152e47ee571ad57c7c57f0eaa9c10e620f03e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189694 Reviewed-by: Stephan Bergmann <stephan.bergmann@collabora.com> Tested-by: Jenkins
140 lines
3.6 KiB
C++
140 lines
3.6 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
/*
|
|
* 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/.
|
|
*/
|
|
#ifndef LO_CLANG_SHARED_PLUGINS
|
|
|
|
#include <algorithm>
|
|
|
|
#include "clang/AST/Attr.h"
|
|
#include "clang/AST/CXXInheritance.h"
|
|
|
|
#include "compat.hxx"
|
|
#include "plugin.hxx"
|
|
|
|
namespace {
|
|
|
|
// cf. Clang's clang::AST::CXXDynamicCastExpr::isAlwaysNull
|
|
// (lib/AST/ExprCXX.cpp):
|
|
bool isAlwaysNull(CXXDynamicCastExpr const * expr) {
|
|
QualType SrcType = expr->getSubExpr()->getType();
|
|
QualType DestType = expr->getType();
|
|
|
|
if (const clang::PointerType *SrcPTy = SrcType->getAs<clang::PointerType>()) {
|
|
SrcType = SrcPTy->getPointeeType();
|
|
#if 0
|
|
DestType = DestType->castAs<PointerType>()->getPointeeType();
|
|
#else
|
|
auto DstPTy = DestType->getAs<clang::PointerType>();
|
|
if (!DstPTy)
|
|
return false;
|
|
DestType = DstPTy->getPointeeType();
|
|
#endif
|
|
}
|
|
|
|
if (DestType->isVoidType())
|
|
return false;
|
|
|
|
#if 0
|
|
const CXXRecordDecl *SrcRD =
|
|
cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
|
|
#else
|
|
auto SrcRT = SrcType->getAs<RecordType>();
|
|
if (!SrcRT)
|
|
return false;
|
|
const CXXRecordDecl *SrcRD = cast<CXXRecordDecl>(compat::getDecl(SrcRT));
|
|
#endif
|
|
|
|
#if 0
|
|
if (!SrcRD->hasAttr<FinalAttr>())
|
|
return false;
|
|
#endif
|
|
|
|
#if 0
|
|
const CXXRecordDecl *DestRD =
|
|
cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
|
|
#else
|
|
auto DestRT = DestType->getAs<RecordType>();
|
|
if (!DestRT)
|
|
return false;
|
|
const CXXRecordDecl *DestRD = cast<CXXRecordDecl>(compat::getDecl(DestRT));
|
|
#endif
|
|
|
|
#if 1
|
|
if (!(SrcRD && DestRD))
|
|
return false;
|
|
|
|
if (DestRD->hasAttr<FinalAttr>()) {
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/true,
|
|
/*DetectVirtual=*/false);
|
|
if (DestRD->isDerivedFrom(SrcRD, Paths) &&
|
|
std::all_of(Paths.begin(), Paths.end(),
|
|
[](CXXBasePath const & Path) {
|
|
return Path.Access != AS_public; }))
|
|
return true;
|
|
}
|
|
|
|
if (!SrcRD->hasAttr<FinalAttr>())
|
|
return false;
|
|
#endif
|
|
|
|
#if 0
|
|
return !DestRD->isDerivedFrom(SrcRD);
|
|
#else
|
|
return !(DestRD->isDerivedFrom(SrcRD)
|
|
|| SrcRD->isDerivedFrom(DestRD)
|
|
|| SrcRD == DestRD);
|
|
#endif
|
|
}
|
|
|
|
class FailedDynCast:
|
|
public loplugin::FilteringPlugin<FailedDynCast>
|
|
{
|
|
public:
|
|
explicit FailedDynCast(loplugin::InstantiationData const & data):
|
|
FilteringPlugin(data) {}
|
|
|
|
bool shouldVisitTemplateInstantiations() const { return true; }
|
|
|
|
bool preRun() override;
|
|
void run() override;
|
|
|
|
bool VisitCXXDynamicCastExpr(CXXDynamicCastExpr const * expr);
|
|
};
|
|
|
|
bool FailedDynCast::preRun() {
|
|
return compiler.getLangOpts().CPlusPlus;
|
|
}
|
|
|
|
void FailedDynCast::run() {
|
|
if (preRun()) {
|
|
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
|
|
}
|
|
}
|
|
|
|
bool FailedDynCast::VisitCXXDynamicCastExpr(CXXDynamicCastExpr const * expr) {
|
|
if (ignoreLocation(expr)) {
|
|
return true;
|
|
}
|
|
if (isAlwaysNull(expr)) {
|
|
report(
|
|
DiagnosticsEngine::Warning,
|
|
"dynamic_cast from %0 to %1 always fails", expr->getBeginLoc())
|
|
<< expr->getSubExpr()->getType() << expr->getType()
|
|
<< expr->getSourceRange();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
loplugin::Plugin::Registration<FailedDynCast> faileddyncast("faileddyncast");
|
|
|
|
} // namespace
|
|
|
|
#endif // LO_CLANG_SHARED_PLUGINS
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|