Skip to contentSkip to conversation

sparse/pre-process: introduce "dissect_mode" option to fix dissect/semind

PATCH In review Oleg Nesterov 2 participants 7 messages Dec 17, 2025 – Jan 19 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparse/pre-process: introduce "dissect_mode" option to fix dissect/semind

I don't quite understand why does expand() -> collect_arg() path update ->pos for each token in the input *list, but this breaks dissect and thus semind.

Test-case:

	$ cat -n PP_POS.c
	     1	#define READ_ONCE(x) x
	     2	#define WRITE_ONCE(x, y) x = y
	     3
	     4	int R, W;
	     5
	     6	void func(void)
	     7	{
	     8	  WRITE_ONCE(
	     9	     W,
	    10	     READ_ONCE(R)
	    11	  );
	    12	}
	$ ./test-dissect PP_POS.c
	   4:5                    def   v R                                int
	   4:8                    def   v W                                int
	   6:6                    def   f func                             void ( ... )
	   8:3   func             -w-   v W                                int
	   8:3   func             -r-   v R                                int

The reported positions of the usage of R and W are wrong, and thus ./semind doesn't work:

	$ ./semind add PP_POS.c
	$ ./semind search -l PP_POS.c:10:16

With this patch:

	$ ./test-dissect PP_POS.c
	   4:5                    def   v R                                int
	   4:8                    def   v W                                int
	   6:6                    def   f func                             void ( ... )
	   9:6   func             -w-   v W                                int
	  10:16  func             -r-   v R                                int
	$ ./semind add PP_POS.c
	$ ./semind search -l PP_POS.c:10:16
	(def) PP_POS.c	4	5		int R, W;
	(-r-) PP_POS.c	10	16	func	READ_ONCE(R)
---
 dissect.c     | 1 +
 options.c     | 1 +
 options.h     | 1 +
 pre-process.c | 8 +++++---
 4 files changed, 8 insertions(+), 3 deletions(-)
dissect.c +1 -0
index a6003afa..5fed8e22 100644
@@ -714,6 +714,7 @@ end:
 
 void dissect(struct reporter *rep, struct string_list *filelist)
 {
+	dissect_mode = 1;
 	reporter = rep;
 
 	DO_LIST(filelist, file, do_file(file));
options.c +1 -0
index 6ee4d878..0f207e80 100644
@@ -71,6 +71,7 @@ int dump_macro_defs = 0;
 int dump_macros_only = 0;
 
 int dissect_show_all_symbols = 0;
+int dissect_mode = 0;
 
 unsigned long fdump_ir;
 int fhosted = 1;
options.h +1 -0
index c2a9551a..b559254d 100644
@@ -71,6 +71,7 @@ extern int dump_macro_defs;
 extern int dump_macros_only;
 
 extern int dissect_show_all_symbols;
+extern int dissect_mode;
 
 extern unsigned long fdump_ir;
 extern int fhosted;
pre-process.c +5 -3
index 3fb25082..64445881 100644
@@ -294,9 +294,11 @@ static struct token *collect_arg(struct token *prev, int vararg, struct position
 		} else if (match_op(next, ',') && !nesting && !vararg) {
 			break;
 		}
-		next->pos.stream = pos->stream;
-		next->pos.line = pos->line;
-		next->pos.pos = pos->pos;
+		if (!dissect_mode) {
+			next->pos.stream = pos->stream;
+			next->pos.line = pos->line;
+			next->pos.pos = pos->pos;
+		}
 		next->pos.newline = 0;
 		p = &next->next;
 	}
Signature · 1 line
2.52.0
maintainer
#1 lore raw

Hi Oleg,

Slowly catching up my back log from the holidays.

Quoted 4 lines · Oleg Nesterov

I don't quite understand why does expand() -> collect_arg() path update ->pos for each token in the input *list, but this breaks dissect and thus semind.

That is a good question, I don't understand why it did that either. I did some digging, inside macro argument list expansion, the "#include " is not allowed. It is not possible to switch streams here. The "pos.pos" is for human consumption anyway, it has no effect on the IR generation. The only visible effect as far as I can tell is related to the preprocessor "-E" in lib.c:

        if (preprocess_only) {
                while (!eof_token(token)) {
                        int prec = 1;
                        struct token *next = token->next;
                        const char *separator = "";
                        if (next->pos.whitespace)
                                separator = " ";
                        if (next->pos.newline) {
                                separator = "\n\t\t\t\t\t";
                                prec = next->pos.pos; <--- use pos as
indentation level.
                                if (prec > 4)
                                        prec = 4;
                        }
                        printf("%s%.*s", show_token(token), prec, separator);
                        token = next;

The "-E" output has some indentation enhancement to turn space into tab level indentation. This "pos" assignment tries to align the indentation context of the input arguments to the same level of the expanding macro name.

Quoted 12 lines

Test-case:

        $ cat -n PP_POS.c
             1  #define READ_ONCE(x) x
             2  #define WRITE_ONCE(x, y) x = y
             3
             4  int R, W;
             5
             6  void func(void)
             7  {
             8    WRITE_ONCE(
             9       W,

With your patch, when doing "-E", the W will get indentation deeper than WRITE_ONCE.

Quoted 11 lines

10 READ_ONCE(R)
11 );
12 }

$ ./test-dissect PP_POS.c
4:5 def v R int
4:8 def v W int
6:6 def f func void ( ... )
8:3 func -w- v W int
8:3 func -r- v R int

Quoted 2 lines

The reported positions of the usage of R and W are wrong, and thus ./semind doesn't work:

It seems to me this enhancement can be used on other macro related expansions as well.

Quoted 31 lines
        $ ./semind add PP_POS.c
        $ ./semind search -l PP_POS.c:10:16

With this patch:

        $ ./test-dissect PP_POS.c
           4:5                    def   v R                                int
           4:8                    def   v W                                int
           6:6                    def   f func                             void ( ... )
           9:6   func             -w-   v W                                int
          10:16  func             -r-   v R                                int
        $ ./semind add PP_POS.c
        $ ./semind search -l PP_POS.c:10:16
        (def) PP_POS.c  4       5               int R, W;
        (-r-) PP_POS.c  10      16      func    READ_ONCE(R)
---
 dissect.c     | 1 +
 options.c     | 1 +
 options.h     | 1 +
 pre-process.c | 8 +++++---
 4 files changed, 8 insertions(+), 3 deletions(-)
dissect.c +0 -0
index a6003afa..5fed8e22 100644

dissect.c end: · line 717

Quoted 4 lines
dissect.c +1 -0
@@ -714,6 +714,7 @@ end:
 
 void dissect(struct reporter *rep, struct string_list *filelist)
 {
+       dissect_mode = 1;

I don't think we need dissect_mode. I am leaning towards enabling it all the time, maybe except for the preprocessor only mode.

Quoted 33 lines
dissect.c +0 -0
        reporter = rep;
 
        DO_LIST(filelist, file, do_file(file));
options.c +1 -0
index 6ee4d878..0f207e80 100644
@@ -71,6 +71,7 @@ int dump_macro_defs = 0;
 int dump_macros_only = 0;
 
 int dissect_show_all_symbols = 0;
+int dissect_mode = 0;
 
 unsigned long fdump_ir;
 int fhosted = 1;
options.h +1 -0
index c2a9551a..b559254d 100644
@@ -71,6 +71,7 @@ extern int dump_macro_defs;
 extern int dump_macros_only;
 
 extern int dissect_show_all_symbols;
+extern int dissect_mode;
 
 extern unsigned long fdump_ir;
 extern int fhosted;
pre-process.c +0 -0
index 3fb25082..64445881 100644
@@ -294,9 +294,11 @@ static struct token *collect_arg(struct token *prev, int vararg, struct position
                } else if (match_op(next, ',') && !nesting && !vararg) {

pre-process.c static struct token *collect_arg(struct token *prev, int vararg, struct position · line 301

Quoted 10 lines
pre-process.c +5 -3
                        break;
                }
-               next->pos.stream = pos->stream;
-               next->pos.line = pos->line;
-               next->pos.pos = pos->pos;
+               if (!dissect_mode) {
+                       next->pos.stream = pos->stream;
+                       next->pos.line = pos->line;
+                       next->pos.pos = pos->pos;
+               }

Maybe change it to "if (preprocess_only)", and fix all the validation error output of the checker. What do you say?

Overall I feel that without this position overwrite is better to locate the real location of the argument of the macro. If anyone knows another reason we should do the position overwrite, please let me know.

Alternatively we can also fix the preprocessor "-E" indentation output. Might not be worth the complexity.

Chris

Quoted 7 lines
pre-process.c +0 -1
                next->pos.newline = 0;
                p = &next->next;
        }
--

2.52.0

#2 lore raw

Hi Chris,

Quoted 31 lines · Chris Li
Quoted 4 lines · Oleg Nesterov

I don't quite understand why does expand() -> collect_arg() path update ->pos for each token in the input *list, but this breaks dissect and thus semind.

That is a good question, I don't understand why it did that either. I did some digging, inside macro argument list expansion, the "#include " is not allowed. It is not possible to switch streams here. The "pos.pos" is for human consumption anyway, it has no effect on the IR generation. The only visible effect as far as I can tell is related to the preprocessor "-E" in lib.c:

        if (preprocess_only) {
                while (!eof_token(token)) {
                        int prec = 1;
                        struct token *next = token->next;
                        const char *separator = "";
                        if (next->pos.whitespace)
                                separator = " ";
                        if (next->pos.newline) {
                                separator = "\n\t\t\t\t\t";
                                prec = next->pos.pos; <--- use pos as
indentation level.
                                if (prec > 4)
                                        prec = 4;
                        }
                        printf("%s%.*s", show_token(token), prec, separator);
                        token = next;
Quoted 4 lines

The "-E" output has some indentation enhancement to turn space into tab level indentation. This "pos" assignment tries to align the indentation context of the input arguments to the same level of the expanding macro name.

Yes, exactly! Initially I tried to simply remove these next->pos.* updates in collect_arg(), but this causes a lot of failures in validation/preprocessor (due to extra indentations) and I failed to find a simple fix for the "if (preprocess_only)" code above. Plus I wasn't comfortable because I don't understand the intent...

Quoted 6 lines
Quoted 3 lines

void dissect(struct reporter *rep, struct string_list *filelist) {
+ dissect_mode = 1;

I don't think we need dissect_mode. I am leaning towards enabling it all the time, maybe except for the preprocessor only mode.

...

Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+               if (!dissect_mode) {
+                       next->pos.stream = pos->stream;
+                       next->pos.line = pos->line;
+                       next->pos.pos = pos->pos;
+               }

Maybe change it to "if (preprocess_only)", and fix all the validation error output of the checker. What do you say?

Agreed! This was my plan B ;)

With this change

- if (!dissect_mode) {
+ if (preprocess_only) {

make check reports 2 failures

-parsing/attr-cleanup.c:10:17: error: argument is not an identifier +parsing/attr-cleanup.c:10:27: error: argument is not an identifier

-sizeof-void.c:20:14: warning: expression using sizeof(void) +sizeof-void.c:20:27: warning: expression using sizeof(void)

but the new positions look more correct.

However. I didn't dare to send this patch because other warnings from sizeof-void.c still blame the column 14, this looks inconsistent... But perhaps we don't really care?

So. I am going to update the changelog and send the trivial V2 below.

Will you agree?

Oleg.
---

pre-process.c +5 -3
index 3fb25082..a4bb6cb6 100644
@@ -294,9 +294,11 @@ static struct token *collect_arg(struct token *prev, int vararg, struct position
 		} else if (match_op(next, ',') && !nesting && !vararg) {
 			break;
 		}
-		next->pos.stream = pos->stream;
-		next->pos.line = pos->line;
-		next->pos.pos = pos->pos;
+		if (preprocess_only) {
+			next->pos.stream = pos->stream;
+			next->pos.line = pos->line;
+			next->pos.pos = pos->pos;
+		}
 		next->pos.newline = 0;
 		p = &next->next;
 	}
validation/parsing/attr-cleanup.c +1 -1
index ac64649c..fa3cb1ca 100644
@@ -24,7 +24,7 @@ int test(int n)
  * check-command: sparse -Wunknown-attribute $file
  *
  * check-error-start
-parsing/attr-cleanup.c:10:17: error: argument is not an identifier
+parsing/attr-cleanup.c:10:27: error: argument is not an identifier
 parsing/attr-cleanup.c:11:39: error: an argument is expected for attribute 'cleanup'
 parsing/attr-cleanup.c:12:40: error: an argument is expected for attribute 'cleanup'
 parsing/attr-cleanup.c:13:43: error: Expected ) after attribute's argument'
validation/sizeof-void.c +1 -1
index 0fd917a2..6792ff02 100644
@@ -36,7 +36,7 @@ sizeof-void.c:16:14: warning: expression using sizeof(void)
 sizeof-void.c:17:14: warning: expression using sizeof(void)
 sizeof-void.c:18:14: warning: expression using sizeof(void)
 sizeof-void.c:19:14: warning: expression using sizeof(void)
-sizeof-void.c:20:14: warning: expression using sizeof(void)
+sizeof-void.c:20:27: warning: expression using sizeof(void)
 sizeof-void.c:21:14: warning: expression using sizeof(void)
 sizeof-void.c:22:14: warning: expression using sizeof(void)
 sizeof-void.c:23:14: warning: expression using sizeof(void)
#3 lore raw
Quoted 13 lines · Oleg Nesterov

Agreed! This was my plan B ;)

With this change

- if (!dissect_mode) {
+ if (preprocess_only) {

make check reports 2 failures

-parsing/attr-cleanup.c:10:17: error: argument is not an identifier +parsing/attr-cleanup.c:10:27: error: argument is not an identifier

Quoted 8 lines

-sizeof-void.c:20:14: warning: expression using sizeof(void) +sizeof-void.c:20:27: warning: expression using sizeof(void)

but the new positions look more correct.

However. I didn't dare to send this patch because other warnings from sizeof-void.c still blame the column 14, this looks inconsistent... But perhaps we don't really care?

On a 2nd thought...

Unlike other warnings, this one (sizeof-void.c:20:27) refers to the inner "sizeof *ptr", so I think that this patch fixes the reported position. So yes, I think we don't care even if the new column == 27 differs from other warnings.

What do you think?

Oleg.

Quoted 52 lines

So. I am going to update the changelog and send the trivial V2 below.

Will you agree?

Oleg.
---

pre-process.c +5 -3
index 3fb25082..a4bb6cb6 100644
@@ -294,9 +294,11 @@ static struct token *collect_arg(struct token *prev, int vararg, struct position
 		} else if (match_op(next, ',') && !nesting && !vararg) {
 			break;
 		}
-		next->pos.stream = pos->stream;
-		next->pos.line = pos->line;
-		next->pos.pos = pos->pos;
+		if (preprocess_only) {
+			next->pos.stream = pos->stream;
+			next->pos.line = pos->line;
+			next->pos.pos = pos->pos;
+		}
 		next->pos.newline = 0;
 		p = &next->next;
 	}
validation/parsing/attr-cleanup.c +1 -1
index ac64649c..fa3cb1ca 100644
@@ -24,7 +24,7 @@ int test(int n)
  * check-command: sparse -Wunknown-attribute $file
  *
  * check-error-start
-parsing/attr-cleanup.c:10:17: error: argument is not an identifier
+parsing/attr-cleanup.c:10:27: error: argument is not an identifier
 parsing/attr-cleanup.c:11:39: error: an argument is expected for attribute 'cleanup'
 parsing/attr-cleanup.c:12:40: error: an argument is expected for attribute 'cleanup'
 parsing/attr-cleanup.c:13:43: error: Expected ) after attribute's argument'
validation/sizeof-void.c +1 -1
index 0fd917a2..6792ff02 100644
@@ -36,7 +36,7 @@ sizeof-void.c:16:14: warning: expression using sizeof(void)
 sizeof-void.c:17:14: warning: expression using sizeof(void)
 sizeof-void.c:18:14: warning: expression using sizeof(void)
 sizeof-void.c:19:14: warning: expression using sizeof(void)
-sizeof-void.c:20:14: warning: expression using sizeof(void)
+sizeof-void.c:20:27: warning: expression using sizeof(void)
 sizeof-void.c:21:14: warning: expression using sizeof(void)
 sizeof-void.c:22:14: warning: expression using sizeof(void)
 sizeof-void.c:23:14: warning: expression using sizeof(void)
maintainer
#4 lore raw
Quoted 24 lines · Oleg Nesterov
Quoted 13 lines · Oleg Nesterov

Agreed! This was my plan B ;)

With this change

- if (!dissect_mode) {
+ if (preprocess_only) {

make check reports 2 failures

-parsing/attr-cleanup.c:10:17: error: argument is not an identifier +parsing/attr-cleanup.c:10:27: error: argument is not an identifier

Quoted 8 lines

-sizeof-void.c:20:14: warning: expression using sizeof(void) +sizeof-void.c:20:27: warning: expression using sizeof(void)

but the new positions look more correct.

However. I didn't dare to send this patch because other warnings from sizeof-void.c still blame the column 14, this looks inconsistent... But perhaps we don't really care?

Quoted 8 lines

On a 2nd thought...

Unlike other warnings, this one (sizeof-void.c:20:27) refers to the inner "sizeof *ptr", so I think that this patch fixes the reported position. So yes, I think we don't care even if the new column == 27 differs from other warnings.

What do you think?

I would just update the checker to have the new expected value matching what new pos so validation can pass without errors.

Chris

#5 lore raw
Quoted 10 lines · Chris Li
Quoted 8 lines · Oleg Nesterov

On a 2nd thought...

Unlike other warnings, this one (sizeof-void.c:20:27) refers to the inner "sizeof *ptr", so I think that this patch fixes the reported position. So yes, I think we don't care even if the new column == 27 differs from other warnings.

Quoted 4 lines
Quoted 1 line

What do you think?

I would just update the checker to have the new expected value matching what new pos so validation can pass without errors.

Yes, this is what I did. See

	[PATCH] sparse/pre-process: don't update next->pos in collect_arg()
	https://lore.kernel.org/all/[email protected]/

I was confused and tried to confuse you... let me explain. With this patch ./sparse -Wpointer-arith validation/sizeof-void.c outputs

validation/sizeof-void.c:16:14: warning: expression using sizeof(void) validation/sizeof-void.c:17:14: warning: expression using sizeof(void) validation/sizeof-void.c:18:14: warning: expression using sizeof(void) validation/sizeof-void.c:19:14: warning: expression using sizeof(void) validation/sizeof-void.c:20:27: warning: expression using sizeof(void) // changed validation/sizeof-void.c:21:14: warning: expression using sizeof(void) validation/sizeof-void.c:22:14: warning: expression using sizeof(void) validation/sizeof-void.c:23:14: warning: expression using sizeof(void)

and somehow I wrongly came to conlusion that my patch is incomplete or inconsistent because it only corrects the warning's position for the line 20.

Now that I actually looked at validation/sizeof-void.c, I see that the code at line 20

	s += is_constexpr(sizeof *ptr);

differs in that it is the inner "sizeof *ptr" which triggers the warning, so I think the patch is fine.

Thank you,

Oleg.

maintainer
#6 lore raw
In reply to Oleg Nesterov
Quoted 40 lines · Oleg Nesterov

Hi Chris,

Quoted 31 lines · Chris Li
Quoted 4 lines · Oleg Nesterov

I don't quite understand why does expand() -> collect_arg() path update ->pos for each token in the input *list, but this breaks dissect and thus semind.

That is a good question, I don't understand why it did that either. I did some digging, inside macro argument list expansion, the "#include " is not allowed. It is not possible to switch streams here. The "pos.pos" is for human consumption anyway, it has no effect on the IR generation. The only visible effect as far as I can tell is related to the preprocessor "-E" in lib.c:

        if (preprocess_only) {
                while (!eof_token(token)) {
                        int prec = 1;
                        struct token *next = token->next;
                        const char *separator = "";
                        if (next->pos.whitespace)
                                separator = " ";
                        if (next->pos.newline) {
                                separator = "\n\t\t\t\t\t";
                                prec = next->pos.pos; <--- use pos as
indentation level.
                                if (prec > 4)
                                        prec = 4;
                        }
                        printf("%s%.*s", show_token(token), prec, separator);
                        token = next;
Quoted 4 lines

The "-E" output has some indentation enhancement to turn space into tab level indentation. This "pos" assignment tries to align the indentation context of the input arguments to the same level of the expanding macro name.

Quoted 5 lines

Yes, exactly! Initially I tried to simply remove these next->pos.* updates in collect_arg(), but this causes a lot of failures in validation/preprocessor (due to extra indentations) and I failed to find a simple fix for the "if (preprocess_only)" code above. Plus I wasn't comfortable because I don't understand the intent...

As far as I can tell, the new position report is more useful to the reader. I haven't heard any objections yet. Let's put it on the sparse-dev for a bit then merge into sparse. In the worst case there is some burning reason to use the old behavior, we can always change the behavior back. It is only software and it is easy to fix.

Quoted 31 lines
Quoted 7 lines
Quoted 4 lines

void dissect(struct reporter *rep, struct string_list *filelist) {
+ dissect_mode = 1;

I don't think we need dissect_mode. I am leaning towards enabling it all the time, maybe except for the preprocessor only mode.

...

Quoted 8 lines

pre-process.c static struct token *collect_arg(struct token *prev, int vararg, struct position · line 301

Quoted 5 lines
pre-process.c +5 -0
+               if (!dissect_mode) {
+                       next->pos.stream = pos->stream;
+                       next->pos.line = pos->line;
+                       next->pos.pos = pos->pos;
+               }

Maybe change it to "if (preprocess_only)", and fix all the validation error output of the checker. What do you say?

Agreed! This was my plan B ;)

With this change

- if (!dissect_mode) {
+ if (preprocess_only) {

make check reports 2 failures

-parsing/attr-cleanup.c:10:17: error: argument is not an identifier +parsing/attr-cleanup.c:10:27: error: argument is not an identifier

Quoted 8 lines

-sizeof-void.c:20:14: warning: expression using sizeof(void) +sizeof-void.c:20:27: warning: expression using sizeof(void)

but the new positions look more correct.

However. I didn't dare to send this patch because other warnings from sizeof-void.c still blame the column 14, this looks inconsistent... But perhaps we don't really care?

Well, we care in the sense that we don't want unnecessary check failures. But we can update the expected output of the validation check to silence the error.

Quoted 4 lines

So. I am going to update the changelog and send the trivial V2 below.

Will you agree?

Agree. As far as I can tell. The new position is more desirable. Let's switch to the new position.

Chris

Quoted 50 lines

Oleg.
---

pre-process.c +5 -3
index 3fb25082..a4bb6cb6 100644
@@ -294,9 +294,11 @@ static struct token *collect_arg(struct token *prev, int vararg, struct position
                } else if (match_op(next, ',') && !nesting && !vararg) {
                        break;
                }
-               next->pos.stream = pos->stream;
-               next->pos.line = pos->line;
-               next->pos.pos = pos->pos;
+               if (preprocess_only) {
+                       next->pos.stream = pos->stream;
+                       next->pos.line = pos->line;
+                       next->pos.pos = pos->pos;
+               }
                next->pos.newline = 0;
                p = &next->next;
        }
validation/parsing/attr-cleanup.c +1 -1
index ac64649c..fa3cb1ca 100644
@@ -24,7 +24,7 @@ int test(int n)
  * check-command: sparse -Wunknown-attribute $file
  *
  * check-error-start
-parsing/attr-cleanup.c:10:17: error: argument is not an identifier
+parsing/attr-cleanup.c:10:27: error: argument is not an identifier
 parsing/attr-cleanup.c:11:39: error: an argument is expected for attribute 'cleanup'
 parsing/attr-cleanup.c:12:40: error: an argument is expected for attribute 'cleanup'
 parsing/attr-cleanup.c:13:43: error: Expected ) after attribute's argument'
validation/sizeof-void.c +1 -1
index 0fd917a2..6792ff02 100644
@@ -36,7 +36,7 @@ sizeof-void.c:16:14: warning: expression using sizeof(void)
 sizeof-void.c:17:14: warning: expression using sizeof(void)
 sizeof-void.c:18:14: warning: expression using sizeof(void)
 sizeof-void.c:19:14: warning: expression using sizeof(void)
-sizeof-void.c:20:14: warning: expression using sizeof(void)
+sizeof-void.c:20:27: warning: expression using sizeof(void)
 sizeof-void.c:21:14: warning: expression using sizeof(void)
 sizeof-void.c:22:14: warning: expression using sizeof(void)
 sizeof-void.c:23:14: warning: expression using sizeof(void)