[clang] [clang][Sema] Move computing enum bits into a separate function (PR #126096)

Michael Buch via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 10 04:17:48 PST 2025


Michael137 wrote:

> > > > This is just moving stuff around so that seems fine. Though, I wonder, could we just pass e.g. an `ArrayRef<Decl*>` to `computeEnumBits()`? The LLDB pr you linked seems to have an `SmallVector<EnumConstantDecl*>`, so that should be possible (I think it does require a `reinterpret_cast` from a `const Decl**` to a `const EnumConstantDecl**`, but we already do that in other places in Clang iirc).
> > > 
> > > 
> > > The reason I suggested the template initially was because in LLDB's case we don't have the `EnumConstantDecl`s in a container. We only have an `EnumDecl`. So the plan was to pass the `enumerators()` iterator into `computeEnumBits`. But if you know of a good way to turn the range into a container that we can pass as an `ArrayRef`, or some non-template way of passing both an `iterator_range` and `ArrayRef` into `computeEnumBits`, that'd be great!
> > 
> > 
> > Yeah, `enumerator()` returns `llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>`, and couldn't find a good way to convert this to an `ArrayRef`. I guess I could make a `SmallVector<EnumConstantDecl*>` and put the copy of pointers there at the point of their creation ([here](https://github.com/llvm/llvm-project/blob/b581ad411d6e178300732410dd8850cd34543ac3/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp#L2373)), since the function return a `EnumConstantDecl`, then discard it.
> 
> Hmm, I feel like you _might_ be able to extract the pointers from the range and do something with those, but I’m not too familiar w/ how that works exactly off the top of my head. I don’t think the template is that big of an issue; it’d be nice not to have it, but if there is no sane/straight-forward way to get around it then I don’t really have a problem w/ it either.

The only way I can see us extracting a raw `clang::Decl*` out of the iterator is if we somehow get to `DeclContext::FirstDecl`/`DeclContext::LastDecl`, and construct the `ArrayRef` from those. But I don't see a good way of doing that. The alternative is to collect all the enumerators into a temporary `SmallVector` on the LLDB side, then pass it as an `ArrayRef`, though that feels a bit silly since we're control of the interface and could just avoid the work. So my personal preference is to stick with the template, but I'll leave it to the Clang maintainers for the final call

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


More information about the cfe-commits mailing list