summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp27
-rw-r--r--clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp6
2 files changed, 4 insertions, 29 deletions
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
index 4977c2d7ed7..bfcef89e6bb 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp
@@ -47,25 +47,6 @@ AST_MATCHER_P(Expr, hasParentIgnoringImpCasts,
return InnerMatcher.matches(*E, Finder, Builder);
}
-AST_MATCHER(ImplicitCastExpr, isArrayToPointerDecay) {
- return Node.getCastKind() == CK_ArrayToPointerDecay;
-}
-
-AST_MATCHER(ImplicitCastExpr, sysSymbolDecayInSysHeader) {
- auto &SM = Finder->getASTContext().getSourceManager();
- if (SM.isInSystemMacro(Node.getLocStart())) {
- if (isa<PredefinedExpr>(Node.getSubExpr()))
- return true;
-
- if (const auto *SymbolDeclRef = dyn_cast<DeclRefExpr>(Node.getSubExpr())) {
- const ValueDecl *SymbolDecl = SymbolDeclRef->getDecl();
- if (SymbolDecl && SM.isInSystemHeader(SymbolDecl->getLocation()))
- return true;
- }
- }
- return false;
-}
-
void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
@@ -75,12 +56,10 @@ void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
// 2) inside a range-for over an array
// 3) if it converts a string literal to a pointer
Finder->addMatcher(
- implicitCastExpr(isArrayToPointerDecay(),
- unless(hasParent(arraySubscriptExpr())),
+ implicitCastExpr(unless(hasParent(arraySubscriptExpr())),
unless(hasParentIgnoringImpCasts(explicitCastExpr())),
unless(isInsideOfRangeBeginEndStmt()),
- unless(hasSourceExpression(stringLiteral())),
- unless(sysSymbolDecayInSysHeader()))
+ unless(hasSourceExpression(stringLiteral())))
.bind("cast"),
this);
}
@@ -88,6 +67,8 @@ void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
void ProBoundsArrayToPointerDecayCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *MatchedCast = Result.Nodes.getNodeAs<ImplicitCastExpr>("cast");
+ if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay)
+ return;
diag(MatchedCast->getExprLoc(), "do not implicitly decay an array into a "
"pointer; consider using gsl::array_view or "
diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
index 22d98bcd121..ce192805dda 100644
--- a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
+++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp
@@ -1,5 +1,4 @@
// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t
-#include <assert.h>
#include <stddef.h>
namespace gsl {
@@ -35,11 +34,6 @@ void f() {
for (auto &e : a) // OK, iteration internally decays array to pointer
e = 1;
-
- assert(false); // OK, array decay inside system header macro
-
- assert(a);
- // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay]
}
const char *g() {
OpenPOWER on IntegriCloud