Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / typeidcomparison.cxx
blob359231f262e61bbf6d1d33a318813e61eed2fce6
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 // Find (in-)equality comparisons between typeid expressions that can never succeed. For now, just
11 // detects cases where the two involved types are structurally different, one a pointer type and the
12 // other a non-pointer type.
14 #ifndef LO_CLANG_SHARED_PLUGINS
16 #include "plugin.hxx"
18 namespace
20 class TypeidComparison final : public loplugin::FilteringPlugin<TypeidComparison>
22 public:
23 explicit TypeidComparison(loplugin::InstantiationData const& data)
24 : FilteringPlugin(data)
28 // For CXXRewrittenBinaryOperator `typeid(...) != typeid(...)`:
29 bool shouldVisitImplicitCode() const { return true; }
31 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
33 void run() override
35 if (preRun())
37 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
41 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* expr)
43 if (ignoreLocation(expr))
45 return true;
47 auto const op = expr->getOperator();
48 if (op != OO_EqualEqual && op != OO_ExclaimEqual)
50 return true;
52 assert(expr->getNumArgs() == 2);
53 auto const e1 = dyn_cast<CXXTypeidExpr>(expr->getArg(0)->IgnoreParenImpCasts());
54 if (e1 == nullptr)
56 return true;
58 auto const e2 = dyn_cast<CXXTypeidExpr>(expr->getArg(1)->IgnoreParenImpCasts());
59 if (e2 == nullptr)
61 return true;
63 auto const t1 = getOperandType(e1);
64 auto const t2 = getOperandType(e2);
65 if (t1->isPointerType() == t2->isPointerType())
67 return true;
69 report(DiagnosticsEngine::Warning,
70 "comparison of type info of mixed pointer and non-pointer types %0 and %1 can never "
71 "succeed",
72 expr->getExprLoc())
73 << t1 << t2 << expr->getSourceRange();
74 return true;
77 private:
78 QualType getOperandType(CXXTypeidExpr const* expr)
80 return expr->isTypeOperand() ? expr->getTypeOperand(compiler.getASTContext())
81 : expr->getExprOperand()->getType();
85 static loplugin::Plugin::Registration<TypeidComparison> typeidcomparison("typeidcomparison");
88 #endif
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */