[clang] [clang] Fix pretty-printing assume_aligned attributes (PR #67331)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 25 23:57:34 PDT 2023


https://github.com/steakhal updated https://github.com/llvm/llvm-project/pull/67331

>From 67e19382fb45fe5e06a5c8de2e7b1434c8b1c68f Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Mon, 25 Sep 2023 15:37:34 +0200
Subject: [PATCH] [clang] Fix pretty-printing assume_aligned attributes

Inside `writePrettyPrintFunction()`, we check if we need to emit the
given argument:
```C++
if (!arg->isOptional() || arg->getIsOmitted() == "false") {
    FoundNonOptArg = true;
    continue;
}
```
For the `AssumeAligned` attribute, the second argument was optional, but
the `getIsOmitted()` returned `false`, thus we treated this argument as
**non-optional** in the end because of that disjunction.

It was because `getIsOmitted()` did not account for `Expr *` type, and
returned `false` on the fallthrough branch.

Fixes #67156
---
 clang/test/AST/attr-print-emit.cpp        |  6 +++
 clang/utils/TableGen/ClangAttrEmitter.cpp | 49 +++++++++++++----------
 2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/clang/test/AST/attr-print-emit.cpp b/clang/test/AST/attr-print-emit.cpp
index cc7413baf10e87a..8c48eb92daba5ee 100644
--- a/clang/test/AST/attr-print-emit.cpp
+++ b/clang/test/AST/attr-print-emit.cpp
@@ -2,6 +2,12 @@
 // RUN: %clang -emit-ast -o %t.ast %s
 // RUN: %clang_cc1 %t.ast -ast-print | FileCheck %s
 
+// CHECK: void *aa() __attribute__((assume_aligned(64)));
+void *aa() __attribute__((assume_aligned(64)));
+
+// CHECK: void *aa2() __attribute__((assume_aligned(64, 8)));
+void *aa2() __attribute__((assume_aligned(64, 8)));
+
 // CHECK: void xla(int a) __attribute__((xray_log_args(1)));
 void xla(int a) __attribute__((xray_log_args(1)));
 
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 7ea09058c3d39f2..effeba51e99ef4f 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -320,11 +320,12 @@ namespace {
     }
 
     std::string getIsOmitted() const override {
-      if (type == "IdentifierInfo *")
+      StringRef T = type;
+      if (T == "Expr *" || T == "IdentifierInfo *")
         return "!get" + getUpperName().str() + "()";
-      if (type == "TypeSourceInfo *")
+      if (T == "TypeSourceInfo *")
         return "!get" + getUpperName().str() + "Loc()";
-      if (type == "ParamIdx")
+      if (T == "ParamIdx")
         return "!get" + getUpperName().str() + "().isValid()";
       return "false";
     }
@@ -1391,52 +1392,58 @@ createArgument(const Record &Arg, StringRef Attr,
   if (ArgName == "AlignedArgument")
     Ptr = std::make_unique<AlignedArgument>(Arg, Attr);
   else if (ArgName == "EnumArgument")
-    Ptr = std::make_unique<EnumArgument>(Arg, Attr);
+    Ptr = std::make_unique<EnumArgument>(Arg, Attr); // "<Type>"
   else if (ArgName == "ExprArgument")
-    Ptr = std::make_unique<ExprArgument>(Arg, Attr);
+    Ptr = std::make_unique<ExprArgument>(Arg, Attr); // "Expr *"
   else if (ArgName == "DeclArgument")
     Ptr = std::make_unique<SimpleArgument>(
-        Arg, Attr, (Arg.getValueAsDef("Kind")->getName() + "Decl *").str());
+        Arg, Attr,
+        (Arg.getValueAsDef("Kind")->getName() + "Decl *")
+            .str()); // "<Kind>Decl *"
   else if (ArgName == "IdentifierArgument")
-    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
+    Ptr = std::make_unique<SimpleArgument>(
+        Arg, Attr, "IdentifierInfo *"); // "IdentifierInfo *"
   else if (ArgName == "DefaultBoolArgument")
     Ptr = std::make_unique<DefaultSimpleArgument>(
-        Arg, Attr, "bool", Arg.getValueAsBit("Default"));
+        Arg, Attr, "bool", Arg.getValueAsBit("Default")); // "bool"
   else if (ArgName == "BoolArgument")
-    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "bool");
+    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "bool"); // "bool"
   else if (ArgName == "DefaultIntArgument")
     Ptr = std::make_unique<DefaultSimpleArgument>(
-        Arg, Attr, "int", Arg.getValueAsInt("Default"));
+        Arg, Attr, "int", Arg.getValueAsInt("Default")); // "int"
   else if (ArgName == "IntArgument")
-    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "int");
+    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "int"); // "int"
   else if (ArgName == "StringArgument")
     Ptr = std::make_unique<StringArgument>(Arg, Attr);
   else if (ArgName == "TypeArgument")
     Ptr = std::make_unique<TypeArgument>(Arg, Attr);
   else if (ArgName == "UnsignedArgument")
-    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
+    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "unsigned"); //"unsigned"
   else if (ArgName == "VariadicUnsignedArgument")
     Ptr = std::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
   else if (ArgName == "VariadicStringArgument")
-    Ptr = std::make_unique<VariadicStringArgument>(Arg, Attr);
+    Ptr = std::make_unique<VariadicStringArgument>(Arg, Attr); // "StringRef"
   else if (ArgName == "VariadicEnumArgument")
-    Ptr = std::make_unique<VariadicEnumArgument>(Arg, Attr);
+    Ptr = std::make_unique<VariadicEnumArgument>(Arg, Attr); // "<Type>"
   else if (ArgName == "VariadicExprArgument")
-    Ptr = std::make_unique<VariadicExprArgument>(Arg, Attr);
+    Ptr = std::make_unique<VariadicExprArgument>(Arg, Attr); // "Expr *"
   else if (ArgName == "VariadicParamIdxArgument")
-    Ptr = std::make_unique<VariadicParamIdxArgument>(Arg, Attr);
+    Ptr = std::make_unique<VariadicParamIdxArgument>(Arg, Attr); // "ParamIdx"
   else if (ArgName == "VariadicParamOrParamIdxArgument")
-    Ptr = std::make_unique<VariadicParamOrParamIdxArgument>(Arg, Attr);
+    Ptr = std::make_unique<VariadicParamOrParamIdxArgument>(Arg, Attr); // "int"
   else if (ArgName == "ParamIdxArgument")
-    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx");
+    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx"); // "ParamIdx"
   else if (ArgName == "VariadicIdentifierArgument")
-    Ptr = std::make_unique<VariadicIdentifierArgument>(Arg, Attr);
+    Ptr = std::make_unique<VariadicIdentifierArgument>(
+        Arg, Attr); // "IdentifierInfo *"
   else if (ArgName == "VersionArgument")
     Ptr = std::make_unique<VersionArgument>(Arg, Attr);
   else if (ArgName == "OMPTraitInfoArgument")
-    Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "OMPTraitInfo *");
+    Ptr = std::make_unique<SimpleArgument>(
+        Arg, Attr, "OMPTraitInfo *"); // "OMPTraitInfo *"
   else if (ArgName == "VariadicOMPInteropInfoArgument")
-    Ptr = std::make_unique<VariadicOMPInteropInfoArgument>(Arg, Attr);
+    Ptr = std::make_unique<VariadicOMPInteropInfoArgument>(
+        Arg, Attr); // "OMPInteropInfo"
 
   if (!Ptr) {
     // Search in reverse order so that the most-derived type is handled first.



More information about the cfe-commits mailing list