Skip to content

Commit bdf4f2b

Browse files
committed
[-Wunsafe-buffer-usage] Generate fix-it for local variable declarations
Use clang fix-its to transform declarations of local variables, which are used for buffer access , to be of std::span type. We placed a few limitations to keep the solution simple: - it only transforms local variable declarations (no parameter declaration); - it only considers single level pointers, i.e., pointers of type T * regardless of whether T is again a pointer; - it only transforms to std::span types (no std::array, or std::span::iterator, or ...); - it can only transform a VarDecl that belongs to a DeclStmt whose has a single child. One of the purposes of keeping this patch simple enough is to first evaluate if fix-it is an appropriate approach to do the transformation. This commit was reverted by 622be09 for a compilation warning and now it is fixed. Reviewed by: NoQ, jkorous Differential revision: https://reviews.llvm.org/D139737
1 parent 82d852c commit bdf4f2b

File tree

7 files changed

+539
-29
lines changed

7 files changed

+539
-29
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ class UnsafeBufferUsageHandler {
3737
/// Invoked when a fix is suggested against a variable.
3838
virtual void handleFixableVariable(const VarDecl *Variable,
3939
FixItList &&List) = 0;
40+
41+
/// Returns the text indicating that the user needs to provide input there:
42+
virtual std::string
43+
getUserFillPlaceHolder(StringRef HintTextToUser = "placeholder") {
44+
std::string s = std::string("<# ");
45+
s += HintTextToUser;
46+
s += " #>";
47+
return s;
48+
}
4049
};
4150

4251
// This function invokes the analysis and allows the caller to react to it

clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ WARNING_GADGET(Decrement)
3030
WARNING_GADGET(ArraySubscript)
3131
WARNING_GADGET(PointerArithmetic)
3232
WARNING_GADGET(UnsafeBufferUsageAttr)
33+
FIXABLE_GADGET(ULCArraySubscript)
3334

3435
#undef FIXABLE_GADGET
3536
#undef WARNING_GADGET

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11790,6 +11790,8 @@ def warn_unsafe_buffer_operation : Warning<
1179011790
InGroup<UnsafeBufferUsage>, DefaultIgnore;
1179111791
def note_unsafe_buffer_operation : Note<
1179211792
"used%select{| in pointer arithmetic| in buffer access}0 here">;
11793+
def note_unsafe_buffer_variable_fixit : Note<
11794+
"change type of '%0' to '%select{std::span|std::array|std::span::iterator}1' to preserve bounds information">;
1179311795
def err_loongarch_builtin_requires_la32 : Error<
1179411796
"this builtin requires target: loongarch32">;
1179511797
} // end of sema component.

0 commit comments

Comments
 (0)