[PATCH] D58609: [clang-tidy] bugprone-string-integer-assignment: Reduce false positives.

Clement Courbet via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 25 07:04:18 PST 2019


courbet updated this revision to Diff 188165.
courbet marked 3 inline comments as done.
courbet added a comment.

- address review comments


Repository:
  rCTE Clang Tools Extra

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58609/new/

https://reviews.llvm.org/D58609

Files:
  clang-tidy/bugprone/StringIntegerAssignmentCheck.cpp
  test/clang-tidy/bugprone-string-integer-assignment.cpp


Index: test/clang-tidy/bugprone-string-integer-assignment.cpp
===================================================================
--- test/clang-tidy/bugprone-string-integer-assignment.cpp
+++ test/clang-tidy/bugprone-string-integer-assignment.cpp
@@ -50,4 +50,8 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: an integer is interpreted as a chara
 // CHECK-FIXES: {{^}}  as = 6;{{$}}
 
+  // Likely character expressions.
+  s += x & 0xff;
+
+  s += 'a' + (x % 26);
 }
Index: clang-tidy/bugprone/StringIntegerAssignmentCheck.cpp
===================================================================
--- clang-tidy/bugprone/StringIntegerAssignmentCheck.cpp
+++ clang-tidy/bugprone/StringIntegerAssignmentCheck.cpp
@@ -35,11 +35,44 @@
       this);
 }
 
+static bool isLikelyCharExpression(const Expr *Argument,
+                                   const ASTContext &Ctx) {
+  const auto *BinOp = dyn_cast<BinaryOperator>(Argument);
+  if (!BinOp) {
+    return false;
+  }
+  const auto *LHS = BinOp->getLHS()->IgnoreParenImpCasts();
+  const auto *RHS = BinOp->getRHS()->IgnoreParenImpCasts();
+  // <expr> & <mask>, mask is a compile time constant.
+  Expr::EvalResult RHSVal;
+  if (BinOp->getOpcode() == BO_And &&
+      (RHS->EvaluateAsInt(RHSVal, Ctx, Expr::SE_AllowSideEffects) ||
+       LHS->EvaluateAsInt(RHSVal, Ctx, Expr::SE_AllowSideEffects))) {
+    return true;
+  }
+  // <char literal> + (<expr> % <mod>), where <base> is a char literal.
+  const auto IsCharPlusModExpr = [](const Expr *L, const Expr *R) {
+    const auto *ROp = dyn_cast<BinaryOperator>(R);
+    return ROp && ROp->getOpcode() == BO_Rem && isa<CharacterLiteral>(L);
+  };
+  if (BinOp->getOpcode() == BO_Add) {
+    if (IsCharPlusModExpr(LHS, RHS) || IsCharPlusModExpr(RHS, LHS)) {
+      return true;
+    }
+  }
+  return false;
+}
+
 void StringIntegerAssignmentCheck::check(
     const MatchFinder::MatchResult &Result) {
   const auto *Argument = Result.Nodes.getNodeAs<Expr>("expr");
   SourceLocation Loc = Argument->getBeginLoc();
 
+  // Try to detect a few common expressions to reduce false positives.
+  if (isLikelyCharExpression(Argument, *Result.Context)) {
+    return;
+  }
+
   auto Diag =
       diag(Loc, "an integer is interpreted as a character code when assigning "
                 "it to a string; if this is intended, cast the integer to the "


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58609.188165.patch
Type: text/x-patch
Size: 2352 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190225/385bfb3e/attachment-0001.bin>


More information about the cfe-commits mailing list