[clang-tools-extra] r346555 - [clang-tidy] fix PR39583 - ignoring ParenCast for string-literals in pro-bounds-array-to-pointer-decay

Jonas Toth via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 9 12:57:29 PST 2018


Author: jonastoth
Date: Fri Nov  9 12:57:28 2018
New Revision: 346555

URL: http://llvm.org/viewvc/llvm-project?rev=346555&view=rev
Log:
[clang-tidy] fix PR39583 - ignoring ParenCast for string-literals in pro-bounds-array-to-pointer-decay

Summary:
The fix to the issue that `const char* p = ("foo")` is diagnosed as decay
is to ignored the ParenCast.
Resolves PR39583

Reviewers: aaron.ballman, alexfh, hokein

Reviewed By: aaron.ballman

Subscribers: nemanjai, xazax.hun, kbarton, cfe-commits

Differential Revision: https://reviews.llvm.org/D54281

Modified:
    clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp

Modified: clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp?rev=346555&r1=346554&r2=346555&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp Fri Nov  9 12:57:28 2018
@@ -58,10 +58,11 @@ void ProBoundsArrayToPointerDecayCheck::
   // 2) inside a range-for over an array
   // 3) if it converts a string literal to a pointer
   Finder->addMatcher(
-      implicitCastExpr(unless(hasParent(arraySubscriptExpr())),
-                       unless(hasParentIgnoringImpCasts(explicitCastExpr())),
-                       unless(isInsideOfRangeBeginEndStmt()),
-                       unless(hasSourceExpression(stringLiteral())))
+      implicitCastExpr(
+          unless(hasParent(arraySubscriptExpr())),
+          unless(hasParentIgnoringImpCasts(explicitCastExpr())),
+          unless(isInsideOfRangeBeginEndStmt()),
+          unless(hasSourceExpression(ignoringParens(stringLiteral()))))
           .bind("cast"),
       this);
 }

Modified: clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp?rev=346555&r1=346554&r2=346555&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp Fri Nov  9 12:57:28 2018
@@ -30,6 +30,7 @@ void f() {
   arrayviewfun(av); // OK
 
   int i = a[0];      // OK
+  int j = a[(1 + 2)];// OK
   pointerfun(&a[0]); // OK
 
   for (auto &e : a) // OK, iteration internally decays array to pointer
@@ -39,6 +40,9 @@ void f() {
 const char *g() {
   return "clang"; // OK, decay string literal to pointer
 }
+const char *g2() {
+    return ("clang"); // OK, ParenExpr hides the literal-pointer decay
+}
 
 void f2(void *const *);
 void bug25362() {




More information about the cfe-commits mailing list