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

128 lines
3.5 KiB
C++

/* -*- 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/.
*/
#ifndef LO_CLANG_SHARED_PLUGINS
#include <vector>
#include "config_clang.h"
#include "plugin.hxx"
// Warn when a variable is referenced from its own initializer. This is not invalid in general (see
// C++17 [basic.life]), but is at least suspicious.
namespace
{
class SelfInit : public loplugin::FilteringPlugin<SelfInit>
{
public:
explicit SelfInit(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
bool PreTraverseVarDecl(VarDecl* decl)
{
decls_.push_back({ decl, decl->getCanonicalDecl() });
return true;
}
bool PostTraverseVarDecl(VarDecl*, bool)
{
decls_.pop_back();
return true;
}
bool TraverseVarDecl(VarDecl* decl)
{
PreTraverseVarDecl(decl);
auto const ret = FilteringPlugin::TraverseVarDecl(decl);
PostTraverseVarDecl(decl, ret);
return ret;
}
bool PreTraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr* expr)
{
if (expr->getKind() == UETT_SizeOf)
return false;
return true;
}
bool TraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr* expr)
{
if (PreTraverseUnaryExprOrTypeTraitExpr(expr))
return FilteringPlugin::TraverseUnaryExprOrTypeTraitExpr(expr);
return true;
}
bool TraverseCXXTypeidExpr(CXXTypeidExpr*) { return true; }
bool PreTraverseCXXTypeidExpr(CXXTypeidExpr*) { return false; }
bool TraverseCXXNoexceptExpr(CXXNoexceptExpr*) { return true; }
bool PreTraverseCXXNoexceptExpr(CXXNoexceptExpr*) { return false; }
bool TraverseDecltypeTypeLoc(DecltypeTypeLoc
#if CLANG_VERSION >= 220000
,
bool
#endif
)
{
return true;
}
bool PreTraverseDecltypeTypeLoc(DecltypeTypeLoc
#if CLANG_VERSION >= 220000
,
bool
#endif
)
{
return false;
}
bool VisitDeclRefExpr(DeclRefExpr const* expr)
{
if (ignoreLocation(expr))
{
return true;
}
for (auto const& i : decls_)
{
if (expr->getDecl()->getCanonicalDecl() == i.canonical)
{
report(
DiagnosticsEngine::Warning,
("referencing a variable during its own initialization is error-prone and thus"
" suspicious"),
expr->getLocation())
<< expr->getSourceRange();
report(DiagnosticsEngine::Note, "variable declared here", i.current->getLocation())
<< i.current->getSourceRange();
}
}
return true;
}
private:
void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
struct Decl
{
VarDecl const* current;
VarDecl const* canonical;
};
std::vector<Decl> decls_;
};
loplugin::Plugin::Registration<SelfInit> selfinit("selfinit");
}
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */