diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp index bfcef89e6bb..4977c2d7ed7 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp @@ -47,6 +47,25 @@ 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; @@ -56,10 +75,12 @@ 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(unless(hasParent(arraySubscriptExpr())), + implicitCastExpr(isArrayToPointerDecay(), + unless(hasParent(arraySubscriptExpr())), unless(hasParentIgnoringImpCasts(explicitCastExpr())), unless(isInsideOfRangeBeginEndStmt()), - unless(hasSourceExpression(stringLiteral()))) + unless(hasSourceExpression(stringLiteral())), + unless(sysSymbolDecayInSysHeader())) .bind("cast"), this); } @@ -67,8 +88,6 @@ 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 " |