[-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas
authorZiqing Luo <ziqing@udel.edu>
Wed, 8 Feb 2023 00:45:44 +0000 (16:45 -0800)
committerZiqing Luo <ziqing@udel.edu>
Wed, 8 Feb 2023 00:54:39 +0000 (16:54 -0800)
commitaef05b5dc5c566bcaa15b66c989ccb8d2841ac71
treeb7f2d62aa876ce687ba96cc7bc188387cc3d3ab9
parent233fd475036d8b92362589249f4de0882f6f2826
[-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
12 files changed:
clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
clang/include/clang/Basic/DiagnosticLexKinds.td
clang/include/clang/Lex/Preprocessor.h
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/lib/Lex/PPLexerChange.cpp
clang/lib/Lex/Pragma.cpp
clang/lib/Lex/Preprocessor.cpp
clang/lib/Sema/AnalysisBasedWarnings.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma-fixit.cpp [new file with mode: 0644]
clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma-misuse.cpp [new file with mode: 0644]
clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma.cpp [new file with mode: 0644]
clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma.h [new file with mode: 0644]