[clang] nolock/noalloc attributes (PR #84983)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 12 18:02:34 PDT 2024


================
@@ -395,6 +395,33 @@ bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
   return checkStringLiteralArgumentAttr(AL, ArgExpr, Str, ArgLocation);
 }
 
+/// Check if the argument \p ArgNum of \p Attr is a compile-time constant
+/// integer (boolean) expression. If not, emit an error and return false.
+bool Sema::checkBoolExprArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
+                                     bool &Value) {
+  if (AL.isInvalid()) {
+    return false;
+  }
+  Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
+  SourceLocation ErrorLoc(AL.getLoc());
+
+  if (AL.isArgIdent(ArgNum)) {
+    IdentifierLoc *IL = AL.getArgAsIdent(ArgNum);
+    ErrorLoc = IL->Loc;
+  } else if (ArgExpr != nullptr) {
+    if (const std::optional<llvm::APSInt> MaybeVal =
+            ArgExpr->getIntegerConstantExpr(Context, &ErrorLoc)) {
+      Value = MaybeVal->getBoolValue();
+      return true;
+    }
+  }
----------------
Sirraide wrote:

> how would we represent nolock(expression) before we can evaluate the expression?

You just store the dependent expression in the attribute. If it’s dependent, then you only check whether it’s convertible (and whether it’s `true`) when you instantiate this attribute by running Sema on it again. As I already mentioned, you can take a look at how `CXXAssumeAttr` is handled (I implemented that one recently; it has to deal w/ almost all of that as well, so you should be able to get an idea as to what you need to do; you can ignore all the custom parsing code for it, though), to get an idea as to what you need to do.

> And when would it be collapsed back into a concretely true or false form?

The expression in the attribute itself? Never, pretty much. I’ll just stay as an expression, and you can evaluate it to figure out whether it’s `true` or `false` (afaik you can wrap the expression in a `ConstantExpr` after evaluating it to avoid having to re-evaluate it over and over again).

Feel free to ask if you have any more questions about this; I recall this having been quite confusing for me as well when I had to figure out how all of this actually works...

https://github.com/llvm/llvm-project/pull/84983


More information about the cfe-commits mailing list