[clang-tools-extra] r254182 - [clang-tidy] cppcoreguidelines-pro-bounds-pointer-arithmetic: ignore generated pointer arithmetic

Matthias Gehre via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 14:32:11 PST 2015


Author: mgehre
Date: Thu Nov 26 16:32:11 2015
New Revision: 254182

URL: http://llvm.org/viewvc/llvm-project?rev=254182&view=rev
Log:
[clang-tidy] cppcoreguidelines-pro-bounds-pointer-arithmetic: ignore generated pointer arithmetic

Summary:
Inside a range-based for-loop over an array, the compiler
generates pointer arithmetic (end = array + size). Don't flag this.

Reviewers: alexfh, sbenza, bkramer, aaron.ballman

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D14582

Modified:
    clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic.cpp

Modified: clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp?rev=254182&r1=254181&r2=254182&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp Thu Nov 26 16:32:11 2015
@@ -22,9 +22,11 @@ void ProBoundsPointerArithmeticCheck::re
 
   // Flag all operators +, -, +=, -=, ++, -- that result in a pointer
   Finder->addMatcher(
-      binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("-"),
-                           hasOperatorName("+="), hasOperatorName("-=")),
-                     hasType(pointerType()))
+      binaryOperator(
+          anyOf(hasOperatorName("+"), hasOperatorName("-"),
+                hasOperatorName("+="), hasOperatorName("-=")),
+          hasType(pointerType()),
+          unless(hasLHS(ignoringImpCasts(declRefExpr(to(isImplicit()))))))
           .bind("expr"),
       this);
 
@@ -36,8 +38,10 @@ void ProBoundsPointerArithmeticCheck::re
 
   // Array subscript on a pointer (not an array) is also pointer arithmetic
   Finder->addMatcher(
-      arraySubscriptExpr(hasBase(ignoringImpCasts(anyOf(hasType(pointerType()),
-                                                        hasType(decayedType(hasDecayedType(pointerType())))))))
+      arraySubscriptExpr(
+          hasBase(ignoringImpCasts(
+              anyOf(hasType(pointerType()),
+                    hasType(decayedType(hasDecayedType(pointerType())))))))
           .bind("expr"),
       this);
 }

Modified: clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic.cpp?rev=254182&r1=254181&r2=254182&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-pointer-arithmetic.cpp Thu Nov 26 16:32:11 2015
@@ -84,4 +84,6 @@ void okay() {
   i = j - 1;
 
   auto diff = p - q; // OK, result is arithmetic
+
+  for(int ii : a) ; // OK, pointer arithmetic generated by compiler
 }




More information about the cfe-commits mailing list