[clang] [clang][NFC] Generalize getSpecificAttr for const attributes (PR #116606)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 18 03:47:13 PST 2024
https://github.com/steakhal created https://github.com/llvm/llvm-project/pull/116606
This patch allows using `getSpecificAttr` for getting `const` attributes. Previously, if users of this API would want to get a const Attribute pointer, they had to pass `getSpecificAttr<const XYZ>()`, to get it compile. It feels like an arbitrary limitation as the constness was already encoded in the Attribute container's value type.
>From 0af9b543de902c8f04300da0220323f59a4531dd Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Mon, 18 Nov 2024 12:26:26 +0100
Subject: [PATCH] [clang][NFC] Generalize getSpecificAttr for const attributes
This patch allows using `getSpecificAttr` for getting `const`
attributes. Previously, if users of this API would want to get a const
Attribute pointer, they had to pass `getSpecificAttr<const XYZ>()`, to
get it compile. It feels like an arbitrary limitation as the constness
was already encoded in the Attribute container's value type.
---
clang/include/clang/AST/AttrIterator.h | 16 +++++++++-------
clang/lib/CodeGen/CGLoopInfo.cpp | 2 +-
2 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/clang/include/clang/AST/AttrIterator.h b/clang/include/clang/AST/AttrIterator.h
index 66571e1cf0b8ec..7e2bb0381d4c8f 100644
--- a/clang/include/clang/AST/AttrIterator.h
+++ b/clang/include/clang/AST/AttrIterator.h
@@ -14,11 +14,13 @@
#define LLVM_CLANG_AST_ATTRITERATOR_H
#include "clang/Basic/LLVM.h"
+#include "llvm/ADT/ADL.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include <cassert>
#include <cstddef>
#include <iterator>
+#include <type_traits>
namespace clang {
@@ -113,13 +115,13 @@ inline bool hasSpecificAttr(const Container& container) {
specific_attr_end<SpecificAttr>(container);
}
template <typename SpecificAttr, typename Container>
-inline SpecificAttr *getSpecificAttr(const Container& container) {
- specific_attr_iterator<SpecificAttr, Container> i =
- specific_attr_begin<SpecificAttr>(container);
- if (i != specific_attr_end<SpecificAttr>(container))
- return *i;
- else
- return nullptr;
+inline auto *getSpecificAttr(const Container &container) {
+ using ValueTy = llvm::detail::ValueOfRange<Container>;
+ using ValuePointeeTy = std::remove_pointer_t<ValueTy>;
+ using IterTy = std::conditional_t<std::is_const_v<ValuePointeeTy>,
+ const SpecificAttr, SpecificAttr>;
+ auto It = specific_attr_begin<IterTy>(container);
+ return It != specific_attr_end<IterTy>(container) ? *It : nullptr;
}
} // namespace clang
diff --git a/clang/lib/CodeGen/CGLoopInfo.cpp b/clang/lib/CodeGen/CGLoopInfo.cpp
index cdff7e50c4ee71..448571221ef81b 100644
--- a/clang/lib/CodeGen/CGLoopInfo.cpp
+++ b/clang/lib/CodeGen/CGLoopInfo.cpp
@@ -811,7 +811,7 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
// Identify loop attribute 'code_align' from Attrs.
// For attribute code_align:
// n - 'llvm.loop.align i32 n' metadata will be emitted.
- if (const auto *CodeAlign = getSpecificAttr<const CodeAlignAttr>(Attrs)) {
+ if (const auto *CodeAlign = getSpecificAttr<CodeAlignAttr>(Attrs)) {
const auto *CE = cast<ConstantExpr>(CodeAlign->getAlignment());
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
setCodeAlign(ArgVal.getSExtValue());
More information about the cfe-commits
mailing list