Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / redundantpointerops.cxx
blob0340b7c12c19debfd89f567491df7beb3350bdf4
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 <cassert>
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <set>
16 #include <clang/AST/CXXInheritance.h>
17 #include "compat.hxx"
18 #include "plugin.hxx"
20 /**
21 * Look for:
22 * (&x)->y
23 * which can be transformed to:
24 * x.y
25 * And
26 * &*x
27 * which can be:
28 * x
30 * @TODO
31 * (*x).y
32 * which can be:
33 * x->y
36 namespace {
38 class RedundantPointerOps:
39 public RecursiveASTVisitor<RedundantPointerOps>, public loplugin::Plugin
41 public:
42 explicit RedundantPointerOps(loplugin::InstantiationData const & data): Plugin(data) {}
44 virtual void run() override
46 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
49 bool VisitFunctionDecl(FunctionDecl const *);
50 bool VisitMemberExpr(MemberExpr const *);
51 bool VisitUnaryOperator(UnaryOperator const *);
54 bool RedundantPointerOps::VisitFunctionDecl(FunctionDecl const * functionDecl)
56 if (ignoreLocation(functionDecl))
57 return true;
58 //functionDecl->dump();
59 return true;
62 bool RedundantPointerOps::VisitMemberExpr(MemberExpr const * memberExpr)
64 if (ignoreLocation(memberExpr))
65 return true;
66 if (memberExpr->getLocStart().isMacroID())
67 return true;
68 auto base = memberExpr->getBase()->IgnoreParenImpCasts();
69 //parentStmt(parentStmt(memberExpr))->dump();
70 if (memberExpr->isArrow())
72 if (auto unaryOp = dyn_cast<UnaryOperator>(base))
74 if (unaryOp->getOpcode() == UO_AddrOf)
75 report(
76 DiagnosticsEngine::Warning, "'&' followed by '->', rather use '.'",
77 memberExpr->getLocStart())
78 << memberExpr->getSourceRange();
81 else if (auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(base))
83 if (operatorCallExpr->getOperator() == OO_Amp)
84 report(
85 DiagnosticsEngine::Warning, "'&' followed by '->', rather use '.'",
86 memberExpr->getLocStart())
87 << memberExpr->getSourceRange();
91 // else
92 // {
93 // if (auto unaryOp = dyn_cast<UnaryOperator>(base))
94 // {
95 // if (unaryOp->getOpcode() == UO_Deref)
96 // report(
97 // DiagnosticsEngine::Warning, "'*' followed by '.', rather use '->'",
98 // memberExpr->getLocStart())
99 // << memberExpr->getSourceRange();
101 // }
102 // }
103 return true;
106 bool RedundantPointerOps::VisitUnaryOperator(UnaryOperator const * unaryOperator)
108 if (ignoreLocation(unaryOperator))
109 return true;
110 if (unaryOperator->getLocStart().isMacroID())
111 return true;
112 if (unaryOperator->getOpcode() != UO_Deref)
113 return true;
114 auto innerOp = dyn_cast<UnaryOperator>(unaryOperator->getSubExpr()->IgnoreParenImpCasts());
115 if (!innerOp || innerOp->getOpcode() != UO_AddrOf)
116 return true;
118 report(
119 DiagnosticsEngine::Warning, "'&' followed by '*', rather use '.'",
120 unaryOperator->getLocStart())
121 << unaryOperator->getSourceRange();
122 return true;
125 loplugin::Plugin::Registration< RedundantPointerOps > X("redundantpointerops");
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */