Skip to contentSkip to conversation

gen_compile_commands: fix overlooked module files

PATCH Superseded John Hubbard 2 participants 3 messages Jun 15, 2022 – Jun 16, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gen_compile_commands: fix overlooked module files

scripts/clang-tools/gen_compile_commands.py incorrectly assumes that each .mod file only contains one line. In fact, such files contain one entry per line, and for some subsystems, there can be many, many lines. For example, Nouveau has 762 entries, but only the first entry was being processed. This problem causes clangd to fail to provide references and definitions for valid files that are part of the current kernel configuration.

This problem only occurs when using Kbuild to generate, like this:

make CC=clang compile_commands.json

It does not occur if you just run gen_compile_commands.py "bare", like this (below):

scripts/clang-tools/gen_compile_commands.py/gen_compile_commands.py .

Fix this by fully processing each .mod file. This fix causes the number of build commands that clangd finds in my kernel build (these numbers are heavily dependent upon .config), from 2848 to 5292, which is an 85% increase.

Fixes ecca4fea1ede4 ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")
Cc Masahiro Yamada <[email protected]>
Cc Nick Desaulniers <[email protected]>
---
 scripts/clang-tools/gen_compile_commands.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/clang-tools/gen_compile_commands.py +4 -3
index 1d1bde1fd45e..53590e886889 100755
@@ -157,10 +157,11 @@ def cmdfiles_for_modorder(modorder):
             if ext != '.ko':
                 sys.exit('{}: module path must end with .ko'.format(ko))
             mod = base + '.mod'
-	    # The first line of *.mod lists the objects that compose the module.
+	    # Read from *.mod, to get a list of objects that compose the module.
             with open(mod) as m:
-                for obj in m.readline().split():
-                    yield to_cmdfile(obj)
+                for line in m.readlines():
+                    for obj in line.split():
+                        yield to_cmdfile(obj)
 
 
 def process_line(root_directory, command_prefix, file_path):
Signature · 1 line
2.36.1
#1 lore raw
Quoted 3 lines · John Hubbard

scripts/clang-tools/gen_compile_commands.py incorrectly assumes that each .mod file only contains one line.

Thanks for catching this.

That assumption was correct until recently.
The first line contained member objects.
The second line, if CONFIG_TRIM_UNUSED_KSYMS=y, contained unresolved symbols

Commit 9413e7640564 ("kbuild: split the second line of *.mod into *.usyms") changed the format of *.mod so member objects are listed per-line.

Quoted 14 lines

In fact, such files contain one
entry per line, and for some subsystems, there can be many, many lines. For example, Nouveau has 762 entries, but only the first entry was being processed. This problem causes clangd to fail to provide references and definitions for valid files that are part of the current kernel configuration.

This problem only occurs when using Kbuild to generate, like this:

make CC=clang compile_commands.json

It does not occur if you just run gen_compile_commands.py "bare", like this (below):

Quoted 8 lines

scripts/clang-tools/gen_compile_commands.py/gen_compile_commands.py .

Fix this by fully processing each .mod file. This fix causes the number of build commands that clangd finds in my kernel build (these numbers are heavily dependent upon .config), from 2848 to 5292, which is an 85% increase.

Fixes ecca4fea1ede4 ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")

This should be

Fixes 9413e7640564 ("kbuild: split the second line of *.mod into *.usyms")

Can you update the commit log?

Quoted 12 lines
Cc Masahiro Yamada <[email protected]>
Cc Nick Desaulniers <[email protected]>
---
 scripts/clang-tools/gen_compile_commands.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/clang-tools/gen_compile_commands.py +0 -0
index 1d1bde1fd45e..53590e886889 100755

scripts/clang-tools/gen_compile_commands.py def cmdfiles_for_modorder(modorder): · line 162

Quoted 9 lines
scripts/clang-tools/gen_compile_commands.py +2 -3
@@ -157,10 +157,11 @@ def cmdfiles_for_modorder(modorder):
             if ext != '.ko':
                 sys.exit('{}: module path must end with .ko'.format(ko))
             mod = base + '.mod'
-           # The first line of *.mod lists the objects that compose the module.
+           # Read from *.mod, to get a list of objects that compose the module.
             with open(mod) as m:
-                for obj in m.readline().split():
-                    yield to_cmdfile(obj)
+                for line in m.readlines():

for line in m:

is simpler, (and maybe will work more efficiently).

One more note, the 'line' iterator is shadowing (overwriting) the outer 'line' iterator, which has been used a few lines above.

with open(modorder) as f:
for line in f:

Maybe, it is safer to use a different name for the inner iterator because shadowing does not work in Python.

scripts/clang-tools/gen_compile_commands.py def cmdfiles_for_modorder(modorder): · line 163

Quoted 1 line
scripts/clang-tools/gen_compile_commands.py +1 -0
+                    for obj in line.split():

This loop is unneeded because each line
contains only one word.
.rstpip() will do.

To sum up, this part can be simpler,
for example like this:

# Read from *.mod, to get a list of objects that compose the module.
with open(mod) as m:
for line2 in m:
yield to_cmdfile(line2.rstrip())

Quoted 7 lines
scripts/clang-tools/gen_compile_commands.py +1 -1
+                        yield to_cmdfile(obj)
 
 
 def process_line(root_directory, command_prefix, file_path):
--

2.36.1

Signature · 2 lines
Best Regards
Masahiro Yamada
#2 lore raw
Quoted 9 lines · Masahiro Yamada
Quoted 1 line
Fixes ecca4fea1ede4 ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")

This should be

Fixes 9413e7640564 ("kbuild: split the second line of *.mod into *.usyms")

Can you update the commit log?

Done.

Quoted 26 lines
Quoted 13 lines
Cc Masahiro Yamada <[email protected]>
Cc Nick Desaulniers <[email protected]>
---
  scripts/clang-tools/gen_compile_commands.py | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
scripts/clang-tools/gen_compile_commands.py +0 -0
index 1d1bde1fd45e..53590e886889 100755

scripts/clang-tools/gen_compile_commands.py def cmdfiles_for_modorder(modorder): · line 162

Quoted 9 lines
scripts/clang-tools/gen_compile_commands.py +2 -3
@@ -157,10 +157,11 @@ def cmdfiles_for_modorder(modorder):
              if ext != '.ko':
                  sys.exit('{}: module path must end with .ko'.format(ko))
              mod = base + '.mod'
-           # The first line of *.mod lists the objects that compose the module.
+           # Read from *.mod, to get a list of objects that compose the module.
              with open(mod) as m:
-                for obj in m.readline().split():
-                    yield to_cmdfile(obj)
+                for line in m.readlines():

for line in m:

Quoted 1 line

is simpler, (and maybe will work more efficiently).

aha, yes.

Quoted 4 lines

One more note, the 'line' iterator is shadowing (overwriting) the outer 'line' iterator, which has been used a few lines above.

I overlooked that point, good catch.

Quoted 21 lines

with open(modorder) as f:
for line in f:

Maybe, it is safer to use a different name for the inner iterator because shadowing does not work in Python.

scripts/clang-tools/gen_compile_commands.py def cmdfiles_for_modorder(modorder): · line 163

Quoted 1 line
scripts/clang-tools/gen_compile_commands.py +1 -0
+                    for obj in line.split():

This loop is unneeded because each line
contains only one word.
.rstpip() will do.

Quoted 8 lines

To sum up, this part can be simpler,
for example like this:

# Read from *.mod, to get a list of objects that compose the module.
with open(mod) as m:
for line2 in m:
yield to_cmdfile(line2.rstrip())

Yes, I'll send out a v2 with all of this. Maybe "mod_line" or something instead of "line2", but otherwise as you've recommended.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA