103 lines · cpp
1// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \2// RUN: -fsafe-buffer-usage-suggestions \3// RUN: -Wno-unused-value -verify %s4 5void basic(int * x) { // expected-warning{{'x' is an unsafe pointer used for buffer access}}6 int *p1 = new int[10]; // not to warn7 int *p2 = new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}8 9#pragma clang unsafe_buffer_usage begin10 p1[5]; // not to warn11 12#define _INCLUDE_NO_WARN13#include "warn-unsafe-buffer-usage-pragma.h" // increment p1 in header14 15 int *p3 = new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}16 17#pragma clang unsafe_buffer_usage end18 p2[5]; //expected-note{{used in buffer access here}}19 p3[5]; //expected-note{{used in buffer access here}}20 x++; //expected-note{{used in pointer arithmetic here}}21#define _INCLUDE_WARN22#include "warn-unsafe-buffer-usage-pragma.h" // increment p2 in header23}24 25 26void withDiagnosticWarning() {27 int *p1 = new int[10]; // not to warn28 int *p2 = new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}29 30 // diagnostics in opt-out region31#pragma clang unsafe_buffer_usage begin32 p1[5]; // not to warn33 p2[5]; // not to warn34#pragma clang diagnostic push35#pragma clang diagnostic warning "-Wunsafe-buffer-usage"36 p1[5]; // not to warn37 p2[5]; // not to warn38#pragma clang diagnostic warning "-Weverything"39 p1[5]; // not to warn expected-warning{{expression result unused}}40 p2[5]; // not to warn expected-warning{{expression result unused}}41#pragma clang diagnostic pop42#pragma clang unsafe_buffer_usage end43 44 // opt-out region under diagnostic warning45#pragma clang diagnostic push46#pragma clang diagnostic warning "-Wunsafe-buffer-usage"47#pragma clang unsafe_buffer_usage begin48 p1[5]; // not to warn49 p2[5]; // not to warn50#pragma clang unsafe_buffer_usage end51#pragma clang diagnostic pop52 53 p2[5]; // expected-note{{used in buffer access here}}54}55 56 57void withDiagnosticIgnore() {58 int *p1 = new int[10]; // not to warn59 int *p2 = new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}60 int *p3 = new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}61 62#pragma clang unsafe_buffer_usage begin63 p1[5]; // not to warn64 p2[5]; // not to warn65#pragma clang diagnostic push66#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"67 p1[5]; // not to warn68 p2[5]; // not to warn69#pragma clang diagnostic ignored "-Weverything"70 p1[5]; // not to warn71 p2[5]; // not to warn72#pragma clang diagnostic pop73#pragma clang unsafe_buffer_usage end74 75#pragma clang diagnostic push76#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"77#pragma clang unsafe_buffer_usage begin78 p1[5]; // not to warn79 p2[5]; // not to warn80#pragma clang unsafe_buffer_usage end81#pragma clang diagnostic pop82 83 p2[5]; // expected-note{{used in buffer access here}}84 85#pragma clang diagnostic push86#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"87#pragma clang unsafe_buffer_usage begin88 p1[5]; // not to warn89 p2[5]; // not to warn90#pragma clang unsafe_buffer_usage end91 p3[5]; // expected-note{{used in buffer access here}}92#pragma clang diagnostic pop93}94 95void noteGoesWithVarDeclWarning() {96#pragma clang diagnostic push97#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"98 int *p = new int[10]; // not to warn99#pragma clang diagnostic pop100 101 p[5]; // not to note since the associated warning is suppressed102}103