extern inline int a(void) { return 1; } this should generate a function called a in the asm when used with -std=c99 but does not.
Subject: Re: New: [C99] extern inline is handled wrong in C99 mode Looks just the same as bugs 10488 and 10489 (closed) to me, they should probably be marked as duplicates of this or reopened and vice versa.
*** Bug 10488 has been marked as a duplicate of this bug. ***
Confirmed based on that other bug and <http://gcc.gnu.org/c99status.html>.
Outline: -1 -- Pre-Summary [this and the Summary constitute the Executive Summary] 0 -- Summary 1 -- What is an inline definition? 2 -- What does constraint #3 of section 6.7.4 mean? 3 -- Some other notes about inline. 4 -- Disclaimer -1 -- Pre-Summary If bug report #11377 comment #2 is (completely) correct, then that would appear to explain away this problem completely. But just in case gcc "extern inline" != c99 "inline" __exactly, I will continue... with what I think C99 says. I can't test the state of gcc (modern versions) as I should. The information below would be useful to someone that wanted a second opinion on some item or other (even if gcc was perfect). It can also be useful to glance over the examples quickly and maybe read the Summary completely (although there are some items not found in the Summary). Maybe later I or someone else can so some testing of some of the trickier points. 0 -- Summary Only 2 types of function definitions exist: inline definitions and external definitions (6.7.4#6 and 6.9#5). This is a partition; any definition of a function falls into exactly one of these 2 categories. By 6.9 and 6.9.1, every translation unit has exactly one or zero definition for a function. This means that the same translation unit cannot have both an inline and an external def. File 1: inline void f() {} //inline definition, external linkage File 2: extern inline void f() {} //external definition, external linkage File 3: static inline void g() {} //inline definition, internal linkage File 4: static h(void); static inline h() {} //external definition, internal linkage static H() {} //ditto. //all of these four files can coexist in a program. [..even if 'g' 'h' and 'H' were 'f'] File 5 //the following 3 function definitions would all violate 6.7.4#3 // inline void f1_bad() {static a;} //violates part a static x1; // inline void f2_bad() {extern x1;} //violates part b // inline void f3_bad() {x1 = 0;} //violates part b //but the following are OK extern inline void f() {static a; extern x1; x1=0;} //not inline def static inline void g() {static a; extern x1; x1=0;} //not external linkage static h(void); static inline h() {static a; extern x1; x1=0;} //neither i.d. nor e.l. There are other detailed examples (of different inline concepts) in the rest of the post that may be massaged into proper gcc tests if necessary. 1 -- What is an inline definition? From sections 6.9 and 6.7.4 of the standard, we can conclude that all function definitions are either "inline definitions" or otherwise they are "external definitions." They can be one or the other but not both. [The constraint from 6.7.4#3 applies only to inline definitions (and with external linkage).] Assuming that an identifier has at least one function definition within the current translation unit, then there can be only one such definition and... it is an inline definition if and only if __all of the function declarations at file scope in that translation unit for that function identifier obey two restrictions. First, each declaration of that function must contain the 'inline' keyword. Second, none of the declarations can include the 'extern' keyword. [Notice that while extern is implicit (according to 6.2.2#5) when no storage class specifier is present, that implicitness(?) does not violate the second condition.] If either of the two restrictions is violated, the definition is an external definition. It is possible for an inline definition to correspond to an identifier with internal linkage just as it can correspond to an identifier with external linkage (all function identifiers will have linkage). Examples: file 1: static inline void f() {} extern int g() {extern f(void); return 0;} inline void f(void); file 2: inline void f() {} extern int g() {extern f(void); return 0;} inline void f(void); file 3: static inline void f() {} extern int g() {extern f(void); return 0;} void f(void); file 4: inline void f() {} extern int g() {extern f(void); return 0;} extern inline void f(void); f has an inline definition in files 1 and 2; it has an external definition in files 3 and 4. In all of the files, the definition for f is found in the first line (an empty body.. for simplicity). In file 1, we note that the first line makes f have internal linkage. There are exactly 2 appearances of f at file scope (as is true for files 2,3, and 4) and in each case 'inline' is used and 'extern' isn't. In file 2, line 1 causes f to have external linkage. As with file 1, the two declarations of f at file scope contain 'inline' but not 'extern'. File 3 has one declaration without inline, so we have an external declaration. The linkage of f is internal. File 4 has one declaration with extern. This makes f's definition be external also, and the linkage is external in this case. 2 -- What does constraint #3 of section 6.7.4 mean? [#3] An inline definition of a function with external linkage shall not contain a definition of a modifiable object with static storage duration, and shall not contain a reference to an identifier with internal linkage. OK, from the examples above, there is only one line (out of 9) that would be relevant to constraint #3. This would be the first line of file 2. It is a definition; it is an inline definition; and it is one of a function with external linkage. [In this case, there is no violation (the function is empty).] Consider a second example [(a nontrivial) inline definition of an identifier with external linkage.. g]: //BEGIN File: int x1=0, x1a; static int x2=0; extern int x3; static int x4; static int x5; void f1(void); static void f2(void); inline void g() { //inline definition; external linkage. // some declarations (potential definitions; also watch out for linkage) extern int x1; //linkage; external linkage; not a definition. extern int x1a; //linkage; external linkage; not a definition. extern int x3; //linkage; external linkage; not a definition. int x10; //no linkage. definition, but auto storage. int x5; //no linkage. definition, but auto storage. const static int x12 = 12; //no linkage. definition of constant object w/ static storage duration. // static int x11; //illegal. no linkage, but definition of modifiable object with static storage duration. // extern int x4; //illegal. internal linkage (though not a definition). // other uses (no definitions, but linkage is still an issue) x1=5; //x1: external linkage. f1(); //f1: external linkage. x5=5; //x5: external linkage. // x2=5; //illegal. x2: internal linkage. // f2(); //illegal. f2: internal linkage. } //END File. In this example, g has an inline definition. Within its body, we look at each identifier and ask two questions which can be categorized as (a) is this an object that is being defined? if so... and (b) does the identifier have internal linkage? I had some trouble with the wording on the linkage test portion (part b): "and shall not contain a reference to an identifier with internal linkage." [First, I'd note that the "shall" in each of the two parts of this constraint means that if there is a failure of either part then gcc must produce a diagnostic.] What troubled me was the word "reference." I searched through most of the standard and there were a number of cases where this word appeared and was used with its customary English language meaning as in "there was a reference to the other bug report" or "forward references." In the other cases, the meaning was the technical meaning as in the definition in 6.2.5#20 of "referenced type," which relates to (C language) pointers. In yet other cases (such as this one), the meaning was ambiguous. In the end, I felt that the intent here was the English meaning. In fact, 6.7.4#8 uses the English common meaning. It makes more sense to see it as that. The idea is that an inline definition must not have any semantic differences than the external definition found in some other translation unit because for every call within a t-u (translation unit) that has an inline definition, either the inline or external version can be used (neither need be "inlined" yet either could be). By disallowing the programmer to "use" or "access" or "reference" an identifier with internal linkage, there is one less class of potential problems since the external definition would never be able to access those values (except as mentioned in the next paragraph).. values which are retained through function calls (state). What if a pointer reference (or ref to ref to ...) to such an identifier is passed to the function... well, that would be ok because either function would get that reference. In the other cases where there could be an effect, the identifier would have to literally appear within the body and that is what I think #3 prohibits (its appearance). *** [Using this understanding of "reference" ...] Within a function such as 'g' (inline def/ exte link), gcc would need to verify the linkage of every identifier it came across no matter how deeply nested (within a declaration or within a statement). This is right, no? 3 -- Some other notes about inline. ** With very few exceptions, the following statements are true: -- there is exactly one external definition for each function (or object, but the focus on functions is because inline definition only applies to functions) with external linkage, within an entire program. -- there can be as little as zero and as many as N-1 inline definitions for a function identifier of external linkage for a program composed of N translation units. ** There is nothing in the std (that I could find) about inline being part of the type of a function; thus, compatibility and compositeness do not factor in, I don't think. This also means that gcc should accept an inline function declaration even after calls to it were already made (earlier in source) based on non-inlined declarations. Inline is a hint, in any case, and gcc can inline or fail to inline with or without that hint (external definitions would have to be created/exported but this would have been done since the earlier declarations, by assumption, were not inline). In the other case, where inline appears and then disappears, that should be accepted just as readily except that a prior potential inline definition would then be known not to be one, so gcc had better be able to build the function body for export use (ie, gcc cannot know until the end of the translation unit, whether or not any given function definition was an inline definition). ** Related to the above, an "inline function" is defined in 6.7.4#5 but in terms of "function," which is not defined. The Std defined function type, function call, etc, but not function. My guess is that this is further evidence that type is not affected by "inline" and this definition is a general usage, working definition, whose precise meaning probably doesn't impact any part of the Standard except trivially. [Am I right? Did I make sense?] ..so as stated before, it should not matter when inline appears in a declaration, even if earlier declarations differed. Inline is a property of the function which, unlike type, is independent of the precise appearance of the declaration in order to determine applicable rules at that point (i.e., you can't have incompleteness or undefinedness because of inline only). [Tests for potential warnings/errors not provide] ** 6.9#5 footnote 136 (which is not normative -- Foreword#6 --> Part 3 ISO/IEC Directives (?)) may be in conflict with 6.7.4#6 as follows... 6.9#5 allows for function identifiers with external linkage to appear 0 or 1 time w/in the program. 6.7.4#6 says that external linkage function identifiers declared with inline must have definition (inline def or external def, regardless) found within the translation unit. These are consistent; what is not consistent is that footnote 136 draws the conclusion that all external linkage identifiers (eg, external linkage inline function, such as either of the 'f' functions in File 1 or File 2 of the Summary) that do not appear within any expressions need not have any definitions (..despite having been declared). This seems to be contrary to 6.7.4#6... if but in a very, very technical way, that doesn't affect 99.99999% of programs. ** If the following aren't part of gcc tests (?), they can be added >> inline int main () {} //should fail (and does): because of 6.7.4#4 >> inline int main (void); //should fail: see previous >> inline int main (void); //need not fail: for any particular freestanding environment. i.e., the previous 2 assumed a hosted environment. >> inline inline void foo() {} //should pass: despite inline repetition. 4 -- Disclaimer I did not really test the above examples with gcc 3.2.2 (which is what I have). I would prefer to test once I have a newer gcc, especially since I expect any number of the tests to fail (I am also not up to speed on interpreting object/assembly output).
Subject: Re: [C99] extern inline is handled wrong in C99 mode On Sat, 7 Aug 2004, hozelda at yahoo dot com wrote: > By 6.9 and 6.9.1, every translation unit has exactly one or zero definition for a > function. This means that the same translation unit cannot have both an inline > and an external def. I don't see how you deduce this. I agree it's the intent, but I see nothing in the text of the standard that specifically prohibits having both inline and external definitions in a translation unit, just the presumption in 6.7.4#6 that there is just one definition of the function in the TU. I mentioned this in an aside to my pre-DR#2 that I sent to the gcc list some time ago <http://www.srcf.ucam.org/~jsm28/gcc/pre-dr-2.txt>. This would also be one of the incompatibilities between C99 and gnu89, as GCC has allowed both inline and external definitions while the intent of C99 seems to be not to allow them. Thus, one more potential problem to fix in glibc should C99 inline be implemented in GCC. > *** [Using this understanding of "reference" ...] Within a function such as 'g' > (inline def/ exte link), gcc would need to verify the linkage of every identifier it > came across no matter how deeply nested (within a declaration or within a > statement). This is right, no? Including those inside sizeof. I don't think this is a tricky interpretative issue. If, in the inline definition, the identifier is used (in the name space of ordinary identifiers) while the internal linkage declaration is in scope, or declared as extern while that declaration is in scope (so linking to the previous internal linkage declaration), this is a constraint violation. If it is used in another namespace, or redeclared as an identifier with no linkage, there is no problem. If it is declared with external linkage in a scope where the internal linkage declaration is hidden, this is compile-time undefined behavior and an error is permitted though not required.
(In reply to comment #5) > Subject: Re: [C99] extern inline is handled wrong in C99 mode > > On Sat, 7 Aug 2004, hozelda at yahoo dot com wrote: > > > By 6.9 and 6.9.1, every translation unit has exactly one or zero definition for a > > function. This means that the same translation unit cannot have both an inline > > and an external def. > > I don't see how you deduce this. I agree it's the intent, but I see > nothing in the text of the standard that specifically prohibits having > both inline and external definitions in a translation unit, just the > presumption in 6.7.4#6 that there is just one definition of the function > in the TU. I mentioned this in an aside to my pre-DR#2 that I sent to the > gcc list some time ago <http://www.srcf.ucam.org/~jsm28/gcc/pre-dr-2.txt>. > > This would also be one of the incompatibilities between C99 and gnu89, as > GCC has allowed both inline and external definitions while the intent of > C99 seems to be not to allow them. Thus, one more potential problem to > fix in glibc should C99 inline be implemented in GCC. > I was a little sloppy to extend 6.9xxx to inline definitions. [The message posted was also a learning process for me and I didn't re-evaluate some held assumptions as much as I should have.] 6.7.4#6 says, "an inline definition ... does not forbid an external definition in __another__ translation unit." 6.7.4#8 says, "Because cels [which has an inline definition] has external linkage and is referenced, an external definition has to appear in another translation unit...." #6 does not place any requirements; the wording simply seems to suggest that such a requirement already existed. #8 doesn't place a requirement either but the wording is as if such a requirement was in place so that its conclusion can be drawn. I would say that #8 by itself would mean that inline definitions and external definitions (for same identifier) cannot co-exhist in the same translation unit (if perhaps in need of rewording as a rule elsewhere), except that it seems to be an extension of the #7 Example, which would then not be normative. My guess is that the intention is to have at most either one ID or else one ED per t-u. If that was the intention, then there appears to be a defect in the Standard, as it cannot be derived from any normative portion, at least as I read 6.9/6.9.1 and 6.7.4. If the intention, instead, is to allow >1 ID and possibly an ED (per t-u), then the Standard seems ok except that the wording (of the examples #7/#8) should probably eventually be fixed. Thus I think you are completely right if the above is all that there is to it. The link you posted is very interesting (well, if one is looking to get to the bottom of things). I'll spend more time with it later. I do know that I thought originally that "prototype" was not evolved/defined as well as it should have been, but eventually I came to just accept certain understandings of it.. I'll revisit that, I guess, especially since you seem to have pinpointed some precise items. > > *** [Using this understanding of "reference" ...] Within a function such as 'g' > > (inline def/ exte link), gcc would need to verify the linkage of every identifier it > > came across no matter how deeply nested (within a declaration or within a > > statement). This is right, no? > > Including those inside sizeof. I don't think this is a tricky > interpretative issue. If, in the inline definition, the identifier is > used (in the name space of ordinary identifiers) while the internal > linkage declaration is in scope, or declared as extern while that > declaration is in scope (so linking to the previous internal linkage > declaration), this is a constraint violation. If it is used in another > namespace, or redeclared as an identifier with no linkage, there is no > problem. If it is declared with external linkage in a scope where the > internal linkage declaration is hidden, this is compile-time undefined > behavior and an error is permitted though not required. > > One person's garbage is another's treasure.. or something like that. Ok, that paragraph you quoted and a few other things were last minute additions after about a week of letting the message sit. Since I don't have that much experience with compiler internals, it just wasn't something I had thought about before so it seemed like a Eureka momment (I was debating over the meaning of "reference" so ...) .. it's as if "oh, let's go back and add code (a fix) so that every single ident is rechecked for ...." Anyway, it was "tricky" for me at that point in time, and also there probably is nothing there that excludes sizeof or any other case. In general, all of these (constant) sizeof rule exceptions are a little annoying in that they are scattered around when perhaps maybe a new concept should be introduced (unevaluable expression.. or something like that [(aside) I think the Java Lang std uses a similar concept and defines a lot of rules for it]). Especially since in some cases something more general than sizeof is evoked (with perhaps a footnote mentioning sizeof)... anyway, these are just some thoughts. I don't want to comment much more on what I said near the end because I should be willing to provide specific code first, to make things concrete. I have an unrelated question. void *h1=0; int (*h2)(int)=0, (*h3)(int)=0; h2=h1; //should not be allowed but gcc 3.2.2 didn't complain h1=h3; //should not be allowed .... Is this legit material for a bug report (ie, is the "should not" in the comments correct)? [I compiled the example with c99 but not with pedantic] I don't think you can perform the 2 assignments above without a cast (and even then, guarantees from #6.3.2.3 may be lost (eg, see #7 and #8 as a pattern)). 6.5.16.1#1 is a constraint (and "one of the following shall hold" would not be satisfied), and I don't think void* is compatible with functype* and I don't think functype can ever be incomplete. There may be gaps in the std or maybe not, so unless you readily agree or disagree, I would need to reread around and come up with specific references. A quick question: is there a way to receive auto emails of bug reports one has participated in (by commenting)? I didn't get jsm's comment as an email of any sort. I checked the pref->email section of my account, but I could not find the option.
Subject: Re: [C99] extern inline is handled wrong in C99 mode On Sun, 8 Aug 2004, hozelda at yahoo dot com wrote: > I have an unrelated question. > void *h1=0; > int (*h2)(int)=0, (*h3)(int)=0; > h2=h1; //should not be allowed but gcc 3.2.2 didn't complain > h1=h3; //should not be allowed .... > > Is this legit material for a bug report (ie, is the "should not" in the comments > correct)? [I compiled the example with c99 but not with pedantic] That's bug 11234, fixed in 3.4.0. I really don't recommend using anything other than CVS mainline for testing conformance points. You should find mainline significantly better than 3.4 in conformance matters although there are many bugs and unimplemented features and little user interest in the finer points of conformance. > A quick question: is there a way to receive auto emails of bug reports one has > participated in (by commenting)? I didn't get jsm's comment as an email of any > sort. I checked the pref->email section of my account, but I could not find the > option. Add yourself to the CC list of the bug reports of interest. You don't need to comment on them to do so.
Subject: Re: [C99] extern inline is handled wrong in C99 mode On Sun, 8 Aug 2004, hozelda at yahoo dot com wrote: > In general, all of these (constant) sizeof rule exceptions are a little annoying in > that they are scattered around when perhaps maybe a new concept should be > introduced (unevaluable expression.. or something like that [(aside) I think the With regard to this as a general point about clarifying the concepts of the standard (and nothing much to do with the subject of this bug report), there are many areas where the explanations given by the C standards in English are perhaps not the best or clearest possible way of explaining the concepts and defining the language. In some places, it may make sense to introduce new concepts, or formalisms, to explain things (sequence points have been an example where several competing formalisms have been produced). In turn, while formalisms make things more precise and help ascertain answers to subtle cases, they can rather reduce the audience who can understand the standard. Have you read Norrish's thesis <http://www.cl.cam.ac.uk/TechReports/UCAM-CL-TR-453.pdf> (imperfectly formalising some aspects of a subset of C90)? Note in particular the comment in chapter 4: This ... tells us nothing about the quality of our semantics with respect to the original specification .... Better would be to have the specification of the semantics inspected by another individual who was both familiar with the fine details of the ISO standard, and the techniques of operational semantics. Unfortunately, such people are hard to find, which is rather an indictment of the divergence between theory and practice in computer science.
Specific minor C99 inline points not implemented: The diagnostic for inline main should be a pedwarn, not a warning, but should only apply when flag_hosted. Duplicate inline is OK in C99. A declaration with both typedef and inline should receive an error, as a typedef isn't an identifier for a function.
*** Bug 26841 has been marked as a duplicate of this bug. ***
I am working on this (the original reported problem).
Fix posted as <http://gcc.gnu.org/ml/gcc-patches/2006-05/msg00328.html>.
Subject: Bug 16622 Author: geoffk Date: Wed Nov 1 04:47:30 2006 New Revision: 118356 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118356 Log: * c-decl.c (grokdeclarator): Don't set DECL_EXTERNAL on inline static functions in c99 mode. PR 16622 * doc/extend.texi (Inline): Update. * c-tree.h (struct language_function): Remove field 'extern_inline'. * c-decl.c (current_extern_inline): Delete. (pop_scope): Adjust test for an undefined nested function. Add warning about undeclared inline function. (diagnose_mismatched_decls): Update comments. Disallow overriding of inline functions in a translation unit in C99. Allow inline declarations in C99 at any time. (merge_decls): Boolize variables. Handle C99 'extern inline' semantics. (grokdeclarator): Set DECL_EXTERNAL here for functions. Handle C99 inline semantics. (start_function): Don't clear current_extern_inline. Don't set DECL_EXTERNAL. (c_push_function_context): Don't push current_extern_inline. (c_pop_function_context): Don't restore current_extern_inline. PR 11377 * c-typeck.c (build_external_ref): Warn about static variables used in extern inline functions. * c-decl.c (start_decl): Warn about static variables declared in extern inline functions. Added: trunk/gcc/testsuite/gcc.dg/inline-13.c trunk/gcc/testsuite/gcc.dg/inline-14.c trunk/gcc/testsuite/gcc.dg/inline-15.c trunk/gcc/testsuite/gcc.dg/inline-16.c trunk/gcc/testsuite/gcc.dg/inline6.c trunk/gcc/testsuite/gcc.dg/inline7.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-decl.c trunk/gcc/c-tree.h trunk/gcc/c-typeck.c trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gcc.dg/inline-10.c
Subject: Bug 16622 Author: geoffk Date: Wed Nov 1 04:48:15 2006 New Revision: 118357 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118357 Log: * c-decl.c (grokdeclarator): Don't set DECL_EXTERNAL on inline static functions in c99 mode. PR 16622 * doc/extend.texi (Inline): Update. * c-tree.h (struct language_function): Remove field 'extern_inline'. * c-decl.c (current_extern_inline): Delete. (pop_scope): Adjust test for an undefined nested function. Add warning about undeclared inline function. (diagnose_mismatched_decls): Update comments. Disallow overriding of inline functions in a translation unit in C99. Allow inline declarations in C99 at any time. (merge_decls): Boolize variables. Handle C99 'extern inline' semantics. (grokdeclarator): Set DECL_EXTERNAL here for functions. Handle C99 inline semantics. (start_function): Don't clear current_extern_inline. Don't set DECL_EXTERNAL. (c_push_function_context): Don't push current_extern_inline. (c_pop_function_context): Don't restore current_extern_inline. PR 11377 * c-typeck.c (build_external_ref): Warn about static variables used in extern inline functions. * c-decl.c (start_decl): Warn about static variables declared in extern inline functions. Modified: trunk/gcc/doc/extend.texi
Why was this applied without a fixincludes for glibc? and a patch to glibc? Since GCC is the GNU Compiler Collection and glibc is the GNU libc so you need to also fix glibc at the same time and now you just broke compiling 95% of the programs that compile with -std=c99.
http://gcc.gnu.org/ml/gcc-patches/2006-05/msg00331.html
This should now be behaving correctly.
>2 -- What does constraint #3 of section 6.7.4 mean? It is not fully as 6.7.4/3 is not diagnosed, I filed this as PR 34735. I guess Geoff forgot about this constraint.