tdf#130857 qt weld: Support "Insert Breaks" dialog
[LibreOffice.git] / compilerplugins / clang / staticconstexpr.cxx
blob4985c6115e3f145645729ac97a979d1b7d6c1c0f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <cassert>
13 #include <stack>
15 #include "check.hxx"
16 #include "plugin.hxx"
17 #include "config_clang.h"
19 /**
20 In MSVC, non-static constexpr objects are initialized at run-time
22 So make sure that function-local vars are declared static.
25 namespace
27 class StaticConstexpr final : public loplugin::FilteringPlugin<StaticConstexpr>
29 public:
30 explicit StaticConstexpr(loplugin::InstantiationData const& data)
31 : FilteringPlugin(data)
35 bool VisitVarDecl(const VarDecl* varDecl)
37 if (ignoreLocation(varDecl))
38 return true;
39 if (!varDecl->isConstexpr())
40 return true;
41 if (!varDecl->isLocalVarDecl())
42 return true;
43 if (varDecl->isStaticLocal())
44 return true;
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())
49 return true;
51 if (varDecl->getType()->isBuiltinType() || varDecl->getType()->isEnumeralType())
52 return true;
53 // ignore the o3tl::getConversionMulDiv stuff
54 loplugin::TypeCheck tc(varDecl->getType());
55 if (tc.ClassOrStruct("pair").StdNamespace())
56 return true;
57 if (tc.Struct("m_and_d").Namespace("detail").Namespace("o3tl"))
58 return true;
59 if (tc.ClassOrStruct("TypedWhichId"))
60 return true;
61 if (tc.ClassOrStruct("Color"))
62 return true;
63 report(DiagnosticsEngine::Warning,
64 "function-local constexpr vars should be declared static", varDecl->getBeginLoc())
65 << varDecl->getSourceRange();
66 return true;
69 bool preRun() override
71 if (!compiler.getLangOpts().CPlusPlus)
72 return false;
73 return true;
76 private:
77 void run() override
79 if (preRun())
81 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
86 loplugin::Plugin::Registration<StaticConstexpr> staticconstexpr("staticconstexpr");
89 #endif
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */