1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
17 #include "config_clang.h"
20 In MSVC, non-static constexpr objects are initialized at run-time
22 So make sure that function-local vars are declared static.
27 class StaticConstexpr final
: public loplugin::FilteringPlugin
<StaticConstexpr
>
30 explicit StaticConstexpr(loplugin::InstantiationData
const& data
)
31 : FilteringPlugin(data
)
35 bool VisitVarDecl(const VarDecl
* varDecl
)
37 if (ignoreLocation(varDecl
))
39 if (!varDecl
->isConstexpr())
41 if (!varDecl
->isLocalVarDecl())
43 if (varDecl
->isStaticLocal())
45 if (auto const functionDecl
= dyn_cast_or_null
<FunctionDecl
>(varDecl
->getDeclContext()))
47 // cannot convert these, definition of a static variable in a constexpr function is a C++23 extension
48 if (functionDecl
->isConstexpr())
51 if (varDecl
->getType()->isBuiltinType() || varDecl
->getType()->isEnumeralType())
53 // ignore the o3tl::getConversionMulDiv stuff
54 loplugin::TypeCheck
tc(varDecl
->getType());
55 if (tc
.ClassOrStruct("pair").StdNamespace())
57 if (tc
.Struct("m_and_d").Namespace("detail").Namespace("o3tl"))
59 if (tc
.ClassOrStruct("TypedWhichId"))
61 if (tc
.ClassOrStruct("Color"))
63 report(DiagnosticsEngine::Warning
,
64 "function-local constexpr vars should be declared static", varDecl
->getBeginLoc())
65 << varDecl
->getSourceRange();
69 bool preRun() override
71 if (!compiler
.getLangOpts().CPlusPlus
)
81 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
86 loplugin::Plugin::Registration
<StaticConstexpr
> staticconstexpr("staticconstexpr");
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */