This is an archive of the discontinued LLVM Phabricator instance.

[-Wunsafe-buffer-usage] Improve fix-its for local variable declarations with null pointer initializers
ClosedPublic

Authored by ziqingluo-90 on Feb 9 2023, 2:16 PM.

Details

Summary

For a local pointer declaration of the form T * p = 0 or T * p.= std::nullptr, we generate fix-its that convert the declaration to std::span<T> p {nullptr, <# placeholder #>}, in cases where p is used in some unsafe operations.

This patch improves the fix-its to result in a simpler form std::span<T> p. It gets rid of the placeholder and keeps the result concise.

Diff Detail

Event Timeline

ziqingluo-90 created this revision.Feb 9 2023, 2:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 9 2023, 2:16 PM
ziqingluo-90 requested review of this revision.Feb 9 2023, 2:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 9 2023, 2:16 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
NoQ added a comment.Feb 14 2023, 5:56 PM

Looks great! Nitpicks.

clang/lib/Analysis/UnsafeBufferUsage.cpp
1017
1031–1032
1206

It's a bit intrusive but probably nicer to simply remove const from all our ASTContext & parameters.

1290–1291

The old check was saying "populateInitializerFixItWithSpan() returns {} on error". So the new check is additionally saying "populateInitializerFixItWithSpan() isn't allowed to have errors anymore", which is arguably the more important message 🤔

ziqingluo-90 added inline comments.Feb 16 2023, 4:07 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1290–1291

yeah, I think we are being consistent on using std::nullopt to represent "something wrong, no fix" and using { } to represent a no-op fix. So the old code is incorrect.

ziqingluo-90 marked 3 inline comments as done.
ziqingluo-90 retitled this revision from [WIP][-Wunsafe-buffer-usage] Improve fix-its for local variable declarations with null pointer initializers to [-Wunsafe-buffer-usage] Improve fix-its for local variable declarations with null pointer initializers.Feb 17 2023, 1:27 PM
NoQ accepted this revision.Mar 7 2023, 2:50 PM

Ok I think this is good to go!

This revision is now accepted and ready to land.Mar 7 2023, 2:50 PM
This revision was landed with ongoing or failed builds.Apr 10 2023, 12:09 PM
This revision was automatically updated to reflect the committed changes.