Skip to content

Commit

Permalink
scripts/coccinelle: Add Coccinelle script for unsigned values
Browse files Browse the repository at this point in the history
Add a Coccinelle script that adds an 'U' to values assigned to
unsigned variables, according ot MISRA-C rule 7.2.

Add a 'report' mode to the script that can be used by developer/CI
and a 'patch' mode that should do the heavy lifting.

Signed-off-by: Patrik Flykt <[email protected]>
  • Loading branch information
pfl authored and nashif committed Dec 5, 2018
1 parent c6727d4 commit 1b48cb6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions scripts/coccinelle/unsigned_suffix.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// Find assignments to unsigned variables and add an 'U' to the value
// Copyright: (C) 2018 Intel Corporation
// Copyright: (C) 2018 Himanshu Jha
// Copyright: (C) 2018 Julia Lawall, Inria/LIP6
// SPDX-License-Identifier: Apache-2.0
// Confidence: High

virtual patch
virtual report

@r_unsigned@
typedef uint8_t, uint16_t, uint32_t, uint64_t, u8_t, u16_t, u32_t, u64_t;
{unsigned char, unsigned short, unsigned int, uint8_t, uint16_t, uint32_t, uint64_t, u8_t, u16_t, u32_t, u64_t} v;
constant C;
position p;
@@

v = C@p

@script:python r_rewrite@
C << r_unsigned.C;
z;
@@
if C.isdigit() != True:
cocci.include_match(False)
coccinelle.z = C + "U"
@r_subst depends on patch@
{unsigned char, unsigned short, unsigned int, uint8_t, uint16_t, uint32_t, uint64_t, u8_t, u16_t, u32_t, u64_t} r_unsigned.v;
constant r_unsigned.C;
identifier r_rewrite.z;
@@

v =
- C
+ z

@script: python depends on report@
p << r_unsigned.p;
@@

msg="WARNING: Unsigned 'U' suffix missing"
coccilib.report.print_report(p[0], msg)

0 comments on commit 1b48cb6

Please sign in to comment.