Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: missing gcc attribute "overwritable" Date: Thu, 09 Jul 2020 10:02:39 -0700 Organization: A noiseless patient Spider Lines: 58 Message-ID: <86o8oofwpc.fsf@linuxsc.com> References: <45a3e0bc-dab7-4bf9-9ca8-5162978d1cbao@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="c87a676e1fef5793b5d15a831e4eb134"; logging-data="30408"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191pda8qlKPuNd6ny3/uBVrskgLQUWPIns=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:rUHNApbqC+JlGv27elZdBeDIcVA= sha1:/uVG+uh/HuNbtJmJXrF6emLfzqA= Xref: csiph.com comp.lang.c:153161 mark.bluemel@gmail.com writes: > On Friday, 3 July 2020 03:33:06 UTC+1, donal...@gmail.com wrote: > >> On Thursday, July 2, 2020 at 1:56:33 PM UTC-4, aotto1...@gmail.com wrote: >> >>> Hi, this is a basic example -> not so difficult to implement :-) >>> >>> #include >>> >>> __attribute__((overwritable)) >>> static int test (int i) { >>> return i+2; >>> } >>> >>> // overwrite >>> static int test (int i) { >>> return i+3; >>> } >> >> // Written manually: >> static int test (int i) { >> return i + 3; >> } >> #define OVERRODE_test >> >> // Written by generator: >> #ifndef OVERRODE_test >> static int test (int i) { >> return i + 2; >> } >> #endif >> >>> void main (void) { >> >> Bonehead. > > I dislike the preprocessor (blind prejudice, I'm sure) and wondered > whether something could be done with function pointers, like a > dispatch table. > > Calls should be through a function pointers, which the generated > code would initialise and any hand-written code subsequently update. If we accept the premise of the problem statement (that is, a way to supersede an automatically generated function with a manually written one), using function pointers has some, hmmm, let me say inefficiencies. For one, calls through a function pointer will inhibit (with high probability) potential inline expansion. Also, any generated function that is superseded (but still has its address taken for storing, if temporarily, in a function pointer) will still be compiled and the code generated for it will (again with high probability) still be present in the executable. Even though some people may find it distasteful, the C preprocessor is a fairly good match for solving the problem here (again under the assumption that we accept the premise that the stated problem is what needs to be solved).