This is an archive of the discontinued LLVM Phabricator instance.

[-Wunsafe-buffer-usage] Refactor and improve for parameter fix-its
ClosedPublic

Authored by ziqingluo-90 on Jul 24 2023, 5:06 PM.

Details

Summary
  • Factor out the code that will be shared by both parameter and local variable fix-its
  • Add a check to ensure that a TypeLoc::isNull is false before using the TypeLoc
  • Remove the special check for whether a fixing variable involves unnamed types. This check is unnecessary now. Because we will give up on fixing a pointer type variable v, if we cannot obtain the source range of the pointee type of v. Using unnamed types is just one of the many causes of such scenarios. Besides, in some cases, we have no problem in referring to unnamed types. Therefore, we remove the special handling since we have a more general solution already.

For example,

typedef struct {int x;} UNNAMED_T;

UNNAMED_T * x; //  we can refer to the unnamed struct using `UNNAMED_T`

typedef struct {int x;} * UNNAMED_PTR;

UNNAMED_PTR y; // we cannot obtain the source range of the pointee type of `y`,	so we will give up
  • Move tests for cv-qualified parameters and unnamed types out of the "...-unsupported.cpp" test file.

Diff Detail

Event Timeline

ziqingluo-90 created this revision.Jul 24 2023, 5:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2023, 5:06 PM
ziqingluo-90 requested review of this revision.Jul 24 2023, 5:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2023, 5:06 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ziqingluo-90 retitled this revision from Refactor and improve for parameter fix-its to [-Wunsafe-buffer-usage] Refactor and improve for parameter fix-its.Jul 24 2023, 5:09 PM
NoQ accepted this revision.Aug 9 2023, 1:36 PM

Very nice, everything looks great!

clang/lib/Analysis/UnsafeBufferUsage.cpp
1796–1797

I'd rather just use literals in expressions. Saves a couple mallocs, a couple memcpys, and a couple lines of code.

(They'll probably turn into variables later anyway, when we transcend beyond span, so arguably not worth fixing.)

clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-unsupported.cpp
141–143

(a bit narrower and less fancy)

This revision is now accepted and ready to land.Aug 9 2023, 1:36 PM
NoQ added a comment.Aug 9 2023, 2:08 PM

Btw, how does this work with int **? Given that the pointee type int * isn't a simple identifier.

Btw, how does this work with int **? Given that the pointee type int * isn't a simple identifier.

It does not require the pointee type to be a simple identifier. The (source range of) pointee type is obtained by analyzing the TypeLoc of a variable declaration.

The limitation involving "identifiers" is that we require the pointee type to be completely on the left of the variable identifier in a variable declaration. As the analysis is based on source locations,
finding the pointee type source range in cases like int a[][10] or int (*a)[10] is non-trivial.

This revision was landed with ongoing or failed builds.Aug 17 2023, 3:28 PM
This revision was automatically updated to reflect the committed changes.
ziqingluo-90 marked 2 inline comments as done.