All of lore.kernel.org
 help / color / mirror / Atom feed
* Issue with inline functions and switch statements
@ 2020-08-03 14:29 Stafford Horne
  2020-08-04 20:25 ` Luc Van Oostenryck
  0 siblings, 1 reply; 2+ messages in thread
From: Stafford Horne @ 2020-08-03 14:29 UTC (permalink / raw)
  To: linux-sparse

Hello,

I am the maintainer of the OpenRISC architecture linux port, and recently have
started to look at the kbuild sparse errors.

The Linux kernel kbuild process is reporting sparse error:

   net/dccp/proto.c: note: in included file (through include/asm-generic/atomic.h, arch/openrisc/include/asm/atomic.h, include/linux/atomic.h, ...):
   arch/openrisc/include/asm/cmpxchg.h:101:29: sparse: sparse: shift too big (32) for type int

Example: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2256733.html

I have traced this down to the case like the below.

It reports an unexpected warning:

   simple_test.c:7:18: warning: shift too big (32) for type int

This is not pssible because size = 4 should never call shift_small.

I have debugged sparse when this is running and it seems that when checking the switch
cases it evaluates all cases, and thinks size = 4 is possible.  I am not sure if this
is a problem with the sparse front end of the check logic.

Can you help?

-Stafford

--

/* From: https://github.com/stffrdhrn/junk/tree/master/sparse-shift
 * Run with : sparse -O2 --arch=openrisc -m32 simple_test.c
 */
#include <stdio.h>

typedef unsigned long int u32;

static inline u32 shift_small(int size)
{
	return 1 << (size * 8);
}

static inline u32 test(int size) {
	printf ("Test size=%d: ", size);
	switch (size) {
	case 1:
	case 2:
		return shift_small(size);
	case 4:
		printf ("not small\n");
		return 99;
	default:
		return 9999;
	}
}

int main(int argc, char ** argv) {
	test(1);

	test(2);

	test(4); // simple_test.c:7:18: warning: shift too big (32) for type int

	return 0;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Issue with inline functions and switch statements
  2020-08-03 14:29 Issue with inline functions and switch statements Stafford Horne
@ 2020-08-04 20:25 ` Luc Van Oostenryck
  0 siblings, 0 replies; 2+ messages in thread
From: Luc Van Oostenryck @ 2020-08-04 20:25 UTC (permalink / raw)
  To: Stafford Horne; +Cc: linux-sparse

On Mon, Aug 03, 2020 at 11:29:03PM +0900, Stafford Horne wrote:
> Hello,

Hi,
 
> I am the maintainer of the OpenRISC architecture linux port, and recently have
> started to look at the kbuild sparse errors.
> 
> The Linux kernel kbuild process is reporting sparse error:
> 
>    net/dccp/proto.c: note: in included file (through include/asm-generic/atomic.h, arch/openrisc/include/asm/atomic.h, include/linux/atomic.h, ...):
>    arch/openrisc/include/asm/cmpxchg.h:101:29: sparse: sparse: shift too big (32) for type int
> 
> Example: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2256733.html
> 
> I have traced this down to the case like the below.
> 
> It reports an unexpected warning:
> 
>    simple_test.c:7:18: warning: shift too big (32) for type int
> 
> This is not pssible because size = 4 should never call shift_small.
> 
> I have debugged sparse when this is running and it seems that when checking the switch
> cases it evaluates all cases, and thinks size = 4 is possible.  I am not sure if this
> is a problem with the sparse front end of the check logic.
> 
> Can you help?

Sure. The problem is sparse sees the expression '1 << (4 * 8)' 
and warns about it before it can know it will never be evaluated.
The exact same problem happens with an even simpler example:
	int foo(void)
	{
		return 0;
		return 1 << 32;
	}

I have an old series for this problem, I would just need to dust off.
It shouldn't take much time, probably just a few days, maximum 10
(but I vaguely remember there was a nasty complication, which is
why it's still pending).

Best regards,
-- Luc

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-04 20:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-03 14:29 Issue with inline functions and switch statements Stafford Horne
2020-08-04 20:25 ` Luc Van Oostenryck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.