Skip to contentSkip to conversation

devtools: forbid variable declaration inside for

PATCH Superseded Thomas Monjalon 4 participants 9 messages Feb 17, 2020 – Jul 3, 2020

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

PATCH 1/1 devtools: forbid variable declaration inside for

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

This coding style is enforced by adding a check of the common patterns like "for (int i;"

The occurrences of the checked pattern are fixed:

	'for *(\(char\|u\?int\|unsigned\)'

In the file dpaa2_sparser.c, the fix is to remove the unused macros.

---
 app/test-fib/main.c                    |  3 ++-
 devtools/checkpatches.sh               |  8 +++++++
 doc/guides/prog_guide/eventdev.rst     |  6 ++++--
 drivers/common/mlx5/mlx5_devx_cmds.c   |  4 ++--
 drivers/crypto/caam_jr/caam_jr.c       |  5 ++++-
 drivers/net/dpaa2/dpaa2_sparser.c      | 30 --------------------------
 lib/librte_eal/windows/eal/eal_lcore.c |  9 ++++----
 7 files changed, 25 insertions(+), 40 deletions(-)
app/test-fib/main.c +2 -1
index 3c29ca461e..16d40a6a02 100644
@@ -363,6 +363,7 @@ static void
 gen_random_rt_6(struct rt_rule_6 *rt, int nh_sz)
 {
 	uint32_t i, j, k = 0;
+	int a;
 
 	if (config.nb_routes_per_depth[0] != 0) {
 		memset(rt[k].addr, 0, 16);
@@ -370,7 +371,7 @@ gen_random_rt_6(struct rt_rule_6 *rt, int nh_sz)
 		rt[k++].nh = rte_rand() & get_max_nh(nh_sz);
 	}
 
-	for (int a = 0; a < 4; a++) {
+	for (a = 0; a < 4; a++) {
 		for (i = 1; i <= 32; i++) {
 			uint32_t rnd;
 			double edge = 0;
devtools/checkpatches.sh +8 -0
index b16bace927..55e7ba05a0 100755
@@ -61,6 +61,14 @@ check_forbidden_additions() { # <patch>
 		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
 		"$1" || res=1
 
+	# forbid variable declaration inside "for" loop
+	awk -v FOLDERS='.' \
+		-v EXPRESSIONS='for *\\((char|u?int|unsigned)' \
+		-v RET_ON_FAIL=1 \
+		-v MESSAGE='Declaring a variable inside for()' \
+		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
+		"$1" || res=1
+
 	# svg figures must be included with wildcard extension
 	# because of png conversion for pdf docs
 	awk -v FOLDERS='doc' \
doc/guides/prog_guide/eventdev.rst +4 -2
index 7bcd7603b1..ccde086f63 100644
@@ -242,9 +242,10 @@ Once queues are set up successfully, create the ports as required.
         };
         int dev_id = 0;
         int rx_port_id = 0;
+        int worker_port_id;
         int err = rte_event_port_setup(dev_id, rx_port_id, &rx_conf);
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
 	        int err = rte_event_port_setup(dev_id, worker_port_id, &worker_conf);
         }
 
@@ -277,8 +278,9 @@ can be achieved like this:
         uint8_t atomic_qs[] = {0, 1};
         uint8_t single_link_q = 2;
         uint8_t priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
+        int worker_port_id;
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
                 int links_made = rte_event_port_link(dev_id, worker_port_id, atomic_qs, NULL, 2);
         }
         int links_made = rte_event_port_link(dev_id, tx_port_id, &single_link_q, &priority, 1);
drivers/common/mlx5/mlx5_devx_cmds.c +2 -2
index d960bc93de..ae9dba2e0c 100644
@@ -412,7 +412,7 @@ mlx5_devx_cmd_query_hca_attr(struct ibv_context *ctx,
 	uint32_t in[MLX5_ST_SZ_DW(query_hca_cap_in)] = {0};
 	uint32_t out[MLX5_ST_SZ_DW(query_hca_cap_out)] = {0};
 	void *hcattr;
-	int status, syndrome, rc;
+	int status, syndrome, rc, i;
 
 	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
 	MLX5_SET(query_hca_cap_in, in, op_mod,
@@ -521,7 +521,7 @@ mlx5_devx_cmd_query_hca_attr(struct ibv_context *ctx,
 	attr->lro_max_msg_sz_mode = MLX5_GET
 					(per_protocol_networking_offload_caps,
 					 hcattr, lro_max_msg_sz_mode);
-	for (int i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
+	for (i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
 		attr->lro_timer_supported_periods[i] =
 			MLX5_GET(per_protocol_networking_offload_caps, hcattr,
 				 lro_timer_supported_periods[i]);
drivers/crypto/caam_jr/caam_jr.c +4 -1
index 8aaa3d45f6..9aa64ab23f 100644
@@ -1353,6 +1353,9 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
 	struct caam_jr_session *ses;
 	struct caam_jr_op_ctx *ctx = NULL;
 	struct sec_job_descriptor_t *jobdescr __rte_unused;
+#if CAAM_JR_DBG
+	int i;
+#endif
 
 	switch (op->sess_type) {
 	case RTE_CRYPTO_OP_WITH_SESSION:
@@ -1415,7 +1418,7 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
 			rte_pktmbuf_data_len(op->sym->m_src));
 
 	printf("\n JD before conversion\n");
-	for (int i = 0; i < 12; i++)
+	for (i = 0; i < 12; i++)
 		printf("\n 0x%08x", ctx->jobdes.desc[i]);
 #endif
 
drivers/net/dpaa2/dpaa2_sparser.c +0 -30
index 7e8fedd818..ba0d500f74 100644
@@ -145,36 +145,6 @@ struct frame_attr_ext frame_attr_ext_arr[] = {
 	/* 112 */ {NULL,                                       0, 0x0000}
 };
 
-#define SWAP_WORD(pr)						\
-do {								\
-	for (int i = 0; i < 4 ; i++) {				\
-		pr[i] = pr[i] ^ pr[6 - i + 1];			\
-		pr[6 - i + 1] = pr[6 - i + 1] ^ pr[i];		\
-		pr[i] = pr[i] ^ pr[6 - i + 1];			\
-	}							\
-} while (0)
-
-#define fa_print_sb()						\
-do {								\
-	if (rte_cpu_to_be_32(*pdw) & frm_attr->fld_mask)	\
-		DPAA2_PMD_DP_DEBUG("t %s : Yes", frm_attr->fld_name);	\
-} while (0)
-
-#define fa_print_sb_ext()					\
-do {								\
-	if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)	\
-		DPAA2_PMD_DP_DEBUG("\t %s : Yes",			\
-			  frm_attr_ext->fld_name);		\
-} while (0)
-
-#define fa_print_mb_ext()					\
-do {								\
-	if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)	\
-		DPAA2_PMD_DP_DEBUG("\t %s : 0x%02x",			\
-			  frm_attr_ext->fld_name,		\
-			  rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask);\
-} while (0)
-
 int dpaa2_eth_load_wriop_soft_parser(struct dpaa2_dev_priv *priv,
 				     enum dpni_soft_sequence_dest dest)
 {
lib/librte_eal/windows/eal/eal_lcore.c +5 -4
index b3a6c63afa..8ee1d6fb78 100644
@@ -27,6 +27,8 @@ static struct _wcpu_map {
 void
 eal_create_cpu_map()
 {
+	unsigned int socket, core, lcore;
+
 	wcpu_map.total_procs =
 		GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
 
@@ -53,10 +55,9 @@ eal_create_cpu_map()
 	 * across the logical cores. For now, split the cores
 	 * equally across the sockets.
 	 */
-	unsigned int lcore = 0;
-	for (unsigned int socket = 0; socket <
-			wcpu_map.proc_sockets; ++socket) {
-		for (unsigned int core = 0;
+	lcore = 0;
+	for (socket = 0; socket < wcpu_map.proc_sockets; ++socket) {
+		for (core = 0;
 			core < (wcpu_map.proc_cores / wcpu_map.proc_sockets);
 			++core) {
 			wcpu_map.wlcore_map[lcore]
Signature · 1 line
2.25.0
#1 lore raw
Quoted 8 lines · Thomas Monjalon

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

Quoted 5 lines

This coding style is enforced by adding a check of the common patterns like "for (int i;"

The occurrences of the checked pattern are fixed:

        'for *(\(char\|u\?int\|unsigned\)'

You missed size_t:
drivers/common/mlx5/mlx5_glue.c: for (size_t i = 0; i < num_actions; i++)

Quoted 39 lines

In the file dpaa2_sparser.c, the fix is to remove the unused macros.

---
 app/test-fib/main.c                    |  3 ++-
 devtools/checkpatches.sh               |  8 +++++++
 doc/guides/prog_guide/eventdev.rst     |  6 ++++--
 drivers/common/mlx5/mlx5_devx_cmds.c   |  4 ++--
 drivers/crypto/caam_jr/caam_jr.c       |  5 ++++-
 drivers/net/dpaa2/dpaa2_sparser.c      | 30 --------------------------
 lib/librte_eal/windows/eal/eal_lcore.c |  9 ++++----
 7 files changed, 25 insertions(+), 40 deletions(-)
app/test-fib/main.c +2 -1
index 3c29ca461e..16d40a6a02 100644
@@ -363,6 +363,7 @@ static void
 gen_random_rt_6(struct rt_rule_6 *rt, int nh_sz)
 {
        uint32_t i, j, k = 0;
+       int a;
 
        if (config.nb_routes_per_depth[0] != 0) {
                memset(rt[k].addr, 0, 16);
@@ -370,7 +371,7 @@ gen_random_rt_6(struct rt_rule_6 *rt, int nh_sz)
                rt[k++].nh = rte_rand() & get_max_nh(nh_sz);
        }
 
-       for (int a = 0; a < 4; a++) {
+       for (a = 0; a < 4; a++) {
                for (i = 1; i <= 32; i++) {
                        uint32_t rnd;
                        double edge = 0;
devtools/checkpatches.sh +0 -0
index b16bace927..55e7ba05a0 100755

devtools/checkpatches.sh check_forbidden_additions() { # <patch> · line 66

Quoted 6 lines
devtools/checkpatches.sh +3 -0
@@ -61,6 +61,14 @@ check_forbidden_additions() { # <patch>
                -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
                "$1" || res=1
 
+       # forbid variable declaration inside "for" loop
+       awk -v FOLDERS='.' \
+               -v EXPRESSIONS='for *\\((char|u?int|unsigned)' \

+ s?size_t

devtools/checkpatches.sh check_forbidden_additions() { # <patch> · line 71

Quoted 5 lines
devtools/checkpatches.sh +5 -0
+               -v RET_ON_FAIL=1 \
+               -v MESSAGE='Declaring a variable inside for()' \
+               -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
+               "$1" || res=1
+

Just a note, base drivers imported "as is" will probably trigger warnings.

Quoted 144 lines
devtools/checkpatches.sh +0 -0
        # svg figures must be included with wildcard extension
        # because of png conversion for pdf docs
        awk -v FOLDERS='doc' \
doc/guides/prog_guide/eventdev.rst +4 -2
index 7bcd7603b1..ccde086f63 100644
@@ -242,9 +242,10 @@ Once queues are set up successfully, create the ports as required.
         };
         int dev_id = 0;
         int rx_port_id = 0;
+        int worker_port_id;
         int err = rte_event_port_setup(dev_id, rx_port_id, &rx_conf);
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
                int err = rte_event_port_setup(dev_id, worker_port_id, &worker_conf);
         }
 
@@ -277,8 +278,9 @@ can be achieved like this:
         uint8_t atomic_qs[] = {0, 1};
         uint8_t single_link_q = 2;
         uint8_t priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
+        int worker_port_id;
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
                 int links_made = rte_event_port_link(dev_id, worker_port_id, atomic_qs, NULL, 2);
         }
         int links_made = rte_event_port_link(dev_id, tx_port_id, &single_link_q, &priority, 1);
drivers/common/mlx5/mlx5_devx_cmds.c +2 -2
index d960bc93de..ae9dba2e0c 100644
@@ -412,7 +412,7 @@ mlx5_devx_cmd_query_hca_attr(struct ibv_context *ctx,
        uint32_t in[MLX5_ST_SZ_DW(query_hca_cap_in)] = {0};
        uint32_t out[MLX5_ST_SZ_DW(query_hca_cap_out)] = {0};
        void *hcattr;
-       int status, syndrome, rc;
+       int status, syndrome, rc, i;
 
        MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
        MLX5_SET(query_hca_cap_in, in, op_mod,
@@ -521,7 +521,7 @@ mlx5_devx_cmd_query_hca_attr(struct ibv_context *ctx,
        attr->lro_max_msg_sz_mode = MLX5_GET
                                        (per_protocol_networking_offload_caps,
                                         hcattr, lro_max_msg_sz_mode);
-       for (int i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
+       for (i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
                attr->lro_timer_supported_periods[i] =
                        MLX5_GET(per_protocol_networking_offload_caps, hcattr,
                                 lro_timer_supported_periods[i]);
drivers/crypto/caam_jr/caam_jr.c +4 -1
index 8aaa3d45f6..9aa64ab23f 100644
@@ -1353,6 +1353,9 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
        struct caam_jr_session *ses;
        struct caam_jr_op_ctx *ctx = NULL;
        struct sec_job_descriptor_t *jobdescr __rte_unused;
+#if CAAM_JR_DBG
+       int i;
+#endif
 
        switch (op->sess_type) {
        case RTE_CRYPTO_OP_WITH_SESSION:
@@ -1415,7 +1418,7 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
                        rte_pktmbuf_data_len(op->sym->m_src));
 
        printf("\n JD before conversion\n");
-       for (int i = 0; i < 12; i++)
+       for (i = 0; i < 12; i++)
                printf("\n 0x%08x", ctx->jobdes.desc[i]);
 #endif
 
drivers/net/dpaa2/dpaa2_sparser.c +0 -30
index 7e8fedd818..ba0d500f74 100644
@@ -145,36 +145,6 @@ struct frame_attr_ext frame_attr_ext_arr[] = {
        /* 112 */ {NULL,                                       0, 0x0000}
 };
 
-#define SWAP_WORD(pr)                                          \
-do {                                                           \
-       for (int i = 0; i < 4 ; i++) {                          \
-               pr[i] = pr[i] ^ pr[6 - i + 1];                  \
-               pr[6 - i + 1] = pr[6 - i + 1] ^ pr[i];          \
-               pr[i] = pr[i] ^ pr[6 - i + 1];                  \
-       }                                                       \
-} while (0)
-
-#define fa_print_sb()                                          \
-do {                                                           \
-       if (rte_cpu_to_be_32(*pdw) & frm_attr->fld_mask)        \
-               DPAA2_PMD_DP_DEBUG("t %s : Yes", frm_attr->fld_name);   \
-} while (0)
-
-#define fa_print_sb_ext()                                      \
-do {                                                           \
-       if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)     \
-               DPAA2_PMD_DP_DEBUG("\t %s : Yes",                       \
-                         frm_attr_ext->fld_name);              \
-} while (0)
-
-#define fa_print_mb_ext()                                      \
-do {                                                           \
-       if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)     \
-               DPAA2_PMD_DP_DEBUG("\t %s : 0x%02x",                    \
-                         frm_attr_ext->fld_name,               \
-                         rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask);\
-} while (0)
-
 int dpaa2_eth_load_wriop_soft_parser(struct dpaa2_dev_priv *priv,
                                     enum dpni_soft_sequence_dest dest)
 {
lib/librte_eal/windows/eal/eal_lcore.c +5 -5
index b3a6c63afa..8ee1d6fb78 100644
@@ -27,6 +27,8 @@ static struct _wcpu_map {
 void
 eal_create_cpu_map()
 {
+       unsigned int socket, core, lcore;
+
        wcpu_map.total_procs =
                GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
 
@@ -53,10 +55,9 @@ eal_create_cpu_map()
         * across the logical cores. For now, split the cores
         * equally across the sockets.
         */
-       unsigned int lcore = 0;
-       for (unsigned int socket = 0; socket <
-                       wcpu_map.proc_sockets; ++socket) {
-               for (unsigned int core = 0;
+       lcore = 0;
+       for (socket = 0; socket < wcpu_map.proc_sockets; ++socket) {
+               for (core = 0;
                        core < (wcpu_map.proc_cores / wcpu_map.proc_sockets);
                        ++core) {
                        wcpu_map.wlcore_map[lcore]
--
Quoted 2 lines

2.25.0

LGTM once size_t is fixed.

Signature · 1 line
David Marchand
#2 lore raw

23/03/2020 14:40, David Marchand:

Quoted 9 lines · Thomas Monjalon
Quoted 8 lines

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

Quoted 8 lines
Quoted 5 lines

This coding style is enforced by adding a check of the common patterns like "for (int i;"

The occurrences of the checked pattern are fixed:

        'for *(\(char\|u\?int\|unsigned\)'

You missed size_t:
drivers/common/mlx5/mlx5_glue.c: for (size_t i = 0; i < num_actions; i++)

Good catch, thanks.

[..]

Quoted 7 lines

devtools/checkpatches.sh check_forbidden_additions() { # <patch> · line 66

Quoted 5 lines
devtools/checkpatches.sh +3 -0
+       # forbid variable declaration inside "for" loop
+       awk -v FOLDERS='.' \
+               -v EXPRESSIONS='for *\\((char|u?int|unsigned)' \

+ s?size_t

OK

Quoted 7 lines

devtools/checkpatches.sh check_forbidden_additions() { # <patch> · line 71

Quoted 5 lines
devtools/checkpatches.sh +5 -0
+               -v RET_ON_FAIL=1 \
+               -v MESSAGE='Declaring a variable inside for()' \
+               -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
+               "$1" || res=1
+

Just a note, base drivers imported "as is" will probably trigger warnings.

Yes, base drivers don't comply with coding rules and trigger lots of warnings.

#3 lore raw
In reply to Thomas Monjalon
Quoted 10 lines · Thomas Monjalon

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

This coding style is enforced by adding a check of the common patterns like "for (int i;"

Could we, or should we, not also revisit the coding standards for this. Those standards are quite old now, and could do with being updated to take account of things like the newer C standards. Obviously for consistency reasons any changes should not be major, but I don't think we should view the guidelines as set in stone either.

For this particular issue, I think generally having all declarations at the beginning of a block is the right thing to do, however, I would support relaxing this rule in a couple of scenarios, specifically:

* declaring a variable like i in a for loop, i.e. the case above
* using const on a variable declaration, marking it as const at first assignment [because I take the view that increased use of const is almost always a good thing]

Regards,
/Bruce

#4 lore raw

23/03/2020 14:59, Bruce Richardson:

Quoted 18 lines · Thomas Monjalon
Quoted 10 lines

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

This coding style is enforced by adding a check of the common patterns like "for (int i;"

Could we, or should we, not also revisit the coding standards for this. Those standards are quite old now, and could do with being updated to take account of things like the newer C standards. Obviously for consistency reasons any changes should not be major, but I don't think we should view the guidelines as set in stone either.

Quoted 8 lines

For this particular issue, I think generally having all declarations at the beginning of a block is the right thing to do, however, I would support relaxing this rule in a couple of scenarios, specifically:

* declaring a variable like i in a for loop, i.e. the case above
* using const on a variable declaration, marking it as const at first assignment [because I take the view that increased use of const is almost always a good thing]

I would tend to agree in general for updating guidelines, and especially in this case.
But because we are not enforcing C99 (or C11) when compiling, some users are still trying to compile with old compilers. As a result, a declaration in a for loop will break compilation with some compilers:

	http://git.dpdk.org/dpdk/commit/?id=bc3282125b

I am OK either way:
- switch globally to C99/C11
- or forbid declaration in for loop
I took the second option because it is easier,
and because it maximizes our compiler tolerance.

#5 lore raw

PATCH v2 1/1 devtools: forbid variable declaration inside for

In reply to Thomas Monjalon

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

This coding style is enforced by adding a check of the common patterns like "for (int i;"

The occurrences of the checked pattern are fixed:

	'for *(\(char\|u\?int\|unsigned\|s\?size_t\)'

In the file dpaa2_sparser.c, the fix is to remove the unused macros.

---
v2:
- check s?size_t and fix one occurence
- fix new occurences in Windows EAL
- test-fib has been fixed in the meantime

---
 devtools/checkpatches.sh             |  8 ++++++++
 doc/guides/prog_guide/eventdev.rst   |  6 ++++--
 drivers/common/mlx5/mlx5_devx_cmds.c |  4 ++--
 drivers/common/mlx5/mlx5_glue.c      |  3 ++-
 drivers/crypto/caam_jr/caam_jr.c     |  5 ++++-
 drivers/net/dpaa2/dpaa2_sparser.c    | 30 ----------------------------
 lib/librte_eal/windows/eal_lcore.c   |  6 ++++--
 7 files changed, 24 insertions(+), 38 deletions(-)
devtools/checkpatches.sh +8 -0
index 42b833e0d7..5763a7e953 100755
@@ -69,6 +69,14 @@ check_forbidden_additions() { # <patch>
 		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
 		"$1" || res=1
 
+	# forbid variable declaration inside "for" loop
+	awk -v FOLDERS='.' \
+		-v EXPRESSIONS='for *\\((char|u?int|unsigned|s?size_t)' \
+		-v RET_ON_FAIL=1 \
+		-v MESSAGE='Declaring a variable inside for()' \
+		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
+		"$1" || res=1
+
 	# svg figures must be included with wildcard extension
 	# because of png conversion for pdf docs
 	awk -v FOLDERS='doc' \
doc/guides/prog_guide/eventdev.rst +4 -2
index 7bcd7603b1..ccde086f63 100644
@@ -242,9 +242,10 @@ Once queues are set up successfully, create the ports as required.
         };
         int dev_id = 0;
         int rx_port_id = 0;
+        int worker_port_id;
         int err = rte_event_port_setup(dev_id, rx_port_id, &rx_conf);
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
 	        int err = rte_event_port_setup(dev_id, worker_port_id, &worker_conf);
         }
 
@@ -277,8 +278,9 @@ can be achieved like this:
         uint8_t atomic_qs[] = {0, 1};
         uint8_t single_link_q = 2;
         uint8_t priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
+        int worker_port_id;
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
                 int links_made = rte_event_port_link(dev_id, worker_port_id, atomic_qs, NULL, 2);
         }
         int links_made = rte_event_port_link(dev_id, tx_port_id, &single_link_q, &priority, 1);
drivers/common/mlx5/mlx5_devx_cmds.c +2 -2
index fba485e724..1c8b36c91a 100644
@@ -416,7 +416,7 @@ mlx5_devx_cmd_query_hca_attr(void *ctx,
 	uint32_t in[MLX5_ST_SZ_DW(query_hca_cap_in)] = {0};
 	uint32_t out[MLX5_ST_SZ_DW(query_hca_cap_out)] = {0};
 	void *hcattr;
-	int status, syndrome, rc;
+	int status, syndrome, rc, i;
 
 	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
 	MLX5_SET(query_hca_cap_in, in, op_mod,
@@ -529,7 +529,7 @@ mlx5_devx_cmd_query_hca_attr(void *ctx,
 	attr->lro_max_msg_sz_mode = MLX5_GET
 					(per_protocol_networking_offload_caps,
 					 hcattr, lro_max_msg_sz_mode);
-	for (int i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
+	for (i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
 		attr->lro_timer_supported_periods[i] =
 			MLX5_GET(per_protocol_networking_offload_caps, hcattr,
 				 lro_timer_supported_periods[i]);
drivers/common/mlx5/mlx5_glue.c +2 -1
index f270f677b7..563941c2df 100644
@@ -587,11 +587,12 @@ mlx5_glue_dv_create_flow(void *matcher,
 	return mlx5dv_dr_rule_create(matcher, match_value, num_actions,
 				     (struct mlx5dv_dr_action **)actions);
 #else
+	size_t i;
 	struct mlx5dv_flow_action_attr actions_attr[8];
 
 	if (num_actions > 8)
 		return NULL;
-	for (size_t i = 0; i < num_actions; i++)
+	for (i = 0; i < num_actions; i++)
 		actions_attr[i] =
 			*((struct mlx5dv_flow_action_attr *)(actions[i]));
 	return mlx5dv_create_flow(matcher, match_value,
drivers/crypto/caam_jr/caam_jr.c +4 -1
index caf2386772..77fa6ff2d3 100644
@@ -1351,6 +1351,9 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
 	struct caam_jr_session *ses;
 	struct caam_jr_op_ctx *ctx = NULL;
 	struct sec_job_descriptor_t *jobdescr __rte_unused;
+#if CAAM_JR_DBG
+	int i;
+#endif
 
 	switch (op->sess_type) {
 	case RTE_CRYPTO_OP_WITH_SESSION:
@@ -1413,7 +1416,7 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
 			rte_pktmbuf_data_len(op->sym->m_src));
 
 	printf("\n JD before conversion\n");
-	for (int i = 0; i < 12; i++)
+	for (i = 0; i < 12; i++)
 		printf("\n 0x%08x", ctx->jobdes.desc[i]);
 #endif
 
drivers/net/dpaa2/dpaa2_sparser.c +0 -30
index 7e8fedd818..ba0d500f74 100644
@@ -145,36 +145,6 @@ struct frame_attr_ext frame_attr_ext_arr[] = {
 	/* 112 */ {NULL,                                       0, 0x0000}
 };
 
-#define SWAP_WORD(pr)						\
-do {								\
-	for (int i = 0; i < 4 ; i++) {				\
-		pr[i] = pr[i] ^ pr[6 - i + 1];			\
-		pr[6 - i + 1] = pr[6 - i + 1] ^ pr[i];		\
-		pr[i] = pr[i] ^ pr[6 - i + 1];			\
-	}							\
-} while (0)
-
-#define fa_print_sb()						\
-do {								\
-	if (rte_cpu_to_be_32(*pdw) & frm_attr->fld_mask)	\
-		DPAA2_PMD_DP_DEBUG("t %s : Yes", frm_attr->fld_name);	\
-} while (0)
-
-#define fa_print_sb_ext()					\
-do {								\
-	if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)	\
-		DPAA2_PMD_DP_DEBUG("\t %s : Yes",			\
-			  frm_attr_ext->fld_name);		\
-} while (0)
-
-#define fa_print_mb_ext()					\
-do {								\
-	if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)	\
-		DPAA2_PMD_DP_DEBUG("\t %s : 0x%02x",			\
-			  frm_attr_ext->fld_name,		\
-			  rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask);\
-} while (0)
-
 int dpaa2_eth_load_wriop_soft_parser(struct dpaa2_dev_priv *priv,
 				     enum dpni_soft_sequence_dest dest)
 {
lib/librte_eal/windows/eal_lcore.c +4 -2
index 82ee454134..760b3fcfed 100644
@@ -29,6 +29,8 @@ static struct _wcpu_map {
 void
 eal_create_cpu_map()
 {
+	unsigned int socket, core;
+
 	wcpu_map.total_procs =
 		GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
 
@@ -56,9 +58,9 @@ eal_create_cpu_map()
 	 * equally across the sockets.
 	 */
 	unsigned int lcore = 0;
-	for (unsigned int socket = 0; socket <
+	for (socket = 0; socket <
 			wcpu_map.proc_sockets; ++socket) {
-		for (unsigned int core = 0;
+		for (core = 0;
 			core < (wcpu_map.proc_cores / wcpu_map.proc_sockets);
 			++core) {
 			wcpu_map.wlcore_map[lcore]
Signature · 1 line
2.26.2
#6 lore raw
Quoted 7 lines · On Sun, 24 May 2020 19:30:07 +0200 Thomas Monjalon <[email protected]> wrote:

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

Quoted 8 lines

This coding style is enforced by adding a check of the common patterns like "for (int i;"

The occurrences of the checked pattern are fixed:

	'for *(\(char\|u\?int\|unsigned\|s\?size_t\)'

In the file dpaa2_sparser.c, the fix is to remove the unused macros.

Surprised that checkpatch doesn't enforce this already. Or maybe kernel flags are different.

Acked-by on v2 1/1
#7 lore raw
In reply to Thomas Monjalon
Quoted 207 lines · Thomas Monjalon

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

This coding style is enforced by adding a check of the common patterns like "for (int i;"

The occurrences of the checked pattern are fixed:

        'for *(\(char\|u\?int\|unsigned\|s\?size_t\)'

In the file dpaa2_sparser.c, the fix is to remove the unused macros.

---
v2:
- check s?size_t and fix one occurence
- fix new occurences in Windows EAL
- test-fib has been fixed in the meantime

---
 devtools/checkpatches.sh             |  8 ++++++++
 doc/guides/prog_guide/eventdev.rst   |  6 ++++--
 drivers/common/mlx5/mlx5_devx_cmds.c |  4 ++--
 drivers/common/mlx5/mlx5_glue.c      |  3 ++-
 drivers/crypto/caam_jr/caam_jr.c     |  5 ++++-
 drivers/net/dpaa2/dpaa2_sparser.c    | 30 ----------------------------
 lib/librte_eal/windows/eal_lcore.c   |  6 ++++--
 7 files changed, 24 insertions(+), 38 deletions(-)
devtools/checkpatches.sh +8 -0
index 42b833e0d7..5763a7e953 100755
@@ -69,6 +69,14 @@ check_forbidden_additions() { # <patch>
                -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
                "$1" || res=1
 
+       # forbid variable declaration inside "for" loop
+       awk -v FOLDERS='.' \
+               -v EXPRESSIONS='for *\\((char|u?int|unsigned|s?size_t)' \
+               -v RET_ON_FAIL=1 \
+               -v MESSAGE='Declaring a variable inside for()' \
+               -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
+               "$1" || res=1
+
        # svg figures must be included with wildcard extension
        # because of png conversion for pdf docs
        awk -v FOLDERS='doc' \
doc/guides/prog_guide/eventdev.rst +4 -2
index 7bcd7603b1..ccde086f63 100644
@@ -242,9 +242,10 @@ Once queues are set up successfully, create the ports as required.
         };
         int dev_id = 0;
         int rx_port_id = 0;
+        int worker_port_id;
         int err = rte_event_port_setup(dev_id, rx_port_id, &rx_conf);
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
                int err = rte_event_port_setup(dev_id, worker_port_id, &worker_conf);
         }
 
@@ -277,8 +278,9 @@ can be achieved like this:
         uint8_t atomic_qs[] = {0, 1};
         uint8_t single_link_q = 2;
         uint8_t priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
+        int worker_port_id;
 
-        for(int worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
+        for (worker_port_id = 1; worker_port_id <= 4; worker_port_id++) {
                 int links_made = rte_event_port_link(dev_id, worker_port_id, atomic_qs, NULL, 2);
         }
         int links_made = rte_event_port_link(dev_id, tx_port_id, &single_link_q, &priority, 1);
drivers/common/mlx5/mlx5_devx_cmds.c +2 -2
index fba485e724..1c8b36c91a 100644
@@ -416,7 +416,7 @@ mlx5_devx_cmd_query_hca_attr(void *ctx,
        uint32_t in[MLX5_ST_SZ_DW(query_hca_cap_in)] = {0};
        uint32_t out[MLX5_ST_SZ_DW(query_hca_cap_out)] = {0};
        void *hcattr;
-       int status, syndrome, rc;
+       int status, syndrome, rc, i;
 
        MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
        MLX5_SET(query_hca_cap_in, in, op_mod,
@@ -529,7 +529,7 @@ mlx5_devx_cmd_query_hca_attr(void *ctx,
        attr->lro_max_msg_sz_mode = MLX5_GET
                                        (per_protocol_networking_offload_caps,
                                         hcattr, lro_max_msg_sz_mode);
-       for (int i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
+       for (i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
                attr->lro_timer_supported_periods[i] =
                        MLX5_GET(per_protocol_networking_offload_caps, hcattr,
                                 lro_timer_supported_periods[i]);
drivers/common/mlx5/mlx5_glue.c +2 -1
index f270f677b7..563941c2df 100644
@@ -587,11 +587,12 @@ mlx5_glue_dv_create_flow(void *matcher,
        return mlx5dv_dr_rule_create(matcher, match_value, num_actions,
                                     (struct mlx5dv_dr_action **)actions);
 #else
+       size_t i;
        struct mlx5dv_flow_action_attr actions_attr[8];
 
        if (num_actions > 8)
                return NULL;
-       for (size_t i = 0; i < num_actions; i++)
+       for (i = 0; i < num_actions; i++)
                actions_attr[i] =
                        *((struct mlx5dv_flow_action_attr *)(actions[i]));
        return mlx5dv_create_flow(matcher, match_value,
drivers/crypto/caam_jr/caam_jr.c +4 -1
index caf2386772..77fa6ff2d3 100644
@@ -1351,6 +1351,9 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
        struct caam_jr_session *ses;
        struct caam_jr_op_ctx *ctx = NULL;
        struct sec_job_descriptor_t *jobdescr __rte_unused;
+#if CAAM_JR_DBG
+       int i;
+#endif
 
        switch (op->sess_type) {
        case RTE_CRYPTO_OP_WITH_SESSION:
@@ -1413,7 +1416,7 @@ caam_jr_enqueue_op(struct rte_crypto_op *op, struct caam_jr_qp *qp)
                        rte_pktmbuf_data_len(op->sym->m_src));
 
        printf("\n JD before conversion\n");
-       for (int i = 0; i < 12; i++)
+       for (i = 0; i < 12; i++)
                printf("\n 0x%08x", ctx->jobdes.desc[i]);
 #endif
 
drivers/net/dpaa2/dpaa2_sparser.c +0 -30
index 7e8fedd818..ba0d500f74 100644
@@ -145,36 +145,6 @@ struct frame_attr_ext frame_attr_ext_arr[] = {
        /* 112 */ {NULL,                                       0, 0x0000}
 };
 
-#define SWAP_WORD(pr)                                          \
-do {                                                           \
-       for (int i = 0; i < 4 ; i++) {                          \
-               pr[i] = pr[i] ^ pr[6 - i + 1];                  \
-               pr[6 - i + 1] = pr[6 - i + 1] ^ pr[i];          \
-               pr[i] = pr[i] ^ pr[6 - i + 1];                  \
-       }                                                       \
-} while (0)
-
-#define fa_print_sb()                                          \
-do {                                                           \
-       if (rte_cpu_to_be_32(*pdw) & frm_attr->fld_mask)        \
-               DPAA2_PMD_DP_DEBUG("t %s : Yes", frm_attr->fld_name);   \
-} while (0)
-
-#define fa_print_sb_ext()                                      \
-do {                                                           \
-       if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)     \
-               DPAA2_PMD_DP_DEBUG("\t %s : Yes",                       \
-                         frm_attr_ext->fld_name);              \
-} while (0)
-
-#define fa_print_mb_ext()                                      \
-do {                                                           \
-       if (rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask)     \
-               DPAA2_PMD_DP_DEBUG("\t %s : 0x%02x",                    \
-                         frm_attr_ext->fld_name,               \
-                         rte_cpu_to_be_16(*pw) & frm_attr_ext->fld_mask);\
-} while (0)
-
 int dpaa2_eth_load_wriop_soft_parser(struct dpaa2_dev_priv *priv,
                                     enum dpni_soft_sequence_dest dest)
 {
lib/librte_eal/windows/eal_lcore.c +4 -3
index 82ee454134..760b3fcfed 100644
@@ -29,6 +29,8 @@ static struct _wcpu_map {
 void
 eal_create_cpu_map()
 {
+       unsigned int socket, core;
+
        wcpu_map.total_procs =
                GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
 
@@ -56,9 +58,9 @@ eal_create_cpu_map()
         * equally across the sockets.
         */
        unsigned int lcore = 0;
-       for (unsigned int socket = 0; socket <
+       for (socket = 0; socket <
                        wcpu_map.proc_sockets; ++socket) {
-               for (unsigned int core = 0;
+               for (core = 0;
                        core < (wcpu_map.proc_cores / wcpu_map.proc_sockets);
                        ++core) {
                        wcpu_map.wlcore_map[lcore]
--
Quoted 2 lines

2.26.2

Signature · 1 line
David Marchand
#8 lore raw
Quoted 12 lines · David Marchand
Quoted 11 lines · Thomas Monjalon

Some compilers raise an error when declaring a variable in the middle of a function. This is a C99 allowance. Even if DPDK switches globally to C99 or C11 standard, the coding rules are for declarations at the beginning of a block:
http://doc.dpdk.org/guides/contributing/coding_style.html#local-variables

This coding style is enforced by adding a check of the common patterns like "for (int i;"

Quoted 6 lines
Quoted 5 lines

The occurrences of the checked pattern are fixed:

        'for *(\(char\|u\?int\|unsigned\|s\?size_t\)'

In the file dpaa2_sparser.c, the fix is to remove the unused macros.

Discarded the (now obsolete) change on windows/eal.c and applied.

Signature · 1 line
David Marchand