[-Wunsafe-buffer-usage] FixableGadget for handling stand alone pointers under UPC
authorMalavikaSamak <malavika2@apple.com>
Fri, 7 Apr 2023 22:11:05 +0000 (15:11 -0700)
committerMalavikaSamak <malavika2@apple.com>
Fri, 7 Apr 2023 22:32:19 +0000 (15:32 -0700)
commita046d187720137d944cece4aa4561f4bceb54e3c
treecdbd02bf43e4f633e8c3719bd7ae36364cc41bf5
parenta7c2102d988b2ae2214f1483d2b4066955b4dc98
[-Wunsafe-buffer-usage] FixableGadget for handling stand alone pointers under UPC

This patch introduces UPCStandalonePointerGadget, a FixableGadget that emits fixits to
handle cases where a pointer identified as unsafe is simply referenced. An example of
such a case is when the pointer is input as an argument to a method call, where we can
not change the type of the argument. For cases where the strategy for the unsafe pointer is
to use std::span, the idea is to extract the underlying pointer by invoking the "data()"
method on the span instance.

For example, the gadget emits a fixit for S3, where S1, S2 are handled by other gadgets:
  S1: int *ptr = new int[10];
  S2: int val1 = ptr[k]; // Unsafe operation on ptr
  S3: foo(ptr); // Some method that accepts raw pointer => FIXIT: foo(ptr.data());

Reviewed by: NoQ, ziqingluo-90, jkorous

Differential revision: https://reviews.llvm.org/D143676
clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp [new file with mode: 0644]