tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: style(5) proposal: forbid extern in .c



On Wed, Mar 15, 2023 at 11:23:05 +0000, Taylor R Campbell wrote:

> Proposal: Forbid extern declarations in .c files.
> 
> extern declarations in .c files invite easily avoided bugs where the
> definition and use have mismatched types, because the compiler doesn't
> have an opportunity to check them.  Fix: Always put the extern
> declaration in a .h file shared by the .c file defining it and the .c
> files using it.
> 
> Pretty simple.  Any objections?
[...]
>  share/misc/style | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/share/misc/style b/share/misc/style
> index 060f01f9eaf7..765d1eced109 100644
> --- a/share/misc/style
> +++ b/share/misc/style
> @@ -345,9 +345,11 @@ function(int a1, int a2, float fl, int a4)
>  	 * declarations next to their first use, and initialize
>  	 * opportunistically. This avoids over-initialization and
>  	 * accidental bugs caused by declaration reordering.
> +	 *
> +	 * Never declare extern variables in .c files.  Declare them in the
> +	 * appropriate .h file shared by the .c file where they are defined
> +	 * and the .c file where they are used.
>  	 */
> -	extern u_char one;
> -	extern char two;
>  	struct foo three, *four;
>  	double five;
>  	int *six, seven;

You talk about just "extern" (and most likely mean that), but the
example/comment in the patch is about nested externs.

Can we compile the tree with -Wnested-externs (currently disabled)?
If that one is enabled the nested externs (as in the example above)
will be caught by the compiler and only file scope externs need to be
discussed in style.

-uwe


Home | Main Index | Thread Index | Old Index