Skip to content

Commit 7ffcfbc

Browse files
Merge pull request #8178 from jkorous-apple/cxx-safe-buffers/integrate/cxx-safe-buffers/std-array-fixits
[-Wunsafe-buffer-usage] Introduce std::array fixits (llvm#80084)
2 parents bada760 + f20531a commit 7ffcfbc

8 files changed

+541
-160
lines changed

clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ class VariableGroupsManager {
4242
virtual VarGrpRef getGroupOfParms() const = 0;
4343
};
4444

45+
// FixitStrategy is a map from variables to the way we plan to emit fixes for
46+
// these variables. It is figured out gradually by trying different fixes
47+
// for different variables depending on gadgets in which these variables
48+
// participate.
49+
class FixitStrategy {
50+
public:
51+
enum class Kind {
52+
Wontfix, // We don't plan to emit a fixit for this variable.
53+
Span, // We recommend replacing the variable with std::span.
54+
Iterator, // We recommend replacing the variable with std::span::iterator.
55+
Array, // We recommend replacing the variable with std::array.
56+
Vector // We recommend replacing the variable with std::vector.
57+
};
58+
59+
private:
60+
using MapTy = llvm::DenseMap<const VarDecl *, Kind>;
61+
62+
MapTy Map;
63+
64+
public:
65+
FixitStrategy() = default;
66+
FixitStrategy(const FixitStrategy &) = delete; // Let's avoid copies.
67+
FixitStrategy &operator=(const FixitStrategy &) = delete;
68+
FixitStrategy(FixitStrategy &&) = default;
69+
FixitStrategy &operator=(FixitStrategy &&) = default;
70+
71+
void set(const VarDecl *VD, Kind K) { Map[VD] = K; }
72+
73+
Kind lookup(const VarDecl *VD) const {
74+
auto I = Map.find(VD);
75+
if (I == Map.end())
76+
return Kind::Wontfix;
77+
78+
return I->second;
79+
}
80+
};
81+
4582
/// The interface that lets the caller handle unsafe buffer usage analysis
4683
/// results by overriding this class's handle... methods.
4784
class UnsafeBufferUsageHandler {
@@ -75,9 +112,11 @@ class UnsafeBufferUsageHandler {
75112
///
76113
/// `D` is the declaration of the callable under analysis that owns `Variable`
77114
/// and all of its group mates.
78-
virtual void handleUnsafeVariableGroup(const VarDecl *Variable,
79-
const VariableGroupsManager &VarGrpMgr,
80-
FixItList &&Fixes, const Decl *D) = 0;
115+
virtual void
116+
handleUnsafeVariableGroup(const VarDecl *Variable,
117+
const VariableGroupsManager &VarGrpMgr,
118+
FixItList &&Fixes, const Decl *D,
119+
const FixitStrategy &VarTargetTypes) = 0;
81120

82121
#ifndef NDEBUG
83122
public:

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11966,9 +11966,9 @@ def warn_unsafe_buffer_operation : Warning<
1196611966
def note_unsafe_buffer_operation : Note<
1196711967
"used%select{| in pointer arithmetic| in buffer access}0 here">;
1196811968
def note_unsafe_buffer_variable_fixit_group : Note<
11969-
"change type of %0 to '%select{std::span|std::array|std::span::iterator}1' to preserve bounds information%select{|, and change %2 to '%select{std::span|std::array|std::span::iterator}1' to propagate bounds information between them}3">;
11969+
"change type of %0 to '%select{std::span' to preserve bounds information|std::array' to label it for hardening|std::span::iterator' to preserve bounds information}1%select{|, and change %2 to '%select{std::span|std::array|std::span::iterator}1' to propagate bounds information between them}3">;
1197011970
def note_unsafe_buffer_variable_fixit_together : Note<
11971-
"change type of %0 to '%select{std::span|std::array|std::span::iterator}1' to preserve bounds information"
11971+
"change type of %0 to '%select{std::span' to preserve bounds information|std::array' to label it for hardening|std::span::iterator' to preserve bounds information}1"
1197211972
"%select{|, and change %2 to safe types to make function %4 bounds-safe}3">;
1197311973
def note_safe_buffer_usage_suggestions_disabled : Note<
1197411974
"pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions">;

0 commit comments

Comments
 (0)