Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / subtlezeroinit.cxx
blob5e98bfcccc59ad72dbf36d2b44cb003ba12a3278
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 #include "plugin.hxx"
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.
18 namespace {
20 class SubtleZeroInit final:
21 public RecursiveASTVisitor<SubtleZeroInit>, public loplugin::Plugin
23 public:
24 explicit SubtleZeroInit(loplugin::InstantiationData const & data):
25 Plugin(data) {}
27 bool VisitCXXNewExpr(CXXNewExpr const * expr) {
28 if (ignoreLocation(expr)) {
29 return true;
31 auto ce = expr->getConstructExpr();
32 if (ce == nullptr) {
33 return true;
35 if (!ce->requiresZeroInitialization()) {
36 return true;
38 report(
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)"),
43 expr->getExprLoc())
44 << ce->getType() << expr->getSourceRange();
45 return true;
48 private:
49 void run() override {
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: */