Files
Stephan Bergmann 060c1b69ef Adapt compilerplugins to recent LLVM 22 trunk change
...<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
2025-08-17 19:42:56 +02:00

135 lines
4.8 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/.
*/
#include <string>
#include <iostream>
#include "plugin.hxx"
#include "check.hxx"
#include "compat.hxx"
#include "clang/AST/CXXInheritance.h"
// Check for calls to OutputDevice methods that are not passing through RenderContext
namespace
{
class RenderContext:
public loplugin::FilteringPlugin<RenderContext>
{
public:
explicit RenderContext(loplugin::InstantiationData const & data):
FilteringPlugin(data) {}
virtual void run() override {
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool TraverseFunctionDecl(const FunctionDecl * decl);
bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *);
private:
bool mbChecking = false;
};
// We use Traverse to set a flag so we can easily ignore certain method calls
bool RenderContext::TraverseFunctionDecl(const FunctionDecl * pFunctionDecl)
{
if (ignoreLocation(pFunctionDecl)) {
return true;
}
if (!pFunctionDecl->hasBody()) {
return true;
}
if ( pFunctionDecl != pFunctionDecl->getCanonicalDecl() ) {
return true;
}
// Ignore methods inside the OutputDevice class
const CXXMethodDecl *pCXXMethodDecl = dyn_cast<CXXMethodDecl>(pFunctionDecl);
if (pCXXMethodDecl) {
if (loplugin::TypeCheck(compat::getCanonicalTagType(compiler.getASTContext(), pCXXMethodDecl->getParent())).Class("OutputDevice").GlobalNamespace())
return true;
}
// we are only currently interested in methods where the first parameter is RenderContext
if (pFunctionDecl->getNumParams() == 0)
return true;
if ( loplugin::TypeCheck(pFunctionDecl->getParamDecl( 0 )->getType()).Class("RenderContext").GlobalNamespace() ) {
return true;
}
mbChecking = true;
TraverseStmt(pFunctionDecl->getBody());
mbChecking = false;
return true;
}
bool RenderContext::VisitCXXMemberCallExpr(const CXXMemberCallExpr* pCXXMemberCallExpr)
{
if (!mbChecking)
return true;
if (ignoreLocation(pCXXMemberCallExpr)) {
return true;
}
const CXXRecordDecl *pCXXRecordDecl = pCXXMemberCallExpr->getRecordDecl();
if (!loplugin::TypeCheck(compat::getCanonicalTagType(compiler.getASTContext(), pCXXRecordDecl)).Class("OutputDevice").GlobalNamespace()) {
return true;
}
// ignore a handful of methods. They will most probably still be present in Window for use during processing outside of the Paint()
// method lifecycle
const CXXMethodDecl *pCXXMethodDecl = pCXXMemberCallExpr->getMethodDecl();
if (pCXXMethodDecl->isInstance()) {
StringRef name = pCXXMethodDecl->getName();
if (name == "LogicToPixel" || name == "GetMapMode" || name == "GetFontMetric" || name == "LogicToLogic"
|| name == "PixelToLogic" || name == "SetDigitLanguage")
{
return true;
}
}
// for calling through a pointer
const ImplicitCastExpr *pImplicitCastExpr = dyn_cast<ImplicitCastExpr>(pCXXMemberCallExpr->getImplicitObjectArgument());
if (pImplicitCastExpr) {
QualType aType = pImplicitCastExpr->getSubExpr()->getType();
if (aType->isPointerType())
aType = aType->getPointeeType();
std::string t2 = aType.getAsString();
if (t2 == "vcl::RenderContext" || t2 == "const vcl::RenderContext")
return true;
}
// for calling through a reference
const DeclRefExpr *pDeclRefExpr = dyn_cast<DeclRefExpr>(pCXXMemberCallExpr->getImplicitObjectArgument());
if (pDeclRefExpr) {
QualType aType = pDeclRefExpr->getType();
std::string t2 = aType.getAsString();
if (t2 == "vcl::RenderContext" || t2 == "const vcl::RenderContext")
return true;
}
// for calling through a chain of methods
const CXXMemberCallExpr *pMemberExpr = dyn_cast<CXXMemberCallExpr>(pCXXMemberCallExpr->getImplicitObjectArgument());
if (pMemberExpr) {
QualType aType = pMemberExpr->getType();
if (aType->isPointerType())
aType = aType->getPointeeType();
std::string t2 = aType.getAsString();
if (t2 == "vcl::RenderContext" || t2 == "const vcl::RenderContext")
return true;
}
report(
DiagnosticsEngine::Warning,
"Should be calling OutputDevice method through RenderContext.",
pCXXMemberCallExpr->getBeginLoc())
<< pCXXMemberCallExpr->getSourceRange();
return true;
}
loplugin::Plugin::Registration< RenderContext > X("rendercontext", false);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */