Skip to content

Commit

Permalink
updated canResolveToExpr to accept both statements and expressions. R…
Browse files Browse the repository at this point in the history
…emoved unnecessary code
  • Loading branch information
usama54321 committed May 24, 2022
1 parent 63ecb7d commit ca81abc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
2 changes: 0 additions & 2 deletions clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
Expand Up @@ -40,8 +40,6 @@ class ExprMutationAnalyzer {
const Stmt *findPointeeMutation(const Decl *Dec);
static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm,
ASTContext &Context);
static bool isUnevaluated(const Expr *Exp, const Stmt &Stm,
ASTContext &Context);

private:
using MutationFinder = const Stmt *(ExprMutationAnalyzer::*)(const Expr *);
Expand Down
69 changes: 33 additions & 36 deletions clang/lib/Analysis/ExprMutationAnalyzer.cpp
Expand Up @@ -39,8 +39,13 @@ AST_MATCHER_P(Expr, maybeEvalCommaExpr, ast_matchers::internal::Matcher<Expr>,
return InnerMatcher.matches(*Result, Finder, Builder);
}

AST_MATCHER_P(Expr, canResolveToExpr, ast_matchers::internal::Matcher<Expr>,
AST_MATCHER_P(Stmt, canResolveToExpr, ast_matchers::internal::Matcher<Stmt>,
InnerMatcher) {
auto *Exp = dyn_cast<Expr>(&Node);
if (!Exp) {
return stmt().matches(Node, Finder, Builder);
}

auto DerivedToBase = [](const ast_matchers::internal::Matcher<Expr> &Inner) {
return implicitCastExpr(anyOf(hasCastKind(CK_DerivedToBase),
hasCastKind(CK_UncheckedDerivedToBase)),
Expand Down Expand Up @@ -71,7 +76,7 @@ AST_MATCHER_P(Expr, canResolveToExpr, ast_matchers::internal::Matcher<Expr>,
IgnoreDerivedToBase(ConditionalOperator),
IgnoreDerivedToBase(ElvisOperator))));

return ComplexMatcher.matches(Node, Finder, Builder);
return ComplexMatcher.matches(*Exp, Finder, Builder);
}

// Similar to 'hasAnyArgument', but does not work because 'InitListExpr' does
Expand Down Expand Up @@ -194,44 +199,36 @@ const Stmt *ExprMutationAnalyzer::tryEachDeclRef(const Decl *Dec,
return nullptr;
}

auto isUnevaluatedMatcher(const Stmt *Exp) {
return anyOf(
// `Exp` is part of the underlying expression of
// decltype/typeof if it has an ancestor of
// typeLoc.
hasAncestor(typeLoc(unless(hasAncestor(unaryExprOrTypeTraitExpr())))),
hasAncestor(expr(anyOf(
// `UnaryExprOrTypeTraitExpr` is unevaluated
// unless it's sizeof on VLA.
unaryExprOrTypeTraitExpr(
unless(sizeOfExpr(hasArgumentOfType(variableArrayType())))),
// `CXXTypeidExpr` is unevaluated unless it's
// applied to an expression of glvalue of
// polymorphic class type.
cxxTypeidExpr(unless(isPotentiallyEvaluated())),
// The controlling expression of
// `GenericSelectionExpr` is unevaluated.
genericSelectionExpr(
hasControllingExpr(hasDescendant(equalsNode(Exp)))),
cxxNoexceptExpr()))));
}

bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp, const Stmt &Stm,
ASTContext &Context) {
return selectFirst<Expr>(NodeID<Expr>::value,
match(findAll(expr(canResolveToExpr(equalsNode(Exp)),
isUnevaluatedMatcher(Exp))
.bind(NodeID<Expr>::value)),
Stm, Context)) != nullptr;
}

bool ExprMutationAnalyzer::isUnevaluated(const Stmt *Exp, const Stmt &Stm,
ASTContext &Context) {
return selectFirst<Stmt>(
NodeID<Expr>::value,
match(findAll(stmt(equalsNode(Exp), isUnevaluatedMatcher(Exp))
.bind(NodeID<Expr>::value)),
Stm, Context)) != nullptr;
match(
findAll(
stmt(canResolveToExpr(equalsNode(Exp)),
anyOf(
// `Exp` is part of the underlying expression of
// decltype/typeof if it has an ancestor of
// typeLoc.
hasAncestor(typeLoc(unless(
hasAncestor(unaryExprOrTypeTraitExpr())))),
hasAncestor(expr(anyOf(
// `UnaryExprOrTypeTraitExpr` is unevaluated
// unless it's sizeof on VLA.
unaryExprOrTypeTraitExpr(unless(sizeOfExpr(
hasArgumentOfType(variableArrayType())))),
// `CXXTypeidExpr` is unevaluated unless it's
// applied to an expression of glvalue of
// polymorphic class type.
cxxTypeidExpr(
unless(isPotentiallyEvaluated())),
// The controlling expression of
// `GenericSelectionExpr` is unevaluated.
genericSelectionExpr(hasControllingExpr(
hasDescendant(equalsNode(Exp)))),
cxxNoexceptExpr())))))
.bind(NodeID<Expr>::value)),
Stm, Context)) != nullptr;
}

bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp) {
Expand Down

0 comments on commit ca81abc

Please sign in to comment.