140 lines · cpp
1// RUN: rm -rf %t2// RUN: mkdir -p %t3// RUN: split-file %s %t4 5// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -emit-module -fmodule-name=safe_buffers_test_base -x c++ %t/safe_buffers_test.modulemap -std=c++20\6// RUN: -o %t/safe_buffers_test_base.pcm -Wunsafe-buffer-usage7// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -emit-module -fmodule-name=safe_buffers_test_textual -x c++ %t/safe_buffers_test.modulemap -std=c++20\8// RUN: -o %t/safe_buffers_test_textual.pcm -Wunsafe-buffer-usage9// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -emit-module -fmodule-name=safe_buffers_test_optout -x c++ %t/safe_buffers_test.modulemap -std=c++20\10// RUN: -fmodule-file=%t/safe_buffers_test_base.pcm -fmodule-file=%t/safe_buffers_test_textual.pcm \11// RUN: -o %t/safe_buffers_test_optout.pcm -Wunsafe-buffer-usage12// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodule-file=%t/safe_buffers_test_optout.pcm -I %t -std=c++20 -Wunsafe-buffer-usage\13// RUN: -verify %t/safe_buffers_optout-explicit.cpp14 15 16// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -fmodule-map-file=%t/safe_buffers_test.modulemap -I%t\17// RUN: -x c++ -std=c++20 -Wunsafe-buffer-usage %t/safe_buffers_optout-implicit.cpp18 19//--- safe_buffers_test.modulemap20module safe_buffers_test_base {21 header "base.h"22}23 24module safe_buffers_test_textual {25 textual header "textual.h"26}27 28module safe_buffers_test_optout {29 explicit module test_sub1 { header "test_sub1.h" }30 explicit module test_sub2 { header "test_sub2.h" }31 use safe_buffers_test_base32}33 34//--- base.h35#ifdef __cplusplus36int base(int *p) {37 int x = p[5];38#pragma clang unsafe_buffer_usage begin39 int y = p[5];40#pragma clang unsafe_buffer_usage end41 return x + y;42}43#endif44 45//--- test_sub1.h46#include "base.h"47 48#ifdef __cplusplus49int sub1(int *p) {50 int x = p[5];51#pragma clang unsafe_buffer_usage begin52 int y = p[5];53#pragma clang unsafe_buffer_usage end54 return x + y + base(p);55}56 57template <typename T>58T sub1_T(T *p) {59 T x = p[5];60#pragma clang unsafe_buffer_usage begin61 T y = p[5];62#pragma clang unsafe_buffer_usage end63 return x + y;64}65#endif66 67//--- test_sub2.h68#include "base.h"69 70#ifdef __cplusplus71int sub2(int *p) {72 int x = p[5];73#pragma clang unsafe_buffer_usage begin74 int y = p[5];75#pragma clang unsafe_buffer_usage end76 return x + y + base(p);77}78#endif79 80//--- textual.h81#ifdef __cplusplus82int textual(int *p) {83 int x = p[5];84 int y = p[5];85 return x + y;86}87#endif88 89//--- safe_buffers_optout-explicit.cpp90#include "test_sub1.h"91#include "test_sub2.h"92 93// Testing safe buffers opt-out region serialization with modules: this94// file loads 2 submodules from top-level module95// `safe_buffers_test_optout`, which uses another top-level module96// `safe_buffers_test_base`. (So the module dependencies form a DAG.)97 98// No expected warnings from base.h, test_sub1, or test_sub2 because they are99// in separate modules, and the explicit commands that builds them have no100// `-Wunsafe-buffer-usage`.101 102int foo(int * p) {103 int x = p[5]; // expected-warning{{unsafe buffer access}} expected-note{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}104#pragma clang unsafe_buffer_usage begin105 int y = p[5];106#pragma clang unsafe_buffer_usage end107 sub1_T(p); // instantiate template108 return sub1(p) + sub2(p);109}110 111#pragma clang unsafe_buffer_usage begin112#include "textual.h" // This header is textually included (i.e., it is in the same TU as %s), so warnings are suppressed113#pragma clang unsafe_buffer_usage end114 115//--- safe_buffers_optout-implicit.cpp116#include "test_sub1.h"117#include "test_sub2.h"118 119// Testing safe buffers opt-out region serialization with modules: this120// file loads 2 submodules from top-level module121// `safe_buffers_test_optout`, which uses another top-level module122// `safe_buffers_test_base`. (So the module dependencies form a DAG.)123 124// No expected warnings from base.h, test_sub1, or test_sub2 because they are125// in separate modules, and the explicit commands that builds them have no126// `-Wunsafe-buffer-usage`.127 128int foo(int * p) {129 int x = p[5]; // expected-warning{{unsafe buffer access}} expected-note{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}130#pragma clang unsafe_buffer_usage begin131 int y = p[5];132#pragma clang unsafe_buffer_usage end133 sub1_T(p); // instantiate template134 return sub1(p) + sub2(p);135}136 137#pragma clang unsafe_buffer_usage begin138#include "textual.h" // This header is textually included (i.e., it is in the same TU as %s), so warnings are suppressed139#pragma clang unsafe_buffer_usage end140