[clang] nonblocking/nonallocating attributes: 2nd pass caller/callee analysis (PR #99656)
Doug Wyatt via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 19 13:42:21 PDT 2024
================
@@ -0,0 +1,256 @@
+// RUN: %clang_cc1 -fsyntax-only -fblocks -fcxx-exceptions -std=c++20 -verify %s
+// These are in a separate file because errors (e.g. incompatible attributes) currently prevent
+// the FXAnalysis pass from running at all.
+
+// This diagnostic is re-enabled and exercised in isolation later in this file.
+#pragma clang diagnostic ignored "-Wperf-constraint-implies-noexcept"
+
+// --- CONSTRAINTS ---
+
+void nb1() [[clang::nonblocking]]
+{
+ int *pInt = new int; // expected-warning {{'nonblocking' function must not allocate or deallocate memory}}
+ delete pInt; // expected-warning {{'nonblocking' function must not allocate or deallocate memory}}
+}
+
+void nb2() [[clang::nonblocking]]
+{
+ static int global; // expected-warning {{'nonblocking' function must not have static locals}}
+}
+
+void nb3() [[clang::nonblocking]]
+{
+ try {
+ throw 42; // expected-warning {{'nonblocking' function must not throw or catch exceptions}}
+ }
+ catch (...) { // expected-warning {{'nonblocking' function must not throw or catch exceptions}}
+ }
+}
+
----------------
dougsonos wrote:
I got this far:
```
def warn_func_effect_violation : Warning<
"'%0' %select{function|constructor|destructor|constructor's member initializer|lambda|block}1 "
"must not %select{allocate or deallocate memory|throw or catch exceptions|"
"have static local variables|use thread-local variables|access ObjC methods or properties}2">,
InGroup<FunctionEffects>;
def note_func_effect_violation : Note<
"%select{function|constructor|destructor|member initializer|lambda|block}0 cannot be inferred '%1' because it "
"%select{allocates or deallocates memory|throws or catches exceptions|"
"has a static local variable|uses a thread-local variable|"
"accesses an ObjC method or property}2">;
def warn_func_effect_calls_func_without_effect : Warning<
"'%0' function must not call non-'%0' function '%1'">,
InGroup<FunctionEffects>;
def note_func_effect_calls_func_without_effect : Note<
"function cannot be inferred '%0' because it calls non-'%0' function '%1'">;
```
Notice that the first two have customized "function". The second and subsequent ones (there's another 6) could do the same thing but they'd have to duplicate those large '%select{}`s. Possibly my aversion to copy/pasting is excessive. But then again I can see other diagnostics that use "function" and could possibly be better if they used the more specific terms too... thus the slippery slope.
https://github.com/llvm/llvm-project/pull/99656
More information about the cfe-commits
mailing list