Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / finalprotected.cxx
blob71b3cfca03f18de33781a82cc6b36efe6106bc57
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <string>
11 #include <iostream>
12 #include <map>
13 #include <set>
15 #include "plugin.hxx"
16 #include "clang/AST/CXXInheritance.h"
18 // Check for final classes that have protected members
20 namespace
23 class FinalProtected:
24 public RecursiveASTVisitor<FinalProtected>, public loplugin::Plugin
26 public:
27 explicit FinalProtected(loplugin::InstantiationData const & data):
28 Plugin(data) {}
30 virtual void run() override {
31 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
34 bool VisitCXXMethodDecl(CXXMethodDecl const *);
35 bool VisitFieldDecl(FieldDecl const *);
39 bool FinalProtected::VisitCXXMethodDecl(CXXMethodDecl const * cxxMethodDecl)
41 if (ignoreLocation(cxxMethodDecl)) {
42 return true;
44 if (cxxMethodDecl->getAccess() != AS_protected) {
45 return true;
47 if (!cxxMethodDecl->getParent()->hasAttr<FinalAttr>()) {
48 return true;
50 cxxMethodDecl = cxxMethodDecl->getCanonicalDecl();
51 report(DiagnosticsEngine::Warning,
52 "final class should not have protected members - convert them to private",
53 cxxMethodDecl->getLocStart())
54 << cxxMethodDecl->getSourceRange();
55 return true;
58 bool FinalProtected::VisitFieldDecl(FieldDecl const * fieldDecl)
60 if (ignoreLocation(fieldDecl)) {
61 return true;
63 if (fieldDecl->getAccess() != AS_protected) {
64 return true;
66 if (!fieldDecl->getParent()->hasAttr<FinalAttr>()) {
67 return true;
69 fieldDecl = fieldDecl->getCanonicalDecl();
70 report(DiagnosticsEngine::Warning,
71 "final class should not have protected members - convert them to private",
72 fieldDecl->getLocStart())
73 << fieldDecl->getSourceRange();
74 return true;
77 loplugin::Plugin::Registration< FinalProtected > X("finalprotected", true);
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */