Skip to content

Commit

Permalink
[Fix]"[-Wunsafe-buffer-usage] Add a new forEachDescendant matcher t…
Browse files Browse the repository at this point in the history
…hat skips callable declarations"

The original patch in commit b2ac5fd
causes compilation errors which can be reproduced by the
`-fdelayed-template-parsing` flag.  This commit fixes the problem.

Related differential revision: https://reviews.llvm.org/D138329
  • Loading branch information
ziqingluo-90 committed Jan 5, 2023
1 parent 8641687 commit ef47a0a
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions clang/lib/Analysis/UnsafeBufferUsage.cpp
Expand Up @@ -15,7 +15,7 @@ using namespace llvm;
using namespace clang;
using namespace ast_matchers;

namespace clang::ast_matchers::internal {
namespace clang::ast_matchers {
// A `RecursiveASTVisitor` that traverses all descendants of a given node "n"
// except for those belonging to a different callable of "n".
class MatchDescendantVisitor
Expand All @@ -26,9 +26,10 @@ class MatchDescendantVisitor
// Creates an AST visitor that matches `Matcher` on all
// descendants of a given node "n" except for the ones
// belonging to a different callable of "n".
MatchDescendantVisitor(const DynTypedMatcher *Matcher, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder,
ASTMatchFinder::BindKind Bind)
MatchDescendantVisitor(const internal::DynTypedMatcher *Matcher,
internal::ASTMatchFinder *Finder,
internal::BoundNodesTreeBuilder *Builder,
internal::ASTMatchFinder::BindKind Bind)
: Matcher(Matcher), Finder(Finder), Builder(Builder), Bind(Bind),
Matches(false) {}

Expand Down Expand Up @@ -86,32 +87,32 @@ class MatchDescendantVisitor
// Returns 'true' if traversal should continue after this function
// returns, i.e. if no match is found or 'Bind' is 'BK_All'.
template <typename T> bool match(const T &Node) {
BoundNodesTreeBuilder RecursiveBuilder(*Builder);
internal::BoundNodesTreeBuilder RecursiveBuilder(*Builder);

if (Matcher->matches(DynTypedNode::create(Node), Finder,
&RecursiveBuilder)) {
ResultBindings.addMatch(RecursiveBuilder);
Matches = true;
if (Bind != ASTMatchFinder::BK_All)
if (Bind != internal::ASTMatchFinder::BK_All)
return false; // Abort as soon as a match is found.
}
return true;
}

const DynTypedMatcher *const Matcher;
ASTMatchFinder *const Finder;
BoundNodesTreeBuilder *const Builder;
BoundNodesTreeBuilder ResultBindings;
const ASTMatchFinder::BindKind Bind;
const internal::DynTypedMatcher *const Matcher;
internal::ASTMatchFinder *const Finder;
internal::BoundNodesTreeBuilder *const Builder;
internal::BoundNodesTreeBuilder ResultBindings;
const internal::ASTMatchFinder::BindKind Bind;
bool Matches;
};

AST_MATCHER_P(Stmt, forEveryDescendant, Matcher<Stmt>, innerMatcher) {
AST_MATCHER_P(Stmt, forEveryDescendant, internal::Matcher<Stmt>, innerMatcher) {
MatchDescendantVisitor Visitor(new DynTypedMatcher(innerMatcher), Finder,
Builder, ASTMatchFinder::BK_All);
return Visitor.findMatch(DynTypedNode::create(Node));
}
} // namespace clang::ast_matchers::internal
} // namespace clang::ast_matchers

namespace {
// Because the analysis revolves around variables and their types, we'll need to
Expand Down

0 comments on commit ef47a0a

Please sign in to comment.