134 Commits

Author SHA1 Message Date
Rashmi Mudduluru
a6ae740e74 [-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage
Differential Revision: https://reviews.llvm.org/D154880
2023-07-26 17:07:36 -07:00
Ziqing Luo
cfcf76c6ad [-Wunsafe-buffer-usage] Ignore the FixableGadgets that will not be fixed at an earlier stage
`FixableGadget`s are not always associated with variables that are unsafe
(warned). For example, they could be associated with variables whose
unsafe operations are suppressed or that are not used in any unsafe
operation. Such `FixableGadget`s will not be fixed. Removing these
`FixableGadget` as early as possible helps improve the performance
and stability of the analysis.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru)

Differential revision: https://reviews.llvm.org/D155524
2023-07-25 16:58:27 -07:00
Rashmi Mudduluru
070358ec92 [-Wunsafe-buffer-usage] Fix a fallthrough case in UPCStandalonePointer getFixits
Differential Revision: https://reviews.llvm.org/D155526
2023-07-25 13:40:33 -07:00
Mark de Wever
910450a28b [NFC][clang] Fixes whitespace.
These changes cause the Buildkite CI to fail.
2023-07-22 16:50:58 +02:00
Rashmi Mudduluru
27c1033783 [WIP][-Wunsafe-buffer-usage] Handle lambda expressions within a method.
Differential Revision: https://reviews.llvm.org/D150386
2023-07-20 10:00:16 -07:00
ziqingluo-90
a6302b6934 [-Wunsafe-buffer-usage] Check source location validity before using TypeLocs
The safe-buffer analysis analyzes TypeLocs of types of variable
declarations in order to get source locations of them.

However, in some cases, the source locations of a TypeLoc are not
valid. Using invalid source locations results in assertion violation
or incorrect analysis or fix-its.

It is still not clear to me in what circumstances a TypeLoc does not
have valid source locations (it looks like a bug in Clang to me, but
it is not our responsibility to fix it). So we will conservatively
give up the analysis when required source locations are not valid.

Reviewed By: NoQ (Artem Dergachev)

Differential Revision: https://reviews.llvm.org/D155667
2023-07-19 15:04:42 -07:00
Ziqing Luo
4b5f17e008 [-Wunsafe-buffer-usage] Do not assert that function parameters have names
It is possible that a function parameter does not have a name even in
a function definition.  This patch deals with such cases in generating
function overload fix-its for safe buffers.

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D155641
2023-07-19 14:14:28 -07:00
Evan Wilde
d7b45945fb
[NFC][clang] Fix format in UnsafeBufferUsage.cpp
There were two whitespaces on an otherwise empty that were causing the
bots to fail due to formatting issues. Deleting extra whitespace now.
2023-07-16 22:45:29 -07:00
ziqingluo-90
a07a6f6c74 Re-land "5b012bf5ab5fcb840fe7f6c8664b8981ce6f24f3"
Removed dependency on `clangSema` from UnsafeBufferAnalysis.
2023-07-15 16:11:37 -07:00
NAKAMURA Takumi
ba7273b025 Reformat 2023-07-15 13:55:11 +09:00
NAKAMURA Takumi
c915908f6d Revert D150338, "[-Wunsafe-buffer-usage] Improving insertion of the [[clang::unsafe_buffer_usage]] attribute"
clangAnalysis should not depend on clangSema.

This reverts commit 5b012bf5ab5fcb840fe7f6c8664b8981ce6f24f3.
2023-07-15 13:55:11 +09:00
ziqingluo-90
5b012bf5ab [-Wunsafe-buffer-usage] Improving insertion of the [[clang::unsafe_buffer_usage]] attribute
For a fix-it that inserts the `[[clang::unsafe_buffer_usage]]`
attribute, it will lookup existing macros defined for the attribute
and use the (last defined such) macro directly. Fix-its will use raw
`[[clang::unsafe_buffer_usage]]` if no such macro is defined.

The implementation mimics how a similar machine for the
`[[fallthrough]]` attribute was implemented.

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D150338
2023-07-14 14:47:07 -07:00
Artem Dergachev
7c7b191434 [-Wunsafe-buffer-usage] Unbreak clang-format on UnsafeBufferUsage.cpp. NFC. 2023-07-10 15:57:10 -07:00
Nikolas Klauser
f6d557ee34 [clang][NFC] Remove trailing whitespaces and enforce it in lib, include and docs
A lot of editors remove trailing whitespaces. This patch removes any trailing whitespaces and makes sure that no new ones are added.

Reviewed By: erichkeane, paulkirth, #libc, philnik

Spies: wangpc, aheejin, MaskRay, pcwang-thead, cfe-commits, libcxx-commits, dschuff, nemanjai, arichardson, kbarton, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, s.egerton, sameer.abuasal, apazos, luismarques, martong, frasercrmck, steakhal, luke

Differential Revision: https://reviews.llvm.org/D151963
2023-06-26 09:34:36 -07:00
Rashmi Mudduluru
db3dcedb9c [-Wunsafe-buffer-usage] Handle pointer initializations for grouping related variables
Differential Revision: https://reviews.llvm.org/D150489
2023-06-21 15:54:09 -07:00
Rashmi Mudduluru
d1ae844dc2 [-Wunsafe-buffer-usage] Do not emit fixits for C++ interfaces with C linkage
Differential Revision: https://reviews.llvm.org/D153064
2023-06-16 13:28:34 -07:00
ziqingluo-90
10e8300536 [-Wunsafe-buffer-usage] Remove the unnecessary const qualifier in safe buffer analysis
Casting away the qualifier raises a warning in 1e270be0886c3a770e7a967679552a02dfc1dca9
2023-06-09 16:41:49 -07:00
ziqingluo-90
1e270be088 [-Wunsafe-buffer-usage] Add fix-its for function parameters using the span strategy
Generate fix-its for function parameters that are raw pointers used
unsafely.  Currently, the analyzer fixes one parameter at a time.

Fix-its for a function parameter includes:

- Fix the parameter declaration of the definition, result in a new
  overload of the function. We call the function with the original
  signature the old overload.
- For any other existing declaration of the old overload, mark it with
  the [[unsafe_buffer_usage]] attribute and generate a new overload
  declaration next to it.
- Creates a new definition for the old overload, which is simply
  defined by a call to the new overload.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru), and
             jkorous (Jan Korous)

Differential revision: https://reviews.llvm.org/D143048
2023-06-09 15:44:38 -07:00
Rashmi Mudduluru
171dfc5462 [-Wunsafe-buffer-usage] Group variables associated by pointer assignments
Differential Revision: https://reviews.llvm.org/D145739
2023-05-25 11:31:27 -07:00
Douglas Yung
2e6325c71f Revert "[-Wunsafe-buffer-usage] Group variables associated by pointer assignments"
This reverts commit ee6b08e99375fc48d1e5848704a66c2e8e57eb3b.

One of the added tests warn-unsafe-buffer-usage-multi-decl-warnings.cpp does
not seem to be deterministic, and seems to be especially problematic on Windows.

Failures of this one test on llvm-clang-x86_64-sie-win:
- https://lab.llvm.org/buildbot/#/builders/216/builds/21758
- https://lab.llvm.org/buildbot/#/builders/216/builds/21761
- https://lab.llvm.org/buildbot/#/builders/216/builds/21762
- https://lab.llvm.org/buildbot/#/builders/216/builds/21765
- https://lab.llvm.org/buildbot/#/builders/216/builds/21770
- https://lab.llvm.org/buildbot/#/builders/216/builds/21771
- https://lab.llvm.org/buildbot/#/builders/216/builds/21773
- https://lab.llvm.org/buildbot/#/builders/216/builds/21776
- https://lab.llvm.org/buildbot/#/builders/216/builds/21777
- https://lab.llvm.org/buildbot/#/builders/216/builds/21778
- https://lab.llvm.org/buildbot/#/builders/216/builds/21779

Other random bot failures:
- https://lab.llvm.org/buildbot/#/builders/65/builds/9821
- https://lab.llvm.org/buildbot/#/builders/65/builds/9822
- https://lab.llvm.org/buildbot/#/builders/65/builds/9824
- https://lab.llvm.org/buildbot/#/builders/119/builds/13440
- https://lab.llvm.org/buildbot/#/builders/119/builds/13442
- https://lab.llvm.org/buildbot/#/builders/119/builds/13444
- https://lab.llvm.org/buildbot/#/builders/119/builds/13445
- https://lab.llvm.org/buildbot/#/builders/60/builds/12156
- https://lab.llvm.org/buildbot/#/builders/60/builds/12157
- https://lab.llvm.org/buildbot/#/builders/60/builds/12160
2023-05-25 02:10:32 -07:00
Rashmi Mudduluru
ee6b08e993 [-Wunsafe-buffer-usage] Group variables associated by pointer assignments
Differential Revision: https://reviews.llvm.org/D145739
2023-05-24 16:20:55 -07:00
Artem Dergachev
b7bdf1996f [-Wunsafe-buffer-usage] Hide fixits/suggestions behind an extra flag.
This patch implements a new clang driver flag -fsafe-buffer-usage-suggestions
which allows turning the smart suggestion machine on and off (defaults to off).
This is valuable for stability reasons, as the machine is being rapidly improved\
and we don't want accidental breakages to ruin the build for innocent users.
It is also arguably useful in general because it enables separation of concerns
between project contributors: some users will actively update the code to
conform to the programming model, while others simply want to make sure that
they aren't regressing it. Finally, there could be other valid reasons to
opt out of suggestions entirely on some codebases (while continuing to enforce
-Wunsafe-buffer-usage warnings), such as lack of access to hardened libc++
(or even to the C++ standard library in general) on the target platform.

When the flag is disabled, the unsafe buffer usage analysis is reduced to
an extremely minimal mode of operation that contains virtually no smarts:
not only it doesn't offer automatic fixits, but also textual suggestions
such as "change the type of this variable to std::span to preserve bounds
information" are not displayed, and in fact the machine doesn't even try
to blame specific variables in the first place, it simply warns on
the operations and leaves everything else to the user. So this flag turns off
a lot more of our complex machinery than what we already turn off in presence
of say -fno-diagnostic-fixit-info.

The flag is discoverable: when it's off, the warnings are accompanied by a note:
telling the user that there's a flag they can use.

Differential Revision: https://reviews.llvm.org/D146669
2023-05-18 17:20:27 -07:00
Manna, Soumi
5ebff1ac1b [NFC][Clang] Fix Coverity issues of copy without assign
This patch adds missing copy/move assignment operator to the class which has user-defined copy/move constructor.

Reviewed By: tahonermann

Differential Revision: https://reviews.llvm.org/D149718
2023-05-14 19:49:28 -07:00
Kazu Hirata
e955e4fba6 [clang] Replace None with std::nullopt in comments (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-05-04 22:42:52 -07:00
MalavikaSamak
b63b2c2350 Reland "[-Wunsafe-buffer-usage] Bug fix: Handles the assertion violations for code within macros"
This reverts commit 84ec1f7725d4f4575474b59467e598d7c5528a4e.
2023-04-24 16:49:13 -07:00
MalavikaSamak
84ec1f7725 Revert "[-Wunsafe-buffer-usage] Bug fix: Handles the assertion violations for code within macros"
This reverts commit 9bd0db80784e30d40a4a65f1b47109c833f05b54.
2023-04-24 16:48:46 -07:00
Jie Fu
1d097ad73b [clang] Fix -Wimplicit-fallthrough in UnsafeBufferUsage.cpp [NFC]
/Users/jiefu/llvm-project/clang/lib/Analysis/UnsafeBufferUsage.cpp:1272:5: error: unannotated fall-through between switch labels [-Werror,-Wimplicit-fallthrough]
    case Strategy::Kind::Wontfix:
    ^
/Users/jiefu/llvm-project/clang/lib/Analysis/UnsafeBufferUsage.cpp:1272:5: note: insert 'LLVM_FALLTHROUGH;' to silence this warning
    case Strategy::Kind::Wontfix:
    ^
    LLVM_FALLTHROUGH;
/Users/jiefu/llvm-project/clang/lib/Analysis/UnsafeBufferUsage.cpp:1272:5: note: insert 'break;' to avoid fall-through
    case Strategy::Kind::Wontfix:
    ^
    break;
1 error generated.
2023-04-25 07:34:39 +08:00
MalavikaSamak
9bd0db8078 [-Wunsafe-buffer-usage] Bug fix: Handles the assertion violations for code within macros
When macros get expanded, the source location for the expanded code received by the Fixable
gadgets is invalid. We do not want to emit fixits for macro expanded code and it currently
crashes the analysis. This patch fixes the assertion violations that were introduced for
handling code with such invalid locations.

Reviewed by: NoQ, ziqingluo-90, jkorous

Differential revision: https://reviews.llvm.org/D146450
2023-04-24 16:10:15 -07:00
MalavikaSamak
9516419c50 Revert "Revert "[-Wunsafe-buffer-usage] Handle unevaluated contexts that contain unsafe buffer usages""
This reverts commit 7bf5f4692ad6f9ba2d5c155f6b630049bb59876f and adding -frtti flag to support PS4/PS5 builds.
2023-04-19 16:53:34 -07:00
MalavikaSamak
7bf5f4692a Revert "[-Wunsafe-buffer-usage] Handle unevaluated contexts that contain unsafe buffer usages"
This reverts commit 777eb4bcfc3265359edb7c979d3e5ac699ad4641.
2023-04-19 16:09:21 -07:00
MalavikaSamak
777eb4bcfc [-Wunsafe-buffer-usage] Handle unevaluated contexts that contain unsafe buffer usages
This patch handles unevaluated contexts to ensure no warnings are produced by the machinery
for buffer access made within an unevaluated contexts. However, such accesses must be
considered by a FixableGadget and produce the necessary fixits.

Reviewed by: NoQ, ziqingluo-90, jkorous

Differential revision: https://reviews.llvm.org/D144905
2023-04-19 15:53:21 -07:00
Manna, Soumi
7e1b62bd9c [NFC][Clang] Fix static analyzer tool remark about missing user-defined assignment operator
Reported by Coverity:

Copy without assign
This class has a user-defined copy constructor but no user-defined assignment operator. If the copy constructor is necessary to manage owned resources then a corresponding assignment operator is usually required. If an object of this type is assigned memory leaks and/or use-after-free errors may occur. Note that a compiler-generated assignment operator will perform only a bit-wise copy for any fields that do not have their own assignment operators defined.

Class has user-written copy constructor but no user-written assignment operator

copy_without_assign: Class <unnamed>::DeclUseTracker has a user-written copy constructor <unnamed>::DeclUseTracker::DeclUseTracker(<unnamed>::DeclUseTracker const &) =delete but no corresponding user-written assignment operator.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D148189
2023-04-17 22:21:30 -04:00
Ziqing Luo
762af11d4c [-Wunsafe-buffer-usage] Add a Fixable for pointer pre-increment
For a pointer type expression `e` of the form `++DRE`, if `e` is under
an Unspecified Pointer Context (UPC) and `DRE` is suppose to be
transformed to have std:span type, we generate fix-its that transform `e` to
`(DRE = DRE.subspan(1)).data()`.

For reference, `e` is in an UPC if `e` is
- an argument of a function call (except the callee has [[unsafe_buffer_usage]] attribute), or
- the operand of a cast-to-(Integer or Boolean) operation; or
- the operand of a pointer subtraction operation; or
- the operand of a pointer comparison operation;

We may extend the definition of UPC by adding more cases later.

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D144304
2023-04-12 14:51:46 -07:00
Ziqing Luo
88f7f018e2 [-Wunsafe-buffer-usage] Match unsafe pointers being casted to bool or participating in pointer subtractions
Add two new unique cases to the Unspecified Pointer Context (UPC),
under which we match unsafe operation patterns:
- A pointer being casted to a boolean value is in a UPC;
- A pointer participating in pointer subtraction is in a UPC.

Reviewed by: NoQ (Artem Dergachev), malavikasamak (Malavika Samak)

Differential revision: https://reviews.llvm.org/D144064
2023-04-11 15:09:51 -07:00
ziqingluo-90
6341301509 [-Wunsafe-buffer-usage] Improve fix-its for local variable declarations with null pointer initializers
For a local pointer declaration of the form `T * p = 0` or `T * p = std::nullptr`,

Before this patch, 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.

Reviewed by: NoQ (Artem Dergachev)

Differential Revision: https://reviews.llvm.org/D143680
2023-04-10 12:08:30 -07:00
MalavikaSamak
a046d18772 [-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
2023-04-07 15:32:19 -07:00
Ziqing Luo
ca6ceeb0d6 Reland "[-Wunsafe-buffer-usage] Fix-Its transforming &DRE[any] to &DRE.data()[any]"
This commit relands 87b5807d3802b932c06d83c4287014872aa2caab, where a
test fails on a few specific targets.  Now hard-code a target
for the test.
2023-04-05 14:54:03 -07:00
David Spickett
d5c428356f Revert "[-Wunsafe-buffer-usage] Fix-Its transforming &DRE[any] to &DRE.data()[any]"
This reverts commit 87b5807d3802b932c06d83c4287014872aa2caab.

The test case is failing on Windows https://lab.llvm.org/buildbot/#/builders/65/builds/8950.
2023-04-05 08:07:19 +00:00
ziqingluo-90
87b5807d38 [-Wunsafe-buffer-usage] Fix-Its transforming &DRE[any] to &DRE.data()[any]
For an expression of the form `&DRE[any]` under an Unspecified
Pointer Context (UPC), we generate a fix-it for it with respect to a
strategy. In case the strategy is `std::span` (it is the only supported
one for now), the fix-it replaces the expression with
`&DRE.data()[any]`.

A UPC includes at least the contexts where
- the expression is being casted to an integer; and
- the expression is an argument of a call to a function that is not marked unsafe.

Reviewed by: NoQ, malavikasamak, t-rasmud, jkorous

Differential revision: https://reviews.llvm.org/D143128
2023-04-04 13:26:45 -07:00
Fangrui Song
45a0433b39 [-Wunsafe-buffer-usage] Add [[fallthrough]] after D143206 2023-03-22 21:02:00 -07:00
MalavikaSamak
e7596a99fc [-Wunsafe-buffer-usage] Add Fixable for simple pointer dereference
This patch introduces PointerDereferenceGadget, a FixableGadget that emits
fixits to handle cases where a pointer that is identified as unsafe is
dereferenced. The current implementation only handles cases where the strategy
is to change the type of the raw pointer to std::span. The fixit for this
strategy is to fetch the first element from the corresponding span instance.

For example for the code below, the PointerDereferenceGadget emits a fixit for
S3 (S1, S2 are to be handled by other gadgets):

  S1: int *ptr = new int[10];
  S2: int val1 = ptr[k]; // Unsafe operation
  S3: int val2 = *ptr; => Fixit: int val2 = ptr[0];

Differential revision: https://reviews.llvm.org/D143206
2023-03-22 15:32:51 -07:00
ziqingluo-90
6a0f2e539b [-Wunsafe-buffer-usage] Add Fixable for dereference of simple ptr arithmetic
For each expression `e` of the form `*(DRE + n)` (or `*(n + DRE)`), where
`DRE` has a pointer type and `n` is an integer literal, `e` will be
transformed to `DRE[n]` (or `n[DRE]` respectively), if
- `e` is at the left-hand side of an assignment or is an lvalue being casted to an rvalue; and
- the variable referred by `DRE` is going to be transformed to be of `std::span` type.

Reviewed by: jkorous, NoQ

Differential revision: https://reviews.llvm.org/D142795
2023-03-20 17:07:03 -07:00
ziqingluo-90
148dc8a2a8 [-Wunsafe-buffer-usage] Reducing non-determinism in diagnostics output stream
The -Wunsafe-buffer-usage analysis outputs diagnostics in the order of
pointer values to associated `VarDecl`s. This creates non-determinism
in the order of diagnostics in output since the order cannot be
guaranteed in pointer values. However, our fix-it tests were written
under the assumption that diagnostics are output in source location
order.  This results in non-deterministic failures in our tests.  This
patch fixes the problem by keeping analysis results sorted by source
locations.

Reviewed by: jkorous, NoQ

Differential revision: https://reviews.llvm.org/D145993
2023-03-13 17:22:22 -07:00
Ziqing Luo
cd2652963b [-Wunsafe-buffer-usage] Fixits for assignments to array subscript expressions
Let generate fix-its to make assignments' left-hand side of the form
`dre[e]` safe if `e` is known to be non-negative.

Commit on behalf of jkorous (Jan Korous)

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D142794
2023-02-23 15:02:46 -08:00
Ziqing Luo
f78c343466 [-Wunsafe-buffer-usage] Create Fix-Its only if they are emitted
`-Wunsafe-buffer-usage` diagnostics shall not emit fix-its if fix-its
are globally disabled.

Commit on behalf of jkorous (Jan Korous)

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D143697
2023-02-23 14:47:43 -08:00
Jan Korous
8b6ae9bd74 [-Wunsafe-buffer-usage] Emit Fix-Its only for C++20 and later standards
The transformation strategy we are bringing up heavily relies on std::span which was introduced as part of C++20.

Differential Revision: https://reviews.llvm.org/D143455
2023-02-09 17:28:27 -08:00
Ziqing Luo
829bcb06ec [-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas
Add a pair of clang pragmas:
- `#pragma clang unsafe_buffer_usage begin` and
- `#pragma clang unsafe_buffer_usage end`,
which specify the start and end of an (unsafe buffer checking) opt-out
region, respectively.

Behaviors of opt-out regions conform to the following rules:

- No nested nor overlapped opt-out regions are allowed. One cannot
  start an opt-out region with `... unsafe_buffer_usage begin` but never
  close it with `... unsafe_buffer_usage end`. Mis-use of the pragmas
  will be warned.
- Warnings raised from unsafe buffer operations inside such an opt-out
  region will always be suppressed. This behavior CANNOT be changed by
  `clang diagnostic` pragmas or command-line flags.
- Warnings raised from unsafe operations outside of such opt-out
  regions may be reported on declarations inside opt-out
  regions. These warnings are NOT suppressed.
- An un-suppressed unsafe operation warning may be attached with
  notes. These notes are NOT suppressed as well regardless of whether
  they are in opt-out regions.

The implementation maintains a separate sequence of location pairs
representing opt-out regions in `Preprocessor`.  The `UnsafeBufferUsage`
analyzer reads the region sequence to check if an unsafe operation is
in an opt-out region. If it is, discard the warning raised from the
operation immediately.

This is a re-land after I reverting it at 9aa00c8a306561c4e3ddb09058e66bae322a0769.
The compilation error should be resolved.

Reviewed by: NoQ

Differential revision: https://reviews.llvm.org/D140179
2023-02-08 14:12:03 -08:00
Ziqing Luo
9aa00c8a30 Revert "[-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas"
This reverts commit aef05b5dc5c566bcaa15b66c989ccb8d2841ac71.
It causes a buildbot failure: https://lab.llvm.org/buildbot/#/builders/216/builds/16879/steps/6/logs/stdio
2023-02-07 17:06:20 -08:00
Ziqing Luo
aef05b5dc5 [-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas
Add a pair of clang pragmas:
- `#pragma clang unsafe_buffer_usage begin` and
- `#pragma clang unsafe_buffer_usage end`,
which specify the start and end of an (unsafe buffer checking) opt-out
region, respectively.

Behaviors of opt-out regions conform to the following rules:

- No nested nor overlapped opt-out regions are allowed. One cannot
  start an opt-out region with `... unsafe_buffer_usage begin` but never
  close it with `... unsafe_buffer_usage end`. Mis-use of the pragmas
  will be warned.
- Warnings raised from unsafe buffer operations inside such an opt-out
  region will always be suppressed. This behavior CANNOT be changed by
  `clang diagnostic` pragmas or command-line flags.
- Warnings raised from unsafe operations outside of such opt-out
  regions may be reported on declarations inside opt-out
  regions. These warnings are NOT suppressed.
- An un-suppressed unsafe operation warning may be attached with
  notes. These notes are NOT suppressed as well regardless of whether
  they are in opt-out regions.

The implementation maintains a separate sequence of location pairs
representing opt-out regions in `Preprocessor`.  The `UnsafeBufferUsage`
analyzer reads the region sequence to check if an unsafe operation is
in an opt-out region. If it is, discard the warning raised from the
operation immediately.

Reviewed by: NoQ

Differential revision: https://reviews.llvm.org/D140179
2023-02-07 16:54:39 -08:00
Ziqing Luo
692da6245d [-Wunsafe-buffer-usage] Filter out conflicting fix-its
Two fix-its conflict if they have overlapping source ranges. We shall
not emit conflicting fix-its.  This patch checks conflicts in fix-its
generated for one variable (including variable declaration fix-its and
variable usage fix-its). If there is any, we do NOT emit any fix-it
for that variable.

Reviewed by: NoQ

Differential revision: https://reviews.llvm.org/D141338
2023-02-07 16:15:28 -08:00