-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[-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
- Loading branch information
1 parent
233fd47
commit aef05b5
Showing
12 changed files
with
435 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma-fixit.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s | ||
|
||
void basic(int * x) { | ||
int tmp; | ||
int *p1 = new int[10]; // no fix | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
int *p2 = new int[10]; | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p2" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}" | ||
#pragma clang unsafe_buffer_usage begin | ||
tmp = p1[5]; | ||
#pragma clang unsafe_buffer_usage end | ||
tmp = p2[5]; | ||
} | ||
|
||
void withDiagnosticWarning() { | ||
int tmp; | ||
int *p1 = new int[10]; // no fix | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
int *p2 = new int[10]; | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p2" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}" | ||
|
||
// diagnostics in opt-out region | ||
#pragma clang unsafe_buffer_usage begin | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic warning "-Wunsafe-buffer-usage" | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang diagnostic warning "-Weverything" | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang diagnostic pop | ||
#pragma clang unsafe_buffer_usage end | ||
|
||
// opt-out region under diagnostic warning | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic warning "-Wunsafe-buffer-usage" | ||
#pragma clang unsafe_buffer_usage begin | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang unsafe_buffer_usage end | ||
#pragma clang diagnostic pop | ||
|
||
tmp = p2[5]; | ||
} | ||
|
||
|
||
void withDiagnosticIgnore() { | ||
int tmp; | ||
int *p1 = new int[10]; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
int *p2 = new int[10]; | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p2" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}" | ||
int *p3 = new int[10]; | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p3" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{" | ||
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}" | ||
|
||
#pragma clang unsafe_buffer_usage begin | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang diagnostic ignored "-Weverything" | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang diagnostic pop | ||
#pragma clang unsafe_buffer_usage end | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" | ||
#pragma clang unsafe_buffer_usage begin | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang unsafe_buffer_usage end | ||
#pragma clang diagnostic pop | ||
|
||
tmp = p2[5]; | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" | ||
#pragma clang unsafe_buffer_usage begin | ||
tmp = p1[5]; // not to warn | ||
tmp = p2[5]; // not to warn | ||
#pragma clang unsafe_buffer_usage end | ||
tmp = p3[5]; // expected-note{{used in buffer access here}} | ||
#pragma clang diagnostic pop | ||
} | ||
|
||
void noteGoesWithVarDeclWarning() { | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" | ||
int *p = new int[10]; // not to warn | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
#pragma clang diagnostic pop | ||
|
||
p[5]; // not to note since the associated warning is suppressed | ||
} |
Oops, something went wrong.