[PATCH] D146342: [WIP][-Wunsafe-buffer-usage] Move the whole analysis to the end of a translation unit

Ziqing Luo via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 17 17:12:52 PDT 2023


ziqingluo-90 created this revision.
ziqingluo-90 added reviewers: NoQ, jkorous, t-rasmud, malavikasamak.
Herald added a subscriber: ChuanqiXu.
Herald added a project: All.
ziqingluo-90 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Our analysis requires a complete view of the translation unit to be conservative.  As mentioned in this patch <https://reviews.llvm.org/D143048#4173832>,  we need to know if there is any function overload of a function `f` declared after `f`.   In addition, we later may want to make global variables safe too.  In such a case, we need to know if a global variable is used somewhere in the translation unit.   Moreover, the analysis now can ignore ill-formed code detected at the end of a TU.

A summary of the change:

1. Adds a TU traversal function in `UnsafeBufferAnalysis` to traverse and analyze each function definition;
2. Removes the old analysis entry in `AnalysisBasedWarnings.cpp`, which was called by `Sema` at the end of parsing a function;
3. Creates a new analysis entry in `AnalysisBasedWarnings.cpp` for `Sema` to call at the end of parsing a TU.

This patch is still work in progress as the existence of the following concerns:

1. Can we move everything in `AnalysisBasedWarnings.cpp` to `Sema`?  So far `AnalysisBasedWarnings` is used to bridge `Sema` and `UnsafeBufferAnalysis` so that the changes are minimal.
2. We probably need a more efficient TU traversal implementation.
3. Current tests are mostly fine except that some notes with message "in instantiation of ... " are missing.  Although these notes are not emitted by our analysis, we better understand why things change.
4. To test this patch on a branch with all ongoing [-Wunsafe-buffer-usage] patches.
5. Maybe there are better solutions?  (Looking for comments!)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146342

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
  clang/include/clang/Sema/AnalysisBasedWarnings.h
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/lib/Sema/Sema.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146342.506226.patch
Type: text/x-patch
Size: 7420 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230318/dc400c9b/attachment-0001.bin>


More information about the cfe-commits mailing list