Skip to contentSkip to conversation

Drop -Wdeclaration-after-statement

PATCH In review Alexey Dobriyan 3 participants 4 messages Feb 25, 2022 – Feb 26, 2022

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Drop -Wdeclaration-after-statement

Putting declarations before statement is relict of single pass compiler era. It was necessary to allocate stack slots before generating code.

Recently added static_assert() is a declaration. -Wdeclaration-after-statement prevents its placement anywhere in the code for no reason.

Placing variable declarations in the beginning of the block increases variable "LOC lifetime" so to speak and chances that it will be misused. This is very low probability bug but still. Declaring variables right before first use will make "LOC lifetime" smaller.

	{
		int x;
		[x is misused due to a typo]
		f(x);	// first correct use
	}

vs

	{
		[can't misuse undeclared variable]
		int x;
		f(x);	// first correct use
	}

---

 Makefile                          |    3 ---
 arch/arm64/kernel/vdso32/Makefile |    1 -
 tools/power/acpi/Makefile.config  |    1 -
 tools/power/cpupower/Makefile     |    1 -
 tools/scripts/Makefile.include    |    1 -
 5 files changed, 7 deletions(-)
Makefile +0 -3
@@ -933,9 +933,6 @@ endif
 # arch Makefile may override CC so keep this after arch Makefile is included
 NOSTDINC_FLAGS += -nostdinc
 
-# warn about C99 declaration after statement
-KBUILD_CFLAGS += -Wdeclaration-after-statement
-
 # Variable Length Arrays (VLAs) should not be used anywhere in the kernel
 KBUILD_CFLAGS += -Wvla
 
arch/arm64/kernel/vdso32/Makefile +0 -1
@@ -71,7 +71,6 @@ VDSO_CFLAGS += -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
                -std=gnu89
 VDSO_CFLAGS  += -O2
 # Some useful compiler-dependent flags from top-level Makefile
-VDSO_CFLAGS += $(call cc32-option,-Wdeclaration-after-statement,)
 VDSO_CFLAGS += $(call cc32-option,-Wno-pointer-sign)
 VDSO_CFLAGS += -fno-strict-overflow
 VDSO_CFLAGS += $(call cc32-option,-Werror=strict-prototypes)
tools/power/acpi/Makefile.config +0 -1
@@ -63,7 +63,6 @@ OPTIMIZATION := $(call cc-supports,-Os,-O2)
 
 WARNINGS := -Wall
 WARNINGS += $(call cc-supports,-Wstrict-prototypes)
-WARNINGS += $(call cc-supports,-Wdeclaration-after-statement)
 
 KERNEL_INCLUDE := $(OUTPUT)include
 ACPICA_INCLUDE := $(srctree)/../../../drivers/acpi/acpica
tools/power/cpupower/Makefile +0 -1
@@ -118,7 +118,6 @@ OPTIMIZATION := $(call cc-supports,-Os,-O2)
 
 WARNINGS := -Wall -Wchar-subscripts -Wpointer-arith -Wsign-compare
 WARNINGS += $(call cc-supports,-Wno-pointer-sign)
-WARNINGS += $(call cc-supports,-Wdeclaration-after-statement)
 WARNINGS += -Wshadow
 
 override CFLAGS += -DVERSION=\"$(VERSION)\" -DPACKAGE=\"$(PACKAGE)\" \
tools/scripts/Makefile.include +0 -1
@@ -21,7 +21,6 @@ endif
 # Include saner warnings here, which can catch bugs:
 #
 EXTRA_WARNINGS := -Wbad-function-cast
-EXTRA_WARNINGS += -Wdeclaration-after-statement
 EXTRA_WARNINGS += -Wformat-security
 EXTRA_WARNINGS += -Wformat-y2k
 EXTRA_WARNINGS += -Winit-self
#1 lore raw

From: Alexey Dobriyan

Quoted 7 lines

Sent: 25 February 2022 08:16

Putting declarations before statement is relict of single pass compiler era. It was necessary to allocate stack slots before generating code.

Recently added static_assert() is a declaration. -Wdeclaration-after-statement prevents its placement anywhere in the code for no reason.

That could enclose its declaration inside a block. But then it wouldn't be usable at global scope (is it anyway?)

Quoted 4 lines

Placing variable declarations in the beginning of the block increases variable "LOC lifetime" so to speak and chances that it will be misused. This is very low probability bug but still. Declaring variables right before first use will make "LOC lifetime" smaller.

NAK it makes it very hard for a human (some of us are) to find the declaration.

Indeed putting them anywhere other that the top of a function or the top of a very short code block makes them hard to find.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#2 lore raw
Quoted 10 lines · David Laight

From: Alexey Dobriyan

Quoted 7 lines

Sent: 25 February 2022 08:16

Putting declarations before statement is relict of single pass compiler era. It was necessary to allocate stack slots before generating code.

Recently added static_assert() is a declaration. -Wdeclaration-after-statement prevents its placement anywhere in the code for no reason.

That could enclose its declaration inside a block.

It could but why put useless characters on the screen?

Quoted 1 line

But then it wouldn't be usable at global scope (is it anyway?)

It is usable in global scope.

Quoted 7 lines
Quoted 4 lines

Placing variable declarations in the beginning of the block increases variable "LOC lifetime" so to speak and chances that it will be misused. This is very low probability bug but still. Declaring variables right before first use will make "LOC lifetime" smaller.

NAK it makes it very hard for a human (some of us are) to find the declaration.

What?

Which editor are you using?

Shift+3 with selection highlighting in vim works just fine. I don't believe Emacs doesn't have an equivalent.

Quoted 2 lines

Indeed putting them anywhere other that the top of a function or the top of a very short code block makes them hard to find.

#3 lore raw
In reply to Alexey Dobriyan
Quoted 17 lines · On Fri, 25 Feb 2022 11:15:58 +0300 Alexey Dobriyan <[email protected]> wrote:

Putting declarations before statement is relict of single pass compiler era. It was necessary to allocate stack slots before generating code.

Recently added static_assert() is a declaration. -Wdeclaration-after-statement prevents its placement anywhere in the code for no reason.

Placing variable declarations in the beginning of the block increases variable "LOC lifetime" so to speak and chances that it will be misused. This is very low probability bug but still. Declaring variables right before first use will make "LOC lifetime" smaller.

	{
		int x;
		[x is misused due to a typo]
		f(x);	// first correct use
	}
Quoted 8 lines

vs

	{
		[can't misuse undeclared variable]
		int x;
		f(x);	// first correct use
	}

Oh man. I so wish you'd cc'ed Linus on this one so we could admire his reaction.