[clang] [clang] Add support for new loop attribute [[clang::code_align()]] (PR #70762)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 6 10:55:17 PST 2023


================
@@ -322,6 +322,57 @@ static Attr *handleUnlikely(Sema &S, Stmt *St, const ParsedAttr &A,
   return ::new (S.Context) UnlikelyAttr(S.Context, A);
 }
 
+CodeAlignAttr *Sema::BuildCodeAlignAttr(const AttributeCommonInfo &CI,
+                                        Expr *E) {
+  if (!E->isValueDependent()) {
+    llvm::APSInt ArgVal;
+    ExprResult Res = VerifyIntegerConstantExpression(E, &ArgVal);
+    if (Res.isInvalid())
+      return nullptr;
+    E = Res.get();
+
+    // This attribute requires a strictly positive value.
+    if (ArgVal <= 0) {
+      Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
+          << CI << /*positive*/ 0;
+      return nullptr;
+    }
+
+    // This attribute requires an integer argument which is a constant power of
+    // two between 1 and 4096 inclusive.
+    int AlignValue = ArgVal.getSExtValue();
+    if (AlignValue > CodeAlignAttr::getMaxValue() || !ArgVal.isPowerOf2()) {
+      Diag(CI.getLoc(), diag::err_attribute_power_of_two_in_range)
----------------
AaronBallman wrote:

```suggestion
    if (AlignValue < CodeAlignAttr::getMinValue() || AlignValue > CodeAlignAttr::getMaxValue() || !ArgVal.isPowerOf2()) {
      Diag(CI.getLoc(), diag::err_attribute_power_of_two_in_range)
```
You'll have to adjust for 80-col limits.

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


More information about the cfe-commits mailing list