Skip to content

Commit 332baec

Browse files
[-Wunsafe-buffer-usage] Reland '[Clang] Optimize -Wunsafe-buffer-usage (llvm#125492)' (#10340)
[Clang] Optimize -Wunsafe-buffer-usage. (llvm#125492) The Clang disgnostic `-Wunsafe-buffer-usage` was adding up to +15% compilation time when used. Profiling showed that most of the overhead comes from the use of ASTMatchers. This change replaces the ASTMatcher infrastructure with simple matching functions and keeps the functionality unchanged. It reduces the overhead added by `-Wunsafe-buffer-usage` by 87.8%, leaving a negligible additional compilation time of 1.7% when the diagnostic is used. **Old version without -Wunsafe-buffer-usage:** ``` $ hyperfine -i -w 1 --runs 5 '/tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20' Benchmark 1: /tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 Time (mean ± σ): 231.035 s ± 3.210 s [User: 229.134 s, System: 1.704 s] Range (min … max): 228.751 s … 236.682 s 5 runs ``` **Old version with -Wunsafe-buffer-usage:** ``` $ hyperfine -i -w 1 --runs 10 '/tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage' Benchmark 1: /tmp/old_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage Time (mean ± σ): 263.840 s ± 0.854 s [User: 262.043 s, System: 1.575 s] Range (min … max): 262.442 s … 265.142 s 10 runs ``` **New version with -Wunsafe-buffer-usage:** ``` $ hyperfine -i -w 1 --runs 10 '/tmp/new_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage' Benchmark 1: /tmp/new_clang -c -std=c++20 /tmp/preprocessed.cc -ferror-limit=20 -Wunsafe-buffer-usage Time (mean ± σ): 235.169 s ± 1.408 s [User: 233.406 s, System: 1.561 s] Range (min … max): 232.221 s … 236.792 s 10 runs ``` Conflicts: clang/lib/Analysis/UnsafeBufferUsage.cpp [-Wunsafe-buffer-usage] Correctly merge "[Clang] Optimize -Wunsafe-buffer-usage" The upstream commit 18a0bd4fc387e16c4bd342c6d3a83366e2ec2bc1 replaces ASTMatchers in `UnsafeBufferUsage.cpp` with plain AST matching. It improves the performance and makes the code easier to debug and optimize. This commit resolves massive conflicts introduced in 18a0bd4fc387e16c4bd342c6d3a83366e2ec2bc1. (rdar://147529568) Co-authored-by: Ivana Ivanovska <[email protected]>
1 parent f4c238a commit 332baec

File tree

2 files changed

+1071
-678
lines changed

2 files changed

+1071
-678
lines changed

clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
#define WARNING_OPTIONAL_GADGET(name) WARNING_GADGET(name)
2525
#endif
2626

27+
/// A `WARNING_GADGET` subset, each of which corresponds to an unsafe
28+
/// interaction with bounds-attributed constructs
29+
#ifndef WARNING_BOUNDS_SAFETY_GADGET
30+
#define WARNING_BOUNDS_SAFETY_GADGET(name) WARNING_GADGET(name)
31+
#endif
32+
2733
/// Safe gadgets correspond to code patterns that aren't unsafe but need to be
2834
/// properly recognized in order to emit correct warnings and fixes over unsafe
2935
/// gadgets.
@@ -39,8 +45,8 @@ WARNING_GADGET(UnsafeBufferUsageAttr)
3945
WARNING_GADGET(UnsafeBufferUsageCtorAttr)
4046
WARNING_GADGET(DataInvocation)
4147
// TO_UPSTREAM(BoundsSafety) ON
42-
WARNING_GADGET(CountAttributedPointerArgument)
43-
WARNING_GADGET(SinglePointerArgument)
48+
WARNING_BOUNDS_SAFETY_GADGET(CountAttributedPointerArgument)
49+
WARNING_BOUNDS_SAFETY_GADGET(SinglePointerArgument)
4450
// TO_UPSTREAM(BoundsSafety) OFF
4551
WARNING_OPTIONAL_GADGET(UnsafeLibcFunctionCall)
4652
WARNING_OPTIONAL_GADGET(SpanTwoParamConstructor) // Uses of `std::span(arg0, arg1)`
@@ -58,4 +64,5 @@ FIXABLE_GADGET(PointerInit)
5864
#undef FIXABLE_GADGET
5965
#undef WARNING_GADGET
6066
#undef WARNING_OPTIONAL_GADGET
67+
#undef WARNING_BOUNDS_SAFETY_GADGET
6168
#undef GADGET

0 commit comments

Comments
 (0)