================
@@ -141,6 +168,67 @@ void UseStdBitCheck::check(const MatchFinder::MatchResult
&Result) {
<< IncludeInserter.createIncludeInsertion(
Source.getFileID(MatchedExpr->getBeginLoc()), "<bit>");
}
+ } else if (const auto *MatchedExpr =
+ Result.Nodes.getNodeAs<Expr>("rotate_expr")) {
+ // Detect if the expression is an explicit cast. If that's the case we
don't
+ // need to insert a cast.
+ const Expr *ParentExprOrSelf = getParentExprOrSelf(MatchedExpr, Context);
+ bool HasExplicitIntegerCast = false;
+ if (const auto *CE = dyn_cast<CastExpr>(ParentExprOrSelf)) {
+ HasExplicitIntegerCast =
+ CE->getType()->isIntegerType() && !isa<ImplicitCastExpr>(CE);
+ }
+
+ const auto *MatchedVarDecl = Result.Nodes.getNodeAs<VarDecl>("v");
+ const llvm::APInt ShiftLeftAmount =
+
Result.Nodes.getNodeAs<IntegerLiteral>("shift_left_amount")->getValue();
+ const llvm::APInt ShiftRightAmount =
+ Result.Nodes.getNodeAs<IntegerLiteral>("shift_right_amount")
+ ->getValue();
+ const uint64_t MatchedVarSize =
+ Context.getTypeSize(MatchedVarDecl->getType());
+
+ // Overflowing shifts
+ if (ShiftLeftAmount.sge(MatchedVarSize))
+ return;
+ if (ShiftRightAmount.sge(MatchedVarSize))
+ return;
+ // Not a rotation.
+ if (MatchedVarSize != (ShiftLeftAmount + ShiftRightAmount))
+ return;
+
+ // Only insert cast if the operand is the result is not subject to cast and
+ // some implicit promotion happened.
+ const bool NeedsIntCast =
+ StrictMode && !HasExplicitIntegerCast &&
+ Context.getTypeSize(MatchedExpr->getType()) > MatchedVarSize;
+ const bool IsRotl = ShiftRightAmount.sge(ShiftLeftAmount);
+
+ const StringRef ReplacementFuncName = IsRotl ? "rotl" : "rotr";
+ const uint64_t ReplacementShiftAmount =
+ (IsRotl ? ShiftLeftAmount : ShiftRightAmount).getZExtValue();
+ auto Diag = diag(MatchedExpr->getBeginLoc(), "use 'std::%0' instead")
+ << ReplacementFuncName;
+ if (auto R = MatchedExpr->getSourceRange();
+ R.getBegin().isMacroID() && !R.getEnd().isMacroID())
----------------
zwuis wrote:
```diff
-R.getBegin().isMacroID() && !R.getEnd().isMacroID())
+R.getBegin().isMacroID() || R.getEnd().isMacroID())
```
https://github.com/llvm/llvm-project/pull/186324
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits