diff --git a/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp @@ -17,11 +17,12 @@ #include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h" #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" #include "clang/Analysis/FlowSensitive/DataflowLattice.h" +#include "clang/Analysis/FlowSensitive/Diagnosis.h" #include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h" -#include "clang/Analysis/FlowSensitive/SourceLocationsLattice.h" #include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h" #include "clang/Basic/SourceLocation.h" #include "llvm/ADT/Any.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/Optional.h" #include "llvm/Support/Error.h" #include @@ -31,16 +32,16 @@ namespace tidy { namespace bugprone { using ast_matchers::MatchFinder; -using dataflow::SourceLocationsLattice; +using dataflow::UncheckedOptionalAccessDiagnosis; using dataflow::UncheckedOptionalAccessModel; using llvm::Optional; static constexpr llvm::StringLiteral FuncID("fun"); -static Optional +static Optional> analyzeFunction(const FunctionDecl &FuncDecl, ASTContext &ASTCtx) { using dataflow::ControlFlowContext; - using dataflow::DataflowAnalysisState; + using dataflow::TypeErasedDataflowAnalysisState; using llvm::Expected; Expected Context = @@ -52,23 +53,31 @@ std::make_unique()); dataflow::Environment Env(AnalysisContext, FuncDecl); UncheckedOptionalAccessModel Analysis(ASTCtx); - Expected>>> + Expected>> BlockToOutputState = - dataflow::runDataflowAnalysis(*Context, Analysis, Env); + dataflow::runTypeErasedDataflowAnalysis(*Context, Analysis, Env); if (!BlockToOutputState) return llvm::None; - assert(Context->getCFG().getExit().getBlockID() < BlockToOutputState->size()); - const Optional> - &ExitBlockState = - (*BlockToOutputState)[Context->getCFG().getExit().getBlockID()]; + const Optional &ExitBlockState = + (*BlockToOutputState)[Context->getCFG().getExit().getBlockID()]; // `runDataflowAnalysis` doesn't guarantee that the exit block is visited; // for example, when it is unreachable. // FIXME: Diagnose violations even when the exit block is unreachable. if (!ExitBlockState) return llvm::None; - return std::move(ExitBlockState->Lattice); + UncheckedOptionalAccessDiagnosis Diagnosis(ASTCtx); + dataflow::Diagnosis> + Diagnose = [&Diagnosis](const Stmt *Stmt, + const UncheckedOptionalAccessModel::Lattice &, + const dataflow::Environment &Env) { + return Diagnosis.diagnose(Stmt, Env); + }; + + return dataflow::diagnoseCFG(*Context, *BlockToOutputState, Env, Analysis, + std::move(Diagnose)); } void UncheckedOptionalAccessCheck::registerMatchers(MatchFinder *Finder) { @@ -97,9 +106,9 @@ if (FuncDecl->isTemplated()) return; - if (Optional Errors = + if (Optional> Errors = analyzeFunction(*FuncDecl, *Result.Context)) - for (const SourceLocation &Loc : Errors->getSourceLocations()) + for (const SourceLocation &Loc : *Errors) diag(Loc, "unchecked access to optional value"); }