From c44c71843f3eca71ef109d2e0730722a6d8e5675 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Tue, 2 Aug 2022 19:33:47 +0000 Subject: [PATCH] [clang][dataflow] Make the type of the post visit callback consistent Make the types of the post visit callbacks in `transferBlock` and `runTypeErasedDataflowAnalysis` consistent. Differential Revision: https://reviews.llvm.org/D131014 Reviewed-by: ymandel, xazax.hun, gribozavr2 --- .../clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp | 5 +++-- .../include/clang/Analysis/FlowSensitive/DataflowAnalysis.h | 8 ++++---- .../Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h | 3 ++- .../Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp | 12 +++++------- clang/unittests/Analysis/FlowSensitive/TestingSupport.h | 6 +++--- .../FlowSensitive/UncheckedOptionalAccessModelTest.cpp | 5 +++-- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp index 8a945a5..5ee1a01 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp @@ -59,10 +59,11 @@ analyzeFunction(const FunctionDecl &FuncDecl, ASTContext &ASTCtx) { BlockToOutputState = dataflow::runDataflowAnalysis( *Context, Analysis, Env, [&ASTCtx, &Diagnoser, &Diagnostics]( - const Stmt *Stmt, + const CFGStmt &Stmt, const DataflowAnalysisState &State) mutable { - auto StmtDiagnostics = Diagnoser.diagnose(ASTCtx, Stmt, State.Env); + auto StmtDiagnostics = + Diagnoser.diagnose(ASTCtx, Stmt.getStmt(), State.Env); llvm::move(StmtDiagnostics, std::back_inserter(Diagnostics)); }); if (!BlockToOutputState) diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h index a8785c5..c7903fd 100644 --- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h +++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h @@ -125,14 +125,14 @@ llvm::Expected &)> + std::function &)> PostVisitStmt = nullptr) { - std::function + std::function PostVisitStmtClosure = nullptr; if (PostVisitStmt != nullptr) { PostVisitStmtClosure = [&PostVisitStmt]( - const Stmt *Stmt, + const CFGStmt &Stmt, const TypeErasedDataflowAnalysisState &State) { auto *Lattice = llvm::any_cast(&State.Lattice.Value); diff --git a/clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h b/clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h index 3a10840..c563a2c 100644 --- a/clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h +++ b/clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h @@ -138,7 +138,8 @@ llvm::Expected>> runTypeErasedDataflowAnalysis( const ControlFlowContext &CFCtx, TypeErasedDataflowAnalysis &Analysis, const Environment &InitEnv, - std::function + std::function PostVisitStmt = nullptr); } // namespace dataflow diff --git a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp index fbb5217..f1c25f8 100644 --- a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp +++ b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp @@ -333,7 +333,8 @@ llvm::Expected>> runTypeErasedDataflowAnalysis( const ControlFlowContext &CFCtx, TypeErasedDataflowAnalysis &Analysis, const Environment &InitEnv, - std::function + std::function PostVisitStmt) { PostOrderCFGView POV(&CFCtx.getCFG()); ForwardDataflowWorklist Worklist(CFCtx.getCFG(), &POV); @@ -398,12 +399,9 @@ runTypeErasedDataflowAnalysis( // Skip blocks that were not evaluated. if (!BlockStates[Block->getBlockID()]) continue; - transferBlock( - CFCtx, BlockStates, *Block, InitEnv, Analysis, - [&PostVisitStmt](const clang::CFGStmt &Stmt, - const TypeErasedDataflowAnalysisState &State) { - PostVisitStmt(Stmt.getStmt(), State); - }); + + transferBlock(CFCtx, BlockStates, *Block, InitEnv, Analysis, + PostVisitStmt); } } diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h b/clang/unittests/Analysis/FlowSensitive/TestingSupport.h index 8cb5008..f09d5f9 100644 --- a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h +++ b/clang/unittests/Analysis/FlowSensitive/TestingSupport.h @@ -76,7 +76,7 @@ llvm::Error checkDataflow( llvm::StringRef Code, ast_matchers::internal::Matcher TargetFuncMatcher, std::function MakeAnalysis, - std::function PostVisitStmt, std::function VerifyResults, ArrayRef Args, @@ -112,11 +112,11 @@ llvm::Error checkDataflow( Environment Env(DACtx, *F); auto Analysis = MakeAnalysis(Context, Env); - std::function + std::function PostVisitStmtClosure = nullptr; if (PostVisitStmt != nullptr) { PostVisitStmtClosure = [&PostVisitStmt, &Context]( - const Stmt *Stmt, + const CFGStmt &Stmt, const TypeErasedDataflowAnalysisState &State) { PostVisitStmt(Context, Stmt, State); }; diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp index 63f8ed3..b21406e 100644 --- a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp @@ -1245,9 +1245,10 @@ private: return UncheckedOptionalAccessModel(Ctx, Options); }, [&Diagnostics, Diagnoser = UncheckedOptionalAccessDiagnoser(Options)]( - ASTContext &Ctx, const Stmt *Stmt, + ASTContext &Ctx, const CFGStmt &Stmt, const TypeErasedDataflowAnalysisState &State) mutable { - auto StmtDiagnostics = Diagnoser.diagnose(Ctx, Stmt, State.Env); + auto StmtDiagnostics = + Diagnoser.diagnose(Ctx, Stmt.getStmt(), State.Env); llvm::move(StmtDiagnostics, std::back_inserter(Diagnostics)); }, [&Diagnostics](AnalysisData AnalysisData) { -- 2.7.4