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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 10:56:22 PDT 2023


================
@@ -322,6 +322,50 @@ 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();
+
+    int align_value = ArgVal.getSExtValue();
+    if (align_value < CodeAlignAttr::getMinValue() ||
+        align_value > CodeAlignAttr::getMaxValue() || !ArgVal.isPowerOf2()) {
+      Diag(CI.getLoc(), diag::err_attribute_power_of_two_in_range)
+          << CI << CodeAlignAttr::getMinValue() << CodeAlignAttr::getMaxValue();
----------------
AaronBallman wrote:

```suggestion
    int AlignValue = ArgVal.getSExtValue();
    if (AlignValue < CodeAlignAttr::getMinValue() ||
        AlignValue > CodeAlignAttr::getMaxValue() || !ArgVal.isPowerOf2()) {
      Diag(CI.getLoc(), diag::err_attribute_power_of_two_in_range)
          << CI << CodeAlignAttr::getMinValue() << CodeAlignAttr::getMaxValue();
```
It would be good to also pass in the alignment value we calculated as part of the diagnostic (because it can be a constant expression, the actual value may not be obvious from the code snippet).

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


More information about the cfe-commits mailing list