[clang] [clang-tools-extra] [clang][analysis] refactor the unevaluated api (PR #117474)
https://github.com/HerrCai0907 closed https://github.com/llvm/llvm-project/pull/117474 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analysis] refactor the unevaluated api (PR #117474)
https://github.com/5chmidti approved this pull request. https://github.com/llvm/llvm-project/pull/117474 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analysis] refactor the unevaluated api (PR #117474)
https://github.com/PiotrZSL approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/117474 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analysis] refactor the unevaluated api (PR #117474)
https://github.com/HerrCai0907 edited https://github.com/llvm/llvm-project/pull/117474 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang][analysis] refactor the unevaluated api (PR #117474)
llvmbot wrote: @llvm/pr-subscribers-clang-analysis Author: Congcong Cai (HerrCai0907) Changes It is unclear for `ExprMutationAnalyzer::isUnevaluated` to accept 2 Stmt. Redesign the API to accept only 1 Stmt and the API will only check whether stmt is substmt of an unevaluated stmt. --- Full diff: https://github.com/llvm/llvm-project/pull/117474.diff 3 Files Affected: - (modified) clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp (+1-1) - (modified) clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h (+4-9) - (modified) clang/lib/Analysis/ExprMutationAnalyzer.cpp (+24-36) ``diff diff --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp index e329588290cd4b..2b2d80ea9346bd 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp @@ -303,7 +303,7 @@ void InfiniteLoopCheck::check(const MatchFinder::MatchResult &Result) { } } - if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *LoopStmt, *Result.Context)) + if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *Result.Context)) return; if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context)) diff --git a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h index c7a5b016c949d0..7442f4aad531b7 100644 --- a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h +++ b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h @@ -47,8 +47,6 @@ class ExprMutationAnalyzer { const Stmt *findPointeeMutation(const Expr *Exp); const Stmt *findPointeeMutation(const Decl *Dec); -static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm, - ASTContext &Context); private: using MutationFinder = const Stmt *(Analyzer::*)(const Expr *); @@ -58,8 +56,6 @@ class ExprMutationAnalyzer { Memoized::ResultMap &MemoizedResults); const Stmt *tryEachDeclRef(const Decl *Dec, MutationFinder Finder); -bool isUnevaluated(const Expr *Exp); - const Stmt *findExprMutation(ArrayRef Matches); const Stmt *findDeclMutation(ArrayRef Matches); const Stmt * @@ -83,6 +79,10 @@ class ExprMutationAnalyzer { ExprMutationAnalyzer(const Stmt &Stm, ASTContext &Context) : Memorized(), A(Stm, Context, Memorized) {} + /// check whether stmt is unevaluated. mutation analyzer will ignore the + /// content in unevaluated stmt. + static bool isUnevaluated(const Stmt *Stm, ASTContext &Context); + bool isMutated(const Expr *Exp) { return findMutation(Exp) != nullptr; } bool isMutated(const Decl *Dec) { return findMutation(Dec) != nullptr; } const Stmt *findMutation(const Expr *Exp) { return A.findMutation(Exp); } @@ -101,11 +101,6 @@ class ExprMutationAnalyzer { return A.findPointeeMutation(Dec); } - static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm, -ASTContext &Context) { -return Analyzer::isUnevaluated(Smt, Stm, Context); - } - private: Memoized Memorized; Analyzer A; diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp index a94b22e051d0e1..be0e8aa5743dd9 100644 --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -236,7 +236,7 @@ const Stmt *ExprMutationAnalyzer::Analyzer::findMutationMemoized( if (!Inserted) return Memoized->second; - if (isUnevaluated(Exp)) + if (ExprMutationAnalyzer::isUnevaluated(Exp, Context)) return nullptr; for (const auto &Finder : Finders) { @@ -268,41 +268,29 @@ ExprMutationAnalyzer::Analyzer::tryEachDeclRef(const Decl *Dec, return nullptr; } -bool ExprMutationAnalyzer::Analyzer::isUnevaluated(const Stmt *Exp, - const Stmt &Stm, - ASTContext &Context) { - return selectFirst( - NodeID::value, - match( - findFirst( - stmt(canResolveToExpr(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(), -
[clang] [clang-tools-extra] [clang][analysis] refactor the unevaluated api (PR #117474)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Congcong Cai (HerrCai0907) Changes It is unclear for `ExprMutationAnalyzer::isUnevaluated` to accept 2 Stmt. Redesign the API to accept only 1 Stmt and the API will only check whether stmt is substmt of an unevaluated stmt. --- Full diff: https://github.com/llvm/llvm-project/pull/117474.diff 3 Files Affected: - (modified) clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp (+1-1) - (modified) clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h (+4-9) - (modified) clang/lib/Analysis/ExprMutationAnalyzer.cpp (+24-36) ``diff diff --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp index e329588290cd4b..2b2d80ea9346bd 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp @@ -303,7 +303,7 @@ void InfiniteLoopCheck::check(const MatchFinder::MatchResult &Result) { } } - if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *LoopStmt, *Result.Context)) + if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *Result.Context)) return; if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context)) diff --git a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h index c7a5b016c949d0..7442f4aad531b7 100644 --- a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h +++ b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h @@ -47,8 +47,6 @@ class ExprMutationAnalyzer { const Stmt *findPointeeMutation(const Expr *Exp); const Stmt *findPointeeMutation(const Decl *Dec); -static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm, - ASTContext &Context); private: using MutationFinder = const Stmt *(Analyzer::*)(const Expr *); @@ -58,8 +56,6 @@ class ExprMutationAnalyzer { Memoized::ResultMap &MemoizedResults); const Stmt *tryEachDeclRef(const Decl *Dec, MutationFinder Finder); -bool isUnevaluated(const Expr *Exp); - const Stmt *findExprMutation(ArrayRef Matches); const Stmt *findDeclMutation(ArrayRef Matches); const Stmt * @@ -83,6 +79,10 @@ class ExprMutationAnalyzer { ExprMutationAnalyzer(const Stmt &Stm, ASTContext &Context) : Memorized(), A(Stm, Context, Memorized) {} + /// check whether stmt is unevaluated. mutation analyzer will ignore the + /// content in unevaluated stmt. + static bool isUnevaluated(const Stmt *Stm, ASTContext &Context); + bool isMutated(const Expr *Exp) { return findMutation(Exp) != nullptr; } bool isMutated(const Decl *Dec) { return findMutation(Dec) != nullptr; } const Stmt *findMutation(const Expr *Exp) { return A.findMutation(Exp); } @@ -101,11 +101,6 @@ class ExprMutationAnalyzer { return A.findPointeeMutation(Dec); } - static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm, -ASTContext &Context) { -return Analyzer::isUnevaluated(Smt, Stm, Context); - } - private: Memoized Memorized; Analyzer A; diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp index a94b22e051d0e1..be0e8aa5743dd9 100644 --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -236,7 +236,7 @@ const Stmt *ExprMutationAnalyzer::Analyzer::findMutationMemoized( if (!Inserted) return Memoized->second; - if (isUnevaluated(Exp)) + if (ExprMutationAnalyzer::isUnevaluated(Exp, Context)) return nullptr; for (const auto &Finder : Finders) { @@ -268,41 +268,29 @@ ExprMutationAnalyzer::Analyzer::tryEachDeclRef(const Decl *Dec, return nullptr; } -bool ExprMutationAnalyzer::Analyzer::isUnevaluated(const Stmt *Exp, - const Stmt &Stm, - ASTContext &Context) { - return selectFirst( - NodeID::value, - match( - findFirst( - stmt(canResolveToExpr(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(), -
[clang] [clang-tools-extra] [clang][analysis] refactor the unevaluated api (PR #117474)
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/117474 It is unclear for `ExprMutationAnalyzer::isUnevaluated` to accept 2 Stmt. Redesign the API to accept only 1 Stmt and the API will only check whether stmt is substmt of an unevaluated stmt. >From b41b8eaa76f504c0e0c07061be16f640f63946d7 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Sun, 24 Nov 2024 18:10:30 +0800 Subject: [PATCH] [clang][analysis] refactor the unevaluated api It is unclear for `ExprMutationAnalyzer::isUnevaluated` to accept 2 Stmt. Redesign the API to accept only 1 Stmt and the API will only check whether stmt is substmt of an unevaluated stmt. --- .../clang-tidy/bugprone/InfiniteLoopCheck.cpp | 2 +- .../Analysis/Analyses/ExprMutationAnalyzer.h | 13 ++-- clang/lib/Analysis/ExprMutationAnalyzer.cpp | 60 --- 3 files changed, 29 insertions(+), 46 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp index e329588290cd4b..2b2d80ea9346bd 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp @@ -303,7 +303,7 @@ void InfiniteLoopCheck::check(const MatchFinder::MatchResult &Result) { } } - if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *LoopStmt, *Result.Context)) + if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *Result.Context)) return; if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context)) diff --git a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h index c7a5b016c949d0..7442f4aad531b7 100644 --- a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h +++ b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h @@ -47,8 +47,6 @@ class ExprMutationAnalyzer { const Stmt *findPointeeMutation(const Expr *Exp); const Stmt *findPointeeMutation(const Decl *Dec); -static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm, - ASTContext &Context); private: using MutationFinder = const Stmt *(Analyzer::*)(const Expr *); @@ -58,8 +56,6 @@ class ExprMutationAnalyzer { Memoized::ResultMap &MemoizedResults); const Stmt *tryEachDeclRef(const Decl *Dec, MutationFinder Finder); -bool isUnevaluated(const Expr *Exp); - const Stmt *findExprMutation(ArrayRef Matches); const Stmt *findDeclMutation(ArrayRef Matches); const Stmt * @@ -83,6 +79,10 @@ class ExprMutationAnalyzer { ExprMutationAnalyzer(const Stmt &Stm, ASTContext &Context) : Memorized(), A(Stm, Context, Memorized) {} + /// check whether stmt is unevaluated. mutation analyzer will ignore the + /// content in unevaluated stmt. + static bool isUnevaluated(const Stmt *Stm, ASTContext &Context); + bool isMutated(const Expr *Exp) { return findMutation(Exp) != nullptr; } bool isMutated(const Decl *Dec) { return findMutation(Dec) != nullptr; } const Stmt *findMutation(const Expr *Exp) { return A.findMutation(Exp); } @@ -101,11 +101,6 @@ class ExprMutationAnalyzer { return A.findPointeeMutation(Dec); } - static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm, -ASTContext &Context) { -return Analyzer::isUnevaluated(Smt, Stm, Context); - } - private: Memoized Memorized; Analyzer A; diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp index a94b22e051d0e1..be0e8aa5743dd9 100644 --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -236,7 +236,7 @@ const Stmt *ExprMutationAnalyzer::Analyzer::findMutationMemoized( if (!Inserted) return Memoized->second; - if (isUnevaluated(Exp)) + if (ExprMutationAnalyzer::isUnevaluated(Exp, Context)) return nullptr; for (const auto &Finder : Finders) { @@ -268,41 +268,29 @@ ExprMutationAnalyzer::Analyzer::tryEachDeclRef(const Decl *Dec, return nullptr; } -bool ExprMutationAnalyzer::Analyzer::isUnevaluated(const Stmt *Exp, - const Stmt &Stm, - ASTContext &Context) { - return selectFirst( - NodeID::value, - match( - findFirst( - stmt(canResolveToExpr(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( - // `UnaryExprOrType