RedundantExpressionCheck.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. //===--- RedundantExpressionCheck.cpp - clang-tidy-------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "RedundantExpressionCheck.h"
  9. #include "../utils/Matchers.h"
  10. #include "../utils/OptionsUtils.h"
  11. #include "clang/AST/ASTContext.h"
  12. #include "clang/AST/ExprConcepts.h"
  13. #include "clang/ASTMatchers/ASTMatchFinder.h"
  14. #include "clang/Basic/LLVM.h"
  15. #include "clang/Basic/SourceLocation.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Lex/Lexer.h"
  18. #include "llvm/ADT/APInt.h"
  19. #include "llvm/ADT/APSInt.h"
  20. #include "llvm/ADT/FoldingSet.h"
  21. #include "llvm/ADT/SmallBitVector.h"
  22. #include "llvm/Support/Casting.h"
  23. #include "llvm/Support/FormatVariadic.h"
  24. #include <algorithm>
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <optional>
  28. #include <string>
  29. #include <vector>
  30. using namespace clang::ast_matchers;
  31. using namespace clang::tidy::matchers;
  32. namespace clang::tidy::misc {
  33. namespace {
  34. using llvm::APSInt;
  35. static constexpr llvm::StringLiteral KnownBannedMacroNames[] = {
  36. "EAGAIN",
  37. "EWOULDBLOCK",
  38. "SIGCLD",
  39. "SIGCHLD",
  40. };
  41. static bool incrementWithoutOverflow(const APSInt &Value, APSInt &Result) {
  42. Result = Value;
  43. ++Result;
  44. return Value < Result;
  45. }
  46. static bool areEquivalentNameSpecifier(const NestedNameSpecifier *Left,
  47. const NestedNameSpecifier *Right) {
  48. llvm::FoldingSetNodeID LeftID, RightID;
  49. Left->Profile(LeftID);
  50. Right->Profile(RightID);
  51. return LeftID == RightID;
  52. }
  53. static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
  54. if (!Left || !Right)
  55. return !Left && !Right;
  56. Left = Left->IgnoreParens();
  57. Right = Right->IgnoreParens();
  58. // Compare classes.
  59. if (Left->getStmtClass() != Right->getStmtClass())
  60. return false;
  61. // Compare children.
  62. Expr::const_child_iterator LeftIter = Left->child_begin();
  63. Expr::const_child_iterator RightIter = Right->child_begin();
  64. while (LeftIter != Left->child_end() && RightIter != Right->child_end()) {
  65. if (!areEquivalentExpr(dyn_cast_or_null<Expr>(*LeftIter),
  66. dyn_cast_or_null<Expr>(*RightIter)))
  67. return false;
  68. ++LeftIter;
  69. ++RightIter;
  70. }
  71. if (LeftIter != Left->child_end() || RightIter != Right->child_end())
  72. return false;
  73. // Perform extra checks.
  74. switch (Left->getStmtClass()) {
  75. default:
  76. return false;
  77. case Stmt::CharacterLiteralClass:
  78. return cast<CharacterLiteral>(Left)->getValue() ==
  79. cast<CharacterLiteral>(Right)->getValue();
  80. case Stmt::IntegerLiteralClass: {
  81. llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue();
  82. llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue();
  83. return LeftLit.getBitWidth() == RightLit.getBitWidth() &&
  84. LeftLit == RightLit;
  85. }
  86. case Stmt::FloatingLiteralClass:
  87. return cast<FloatingLiteral>(Left)->getValue().bitwiseIsEqual(
  88. cast<FloatingLiteral>(Right)->getValue());
  89. case Stmt::StringLiteralClass:
  90. return cast<StringLiteral>(Left)->getBytes() ==
  91. cast<StringLiteral>(Right)->getBytes();
  92. case Stmt::CXXOperatorCallExprClass:
  93. return cast<CXXOperatorCallExpr>(Left)->getOperator() ==
  94. cast<CXXOperatorCallExpr>(Right)->getOperator();
  95. case Stmt::DependentScopeDeclRefExprClass:
  96. if (cast<DependentScopeDeclRefExpr>(Left)->getDeclName() !=
  97. cast<DependentScopeDeclRefExpr>(Right)->getDeclName())
  98. return false;
  99. return areEquivalentNameSpecifier(
  100. cast<DependentScopeDeclRefExpr>(Left)->getQualifier(),
  101. cast<DependentScopeDeclRefExpr>(Right)->getQualifier());
  102. case Stmt::DeclRefExprClass:
  103. return cast<DeclRefExpr>(Left)->getDecl() ==
  104. cast<DeclRefExpr>(Right)->getDecl();
  105. case Stmt::MemberExprClass:
  106. return cast<MemberExpr>(Left)->getMemberDecl() ==
  107. cast<MemberExpr>(Right)->getMemberDecl();
  108. case Stmt::CXXFoldExprClass:
  109. return cast<CXXFoldExpr>(Left)->getOperator() ==
  110. cast<CXXFoldExpr>(Right)->getOperator();
  111. case Stmt::CXXFunctionalCastExprClass:
  112. case Stmt::CStyleCastExprClass:
  113. return cast<ExplicitCastExpr>(Left)->getTypeAsWritten() ==
  114. cast<ExplicitCastExpr>(Right)->getTypeAsWritten();
  115. case Stmt::CallExprClass:
  116. case Stmt::ImplicitCastExprClass:
  117. case Stmt::ArraySubscriptExprClass:
  118. return true;
  119. case Stmt::UnaryOperatorClass:
  120. if (cast<UnaryOperator>(Left)->isIncrementDecrementOp())
  121. return false;
  122. return cast<UnaryOperator>(Left)->getOpcode() ==
  123. cast<UnaryOperator>(Right)->getOpcode();
  124. case Stmt::BinaryOperatorClass:
  125. if (cast<BinaryOperator>(Left)->isAssignmentOp())
  126. return false;
  127. return cast<BinaryOperator>(Left)->getOpcode() ==
  128. cast<BinaryOperator>(Right)->getOpcode();
  129. case Stmt::UnaryExprOrTypeTraitExprClass:
  130. const auto *LeftUnaryExpr =
  131. cast<UnaryExprOrTypeTraitExpr>(Left);
  132. const auto *RightUnaryExpr =
  133. cast<UnaryExprOrTypeTraitExpr>(Right);
  134. if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType())
  135. return LeftUnaryExpr->getArgumentType() ==
  136. RightUnaryExpr->getArgumentType();
  137. if (!LeftUnaryExpr->isArgumentType() && !RightUnaryExpr->isArgumentType())
  138. return areEquivalentExpr(LeftUnaryExpr->getArgumentExpr(),
  139. RightUnaryExpr->getArgumentExpr());
  140. return false;
  141. }
  142. }
  143. // For a given expression 'x', returns whether the ranges covered by the
  144. // relational operators are equivalent (i.e. x <= 4 is equivalent to x < 5).
  145. static bool areEquivalentRanges(BinaryOperatorKind OpcodeLHS,
  146. const APSInt &ValueLHS,
  147. BinaryOperatorKind OpcodeRHS,
  148. const APSInt &ValueRHS) {
  149. assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
  150. "Values must be ordered");
  151. // Handle the case where constants are the same: x <= 4 <==> x <= 4.
  152. if (APSInt::compareValues(ValueLHS, ValueRHS) == 0)
  153. return OpcodeLHS == OpcodeRHS;
  154. // Handle the case where constants are off by one: x <= 4 <==> x < 5.
  155. APSInt ValueLhsPlus1;
  156. return ((OpcodeLHS == BO_LE && OpcodeRHS == BO_LT) ||
  157. (OpcodeLHS == BO_GT && OpcodeRHS == BO_GE)) &&
  158. incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
  159. APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0;
  160. }
  161. // For a given expression 'x', returns whether the ranges covered by the
  162. // relational operators are fully disjoint (i.e. x < 4 and x > 7).
  163. static bool areExclusiveRanges(BinaryOperatorKind OpcodeLHS,
  164. const APSInt &ValueLHS,
  165. BinaryOperatorKind OpcodeRHS,
  166. const APSInt &ValueRHS) {
  167. assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
  168. "Values must be ordered");
  169. // Handle cases where the constants are the same.
  170. if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
  171. switch (OpcodeLHS) {
  172. case BO_EQ:
  173. return OpcodeRHS == BO_NE || OpcodeRHS == BO_GT || OpcodeRHS == BO_LT;
  174. case BO_NE:
  175. return OpcodeRHS == BO_EQ;
  176. case BO_LE:
  177. return OpcodeRHS == BO_GT;
  178. case BO_GE:
  179. return OpcodeRHS == BO_LT;
  180. case BO_LT:
  181. return OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
  182. case BO_GT:
  183. return OpcodeRHS == BO_EQ || OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
  184. default:
  185. return false;
  186. }
  187. }
  188. // Handle cases where the constants are different.
  189. if ((OpcodeLHS == BO_EQ || OpcodeLHS == BO_LT || OpcodeLHS == BO_LE) &&
  190. (OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE))
  191. return true;
  192. // Handle the case where constants are off by one: x > 5 && x < 6.
  193. APSInt ValueLhsPlus1;
  194. if (OpcodeLHS == BO_GT && OpcodeRHS == BO_LT &&
  195. incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
  196. APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0)
  197. return true;
  198. return false;
  199. }
  200. // Returns whether the ranges covered by the union of both relational
  201. // expressions cover the whole domain (i.e. x < 10 and x > 0).
  202. static bool rangesFullyCoverDomain(BinaryOperatorKind OpcodeLHS,
  203. const APSInt &ValueLHS,
  204. BinaryOperatorKind OpcodeRHS,
  205. const APSInt &ValueRHS) {
  206. assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 &&
  207. "Values must be ordered");
  208. // Handle cases where the constants are the same: x < 5 || x >= 5.
  209. if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) {
  210. switch (OpcodeLHS) {
  211. case BO_EQ:
  212. return OpcodeRHS == BO_NE;
  213. case BO_NE:
  214. return OpcodeRHS == BO_EQ;
  215. case BO_LE:
  216. return OpcodeRHS == BO_GT || OpcodeRHS == BO_GE;
  217. case BO_LT:
  218. return OpcodeRHS == BO_GE;
  219. case BO_GE:
  220. return OpcodeRHS == BO_LT || OpcodeRHS == BO_LE;
  221. case BO_GT:
  222. return OpcodeRHS == BO_LE;
  223. default:
  224. return false;
  225. }
  226. }
  227. // Handle the case where constants are off by one: x <= 4 || x >= 5.
  228. APSInt ValueLhsPlus1;
  229. if (OpcodeLHS == BO_LE && OpcodeRHS == BO_GE &&
  230. incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) &&
  231. APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0)
  232. return true;
  233. // Handle cases where the constants are different: x > 4 || x <= 7.
  234. if ((OpcodeLHS == BO_GT || OpcodeLHS == BO_GE) &&
  235. (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE))
  236. return true;
  237. // Handle cases where constants are different but both ops are !=, like:
  238. // x != 5 || x != 10
  239. if (OpcodeLHS == BO_NE && OpcodeRHS == BO_NE)
  240. return true;
  241. return false;
  242. }
  243. static bool rangeSubsumesRange(BinaryOperatorKind OpcodeLHS,
  244. const APSInt &ValueLHS,
  245. BinaryOperatorKind OpcodeRHS,
  246. const APSInt &ValueRHS) {
  247. int Comparison = APSInt::compareValues(ValueLHS, ValueRHS);
  248. switch (OpcodeLHS) {
  249. case BO_EQ:
  250. return OpcodeRHS == BO_EQ && Comparison == 0;
  251. case BO_NE:
  252. return (OpcodeRHS == BO_NE && Comparison == 0) ||
  253. (OpcodeRHS == BO_EQ && Comparison != 0) ||
  254. (OpcodeRHS == BO_LT && Comparison >= 0) ||
  255. (OpcodeRHS == BO_LE && Comparison > 0) ||
  256. (OpcodeRHS == BO_GT && Comparison <= 0) ||
  257. (OpcodeRHS == BO_GE && Comparison < 0);
  258. case BO_LT:
  259. return ((OpcodeRHS == BO_LT && Comparison >= 0) ||
  260. (OpcodeRHS == BO_LE && Comparison > 0) ||
  261. (OpcodeRHS == BO_EQ && Comparison > 0));
  262. case BO_GT:
  263. return ((OpcodeRHS == BO_GT && Comparison <= 0) ||
  264. (OpcodeRHS == BO_GE && Comparison < 0) ||
  265. (OpcodeRHS == BO_EQ && Comparison < 0));
  266. case BO_LE:
  267. return (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE || OpcodeRHS == BO_EQ) &&
  268. Comparison >= 0;
  269. case BO_GE:
  270. return (OpcodeRHS == BO_GT || OpcodeRHS == BO_GE || OpcodeRHS == BO_EQ) &&
  271. Comparison <= 0;
  272. default:
  273. return false;
  274. }
  275. }
  276. static void transformSubToCanonicalAddExpr(BinaryOperatorKind &Opcode,
  277. APSInt &Value) {
  278. if (Opcode == BO_Sub) {
  279. Opcode = BO_Add;
  280. Value = -Value;
  281. }
  282. }
  283. // to use in the template below
  284. static OverloadedOperatorKind getOp(const BinaryOperator *Op) {
  285. return BinaryOperator::getOverloadedOperator(Op->getOpcode());
  286. }
  287. static OverloadedOperatorKind getOp(const CXXOperatorCallExpr *Op) {
  288. if (Op->getNumArgs() != 2)
  289. return OO_None;
  290. return Op->getOperator();
  291. }
  292. static std::pair<const Expr *, const Expr *>
  293. getOperands(const BinaryOperator *Op) {
  294. return {Op->getLHS()->IgnoreParenImpCasts(),
  295. Op->getRHS()->IgnoreParenImpCasts()};
  296. }
  297. static std::pair<const Expr *, const Expr *>
  298. getOperands(const CXXOperatorCallExpr *Op) {
  299. return {Op->getArg(0)->IgnoreParenImpCasts(),
  300. Op->getArg(1)->IgnoreParenImpCasts()};
  301. }
  302. template <typename TExpr>
  303. static const TExpr *checkOpKind(const Expr *TheExpr,
  304. OverloadedOperatorKind OpKind) {
  305. const auto *AsTExpr = dyn_cast_or_null<TExpr>(TheExpr);
  306. if (AsTExpr && getOp(AsTExpr) == OpKind)
  307. return AsTExpr;
  308. return nullptr;
  309. }
  310. // returns true if a subexpression has two directly equivalent operands and
  311. // is already handled by operands/parametersAreEquivalent
  312. template <typename TExpr, unsigned N>
  313. static bool collectOperands(const Expr *Part,
  314. SmallVector<const Expr *, N> &AllOperands,
  315. OverloadedOperatorKind OpKind) {
  316. if (const auto *BinOp = checkOpKind<TExpr>(Part, OpKind)) {
  317. const std::pair<const Expr *, const Expr *> Operands = getOperands(BinOp);
  318. if (areEquivalentExpr(Operands.first, Operands.second))
  319. return true;
  320. return collectOperands<TExpr>(Operands.first, AllOperands, OpKind) ||
  321. collectOperands<TExpr>(Operands.second, AllOperands, OpKind);
  322. }
  323. AllOperands.push_back(Part);
  324. return false;
  325. }
  326. template <typename TExpr>
  327. static bool hasSameOperatorParent(const Expr *TheExpr,
  328. OverloadedOperatorKind OpKind,
  329. ASTContext &Context) {
  330. // IgnoreParenImpCasts logic in reverse: skip surrounding uninteresting nodes
  331. const DynTypedNodeList Parents = Context.getParents(*TheExpr);
  332. for (DynTypedNode DynParent : Parents) {
  333. if (const auto *Parent = DynParent.get<Expr>()) {
  334. bool Skip = isa<ParenExpr>(Parent) || isa<ImplicitCastExpr>(Parent) ||
  335. isa<FullExpr>(Parent) ||
  336. isa<MaterializeTemporaryExpr>(Parent);
  337. if (Skip && hasSameOperatorParent<TExpr>(Parent, OpKind, Context))
  338. return true;
  339. if (checkOpKind<TExpr>(Parent, OpKind))
  340. return true;
  341. }
  342. }
  343. return false;
  344. }
  345. template <typename TExpr>
  346. static bool
  347. markDuplicateOperands(const TExpr *TheExpr,
  348. ast_matchers::internal::BoundNodesTreeBuilder *Builder,
  349. ASTContext &Context) {
  350. const OverloadedOperatorKind OpKind = getOp(TheExpr);
  351. if (OpKind == OO_None)
  352. return false;
  353. // if there are no nested operators of the same kind, it's handled by
  354. // operands/parametersAreEquivalent
  355. const std::pair<const Expr *, const Expr *> Operands = getOperands(TheExpr);
  356. if (!(checkOpKind<TExpr>(Operands.first, OpKind) ||
  357. checkOpKind<TExpr>(Operands.second, OpKind)))
  358. return false;
  359. // if parent is the same kind of operator, it's handled by a previous call to
  360. // markDuplicateOperands
  361. if (hasSameOperatorParent<TExpr>(TheExpr, OpKind, Context))
  362. return false;
  363. SmallVector<const Expr *, 4> AllOperands;
  364. if (collectOperands<TExpr>(Operands.first, AllOperands, OpKind))
  365. return false;
  366. if (collectOperands<TExpr>(Operands.second, AllOperands, OpKind))
  367. return false;
  368. size_t NumOperands = AllOperands.size();
  369. llvm::SmallBitVector Duplicates(NumOperands);
  370. for (size_t I = 0; I < NumOperands; I++) {
  371. if (Duplicates[I])
  372. continue;
  373. bool FoundDuplicates = false;
  374. for (size_t J = I + 1; J < NumOperands; J++) {
  375. if (AllOperands[J]->HasSideEffects(Context))
  376. break;
  377. if (areEquivalentExpr(AllOperands[I], AllOperands[J])) {
  378. FoundDuplicates = true;
  379. Duplicates.set(J);
  380. Builder->setBinding(SmallString<11>(llvm::formatv("duplicate{0}", J)),
  381. DynTypedNode::create(*AllOperands[J]));
  382. }
  383. }
  384. if (FoundDuplicates)
  385. Builder->setBinding(SmallString<11>(llvm::formatv("duplicate{0}", I)),
  386. DynTypedNode::create(*AllOperands[I]));
  387. }
  388. return Duplicates.any();
  389. }
  390. AST_MATCHER(Expr, isIntegerConstantExpr) {
  391. if (Node.isInstantiationDependent())
  392. return false;
  393. return Node.isIntegerConstantExpr(Finder->getASTContext());
  394. }
  395. AST_MATCHER(Expr, isRequiresExpr) { return isa<RequiresExpr>(Node); }
  396. AST_MATCHER(BinaryOperator, operandsAreEquivalent) {
  397. return areEquivalentExpr(Node.getLHS(), Node.getRHS());
  398. }
  399. AST_MATCHER(BinaryOperator, nestedOperandsAreEquivalent) {
  400. return markDuplicateOperands(&Node, Builder, Finder->getASTContext());
  401. }
  402. AST_MATCHER(ConditionalOperator, expressionsAreEquivalent) {
  403. return areEquivalentExpr(Node.getTrueExpr(), Node.getFalseExpr());
  404. }
  405. AST_MATCHER(CallExpr, parametersAreEquivalent) {
  406. return Node.getNumArgs() == 2 &&
  407. areEquivalentExpr(Node.getArg(0), Node.getArg(1));
  408. }
  409. AST_MATCHER(CXXOperatorCallExpr, nestedParametersAreEquivalent) {
  410. return markDuplicateOperands(&Node, Builder, Finder->getASTContext());
  411. }
  412. AST_MATCHER(BinaryOperator, binaryOperatorIsInMacro) {
  413. return Node.getOperatorLoc().isMacroID();
  414. }
  415. AST_MATCHER(ConditionalOperator, conditionalOperatorIsInMacro) {
  416. return Node.getQuestionLoc().isMacroID() || Node.getColonLoc().isMacroID();
  417. }
  418. AST_MATCHER(Expr, isMacro) { return Node.getExprLoc().isMacroID(); }
  419. AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<llvm::StringLiteral>, Names) {
  420. const SourceManager &SM = Finder->getASTContext().getSourceManager();
  421. const LangOptions &LO = Finder->getASTContext().getLangOpts();
  422. SourceLocation Loc = Node.getExprLoc();
  423. while (Loc.isMacroID()) {
  424. StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LO);
  425. if (llvm::is_contained(Names, MacroName))
  426. return true;
  427. Loc = SM.getImmediateMacroCallerLoc(Loc);
  428. }
  429. return false;
  430. }
  431. // Returns a matcher for integer constant expressions.
  432. static ast_matchers::internal::Matcher<Expr>
  433. matchIntegerConstantExpr(StringRef Id) {
  434. std::string CstId = (Id + "-const").str();
  435. return expr(isIntegerConstantExpr()).bind(CstId);
  436. }
  437. // Retrieves the integer expression matched by 'matchIntegerConstantExpr' with
  438. // name 'Id' and stores it into 'ConstExpr', the value of the expression is
  439. // stored into `Value`.
  440. static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result,
  441. StringRef Id, APSInt &Value,
  442. const Expr *&ConstExpr) {
  443. std::string CstId = (Id + "-const").str();
  444. ConstExpr = Result.Nodes.getNodeAs<Expr>(CstId);
  445. if (!ConstExpr)
  446. return false;
  447. std::optional<llvm::APSInt> R =
  448. ConstExpr->getIntegerConstantExpr(*Result.Context);
  449. if (!R)
  450. return false;
  451. Value = *R;
  452. return true;
  453. }
  454. // Overloaded `retrieveIntegerConstantExpr` for compatibility.
  455. static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result,
  456. StringRef Id, APSInt &Value) {
  457. const Expr *ConstExpr = nullptr;
  458. return retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr);
  459. }
  460. // Returns a matcher for symbolic expressions (matches every expression except
  461. // ingeter constant expressions).
  462. static ast_matchers::internal::Matcher<Expr> matchSymbolicExpr(StringRef Id) {
  463. std::string SymId = (Id + "-sym").str();
  464. return ignoringParenImpCasts(
  465. expr(unless(isIntegerConstantExpr())).bind(SymId));
  466. }
  467. // Retrieves the expression matched by 'matchSymbolicExpr' with name 'Id' and
  468. // stores it into 'SymExpr'.
  469. static bool retrieveSymbolicExpr(const MatchFinder::MatchResult &Result,
  470. StringRef Id, const Expr *&SymExpr) {
  471. std::string SymId = (Id + "-sym").str();
  472. if (const auto *Node = Result.Nodes.getNodeAs<Expr>(SymId)) {
  473. SymExpr = Node;
  474. return true;
  475. }
  476. return false;
  477. }
  478. // Match a binary operator between a symbolic expression and an integer constant
  479. // expression.
  480. static ast_matchers::internal::Matcher<Expr>
  481. matchBinOpIntegerConstantExpr(StringRef Id) {
  482. const auto BinOpCstExpr =
  483. expr(anyOf(binaryOperator(hasAnyOperatorName("+", "|", "&"),
  484. hasOperands(matchSymbolicExpr(Id),
  485. matchIntegerConstantExpr(Id))),
  486. binaryOperator(hasOperatorName("-"),
  487. hasLHS(matchSymbolicExpr(Id)),
  488. hasRHS(matchIntegerConstantExpr(Id)))))
  489. .bind(Id);
  490. return ignoringParenImpCasts(BinOpCstExpr);
  491. }
  492. // Retrieves sub-expressions matched by 'matchBinOpIntegerConstantExpr' with
  493. // name 'Id'.
  494. static bool
  495. retrieveBinOpIntegerConstantExpr(const MatchFinder::MatchResult &Result,
  496. StringRef Id, BinaryOperatorKind &Opcode,
  497. const Expr *&Symbol, APSInt &Value) {
  498. if (const auto *BinExpr = Result.Nodes.getNodeAs<BinaryOperator>(Id)) {
  499. Opcode = BinExpr->getOpcode();
  500. return retrieveSymbolicExpr(Result, Id, Symbol) &&
  501. retrieveIntegerConstantExpr(Result, Id, Value);
  502. }
  503. return false;
  504. }
  505. // Matches relational expressions: 'Expr <op> k' (i.e. x < 2, x != 3, 12 <= x).
  506. static ast_matchers::internal::Matcher<Expr>
  507. matchRelationalIntegerConstantExpr(StringRef Id) {
  508. std::string CastId = (Id + "-cast").str();
  509. std::string SwapId = (Id + "-swap").str();
  510. std::string NegateId = (Id + "-negate").str();
  511. std::string OverloadId = (Id + "-overload").str();
  512. std::string ConstId = (Id + "-const").str();
  513. const auto RelationalExpr = ignoringParenImpCasts(binaryOperator(
  514. isComparisonOperator(), expr().bind(Id),
  515. anyOf(allOf(hasLHS(matchSymbolicExpr(Id)),
  516. hasRHS(matchIntegerConstantExpr(Id))),
  517. allOf(hasLHS(matchIntegerConstantExpr(Id)),
  518. hasRHS(matchSymbolicExpr(Id)), expr().bind(SwapId)))));
  519. // A cast can be matched as a comparator to zero. (i.e. if (x) is equivalent
  520. // to if (x != 0)).
  521. const auto CastExpr =
  522. implicitCastExpr(hasCastKind(CK_IntegralToBoolean),
  523. hasSourceExpression(matchSymbolicExpr(Id)))
  524. .bind(CastId);
  525. const auto NegateRelationalExpr =
  526. unaryOperator(hasOperatorName("!"),
  527. hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))
  528. .bind(NegateId);
  529. // Do not bind to double negation.
  530. const auto NegateNegateRelationalExpr =
  531. unaryOperator(hasOperatorName("!"),
  532. hasUnaryOperand(unaryOperator(
  533. hasOperatorName("!"),
  534. hasUnaryOperand(anyOf(CastExpr, RelationalExpr)))));
  535. const auto OverloadedOperatorExpr =
  536. cxxOperatorCallExpr(
  537. hasAnyOverloadedOperatorName("==", "!=", "<", "<=", ">", ">="),
  538. // Filter noisy false positives.
  539. unless(isMacro()), unless(isInTemplateInstantiation()),
  540. anyOf(hasLHS(ignoringParenImpCasts(integerLiteral().bind(ConstId))),
  541. hasRHS(ignoringParenImpCasts(integerLiteral().bind(ConstId)))))
  542. .bind(OverloadId);
  543. return anyOf(RelationalExpr, CastExpr, NegateRelationalExpr,
  544. NegateNegateRelationalExpr, OverloadedOperatorExpr);
  545. }
  546. // Checks whether a function param is non constant reference type, and may
  547. // be modified in the function.
  548. static bool isNonConstReferenceType(QualType ParamType) {
  549. return ParamType->isReferenceType() &&
  550. !ParamType.getNonReferenceType().isConstQualified();
  551. }
  552. // Checks whether the arguments of an overloaded operator can be modified in the
  553. // function.
  554. // For operators that take an instance and a constant as arguments, only the
  555. // first argument (the instance) needs to be checked, since the constant itself
  556. // is a temporary expression. Whether the second parameter is checked is
  557. // controlled by the parameter `ParamsToCheckCount`.
  558. static bool
  559. canOverloadedOperatorArgsBeModified(const CXXOperatorCallExpr *OperatorCall,
  560. bool CheckSecondParam) {
  561. const auto *OperatorDecl =
  562. dyn_cast_or_null<FunctionDecl>(OperatorCall->getCalleeDecl());
  563. // if we can't find the declaration, conservatively assume it can modify
  564. // arguments
  565. if (!OperatorDecl)
  566. return true;
  567. unsigned ParamCount = OperatorDecl->getNumParams();
  568. // Overloaded operators declared inside a class have only one param.
  569. // These functions must be declared const in order to not be able to modify
  570. // the instance of the class they are called through.
  571. if (ParamCount == 1 &&
  572. !OperatorDecl->getType()->castAs<FunctionType>()->isConst())
  573. return true;
  574. if (isNonConstReferenceType(OperatorDecl->getParamDecl(0)->getType()))
  575. return true;
  576. return CheckSecondParam && ParamCount == 2 &&
  577. isNonConstReferenceType(OperatorDecl->getParamDecl(1)->getType());
  578. }
  579. // Retrieves sub-expressions matched by 'matchRelationalIntegerConstantExpr'
  580. // with name 'Id'.
  581. static bool retrieveRelationalIntegerConstantExpr(
  582. const MatchFinder::MatchResult &Result, StringRef Id,
  583. const Expr *&OperandExpr, BinaryOperatorKind &Opcode, const Expr *&Symbol,
  584. APSInt &Value, const Expr *&ConstExpr) {
  585. std::string CastId = (Id + "-cast").str();
  586. std::string SwapId = (Id + "-swap").str();
  587. std::string NegateId = (Id + "-negate").str();
  588. std::string OverloadId = (Id + "-overload").str();
  589. if (const auto *Bin = Result.Nodes.getNodeAs<BinaryOperator>(Id)) {
  590. // Operand received with explicit comparator.
  591. Opcode = Bin->getOpcode();
  592. OperandExpr = Bin;
  593. if (!retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr))
  594. return false;
  595. } else if (const auto *Cast = Result.Nodes.getNodeAs<CastExpr>(CastId)) {
  596. // Operand received with implicit comparator (cast).
  597. Opcode = BO_NE;
  598. OperandExpr = Cast;
  599. Value = APSInt(32, false);
  600. } else if (const auto *OverloadedOperatorExpr =
  601. Result.Nodes.getNodeAs<CXXOperatorCallExpr>(OverloadId)) {
  602. if (canOverloadedOperatorArgsBeModified(OverloadedOperatorExpr, false))
  603. return false;
  604. bool IntegerConstantIsFirstArg = false;
  605. if (const auto *Arg = OverloadedOperatorExpr->getArg(1)) {
  606. if (!Arg->isValueDependent() &&
  607. !Arg->isIntegerConstantExpr(*Result.Context)) {
  608. IntegerConstantIsFirstArg = true;
  609. if (const auto *Arg = OverloadedOperatorExpr->getArg(0)) {
  610. if (!Arg->isValueDependent() &&
  611. !Arg->isIntegerConstantExpr(*Result.Context))
  612. return false;
  613. } else
  614. return false;
  615. }
  616. } else
  617. return false;
  618. Symbol = OverloadedOperatorExpr->getArg(IntegerConstantIsFirstArg ? 1 : 0);
  619. OperandExpr = OverloadedOperatorExpr;
  620. Opcode = BinaryOperator::getOverloadedOpcode(OverloadedOperatorExpr->getOperator());
  621. if (!retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr))
  622. return false;
  623. if (!BinaryOperator::isComparisonOp(Opcode))
  624. return false;
  625. // The call site of this function expects the constant on the RHS,
  626. // so change the opcode accordingly.
  627. if (IntegerConstantIsFirstArg)
  628. Opcode = BinaryOperator::reverseComparisonOp(Opcode);
  629. return true;
  630. } else {
  631. return false;
  632. }
  633. if (!retrieveSymbolicExpr(Result, Id, Symbol))
  634. return false;
  635. if (Result.Nodes.getNodeAs<Expr>(SwapId))
  636. Opcode = BinaryOperator::reverseComparisonOp(Opcode);
  637. if (Result.Nodes.getNodeAs<Expr>(NegateId))
  638. Opcode = BinaryOperator::negateComparisonOp(Opcode);
  639. return true;
  640. }
  641. // Checks for expressions like (X == 4) && (Y != 9)
  642. static bool areSidesBinaryConstExpressions(const BinaryOperator *&BinOp, const ASTContext *AstCtx) {
  643. const auto *LhsBinOp = dyn_cast<BinaryOperator>(BinOp->getLHS());
  644. const auto *RhsBinOp = dyn_cast<BinaryOperator>(BinOp->getRHS());
  645. if (!LhsBinOp || !RhsBinOp)
  646. return false;
  647. auto IsIntegerConstantExpr = [AstCtx](const Expr *E) {
  648. return !E->isValueDependent() && E->isIntegerConstantExpr(*AstCtx);
  649. };
  650. if ((IsIntegerConstantExpr(LhsBinOp->getLHS()) ||
  651. IsIntegerConstantExpr(LhsBinOp->getRHS())) &&
  652. (IsIntegerConstantExpr(RhsBinOp->getLHS()) ||
  653. IsIntegerConstantExpr(RhsBinOp->getRHS())))
  654. return true;
  655. return false;
  656. }
  657. // Retrieves integer constant subexpressions from binary operator expressions
  658. // that have two equivalent sides.
  659. // E.g.: from (X == 5) && (X == 5) retrieves 5 and 5.
  660. static bool retrieveConstExprFromBothSides(const BinaryOperator *&BinOp,
  661. BinaryOperatorKind &MainOpcode,
  662. BinaryOperatorKind &SideOpcode,
  663. const Expr *&LhsConst,
  664. const Expr *&RhsConst,
  665. const ASTContext *AstCtx) {
  666. assert(areSidesBinaryConstExpressions(BinOp, AstCtx) &&
  667. "Both sides of binary operator must be constant expressions!");
  668. MainOpcode = BinOp->getOpcode();
  669. const auto *BinOpLhs = cast<BinaryOperator>(BinOp->getLHS());
  670. const auto *BinOpRhs = cast<BinaryOperator>(BinOp->getRHS());
  671. auto IsIntegerConstantExpr = [AstCtx](const Expr *E) {
  672. return !E->isValueDependent() && E->isIntegerConstantExpr(*AstCtx);
  673. };
  674. LhsConst = IsIntegerConstantExpr(BinOpLhs->getLHS()) ? BinOpLhs->getLHS()
  675. : BinOpLhs->getRHS();
  676. RhsConst = IsIntegerConstantExpr(BinOpRhs->getLHS()) ? BinOpRhs->getLHS()
  677. : BinOpRhs->getRHS();
  678. if (!LhsConst || !RhsConst)
  679. return false;
  680. assert(BinOpLhs->getOpcode() == BinOpRhs->getOpcode() &&
  681. "Sides of the binary operator must be equivalent expressions!");
  682. SideOpcode = BinOpLhs->getOpcode();
  683. return true;
  684. }
  685. static bool isSameRawIdentifierToken(const Token &T1, const Token &T2,
  686. const SourceManager &SM) {
  687. if (T1.getKind() != T2.getKind())
  688. return false;
  689. if (T1.isNot(tok::raw_identifier))
  690. return true;
  691. if (T1.getLength() != T2.getLength())
  692. return false;
  693. return StringRef(SM.getCharacterData(T1.getLocation()), T1.getLength()) ==
  694. StringRef(SM.getCharacterData(T2.getLocation()), T2.getLength());
  695. }
  696. bool isTokAtEndOfExpr(SourceRange ExprSR, Token T, const SourceManager &SM) {
  697. return SM.getExpansionLoc(ExprSR.getEnd()) == T.getLocation();
  698. }
  699. /// Returns true if both LhsExpr and RhsExpr are
  700. /// macro expressions and they are expanded
  701. /// from different macros.
  702. static bool areExprsFromDifferentMacros(const Expr *LhsExpr,
  703. const Expr *RhsExpr,
  704. const ASTContext *AstCtx) {
  705. if (!LhsExpr || !RhsExpr)
  706. return false;
  707. SourceRange Lsr = LhsExpr->getSourceRange();
  708. SourceRange Rsr = RhsExpr->getSourceRange();
  709. if (!Lsr.getBegin().isMacroID() || !Rsr.getBegin().isMacroID())
  710. return false;
  711. const SourceManager &SM = AstCtx->getSourceManager();
  712. const LangOptions &LO = AstCtx->getLangOpts();
  713. std::pair<FileID, unsigned> LsrLocInfo =
  714. SM.getDecomposedLoc(SM.getExpansionLoc(Lsr.getBegin()));
  715. std::pair<FileID, unsigned> RsrLocInfo =
  716. SM.getDecomposedLoc(SM.getExpansionLoc(Rsr.getBegin()));
  717. llvm::MemoryBufferRef MB = SM.getBufferOrFake(LsrLocInfo.first);
  718. const char *LTokenPos = MB.getBufferStart() + LsrLocInfo.second;
  719. const char *RTokenPos = MB.getBufferStart() + RsrLocInfo.second;
  720. Lexer LRawLex(SM.getLocForStartOfFile(LsrLocInfo.first), LO,
  721. MB.getBufferStart(), LTokenPos, MB.getBufferEnd());
  722. Lexer RRawLex(SM.getLocForStartOfFile(RsrLocInfo.first), LO,
  723. MB.getBufferStart(), RTokenPos, MB.getBufferEnd());
  724. Token LTok, RTok;
  725. do { // Compare the expressions token-by-token.
  726. LRawLex.LexFromRawLexer(LTok);
  727. RRawLex.LexFromRawLexer(RTok);
  728. } while (!LTok.is(tok::eof) && !RTok.is(tok::eof) &&
  729. isSameRawIdentifierToken(LTok, RTok, SM) &&
  730. !isTokAtEndOfExpr(Lsr, LTok, SM) &&
  731. !isTokAtEndOfExpr(Rsr, RTok, SM));
  732. return (!isTokAtEndOfExpr(Lsr, LTok, SM) ||
  733. !isTokAtEndOfExpr(Rsr, RTok, SM)) ||
  734. !isSameRawIdentifierToken(LTok, RTok, SM);
  735. }
  736. static bool areExprsMacroAndNonMacro(const Expr *&LhsExpr,
  737. const Expr *&RhsExpr) {
  738. if (!LhsExpr || !RhsExpr)
  739. return false;
  740. SourceLocation LhsLoc = LhsExpr->getExprLoc();
  741. SourceLocation RhsLoc = RhsExpr->getExprLoc();
  742. return LhsLoc.isMacroID() != RhsLoc.isMacroID();
  743. }
  744. } // namespace
  745. void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
  746. const auto AnyLiteralExpr = ignoringParenImpCasts(
  747. anyOf(cxxBoolLiteral(), characterLiteral(), integerLiteral()));
  748. const auto BannedIntegerLiteral =
  749. integerLiteral(expandedByMacro(KnownBannedMacroNames));
  750. const auto BannedAncestor = expr(isRequiresExpr());
  751. // Binary with equivalent operands, like (X != 2 && X != 2).
  752. Finder->addMatcher(
  753. traverse(TK_AsIs,
  754. binaryOperator(
  755. anyOf(isComparisonOperator(),
  756. hasAnyOperatorName("-", "/", "%", "|", "&", "^", "&&",
  757. "||", "=")),
  758. operandsAreEquivalent(),
  759. // Filter noisy false positives.
  760. unless(isInTemplateInstantiation()),
  761. unless(binaryOperatorIsInMacro()),
  762. unless(hasType(realFloatingPointType())),
  763. unless(hasEitherOperand(hasType(realFloatingPointType()))),
  764. unless(hasLHS(AnyLiteralExpr)),
  765. unless(hasDescendant(BannedIntegerLiteral)),
  766. unless(hasAncestor(BannedAncestor)))
  767. .bind("binary")),
  768. this);
  769. // Logical or bitwise operator with equivalent nested operands, like (X && Y
  770. // && X) or (X && (Y && X))
  771. Finder->addMatcher(
  772. binaryOperator(hasAnyOperatorName("|", "&", "||", "&&", "^"),
  773. nestedOperandsAreEquivalent(),
  774. // Filter noisy false positives.
  775. unless(isInTemplateInstantiation()),
  776. unless(binaryOperatorIsInMacro()),
  777. // TODO: if the banned macros are themselves duplicated
  778. unless(hasDescendant(BannedIntegerLiteral)),
  779. unless(hasAncestor(BannedAncestor)))
  780. .bind("nested-duplicates"),
  781. this);
  782. // Conditional (ternary) operator with equivalent operands, like (Y ? X : X).
  783. Finder->addMatcher(
  784. traverse(TK_AsIs,
  785. conditionalOperator(expressionsAreEquivalent(),
  786. // Filter noisy false positives.
  787. unless(conditionalOperatorIsInMacro()),
  788. unless(isInTemplateInstantiation()),
  789. unless(hasAncestor(BannedAncestor)))
  790. .bind("cond")),
  791. this);
  792. // Overloaded operators with equivalent operands.
  793. Finder->addMatcher(
  794. traverse(TK_AsIs,
  795. cxxOperatorCallExpr(
  796. hasAnyOverloadedOperatorName("-", "/", "%", "|", "&", "^",
  797. "==", "!=", "<", "<=", ">",
  798. ">=", "&&", "||", "="),
  799. parametersAreEquivalent(),
  800. // Filter noisy false positives.
  801. unless(isMacro()), unless(isInTemplateInstantiation()),
  802. unless(hasAncestor(BannedAncestor)))
  803. .bind("call")),
  804. this);
  805. // Overloaded operators with equivalent operands.
  806. Finder->addMatcher(
  807. cxxOperatorCallExpr(
  808. hasAnyOverloadedOperatorName("|", "&", "||", "&&", "^"),
  809. nestedParametersAreEquivalent(), argumentCountIs(2),
  810. // Filter noisy false positives.
  811. unless(isMacro()), unless(isInTemplateInstantiation()),
  812. unless(hasAncestor(BannedAncestor)))
  813. .bind("nested-duplicates"),
  814. this);
  815. // Match expressions like: !(1 | 2 | 3)
  816. Finder->addMatcher(
  817. traverse(TK_AsIs,
  818. implicitCastExpr(
  819. hasImplicitDestinationType(isInteger()),
  820. has(unaryOperator(
  821. hasOperatorName("!"),
  822. hasUnaryOperand(ignoringParenImpCasts(binaryOperator(
  823. hasAnyOperatorName("|", "&"),
  824. hasLHS(anyOf(
  825. binaryOperator(hasAnyOperatorName("|", "&")),
  826. integerLiteral())),
  827. hasRHS(integerLiteral())))))
  828. .bind("logical-bitwise-confusion")),
  829. unless(hasAncestor(BannedAncestor)))),
  830. this);
  831. // Match expressions like: (X << 8) & 0xFF
  832. Finder->addMatcher(
  833. traverse(TK_AsIs,
  834. binaryOperator(
  835. hasOperatorName("&"),
  836. hasOperands(ignoringParenImpCasts(binaryOperator(
  837. hasOperatorName("<<"),
  838. hasRHS(ignoringParenImpCasts(
  839. integerLiteral().bind("shift-const"))))),
  840. ignoringParenImpCasts(
  841. integerLiteral().bind("and-const"))),
  842. unless(hasAncestor(BannedAncestor)))
  843. .bind("left-right-shift-confusion")),
  844. this);
  845. // Match common expressions and apply more checks to find redundant
  846. // sub-expressions.
  847. // a) Expr <op> K1 == K2
  848. // b) Expr <op> K1 == Expr
  849. // c) Expr <op> K1 == Expr <op> K2
  850. // see: 'checkArithmeticExpr' and 'checkBitwiseExpr'
  851. const auto BinOpCstLeft = matchBinOpIntegerConstantExpr("lhs");
  852. const auto BinOpCstRight = matchBinOpIntegerConstantExpr("rhs");
  853. const auto CstRight = matchIntegerConstantExpr("rhs");
  854. const auto SymRight = matchSymbolicExpr("rhs");
  855. // Match expressions like: x <op> 0xFF == 0xF00.
  856. Finder->addMatcher(
  857. traverse(TK_AsIs, binaryOperator(isComparisonOperator(),
  858. hasOperands(BinOpCstLeft, CstRight),
  859. unless(hasAncestor(BannedAncestor)))
  860. .bind("binop-const-compare-to-const")),
  861. this);
  862. // Match expressions like: x <op> 0xFF == x.
  863. Finder->addMatcher(
  864. traverse(
  865. TK_AsIs,
  866. binaryOperator(isComparisonOperator(),
  867. anyOf(allOf(hasLHS(BinOpCstLeft), hasRHS(SymRight)),
  868. allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft))),
  869. unless(hasAncestor(BannedAncestor)))
  870. .bind("binop-const-compare-to-sym")),
  871. this);
  872. // Match expressions like: x <op> 10 == x <op> 12.
  873. Finder->addMatcher(
  874. traverse(TK_AsIs,
  875. binaryOperator(isComparisonOperator(), hasLHS(BinOpCstLeft),
  876. hasRHS(BinOpCstRight),
  877. // Already reported as redundant.
  878. unless(operandsAreEquivalent()),
  879. unless(hasAncestor(BannedAncestor)))
  880. .bind("binop-const-compare-to-binop-const")),
  881. this);
  882. // Match relational expressions combined with logical operators and find
  883. // redundant sub-expressions.
  884. // see: 'checkRelationalExpr'
  885. // Match expressions like: x < 2 && x > 2.
  886. const auto ComparisonLeft = matchRelationalIntegerConstantExpr("lhs");
  887. const auto ComparisonRight = matchRelationalIntegerConstantExpr("rhs");
  888. Finder->addMatcher(
  889. traverse(TK_AsIs,
  890. binaryOperator(hasAnyOperatorName("||", "&&"),
  891. hasLHS(ComparisonLeft), hasRHS(ComparisonRight),
  892. // Already reported as redundant.
  893. unless(operandsAreEquivalent()),
  894. unless(hasAncestor(BannedAncestor)))
  895. .bind("comparisons-of-symbol-and-const")),
  896. this);
  897. }
  898. void RedundantExpressionCheck::checkArithmeticExpr(
  899. const MatchFinder::MatchResult &Result) {
  900. APSInt LhsValue, RhsValue;
  901. const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr;
  902. BinaryOperatorKind LhsOpcode, RhsOpcode;
  903. if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
  904. "binop-const-compare-to-sym")) {
  905. BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
  906. if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
  907. LhsValue) ||
  908. !retrieveSymbolicExpr(Result, "rhs", RhsSymbol) ||
  909. !areEquivalentExpr(LhsSymbol, RhsSymbol))
  910. return;
  911. // Check expressions: x + k == x or x - k == x.
  912. if (LhsOpcode == BO_Add || LhsOpcode == BO_Sub) {
  913. if ((LhsValue != 0 && Opcode == BO_EQ) ||
  914. (LhsValue == 0 && Opcode == BO_NE))
  915. diag(ComparisonOperator->getOperatorLoc(),
  916. "logical expression is always false");
  917. else if ((LhsValue == 0 && Opcode == BO_EQ) ||
  918. (LhsValue != 0 && Opcode == BO_NE))
  919. diag(ComparisonOperator->getOperatorLoc(),
  920. "logical expression is always true");
  921. }
  922. } else if (const auto *ComparisonOperator =
  923. Result.Nodes.getNodeAs<BinaryOperator>(
  924. "binop-const-compare-to-binop-const")) {
  925. BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
  926. if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
  927. LhsValue) ||
  928. !retrieveBinOpIntegerConstantExpr(Result, "rhs", RhsOpcode, RhsSymbol,
  929. RhsValue) ||
  930. !areEquivalentExpr(LhsSymbol, RhsSymbol))
  931. return;
  932. transformSubToCanonicalAddExpr(LhsOpcode, LhsValue);
  933. transformSubToCanonicalAddExpr(RhsOpcode, RhsValue);
  934. // Check expressions: x + 1 == x + 2 or x + 1 != x + 2.
  935. if (LhsOpcode == BO_Add && RhsOpcode == BO_Add) {
  936. if ((Opcode == BO_EQ && APSInt::compareValues(LhsValue, RhsValue) == 0) ||
  937. (Opcode == BO_NE && APSInt::compareValues(LhsValue, RhsValue) != 0)) {
  938. diag(ComparisonOperator->getOperatorLoc(),
  939. "logical expression is always true");
  940. } else if ((Opcode == BO_EQ &&
  941. APSInt::compareValues(LhsValue, RhsValue) != 0) ||
  942. (Opcode == BO_NE &&
  943. APSInt::compareValues(LhsValue, RhsValue) == 0)) {
  944. diag(ComparisonOperator->getOperatorLoc(),
  945. "logical expression is always false");
  946. }
  947. }
  948. }
  949. }
  950. static bool exprEvaluatesToZero(BinaryOperatorKind Opcode, APSInt Value) {
  951. return (Opcode == BO_And || Opcode == BO_AndAssign) && Value == 0;
  952. }
  953. static bool exprEvaluatesToBitwiseNegatedZero(BinaryOperatorKind Opcode,
  954. APSInt Value) {
  955. return (Opcode == BO_Or || Opcode == BO_OrAssign) && ~Value == 0;
  956. }
  957. static bool exprEvaluatesToSymbolic(BinaryOperatorKind Opcode, APSInt Value) {
  958. return ((Opcode == BO_Or || Opcode == BO_OrAssign) && Value == 0) ||
  959. ((Opcode == BO_And || Opcode == BO_AndAssign) && ~Value == 0);
  960. }
  961. void RedundantExpressionCheck::checkBitwiseExpr(
  962. const MatchFinder::MatchResult &Result) {
  963. if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
  964. "binop-const-compare-to-const")) {
  965. BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
  966. APSInt LhsValue, RhsValue;
  967. const Expr *LhsSymbol = nullptr;
  968. BinaryOperatorKind LhsOpcode;
  969. if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
  970. LhsValue) ||
  971. !retrieveIntegerConstantExpr(Result, "rhs", RhsValue))
  972. return;
  973. uint64_t LhsConstant = LhsValue.getZExtValue();
  974. uint64_t RhsConstant = RhsValue.getZExtValue();
  975. SourceLocation Loc = ComparisonOperator->getOperatorLoc();
  976. // Check expression: x & k1 == k2 (i.e. x & 0xFF == 0xF00)
  977. if (LhsOpcode == BO_And && (LhsConstant & RhsConstant) != RhsConstant) {
  978. if (Opcode == BO_EQ)
  979. diag(Loc, "logical expression is always false");
  980. else if (Opcode == BO_NE)
  981. diag(Loc, "logical expression is always true");
  982. }
  983. // Check expression: x | k1 == k2 (i.e. x | 0xFF == 0xF00)
  984. if (LhsOpcode == BO_Or && (LhsConstant | RhsConstant) != RhsConstant) {
  985. if (Opcode == BO_EQ)
  986. diag(Loc, "logical expression is always false");
  987. else if (Opcode == BO_NE)
  988. diag(Loc, "logical expression is always true");
  989. }
  990. } else if (const auto *IneffectiveOperator =
  991. Result.Nodes.getNodeAs<BinaryOperator>(
  992. "ineffective-bitwise")) {
  993. APSInt Value;
  994. const Expr *Sym = nullptr, *ConstExpr = nullptr;
  995. if (!retrieveSymbolicExpr(Result, "ineffective-bitwise", Sym) ||
  996. !retrieveIntegerConstantExpr(Result, "ineffective-bitwise", Value,
  997. ConstExpr))
  998. return;
  999. if((Value != 0 && ~Value != 0) || Sym->getExprLoc().isMacroID())
  1000. return;
  1001. SourceLocation Loc = IneffectiveOperator->getOperatorLoc();
  1002. BinaryOperatorKind Opcode = IneffectiveOperator->getOpcode();
  1003. if (exprEvaluatesToZero(Opcode, Value)) {
  1004. diag(Loc, "expression always evaluates to 0");
  1005. } else if (exprEvaluatesToBitwiseNegatedZero(Opcode, Value)) {
  1006. SourceRange ConstExprRange(ConstExpr->getBeginLoc(),
  1007. ConstExpr->getEndLoc());
  1008. StringRef ConstExprText = Lexer::getSourceText(
  1009. CharSourceRange::getTokenRange(ConstExprRange), *Result.SourceManager,
  1010. Result.Context->getLangOpts());
  1011. diag(Loc, "expression always evaluates to '%0'") << ConstExprText;
  1012. } else if (exprEvaluatesToSymbolic(Opcode, Value)) {
  1013. SourceRange SymExprRange(Sym->getBeginLoc(), Sym->getEndLoc());
  1014. StringRef ExprText = Lexer::getSourceText(
  1015. CharSourceRange::getTokenRange(SymExprRange), *Result.SourceManager,
  1016. Result.Context->getLangOpts());
  1017. diag(Loc, "expression always evaluates to '%0'") << ExprText;
  1018. }
  1019. }
  1020. }
  1021. void RedundantExpressionCheck::checkRelationalExpr(
  1022. const MatchFinder::MatchResult &Result) {
  1023. if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
  1024. "comparisons-of-symbol-and-const")) {
  1025. // Matched expressions are: (x <op> k1) <REL> (x <op> k2).
  1026. // E.g.: (X < 2) && (X > 4)
  1027. BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
  1028. const Expr *LhsExpr = nullptr, *RhsExpr = nullptr;
  1029. const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr;
  1030. const Expr *LhsConst = nullptr, *RhsConst = nullptr;
  1031. BinaryOperatorKind LhsOpcode, RhsOpcode;
  1032. APSInt LhsValue, RhsValue;
  1033. if (!retrieveRelationalIntegerConstantExpr(
  1034. Result, "lhs", LhsExpr, LhsOpcode, LhsSymbol, LhsValue, LhsConst) ||
  1035. !retrieveRelationalIntegerConstantExpr(
  1036. Result, "rhs", RhsExpr, RhsOpcode, RhsSymbol, RhsValue, RhsConst) ||
  1037. !areEquivalentExpr(LhsSymbol, RhsSymbol))
  1038. return;
  1039. // Bring expr to a canonical form: smallest constant must be on the left.
  1040. if (APSInt::compareValues(LhsValue, RhsValue) > 0) {
  1041. std::swap(LhsExpr, RhsExpr);
  1042. std::swap(LhsValue, RhsValue);
  1043. std::swap(LhsSymbol, RhsSymbol);
  1044. std::swap(LhsOpcode, RhsOpcode);
  1045. }
  1046. // Constants come from two different macros, or one of them is a macro.
  1047. if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
  1048. areExprsMacroAndNonMacro(LhsConst, RhsConst))
  1049. return;
  1050. if ((Opcode == BO_LAnd || Opcode == BO_LOr) &&
  1051. areEquivalentRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
  1052. diag(ComparisonOperator->getOperatorLoc(),
  1053. "equivalent expression on both sides of logical operator");
  1054. return;
  1055. }
  1056. if (Opcode == BO_LAnd) {
  1057. if (areExclusiveRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
  1058. diag(ComparisonOperator->getOperatorLoc(),
  1059. "logical expression is always false");
  1060. } else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
  1061. diag(LhsExpr->getExprLoc(), "expression is redundant");
  1062. } else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) {
  1063. diag(RhsExpr->getExprLoc(), "expression is redundant");
  1064. }
  1065. }
  1066. if (Opcode == BO_LOr) {
  1067. if (rangesFullyCoverDomain(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
  1068. diag(ComparisonOperator->getOperatorLoc(),
  1069. "logical expression is always true");
  1070. } else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) {
  1071. diag(RhsExpr->getExprLoc(), "expression is redundant");
  1072. } else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) {
  1073. diag(LhsExpr->getExprLoc(), "expression is redundant");
  1074. }
  1075. }
  1076. }
  1077. }
  1078. void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) {
  1079. if (const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binary")) {
  1080. // If the expression's constants are macros, check whether they are
  1081. // intentional.
  1082. if (areSidesBinaryConstExpressions(BinOp, Result.Context)) {
  1083. const Expr *LhsConst = nullptr, *RhsConst = nullptr;
  1084. BinaryOperatorKind MainOpcode, SideOpcode;
  1085. if (!retrieveConstExprFromBothSides(BinOp, MainOpcode, SideOpcode,
  1086. LhsConst, RhsConst, Result.Context))
  1087. return;
  1088. if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
  1089. areExprsMacroAndNonMacro(LhsConst, RhsConst))
  1090. return;
  1091. }
  1092. diag(BinOp->getOperatorLoc(), "both sides of operator are equivalent");
  1093. }
  1094. if (const auto *CondOp =
  1095. Result.Nodes.getNodeAs<ConditionalOperator>("cond")) {
  1096. const Expr *TrueExpr = CondOp->getTrueExpr();
  1097. const Expr *FalseExpr = CondOp->getFalseExpr();
  1098. if (areExprsFromDifferentMacros(TrueExpr, FalseExpr, Result.Context) ||
  1099. areExprsMacroAndNonMacro(TrueExpr, FalseExpr))
  1100. return;
  1101. diag(CondOp->getColonLoc(),
  1102. "'true' and 'false' expressions are equivalent");
  1103. }
  1104. if (const auto *Call = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("call")) {
  1105. if (canOverloadedOperatorArgsBeModified(Call, true))
  1106. return;
  1107. diag(Call->getOperatorLoc(),
  1108. "both sides of overloaded operator are equivalent");
  1109. }
  1110. if (const auto *Op = Result.Nodes.getNodeAs<Expr>("nested-duplicates")) {
  1111. const auto *Call = dyn_cast<CXXOperatorCallExpr>(Op);
  1112. if (Call && canOverloadedOperatorArgsBeModified(Call, true))
  1113. return;
  1114. StringRef Message =
  1115. Call ? "overloaded operator has equivalent nested operands"
  1116. : "operator has equivalent nested operands";
  1117. const auto Diag = diag(Op->getExprLoc(), Message);
  1118. for (const auto &KeyValue : Result.Nodes.getMap()) {
  1119. if (StringRef(KeyValue.first).startswith("duplicate"))
  1120. Diag << KeyValue.second.getSourceRange();
  1121. }
  1122. }
  1123. if (const auto *NegateOperator =
  1124. Result.Nodes.getNodeAs<UnaryOperator>("logical-bitwise-confusion")) {
  1125. SourceLocation OperatorLoc = NegateOperator->getOperatorLoc();
  1126. auto Diag =
  1127. diag(OperatorLoc,
  1128. "ineffective logical negation operator used; did you mean '~'?");
  1129. SourceLocation LogicalNotLocation = OperatorLoc.getLocWithOffset(1);
  1130. if (!LogicalNotLocation.isMacroID())
  1131. Diag << FixItHint::CreateReplacement(
  1132. CharSourceRange::getCharRange(OperatorLoc, LogicalNotLocation), "~");
  1133. }
  1134. if (const auto *BinaryAndExpr = Result.Nodes.getNodeAs<BinaryOperator>(
  1135. "left-right-shift-confusion")) {
  1136. const auto *ShiftingConst = Result.Nodes.getNodeAs<Expr>("shift-const");
  1137. assert(ShiftingConst && "Expr* 'ShiftingConst' is nullptr!");
  1138. std::optional<llvm::APSInt> ShiftingValue =
  1139. ShiftingConst->getIntegerConstantExpr(*Result.Context);
  1140. if (!ShiftingValue)
  1141. return;
  1142. const auto *AndConst = Result.Nodes.getNodeAs<Expr>("and-const");
  1143. assert(AndConst && "Expr* 'AndCont' is nullptr!");
  1144. std::optional<llvm::APSInt> AndValue =
  1145. AndConst->getIntegerConstantExpr(*Result.Context);
  1146. if (!AndValue)
  1147. return;
  1148. // If ShiftingConst is shifted left with more bits than the position of the
  1149. // leftmost 1 in the bit representation of AndValue, AndConstant is
  1150. // ineffective.
  1151. if (AndValue->getActiveBits() > *ShiftingValue)
  1152. return;
  1153. auto Diag = diag(BinaryAndExpr->getOperatorLoc(),
  1154. "ineffective bitwise and operation");
  1155. }
  1156. // Check for the following bound expressions:
  1157. // - "binop-const-compare-to-sym",
  1158. // - "binop-const-compare-to-binop-const",
  1159. // Produced message:
  1160. // -> "logical expression is always false/true"
  1161. checkArithmeticExpr(Result);
  1162. // Check for the following bound expression:
  1163. // - "binop-const-compare-to-const",
  1164. // - "ineffective-bitwise"
  1165. // Produced message:
  1166. // -> "logical expression is always false/true"
  1167. // -> "expression always evaluates to ..."
  1168. checkBitwiseExpr(Result);
  1169. // Check for te following bound expression:
  1170. // - "comparisons-of-symbol-and-const",
  1171. // Produced messages:
  1172. // -> "equivalent expression on both sides of logical operator",
  1173. // -> "logical expression is always false/true"
  1174. // -> "expression is redundant"
  1175. checkRelationalExpr(Result);
  1176. }
  1177. } // namespace clang::tidy::misc