[clang] [clang][dataflow] Change `diagnoseFunction` to take type of diagnostic list instead of diagnostic itself. (PR #66014)

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 11 14:46:05 PDT 2023


https://github.com/ymand created https://github.com/llvm/llvm-project/pull/66014:

The template is agnostic as to the type used by the list, as long as it is
compatible with `llvm::move` and `std::back_inserter`.  In practice, we've
encountered analyses which use different types (`llvm::SmallVector` vs
`std::vector`), so it seems preferable to leave this open to the caller.


>From 6eda5a1fc6200027d6ae99dc5eff69aa88962c81 Mon Sep 17 00:00:00 2001
From: Yitzhak Mandelbaum <yitzhakm at google.com>
Date: Mon, 11 Sep 2023 21:34:00 +0000
Subject: [PATCH] [clang][dataflow] Change `diagnoseFunction` to take type of
 diagnostic list instead of diagnostic itself.

The template is agnostic as to the type used by the list, as long as it is
compatible with `llvm::move` and `std::back_inserter`.  In practice, we've
encountered analyses which use different types (`llvm::SmallVector` vs
`std::vector`), so it seems preferable to leave this open.
---
 .../clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp  | 4 ++--
 .../clang/Analysis/FlowSensitive/DataflowAnalysis.h       | 8 ++++----
 .../FlowSensitive/TypeErasedDataflowAnalysisTest.cpp      | 4 ++--
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp
index 183beb6bfb87d80..5811e2a4cd02266 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp
@@ -56,8 +56,8 @@ void UncheckedOptionalAccessCheck::check(
   // `diagnoseFunction` with config options.
   if (llvm::Expected<std::vector<SourceLocation>> Locs =
           dataflow::diagnoseFunction<UncheckedOptionalAccessModel,
-                                     SourceLocation>(*FuncDecl, *Result.Context,
-                                                     Diagnoser))
+                                     std::vector<SourceLocation>>(
+              *FuncDecl, *Result.Context, Diagnoser))
     for (const SourceLocation &Loc : *Locs)
       diag(Loc, "unchecked access to optional value");
   else
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
index abd34f40922121e..fd2d7fce09073bf 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
@@ -245,10 +245,10 @@ runDataflowAnalysis(
 ///   iterations.
 /// - This limit is still low enough to keep runtimes acceptable (on typical
 ///   machines) in cases where we hit the limit.
-template <typename AnalysisT, typename Diagnostic>
-llvm::Expected<std::vector<Diagnostic>> diagnoseFunction(
+template <typename AnalysisT, typename DiagnosticList>
+llvm::Expected<DiagnosticList> diagnoseFunction(
     const FunctionDecl &FuncDecl, ASTContext &ASTCtx,
-    llvm::function_ref<std::vector<Diagnostic>(
+    llvm::function_ref<DiagnosticList(
         const CFGElement &, ASTContext &,
         const TransferStateForDiagnostics<typename AnalysisT::Lattice> &)>
         Diagnoser,
@@ -263,7 +263,7 @@ llvm::Expected<std::vector<Diagnostic>> diagnoseFunction(
   DataflowAnalysisContext AnalysisContext(std::move(OwnedSolver));
   Environment Env(AnalysisContext, FuncDecl);
   AnalysisT Analysis(ASTCtx);
-  std::vector<Diagnostic> Diagnostics;
+  DiagnosticList Diagnostics;
   if (llvm::Error Err =
           runTypeErasedDataflowAnalysis(
               *Context, Analysis, Env,
diff --git a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
index af34a2afd24685a..3a8253508016caa 100644
--- a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
@@ -98,12 +98,12 @@ TEST(DataflowAnalysisTest, DiagnoseFunctionDiagnoserCalledOnEachElement) {
       cast<FunctionDecl>(findValueDecl(AST->getASTContext(), "target"));
   auto Diagnoser = [](const CFGElement &Elt, ASTContext &,
                       const TransferStateForDiagnostics<NoopLattice> &) {
-    std::vector<std::string> Diagnostics(1);
+    llvm::SmallVector<std::string> Diagnostics(1);
     llvm::raw_string_ostream OS(Diagnostics.front());
     Elt.dumpToStream(OS);
     return Diagnostics;
   };
-  auto Result = diagnoseFunction<NoopAnalysis, std::string>(
+  auto Result = diagnoseFunction<NoopAnalysis, llvm::SmallVector<std::string>>(
       *Func, AST->getASTContext(), Diagnoser);
   // `diagnoseFunction` provides no guarantees about the order in which elements
   // are visited, so we use `UnorderedElementsAre`.



More information about the cfe-commits mailing list