[en_US] Added 13 autocorrect words
[LibreOffice.git] / compilerplugins / clang / selfinit.cxx
blob35ce37278f2a354a5050160dbeeaeb6797415150
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 <vector>
12 #include "plugin.hxx"
14 // Warn when a variable is referenced from its own initializer. This is not invalid in general (see
15 // C++17 [basic.life]), but is at least suspicious.
17 namespace
19 class SelfInit : public loplugin::FilteringPlugin<SelfInit>
21 public:
22 explicit SelfInit(loplugin::InstantiationData const& data)
23 : FilteringPlugin(data)
27 bool TraverseVarDecl(VarDecl* decl)
29 decls_.push_back({ decl, decl->getCanonicalDecl() });
30 auto const ret = FilteringPlugin::TraverseVarDecl(decl);
31 decls_.pop_back();
32 return ret;
35 bool TraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr* expr)
37 if (expr->getKind() == UETT_SizeOf)
39 return true;
41 return FilteringPlugin::TraverseUnaryExprOrTypeTraitExpr(expr);
44 bool TraverseCXXTypeidExpr(CXXTypeidExpr const*) { return true; }
46 bool TraverseCXXNoexceptExpr(CXXNoexceptExpr const*) { return true; }
48 bool TraverseDecltypeTypeLoc(DecltypeTypeLoc) { return true; }
50 bool VisitDeclRefExpr(DeclRefExpr const* expr)
52 if (ignoreLocation(expr))
54 return true;
56 for (auto const& i : decls_)
58 if (expr->getDecl()->getCanonicalDecl() == i.canonical)
60 report(
61 DiagnosticsEngine::Warning,
62 ("referencing a variable during its own initialization is error-prone and thus"
63 " suspicious"),
64 expr->getLocation())
65 << expr->getSourceRange();
66 report(DiagnosticsEngine::Note, "variable declared here", i.current->getLocation())
67 << i.current->getSourceRange();
70 return true;
73 private:
74 void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
76 struct Decl
78 VarDecl const* current;
79 VarDecl const* canonical;
82 std::vector<Decl> decls_;
85 loplugin::Plugin::Registration<SelfInit> X("selfinit");
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */