1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
16 // Like clang::Stmt::IgnoreImplicit (lib/AST/Stmt.cpp), but also looking through implicit
17 // UserDefinedConversion's member function call:
18 Expr
const * ignoreAllImplicit(Expr
const * expr
) {
19 if (auto const e
= dyn_cast
<ExprWithCleanups
>(expr
)) {
20 expr
= e
->getSubExpr();
22 if (auto const e
= dyn_cast
<MaterializeTemporaryExpr
>(expr
)) {
23 expr
= e
->GetTemporaryExpr();
25 if (auto const e
= dyn_cast
<CXXBindTemporaryExpr
>(expr
)) {
26 expr
= e
->getSubExpr();
28 while (auto const e
= dyn_cast
<ImplicitCastExpr
>(expr
)) {
29 expr
= e
->getSubExpr();
30 if (e
->getCastKind() == CK_UserDefinedConversion
) {
31 auto const ce
= cast
<CXXMemberCallExpr
>(expr
);
32 assert(ce
->getNumArgs() == 0);
33 expr
= ce
->getImplicitObjectArgument();
39 Expr
const * ignoreParenImpCastAndComma(Expr
const * expr
) {
41 expr
= expr
->IgnoreParenImpCasts();
42 auto e
= dyn_cast
<BinaryOperator
>(expr
);
43 if (e
== nullptr || e
->getOpcode() != BO_Comma
) {
50 Expr
const * getSubExprOfLogicalNegation(Expr
const * expr
) {
51 auto e
= dyn_cast
<UnaryOperator
>(ignoreParenImpCastAndComma(expr
));
52 return e
== nullptr || e
->getOpcode() != UO_LNot
53 ? nullptr : e
->getSubExpr();
56 enum class Value
{ Unknown
, False
, True
};
58 Value
getValue(Expr
const * expr
) {
59 expr
= ignoreParenImpCastAndComma(expr
);
60 if (expr
->getType()->isBooleanType()) {
61 // Instead going via Expr::isCXX11ConstantExpr would turn up exactly one
62 // additional place in svx/source/dialog/framelinkarray.cxx
64 // const bool DIAG_DBL_CLIP_DEFAULT = false;
66 // ... = mxImpl.get() ? mxImpl->mbDiagDblClip : DIAG_DBL_CLIP_DEFAULT;
68 // where it is unclear whether it is not actually better to consider
69 // DIAG_DBL_CLIP_DEFAULT a tunable parameter (and thus not to simplify):
70 auto lit
= dyn_cast
<CXXBoolLiteralExpr
>(expr
);
72 return lit
->getValue() ? Value::True
: Value::False
;
75 return Value::Unknown
;
79 public RecursiveASTVisitor
<SimplifyBool
>, public loplugin::Plugin
82 explicit SimplifyBool(loplugin::InstantiationData
const & data
):
87 bool VisitUnaryLNot(UnaryOperator
const * expr
);
89 bool VisitBinLT(BinaryOperator
const * expr
);
91 bool VisitBinGT(BinaryOperator
const * expr
);
93 bool VisitBinLE(BinaryOperator
const * expr
);
95 bool VisitBinGE(BinaryOperator
const * expr
);
97 bool VisitBinEQ(BinaryOperator
const * expr
);
99 bool VisitBinNE(BinaryOperator
const * expr
);
101 bool VisitConditionalOperator(ConditionalOperator
const * expr
);
104 void SimplifyBool::run() {
105 if (compiler
.getLangOpts().CPlusPlus
) {
106 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
110 bool SimplifyBool::VisitUnaryLNot(UnaryOperator
const * expr
) {
111 if (ignoreLocation(expr
)) {
114 auto e
= getSubExprOfLogicalNegation(expr
->getSubExpr());
116 // Ignore macros, otherwise
117 // OSL_ENSURE(!b, ...);
119 if (e
->getLocStart().isMacroID())
121 // double logical not of an int is an idiom to convert to bool
122 auto const sub
= ignoreAllImplicit(e
);
123 if (!sub
->getType()->isBooleanType())
126 DiagnosticsEngine::Warning
,
127 ("double logical negation expression of the form '!!A' (with A of type"
128 " %0) can %select{logically|literally}1 be simplified as 'A'"),
131 << sub
->getType()->isBooleanType()
132 << expr
->getSourceRange();
135 if (auto binaryOp
= dyn_cast
<BinaryOperator
>(expr
->getSubExpr()->IgnoreParenImpCasts())) {
136 // Ignore macros, otherwise
137 // OSL_ENSURE(!b, ...);
139 if (binaryOp
->getLocStart().isMacroID())
141 auto t
= binaryOp
->getLHS()->IgnoreImpCasts()->getType()->getUnqualifiedDesugaredType();
142 // RecordType would require more smarts - we'd need to verify that an inverted operator actually existed
143 if (t
->isTemplateTypeParmType() || t
->isRecordType() || t
->isDependentType())
145 // for floating point (with NaN) !(x<y) need not be equivalent to x>=y
146 if (t
->isFloatingType() ||
147 binaryOp
->getRHS()->IgnoreImpCasts()->getType()->getUnqualifiedDesugaredType()->isFloatingType())
149 if (!binaryOp
->isComparisonOp())
152 DiagnosticsEngine::Warning
,
153 ("logical negation of comparison operator, can be simplified by inverting operator"),
155 << expr
->getSourceRange();
160 bool SimplifyBool::VisitBinLT(BinaryOperator
const * expr
) {
161 if (ignoreLocation(expr
)) {
164 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
165 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
169 auto v1
= getValue(expr
->getLHS());
170 auto v2
= getValue(expr
->getRHS());
178 DiagnosticsEngine::Warning
,
179 ("less-than expression of the form 'A < false' (with A of type"
180 " %0) can logically be simplified as 'false'"),
182 << expr
->getLHS()->IgnoreImpCasts()->getType()
183 << expr
->getSourceRange();
187 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
190 DiagnosticsEngine::Warning
,
191 ("less-than expression of the form 'A < true' (with A"
192 " of type %0) can %select{logically|literally}1 be"
193 " simplified as '!A'"),
195 << expr
->getLHS()->IgnoreImpCasts()->getType()
196 << (expr
->getLHS()->IgnoreImpCasts()->getType()
198 << expr
->getSourceRange();
201 DiagnosticsEngine::Warning
,
202 ("less-than expression of the form '!A < true' (with A"
203 " of type %0) can %select{logically|literally}1 be"
204 " simplified as 'A'"),
206 << e
->IgnoreImpCasts()->getType()
207 << e
->IgnoreImpCasts()->getType()->isBooleanType()
208 << expr
->getSourceRange();
218 DiagnosticsEngine::Warning
,
219 ("less-than expression of the form 'false < A' (with A of type"
220 " %0) can %select{logically|literally}1 be simplified as 'A'"),
222 << expr
->getRHS()->IgnoreImpCasts()->getType()
223 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
224 << expr
->getSourceRange();
228 DiagnosticsEngine::Warning
,
229 ("less-than expression of the form 'false < false' can"
230 " literally be simplified as 'false'"),
232 << expr
->getSourceRange();
236 DiagnosticsEngine::Warning
,
237 ("less-than expression of the form 'false < true' can"
238 " literally be simplified as 'true'"),
240 << expr
->getSourceRange();
248 DiagnosticsEngine::Warning
,
249 ("less-than expression of the form 'true < A' (with A of type"
250 " %0) can logically be simplified as 'false'"),
252 << expr
->getRHS()->IgnoreImpCasts()->getType()
253 << expr
->getSourceRange();
257 DiagnosticsEngine::Warning
,
258 ("less-than expression of the form 'true < false' can"
259 " literally be simplified as 'false'"),
261 << expr
->getSourceRange();
265 DiagnosticsEngine::Warning
,
266 ("less-than expression of the form 'true < true' can"
267 " literally be simplified as 'false'"),
269 << expr
->getSourceRange();
277 bool SimplifyBool::VisitBinGT(BinaryOperator
const * expr
) {
278 if (ignoreLocation(expr
)) {
281 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
282 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
286 auto v1
= getValue(expr
->getLHS());
287 auto v2
= getValue(expr
->getRHS());
295 DiagnosticsEngine::Warning
,
296 ("greater-than expression of the form 'A > false' (with A of"
297 " type %0) can %select{logically|literally}1 be simplified as"
300 << expr
->getLHS()->IgnoreImpCasts()->getType()
301 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
302 << expr
->getSourceRange();
306 DiagnosticsEngine::Warning
,
307 ("greater-than expression of the form 'A > true' (with A of"
308 " type %0) can logically be simplified as 'false'"),
310 << expr
->getLHS()->IgnoreImpCasts()->getType()
311 << expr
->getSourceRange();
319 DiagnosticsEngine::Warning
,
320 ("greater-than expression of the form 'false > A' (with A of"
321 " type %0) can logically be simplified as 'false'"),
323 << expr
->getRHS()->IgnoreImpCasts()->getType()
324 << expr
->getSourceRange();
328 DiagnosticsEngine::Warning
,
329 ("greater-than expression of the form 'false > false' can"
330 " literally be simplified as 'false'"),
332 << expr
->getSourceRange();
336 DiagnosticsEngine::Warning
,
337 ("greater-than expression of the form 'false > true' can"
338 " literally be simplified as 'false'"),
340 << expr
->getSourceRange();
348 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
351 DiagnosticsEngine::Warning
,
352 ("greater-than expression of the form 'true > A' (with"
353 " A of type %0) can %select{logically|literally}1 be"
354 " simplified as '!A'"),
356 << expr
->getRHS()->IgnoreImpCasts()->getType()
357 << (expr
->getRHS()->IgnoreImpCasts()->getType()
359 << expr
->getSourceRange();
362 DiagnosticsEngine::Warning
,
363 ("greater-than expression of the form 'true > !A' (with"
364 " A of type %0) can %select{logically|literally}1 be"
365 " simplified as 'A'"),
367 << e
->IgnoreImpCasts()->getType()
368 << e
->IgnoreImpCasts()->getType()->isBooleanType()
369 << expr
->getSourceRange();
375 DiagnosticsEngine::Warning
,
376 ("greater-than expression of the form 'true > false' can"
377 " literally be simplified as 'true'"),
379 << expr
->getSourceRange();
383 DiagnosticsEngine::Warning
,
384 ("greater-than expression of the form 'true > true' can"
385 " literally be simplified as 'false'"),
387 << expr
->getSourceRange();
395 bool SimplifyBool::VisitBinLE(BinaryOperator
const * expr
) {
396 if (ignoreLocation(expr
)) {
399 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
400 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
404 auto v1
= getValue(expr
->getLHS());
405 auto v2
= getValue(expr
->getRHS());
413 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
416 DiagnosticsEngine::Warning
,
417 ("less-than-or-equal-to expression of the form 'A <="
418 " false' (with A of type %0) can"
419 " %select{logically|literally}1 be simplified as"
422 << expr
->getLHS()->IgnoreImpCasts()->getType()
423 << (expr
->getLHS()->IgnoreImpCasts()->getType()
425 << expr
->getSourceRange();
428 DiagnosticsEngine::Warning
,
429 ("less-than-or-equal-to expression of the form '!A <="
430 " false' (with A of type %0) can"
431 " %select{logically|literally}1 be simplified as 'A'"),
433 << e
->IgnoreImpCasts()->getType()
434 << e
->IgnoreImpCasts()->getType()->isBooleanType()
435 << expr
->getSourceRange();
441 DiagnosticsEngine::Warning
,
442 ("less-than-or-equal-to expression of the form 'A <= true'"
443 " (with A of type %0) can logically be simplified as 'true'"),
445 << expr
->getLHS()->IgnoreImpCasts()->getType()
446 << expr
->getSourceRange();
454 DiagnosticsEngine::Warning
,
455 ("less-than-or-equal-to expression of the form 'false <= A'"
456 " (with A of type %0) can logically be simplified as 'true'"),
458 << expr
->getRHS()->IgnoreImpCasts()->getType()
459 << expr
->getSourceRange();
463 DiagnosticsEngine::Warning
,
464 ("less-than-or-equal-to expression of the form 'false <= false'"
465 " can literally be simplified as 'true'"),
467 << expr
->getSourceRange();
471 DiagnosticsEngine::Warning
,
472 ("less-than-or-equal-to expression of the form 'false <= true'"
473 " can literally be simplified as 'true'"),
475 << expr
->getSourceRange();
483 DiagnosticsEngine::Warning
,
484 ("less-than-or-equal-to expression of the form 'true <= A'"
485 " (with A of type %0) can %select{logically|literally}1 be"
486 " simplified as 'A'"),
488 << expr
->getRHS()->IgnoreImpCasts()->getType()
489 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
490 << expr
->getSourceRange();
494 DiagnosticsEngine::Warning
,
495 ("less-than-or-equal-to expression of the form 'true <= false'"
496 " can literally be simplified as 'false'"),
498 << expr
->getSourceRange();
502 DiagnosticsEngine::Warning
,
503 ("less-than-or-equal-to expression of the form 'true <= true'"
504 " can literally be simplified as 'true'"),
506 << expr
->getSourceRange();
514 bool SimplifyBool::VisitBinGE(BinaryOperator
const * expr
) {
515 if (ignoreLocation(expr
)) {
518 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
519 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
523 auto v1
= getValue(expr
->getLHS());
524 auto v2
= getValue(expr
->getRHS());
532 DiagnosticsEngine::Warning
,
533 ("greater-than-or-equal-to expression of the form 'A >= false'"
534 " (with A of type %0) can logically be simplified as 'true'"),
536 << expr
->getLHS()->IgnoreImpCasts()->getType()
537 << expr
->getSourceRange();
541 DiagnosticsEngine::Warning
,
542 ("greater-than-or-equal-to expression of the form 'A >= true'"
543 " (with A of type %0) can %select{logically|literally}1 be"
544 " simplified as 'A'"),
546 << expr
->getLHS()->IgnoreImpCasts()->getType()
547 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
548 << expr
->getSourceRange();
556 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
559 DiagnosticsEngine::Warning
,
560 ("greater-than-or-equal-to expression of the form"
561 " 'false >= A' (with A of type %0) can"
562 " %select{logically|literally}1 be simplified as"
565 << expr
->getRHS()->IgnoreImpCasts()->getType()
566 << (expr
->getRHS()->IgnoreImpCasts()->getType()
568 << expr
->getSourceRange();
571 DiagnosticsEngine::Warning
,
572 ("greater-than-or-equal-to expression of the form"
573 " 'false >= !A' (with A of type %0) can"
574 " %select{logically|literally}1 be simplified as 'A'"),
576 << e
->IgnoreImpCasts()->getType()
577 << e
->IgnoreImpCasts()->getType()->isBooleanType()
578 << expr
->getSourceRange();
584 DiagnosticsEngine::Warning
,
585 ("greater-than-or-equal-to expression of the form 'false >="
586 " false' can literally be simplified as 'true'"),
588 << expr
->getSourceRange();
592 DiagnosticsEngine::Warning
,
593 ("greater-than-or-equal-to expression of the form 'false >="
594 " true' can literally be simplified as 'false'"),
596 << expr
->getSourceRange();
604 DiagnosticsEngine::Warning
,
605 ("greater-than-or-equal-to expression of the form 'true >= A'"
606 " (with A of type %0) can logically be simplified as 'true'"),
608 << expr
->getRHS()->IgnoreImpCasts()->getType()
609 << expr
->getSourceRange();
613 DiagnosticsEngine::Warning
,
614 ("greater-than-or-equal-to expression of the form 'true >="
615 " false' can literally be simplified as 'true'"),
617 << expr
->getSourceRange();
621 DiagnosticsEngine::Warning
,
622 ("greater-than-or-equal-to expression of the form 'true >="
623 " true' can literally be simplified as 'true'"),
625 << expr
->getSourceRange();
633 bool SimplifyBool::VisitBinEQ(BinaryOperator
const * expr
) {
634 if (ignoreLocation(expr
)) {
637 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
638 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
642 auto v1
= getValue(expr
->getLHS());
643 auto v2
= getValue(expr
->getRHS());
651 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
654 DiagnosticsEngine::Warning
,
655 ("equal-to expression of the form 'A == false' (with A"
656 " of type %0) can %select{logically|literally}1 be"
657 " simplified as '!A'"),
659 << expr
->getLHS()->IgnoreImpCasts()->getType()
660 << (expr
->getLHS()->IgnoreImpCasts()->getType()
662 << expr
->getSourceRange();
665 DiagnosticsEngine::Warning
,
666 ("equal-to expression of the form '!A == false' (with A"
667 " of type %0) can %select{logically|literally}1 be"
668 " simplified as 'A'"),
670 << e
->IgnoreImpCasts()->getType()
671 << e
->IgnoreImpCasts()->getType()->isBooleanType()
672 << expr
->getSourceRange();
678 DiagnosticsEngine::Warning
,
679 ("equal-to expression of the form 'A == true' (with A of type"
680 " %0) can %select{logically|literally}1 be simplified as 'A'"),
682 << expr
->getLHS()->IgnoreImpCasts()->getType()
683 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
684 << expr
->getSourceRange();
692 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
695 DiagnosticsEngine::Warning
,
696 ("equal-to expression of the form 'false == A' (with A"
697 " of type %0) can %select{logically|literally}1 be"
698 " simplified as '!A'"),
700 << expr
->getRHS()->IgnoreImpCasts()->getType()
701 << (expr
->getRHS()->IgnoreImpCasts()->getType()
703 << expr
->getSourceRange();
706 DiagnosticsEngine::Warning
,
707 ("equal-to expression of the form 'false == !A' (with A"
708 " of type %0) can %select{logically|literally}1 be"
709 " simplified as 'A'"),
711 << e
->IgnoreImpCasts()->getType()
712 << e
->IgnoreImpCasts()->getType()->isBooleanType()
713 << expr
->getSourceRange();
719 DiagnosticsEngine::Warning
,
720 ("equal-to expression of the form 'false == false' can"
721 " literally be simplified as 'true'"),
723 << expr
->getSourceRange();
727 DiagnosticsEngine::Warning
,
728 ("equal-to expression of the form 'false == true' can"
729 " literally be simplified as 'false'"),
731 << expr
->getSourceRange();
739 DiagnosticsEngine::Warning
,
740 ("equal-to expression of the form 'true == A' (with A of type"
741 " %0) can %select{logically|literally}1 be simplified as 'A'"),
743 << expr
->getRHS()->IgnoreImpCasts()->getType()
744 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
745 << expr
->getSourceRange();
749 DiagnosticsEngine::Warning
,
750 ("equal-to expression of the form 'true == false' can"
751 " literally be simplified as 'false'"),
753 << expr
->getSourceRange();
757 DiagnosticsEngine::Warning
,
758 ("equal-to expression of the form 'true == true' can"
759 " literally be simplified as 'true'"),
761 << expr
->getSourceRange();
769 bool SimplifyBool::VisitBinNE(BinaryOperator
const * expr
) {
770 if (ignoreLocation(expr
)) {
773 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
774 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
778 auto v1
= getValue(expr
->getLHS());
779 auto v2
= getValue(expr
->getRHS());
787 DiagnosticsEngine::Warning
,
788 ("not-equal-to expression of the form 'A != false' (with A of"
789 " type %0) can %select{logically|literally}1 be simplified as"
792 << expr
->getLHS()->IgnoreImpCasts()->getType()
793 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
794 << expr
->getSourceRange();
798 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
801 DiagnosticsEngine::Warning
,
802 ("not-equal-to expression of the form 'A != true' (with"
803 " A of type %0) can %select{logically|literally}1 be"
804 " simplified as '!A'"),
806 << expr
->getLHS()->IgnoreImpCasts()->getType()
807 << (expr
->getLHS()->IgnoreImpCasts()->getType()
809 << expr
->getSourceRange();
812 DiagnosticsEngine::Warning
,
813 ("not-equal-to expression of the form '!A != true'"
814 " (with A of type %0) can"
815 " %select{logically|literally}1 be simplified as 'A'"),
817 << e
->IgnoreImpCasts()->getType()
818 << e
->IgnoreImpCasts()->getType()->isBooleanType()
819 << expr
->getSourceRange();
829 DiagnosticsEngine::Warning
,
830 ("not-equal-to expression of the form 'false != A' (with A of"
831 " type %0) can %select{logically|literally}1 be simplified as"
834 << expr
->getRHS()->IgnoreImpCasts()->getType()
835 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
836 << expr
->getSourceRange();
840 DiagnosticsEngine::Warning
,
841 ("not-equal-to expression of the form 'false != false' can"
842 " literally be simplified as 'false'"),
844 << expr
->getSourceRange();
848 DiagnosticsEngine::Warning
,
849 ("not-equal-to expression of the form 'false != true' can"
850 " literally be simplified as 'true'"),
852 << expr
->getSourceRange();
860 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
863 DiagnosticsEngine::Warning
,
864 ("not-equal-to expression of the form 'true != A' (with"
865 " A of type %0) can %select{logically|literally}1 be"
866 " simplified as '!A'"),
868 << expr
->getRHS()->IgnoreImpCasts()->getType()
869 << (expr
->getRHS()->IgnoreImpCasts()->getType()
871 << expr
->getSourceRange();
874 DiagnosticsEngine::Warning
,
875 ("not-equal-to expression of the form 'true != !A'"
876 " (with A of type %0) can"
877 " %select{logically|literally}1 be simplified as 'A'"),
879 << e
->IgnoreImpCasts()->getType()
880 << e
->IgnoreImpCasts()->getType()->isBooleanType()
881 << expr
->getSourceRange();
887 DiagnosticsEngine::Warning
,
888 ("not-equal-to expression of the form 'true != false' can"
889 " literally be simplified as 'true'"),
891 << expr
->getSourceRange();
895 DiagnosticsEngine::Warning
,
896 ("not-equal-to expression of the form 'true != true' can"
897 " literally be simplified as 'false'"),
899 << expr
->getSourceRange();
907 bool SimplifyBool::VisitConditionalOperator(ConditionalOperator
const * expr
) {
908 if (ignoreLocation(expr
)) {
911 auto v1
= getValue(expr
->getTrueExpr());
912 auto v2
= getValue(expr
->getFalseExpr());
920 DiagnosticsEngine::Warning
,
921 ("conditional expression of the form 'A ? B : false' (with A of"
922 " type %0 and B of type %1) can %select{logically|literally}2"
923 " be simplified as 'A && B'"),
925 << expr
->getCond()->IgnoreImpCasts()->getType()
926 << expr
->getTrueExpr()->IgnoreImpCasts()->getType()
927 << ((expr
->getCond()->IgnoreImpCasts()->getType()
929 && (expr
->getTrueExpr()->IgnoreImpCasts()->getType()
931 << expr
->getSourceRange();
935 auto e
= getSubExprOfLogicalNegation(expr
->getCond());
938 DiagnosticsEngine::Warning
,
939 ("conditional expression of the form 'A ? B : true'"
940 " (with A of type %0 and B of type %1) can"
941 " %select{logically|literally}2 be simplified as '!A"
944 << expr
->getCond()->IgnoreImpCasts()->getType()
945 << expr
->getTrueExpr()->IgnoreImpCasts()->getType()
946 << ((expr
->getCond()->IgnoreImpCasts()->getType()
948 && (expr
->getTrueExpr()->IgnoreImpCasts()->getType()
950 << expr
->getSourceRange();
953 DiagnosticsEngine::Warning
,
954 ("conditional expression of the form '!A ? B : true'"
955 " (with A of type %0 and B of type %1) can"
956 " %select{logically|literally}2 be simplified as 'A ||"
959 << e
->IgnoreImpCasts()->getType()
960 << expr
->getTrueExpr()->IgnoreImpCasts()->getType()
961 << (e
->IgnoreImpCasts()->getType()->isBooleanType()
962 && (expr
->getTrueExpr()->IgnoreImpCasts()
963 ->getType()->isBooleanType()))
964 << expr
->getSourceRange();
974 auto e
= getSubExprOfLogicalNegation(expr
->getCond());
977 DiagnosticsEngine::Warning
,
978 ("conditional expression of the form 'A ? false : B'"
979 " (with A of type %0 and B of type %1) can"
980 " %select{logically|literally}2 be simplified as '!A"
983 << expr
->getCond()->IgnoreImpCasts()->getType()
984 << expr
->getFalseExpr()->IgnoreImpCasts()->getType()
985 << ((expr
->getCond()->IgnoreImpCasts()->getType()
987 && (expr
->getFalseExpr()->IgnoreImpCasts()
988 ->getType()->isBooleanType()))
989 << expr
->getSourceRange();
992 DiagnosticsEngine::Warning
,
993 ("conditional expression of the form '!A ? false : B'"
994 " (with A of type %0 and B of type %1) can"
995 " %select{logically|literally}2 be simplified as 'A &&"
998 << e
->IgnoreImpCasts()->getType()
999 << expr
->getFalseExpr()->IgnoreImpCasts()->getType()
1000 << (e
->IgnoreImpCasts()->getType()->isBooleanType()
1001 && (expr
->getFalseExpr()->IgnoreImpCasts()
1002 ->getType()->isBooleanType()))
1003 << expr
->getSourceRange();
1009 DiagnosticsEngine::Warning
,
1010 ("conditional expression of the form 'A ? false : false' (with"
1011 " A of type %0) can logically be simplified as 'false'"),
1012 expr
->getLocStart())
1013 << expr
->getCond()->IgnoreImpCasts()->getType()
1014 << expr
->getSourceRange();
1018 auto e
= getSubExprOfLogicalNegation(expr
->getCond());
1021 DiagnosticsEngine::Warning
,
1022 ("conditional expression of the form 'A ? false : true'"
1023 " (with A of type %0) can"
1024 " %select{logically|literally}1 be simplified as"
1026 expr
->getLocStart())
1027 << expr
->getCond()->IgnoreImpCasts()->getType()
1028 << (expr
->getCond()->IgnoreImpCasts()->getType()
1030 << expr
->getSourceRange();
1033 DiagnosticsEngine::Warning
,
1034 ("conditional expression of the form '!A ? false :"
1035 " true' (with A of type %0) can"
1036 " %select{logically|literally}1 be simplified as 'A'"),
1037 expr
->getLocStart())
1038 << e
->IgnoreImpCasts()->getType()
1039 << e
->IgnoreImpCasts()->getType()->isBooleanType()
1040 << expr
->getSourceRange();
1048 case Value::Unknown
:
1050 DiagnosticsEngine::Warning
,
1051 ("conditional expression of the form 'A ? true : B' (with A of"
1052 " type %0 and B of type %1) can %select{logically|literally}2"
1053 " be simplified as 'A || B'"),
1054 expr
->getLocStart())
1055 << expr
->getCond()->IgnoreImpCasts()->getType()
1056 << expr
->getFalseExpr()->IgnoreImpCasts()->getType()
1057 << ((expr
->getCond()->IgnoreImpCasts()->getType()
1059 && (expr
->getFalseExpr()->IgnoreImpCasts()->getType()
1061 << expr
->getSourceRange();
1065 DiagnosticsEngine::Warning
,
1066 ("conditional expression of the form 'A ? true : false' (with A"
1067 " of type %0) can %select{logically|literally}1 be simplified"
1069 expr
->getLocStart())
1070 << expr
->getCond()->IgnoreImpCasts()->getType()
1071 << expr
->getCond()->IgnoreImpCasts()->getType()->isBooleanType()
1072 << expr
->getSourceRange();
1076 DiagnosticsEngine::Warning
,
1077 ("conditional expression of the form 'A ? true : true' (with A"
1078 " of type %0) can logically be simplified as 'true'"),
1079 expr
->getLocStart())
1080 << expr
->getCond()->IgnoreImpCasts()->getType()
1081 << expr
->getSourceRange();
1089 loplugin::Plugin::Registration
<SimplifyBool
> X("simplifybool");
1093 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */