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
14 // Find occurrences of 'new T()' where the instance is zero-initialized upfront
15 // since C++11. For one, in many cases this may be unnecessary and unintended,
16 // as the code was written before C++11. For another, the zero-initialization
17 // would go away when T gets a user-provided default constructor, for example,
18 // so better make any necessary initialization more explicit in the code.
22 class SubtleZeroInit final
:
23 public loplugin::FilteringPlugin
<SubtleZeroInit
>
26 explicit SubtleZeroInit(loplugin::InstantiationData
const & data
):
27 FilteringPlugin(data
) {}
29 bool VisitCXXNewExpr(CXXNewExpr
const * expr
) {
30 if (ignoreLocation(expr
)) {
33 auto ce
= expr
->getConstructExpr();
37 if (!ce
->requiresZeroInitialization()) {
41 DiagnosticsEngine::Warning
,
42 ("if zero-initialization of %0 is intentional here, better make"
43 " that more explicit (e.g., assigning to members, default"
44 " constructor, default member initializers, std::memset)"),
46 << ce
->getType() << expr
->getSourceRange();
50 virtual bool preRun() override
{
51 return compiler
.getLangOpts().CPlusPlus
;
54 virtual void run() override
{
56 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
61 static loplugin::Plugin::Registration
<SubtleZeroInit
> subtlezeroinit("subtlezeroinit");
65 #endif // LO_CLANG_SHARED_PLUGINS
67 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */