> On Jul 24, 2025, at 11:03, Martin Uecker <ma.uec...@gmail.com> wrote: >>>> __counted_by(M)); >>>> ``` >>>> It's kind of gross to need two attributes to do the same notional >>>> thing, but it does solve the vast majority of the usages seen in the >>>> wild if you're willing to accept some awkwardness around things like: >>>> ``` >>>> struct S { >>>> char *buffer; >>>> int *len __counts(buffer); // Note that len is a pointer >>>> }; >>>> ``` >>>> because we'd need the semantics of `counts` to include dereferencing >>>> to the `int` in order to be a valid count. We'd be sacrificing the >>>> ability to handle the "others my script couldn't categorize", but >>>> that's 28 out of the 905 total cases and maybe that's acceptable? >>> >>> So what do you think about the solution Qing mentioned: >>> >>> struct { >>> char *buf __counted_by_expr(int len; len + 7); >>> int len; >>> }; >>> >>> which would be very flexible and support all possible use cases >>> and has no parsing or semantic interpretation issues. >> >> Personally, I'm not excited by it because one of the big sticking >> points in the Clang community is shared header files with C++. Because >> these attributes are used on structures and functions, the two most >> common things you'll find in a shared header file, we *really* want >> the feature to be workable in both languages to the greatest extent >> possible. And once we care about C++, things get so much harder due to >> the extra complexity it brings. So, for example, we'd have to figure >> out how to handle things like: >> ``` >> template <typename Ty> >> struct S { >> char *buffer __counted_by_expr(Ty len; len + 7); >> int len; // Oooooops >> }; >> >> template <typename Ty, typename Uy> >> struct T { >> char *buffer __counted_by_expr(Ty len; len + 7); >> Uy len; // Grrrrr >> }; >> ``` > > For my understanding: What is the problem here? I would be an > error if the declared type of len is inconsistent between the > attribute and the type that cames later in the member. I guess > a compiler could also warn already when it sees a template > like this where it refers to different template arguments. > > But then, templates also certainly do not appear in shared headers, > so I am not sure why Clang could not simmply offer both, > a late-parsing version and also a C-compatible __counted_by_expr.
Agreed -:) Based on the long discussion so far (more than 7 months, and within GCC community, within CLANG community, between these two communities…) I think the best practical and compromised solution is: CLANG offers both A. A late-parsing version based on APPLE’s -fbounds-sefety implementation (Which favors C++ compatible more than C compatible) counted_by (expression) B. A C-comptatible version counted_by_expr (forward decls; expression) GCC offers the above version B. Clang users can choose either of A or B based on their preference. > > I can understand if this is not your first choice, but it seems > to be a reasonable compromise to me. > > >> I think it's possible to handle these situations, but we'd have to sit >> down and think through all the edge cases and whether we can handle >> them all with some reasonable QoI. I think we'd ultimately run into >> the same concerns here as we ran into with forward declared >> parameters. I think the reason folks in Clang are more comfortable >> with late parsing is because it means the user doesn't have to repeat >> the type and the name which makes for less chances for the user to >> screw things up and get into these weird situations. There can be >> other weird situations with late parsing too, of course, but I think >> the scope of those edge cases is a bit narrower. > > TBH, I am not terrible convinced about this argument. > > If I understood it correctly, the late parsing design seems to make > no distinctions between which identifiers is used, the local or > the global one and just prefers the local one if it exists, possibly > giving a warning if there is also a global one. So, the late parsing design implicitly adds a new C++-like scoping (member scope) into C language as I summarized in my writeup: https://gcc.gnu.org/pipermail/gcc-patches/2025-March/677024.html Appendix A: Scope of variables in C and C++ --The hints to the design of counted_by in C From my understanding, this is a big change to the C language. (I am not a language expert, though -:). > > My C++ examples shows that you can easily run into UB here in C++, > especially since subtle differnt rules apply in different but very > similar scenarious. How can this not be error prone? > > > The forward declaration, the [.N] syntax, and also __self__ etc. > would all make this explicit which identifiers is meant. Yes, all these 3 approaches resolve the scoping issue with a proper language extension. Based on our previous discussion, forward declaration approach is the best compromised solution to be consistent with CLANG. At the same time, forward declaration approach Also resolves the issue that counted_by field is declared AFTER the pointer field. So, I think that the forward declaration approach is the best one. > >> >> The other downside is that we have more attributes that need to >> support something similar, like the thread safety attributes (which I >> believe is also an important use case for the Linux kernel folks?). We >> could do this dance on a per-attribute basis, but if the solution >> worked for all attributes *and* array extents at the same time, that >> would be nice. Not certain it's a requirement though. > > True. But if it is to work properly for arrays in C too, then the > C constraints are also important IMHO, not just the C++ rules. From my understanding, -fbounds-safety is a C extension in the beginning https://clang.llvm.org/docs/BoundsSafety.html Why C++ compatibility is more important than C compatibility for CLANG’s design? > >> >>> The the thing is that WG14 had (weak) consensus for parameter >>> forward declarations and I think more consensus for [.N] >>> syntax in structures already. So I had hoped that we will be >>> able to make progress on this. >> >> Question on the .N syntax: I thought I heard that this was something >> GCC could handle, but that it still requires late parsing to ensure >> type information for N is available and that was a problem. e.g., >> >> void func(char *buffer __counted_by(.N * sizeof(.N)), int N); > >> >> where we'd need to know both the name and the type. Am I wrong about >> that being a challenge for GCC to support? > > I think it is generally a challenge to support. One could certainly > store away the tokens and parse them later (this is certainly doable), > but it adds a lot of issues because you need to add a lot of constraints > for things which should then not be allwoed. And it is still not an > acceptable solution for size arguments in C. .N syntax will definitely resolve the scoping issue, but cannot resolve the issue that the counted_by field (or parameter) is declared after the pointer. Forward declaration approach should resolve this issue from my understanding. That’s one of the reason I prefer that approach since it resolves these two issues together and naturally. Qing > > .N would work here if you combine with a rule such as ".N" is always > converted to "size_t". Or you require an explicit cast if is different > to "size_t" . > > > Martin > > >> If so, I think it may be >> plausible in Clang (implementation-wise, if we can handle late parsing >> without the dot, it sure seems like handling it with the dot won't be >> any harder). Whether the community will go for it or not, I'm not >> certain, but if GCC can support it, I can try to sell it to Clang >> folks as a good compromise. >> >> ~Aaron >