1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
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
20 class TypeidComparison final
: public loplugin::FilteringPlugin
<TypeidComparison
>
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
; }
37 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
41 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr
const* expr
)
43 if (ignoreLocation(expr
))
47 auto const op
= expr
->getOperator();
48 if (op
!= OO_EqualEqual
&& op
!= OO_ExclaimEqual
)
52 assert(expr
->getNumArgs() == 2);
53 auto const e1
= dyn_cast
<CXXTypeidExpr
>(expr
->getArg(0)->IgnoreParenImpCasts());
58 auto const e2
= dyn_cast
<CXXTypeidExpr
>(expr
->getArg(1)->IgnoreParenImpCasts());
63 auto const t1
= getOperandType(e1
);
64 auto const t2
= getOperandType(e2
);
65 if (t1
->isPointerType() == t2
->isPointerType())
69 report(DiagnosticsEngine::Warning
,
70 "comparison of type info of mixed pointer and non-pointer types %0 and %1 can never "
73 << t1
<< t2
<< expr
->getSourceRange();
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");
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */