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

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 1 10:18:11 PDT 2023


================
@@ -322,6 +322,56 @@ 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 a single constant power of two greater than zero.
+    if (!ArgVal.isPowerOf2()) {
+      Diag(E->getExprLoc(), diag::err_attribute_argument_not_power_of_two)
+          << CI;
+      return nullptr;
+    }
+  }
+
+  return new (Context) CodeAlignAttr(Context, CI, E);
+}
+
+static Attr *handleCodeAlignAttr(Sema &S, Stmt *St, const ParsedAttr &A) {
+
+  Expr *E = A.getArgAsExpr(0);
+  return S.BuildCodeAlignAttr(A, E);
+}
+
+// Emit duplicate error for [[clang::code_align()]] attribute.
+template <typename LoopAttrT>
+static void
+CheckForDuplicateLoopAttribute(Sema &S,
+                               const SmallVectorImpl<const Attr *> &Attrs) {
+  const LoopAttrT *LoopAttr = nullptr;
+
+  for (const auto *I : Attrs) {
+    if (LoopAttr && isa<LoopAttrT>(I)) {
----------------
erichkeane wrote:

This is another where some sort of iteration on find_if is significantly better.

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


More information about the cfe-commits mailing list