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/.
12 // Find occurrences of 'new T()' where the instance is zero-initialized upfront
13 // since C++11. For one, in many cases this may be unnecessary and unintended,
14 // as the code was written before C++11. For another, the zero-initialization
15 // would go away when T gets a user-provided default constructor, for example,
16 // so better make any necessary initialization more explicit in the code.
20 class SubtleZeroInit final
:
21 public RecursiveASTVisitor
<SubtleZeroInit
>, public loplugin::Plugin
24 explicit SubtleZeroInit(loplugin::InstantiationData
const & data
):
27 bool VisitCXXNewExpr(CXXNewExpr
const * expr
) {
28 if (ignoreLocation(expr
)) {
31 auto ce
= expr
->getConstructExpr();
35 if (!ce
->requiresZeroInitialization()) {
39 DiagnosticsEngine::Warning
,
40 ("if zero-initialization of %0 is intentional here, better make"
41 " that more explicit (e.g., assigning to members, default"
42 " constructor, default member initializers, std::memset)"),
44 << ce
->getType() << expr
->getSourceRange();
50 if (compiler
.getLangOpts().CPlusPlus
) {
51 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
56 static loplugin::Plugin::Registration
<SubtleZeroInit
> reg("subtlezeroinit");
60 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */