[-Wunsafe-buffer-usage] Generate fix-it for local variable declarations
authorZiqing Luo <ziqing@udel.edu>
Tue, 7 Feb 2023 23:38:21 +0000 (15:38 -0800)
committerZiqing Luo <ziqing@udel.edu>
Tue, 7 Feb 2023 23:40:19 +0000 (15:40 -0800)
commitbdf4f2bea50e87f5b9273e3bbc9a7753bca3a6bb
tree1bbba908ea4f4a77511c4b8120c06ab48649705c
parent82d852c69f406f157ed601d66229d9d917e21f83
[-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 622be09c815266632e204eaf1c7a35f050220459
for a compilation warning and now it is fixed.

Reviewed by: NoQ, jkorous

Differential revision: https://reviews.llvm.org/D139737
clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/lib/Sema/AnalysisBasedWarnings.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-span.cpp [new file with mode: 0644]
clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp