Skip to content

Commit 004a7ce

Browse files
authored
[clang][dataflow] Change diagnoseFunction to use llvm::SmallVector instead of std::vector. (#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.
1 parent bf85f27 commit 004a7ce

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
1414
#include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h"
1515
#include "clang/Basic/SourceLocation.h"
16+
#include "llvm/ADT/SmallVector.h"
1617
#include "llvm/Support/Error.h"
17-
#include <memory>
18-
#include <optional>
19-
#include <vector>
2018

2119
namespace clang::tidy::bugprone {
2220
using ast_matchers::MatchFinder;
@@ -54,7 +52,7 @@ void UncheckedOptionalAccessCheck::check(
5452
UncheckedOptionalAccessDiagnoser Diagnoser(ModelOptions);
5553
// FIXME: Allow user to set the (defaulted) SAT iterations max for
5654
// `diagnoseFunction` with config options.
57-
if (llvm::Expected<std::vector<SourceLocation>> Locs =
55+
if (llvm::Expected<llvm::SmallVector<SourceLocation>> Locs =
5856
dataflow::diagnoseFunction<UncheckedOptionalAccessModel,
5957
SourceLocation>(*FuncDecl, *Result.Context,
6058
Diagnoser))

clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/ADT/Any.h"
3232
#include "llvm/ADT/STLExtras.h"
3333
#include "llvm/ADT/STLFunctionalExtras.h"
34+
#include "llvm/ADT/SmallVector.h"
3435
#include "llvm/Support/Errc.h"
3536
#include "llvm/Support/Error.h"
3637

@@ -246,9 +247,9 @@ runDataflowAnalysis(
246247
/// - This limit is still low enough to keep runtimes acceptable (on typical
247248
/// machines) in cases where we hit the limit.
248249
template <typename AnalysisT, typename Diagnostic>
249-
llvm::Expected<std::vector<Diagnostic>> diagnoseFunction(
250+
llvm::Expected<llvm::SmallVector<Diagnostic>> diagnoseFunction(
250251
const FunctionDecl &FuncDecl, ASTContext &ASTCtx,
251-
llvm::function_ref<std::vector<Diagnostic>(
252+
llvm::function_ref<llvm::SmallVector<Diagnostic>(
252253
const CFGElement &, ASTContext &,
253254
const TransferStateForDiagnostics<typename AnalysisT::Lattice> &)>
254255
Diagnoser,
@@ -263,7 +264,7 @@ llvm::Expected<std::vector<Diagnostic>> diagnoseFunction(
263264
DataflowAnalysisContext AnalysisContext(std::move(OwnedSolver));
264265
Environment Env(AnalysisContext, FuncDecl);
265266
AnalysisT Analysis(ASTCtx);
266-
std::vector<Diagnostic> Diagnostics;
267+
llvm::SmallVector<Diagnostic> Diagnostics;
267268
if (llvm::Error Err =
268269
runTypeErasedDataflowAnalysis(
269270
*Context, Analysis, Env,

clang/include/clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
2222
#include "clang/Analysis/FlowSensitive/NoopLattice.h"
2323
#include "clang/Basic/SourceLocation.h"
24-
#include <vector>
24+
#include "llvm/ADT/SmallVector.h"
2525

2626
namespace clang {
2727
namespace dataflow {
@@ -74,14 +74,14 @@ class UncheckedOptionalAccessDiagnoser {
7474
UncheckedOptionalAccessDiagnoser(
7575
UncheckedOptionalAccessModelOptions Options = {});
7676

77-
std::vector<SourceLocation>
77+
llvm::SmallVector<SourceLocation>
7878
operator()(const CFGElement &Elt, ASTContext &Ctx,
7979
const TransferStateForDiagnostics<NoopLattice> &State) {
8080
return DiagnoseMatchSwitch(Elt, Ctx, State.Env);
8181
}
8282

8383
private:
84-
CFGMatchSwitch<const Environment, std::vector<SourceLocation>>
84+
CFGMatchSwitch<const Environment, llvm::SmallVector<SourceLocation>>
8585
DiagnoseMatchSwitch;
8686
};
8787

clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <memory>
3535
#include <optional>
3636
#include <utility>
37-
#include <vector>
3837

3938
namespace clang {
4039
namespace dataflow {
@@ -913,8 +912,8 @@ auto buildTransferMatchSwitch() {
913912
.Build();
914913
}
915914

916-
std::vector<SourceLocation> diagnoseUnwrapCall(const Expr *ObjectExpr,
917-
const Environment &Env) {
915+
llvm::SmallVector<SourceLocation> diagnoseUnwrapCall(const Expr *ObjectExpr,
916+
const Environment &Env) {
918917
if (auto *OptionalVal = getValueBehindPossiblePointer(*ObjectExpr, Env)) {
919918
auto *Prop = OptionalVal->getProperty("has_value");
920919
if (auto *HasValueVal = cast_or_null<BoolValue>(Prop)) {
@@ -935,7 +934,8 @@ auto buildDiagnoseMatchSwitch(
935934
// lot of duplicated work (e.g. string comparisons), consider providing APIs
936935
// that avoid it through memoization.
937936
auto IgnorableOptional = ignorableOptional(Options);
938-
return CFGMatchSwitchBuilder<const Environment, std::vector<SourceLocation>>()
937+
return CFGMatchSwitchBuilder<const Environment,
938+
llvm::SmallVector<SourceLocation>>()
939939
// optional::value
940940
.CaseOfCFGStmt<CXXMemberCallExpr>(
941941
valueCall(IgnorableOptional),

clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TEST(DataflowAnalysisTest, DiagnoseFunctionDiagnoserCalledOnEachElement) {
9898
cast<FunctionDecl>(findValueDecl(AST->getASTContext(), "target"));
9999
auto Diagnoser = [](const CFGElement &Elt, ASTContext &,
100100
const TransferStateForDiagnostics<NoopLattice> &) {
101-
std::vector<std::string> Diagnostics(1);
101+
llvm::SmallVector<std::string> Diagnostics(1);
102102
llvm::raw_string_ostream OS(Diagnostics.front());
103103
Elt.dumpToStream(OS);
104104
return Diagnostics;

0 commit comments

Comments
 (0)