[clang] [clang] Fix pretty-printing assume_aligned attributes (PR #67331)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 26 06:55:26 PDT 2023
https://github.com/steakhal updated https://github.com/llvm/llvm-project/pull/67331
>From 988dcf4f568a58d9b1127b2adf8890a7c7867ac5 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 1/2] [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 | 7 ++++---
2 files changed, 10 insertions(+), 3 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..f036656a7685fd6 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 == "IdentifierInfo *" || T == "Expr *")
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";
}
>From bcc34ac3028a2f5d1107a219d02b0f55db1eb0f5 Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Tue, 26 Sep 2023 15:54:37 +0200
Subject: [PATCH 2/2] Assert an exhaustive list where we return false
---
clang/utils/TableGen/ClangAttrEmitter.cpp | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index f036656a7685fd6..976011c13d060c9 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -320,13 +320,19 @@ namespace {
}
std::string getIsOmitted() const override {
- StringRef T = type;
- if (T == "IdentifierInfo *" || T == "Expr *")
+ auto IsOneOf = [](StringRef subject, auto... list) {
+ return ((subject == list) || ...);
+ };
+
+ if (IsOneOf(type, "IdentifierInfo *", "Expr *"))
return "!get" + getUpperName().str() + "()";
- if (T == "TypeSourceInfo *")
+ if (IsOneOf(type, "TypeSourceInfo *"))
return "!get" + getUpperName().str() + "Loc()";
- if (T == "ParamIdx")
+ if (IsOneOf(type, "ParamIdx"))
return "!get" + getUpperName().str() + "().isValid()";
+
+ assert(IsOneOf(type, "unsigned", "int", "bool", "FunctionDecl *",
+ "VarDecl *"));
return "false";
}
More information about the cfe-commits
mailing list