[PATCH] D14517: Fix bug 25362 "cppcoreguidelines-pro-bounds-array-to-pointer-decay does not consider const"
Matthias Gehre via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 9 14:40:50 PST 2015
mgehre created this revision.
mgehre added reviewers: alexfh, sbenza, bkramer, aaron.ballman.
mgehre added a subscriber: cfe-commits.
The current matcher is
implicitCastExpr(unless(hasParent(explicitCastExpr())))
but the AST in the bug is
`-CXXStaticCastExpr 0x2bb64f8 <col:21, col:55> 'void *const *'
static_cast<void *const *> <NoOp>
`-ImplicitCastExpr 0x2bb64e0 <col:47> 'void *const *' <NoOp>
`-ImplicitCastExpr 0x2bb64c8 <col:47> 'void **'
<ArrayToPointerDecay>
`-DeclRefExpr 0x2bb6458 <col:47> 'void *[2]' lvalue Var
0x2bb59d0 'addrlist' 'void *[2]'
i.e. an ImplicitCastExpr (const cast) between decay and explicit cast.
http://reviews.llvm.org/D14517
Files:
clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
Index: test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
===================================================================
--- test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
+++ test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
@@ -39,3 +39,9 @@
const char *g() {
return "clang"; // OK, decay string literal to pointer
}
+
+void bug25362() {
+ void *addrlist[2];
+ void backtrace_symbols(void *const *buffer);
+ backtrace_symbols(static_cast<void *const*>(addrlist)); // OK, explicit cast
+}
Index: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
===================================================================
--- clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
+++ clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
@@ -28,6 +28,21 @@
.matches(Node, Finder, Builder);
}
+AST_MATCHER_P(Expr, hasParentIgnoringImpCasts,
+ ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
+ auto E = &Node;
+ do {
+ auto Parents = Finder->getASTContext().getParents(*E);
+ if (Parents.size() != 1)
+ return false;
+ E = Parents[0].get<Expr>();
+ if (!E)
+ return false;
+ } while (isa<ImplicitCastExpr>(E));
+
+ return InnerMatcher.matches(*E, Finder, Builder);
+}
+
void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
@@ -38,7 +53,7 @@
// 3) if it converts a string literal to a pointer
Finder->addMatcher(
implicitCastExpr(unless(hasParent(arraySubscriptExpr())),
- unless(hasParent(explicitCastExpr())),
+ unless(hasParentIgnoringImpCasts(explicitCastExpr())),
unless(isInsideOfRangeBeginEndStmt()),
unless(hasSourceExpression(stringLiteral())))
.bind("cast"),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14517.39756.patch
Type: text/x-patch
Size: 1929 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151109/629bcd5e/attachment.bin>
More information about the cfe-commits
mailing list