From: Collin Funk <collin.funk1@gmail.com>
Date: Mon, 25 Mar 2024 08:56:24 +0000 (-0700)
Subject: gnulib-tool.py: Fix output of #include directive advice.
X-Git-Tag: v1.0~229
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=4d2e12f81ff8624609f3976069a7c992f3926ddf;p=gnulib.git

gnulib-tool.py: Fix output of #include directive advice.

* pygnulib/GLImport.py (GLImport.execute): Copy comment from
gnulib-tool.sh with modified variable names. Search the 'Include:' as a
whole instead of each individual line.
---

diff --git a/ChangeLog b/ChangeLog
index 01c738c021..0c08adbda1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-03-25  Collin Funk  <collin.funk1@gmail.com>
+
+	gnulib-tool.py: Fix output of #include directive advice.
+	* pygnulib/GLImport.py (GLImport.execute): Copy comment from
+	gnulib-tool.sh with modified variable names. Search the 'Include:' as a
+	whole instead of each individual line.
+
 2024-03-25  Bruno Haible  <bruno@clisp.org>
 
 	gnulib-tool.py: Simplify last change.
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index 803f2d85dd..5de9cdfc47 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -1434,34 +1434,36 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
         # Finish the work.
         print('Finished.\n')
         print('You may need to add #include directives for the following .h files.')
-        modules = sorted(set([ module
-                               for module in self.moduletable['base']
-                               if module in self.moduletable['main'] ]))
+        # Intersect 'base' modules and 'main' modules
+        # (since 'base' modules is not necessarily of subset of 'main' modules
+        # - some may have been skipped through --avoid, and since the elements of
+        # 'main' modules but not in 'base' modules can go away without explicit
+        # notice - through changes in the module dependencies).
+        modules = sorted(set(self.moduletable['base']).intersection(self.moduletable['main']))
         # First the #include <...> directives without #ifs, sorted for convenience,
         # then the #include "..." directives without #ifs, sorted for convenience,
         # then the #include directives that are surrounded by #ifs. Not sorted.
-        includes_angles = list()
-        includes_quotes = list()
-        includes_if = list()
+        include_angles = []
+        include_quotes = []
+        include_if = []
         for module in modules:
             include = module.getInclude()
-            for include in include.split('\n'):
-                if '%s#if' % constants.NL in '%s%s' % (constants.NL, include):
-                    includes_if += [include]
-                # if '%s#if' % constants.NL in '%s%s' % (constants.NL, include)
-                else:
-                    if 'include "' in include:
-                        includes_quotes += [include]
-                    else:  # if 'include "' not in include
-                        includes_angles += [include]
-        includes_angles = sorted(set(includes_angles))
-        includes_quotes = sorted(set(includes_quotes))
-        includes = includes_angles + includes_quotes + includes_if
-        includes = [ include
-                     for include in includes
-                     if include.split() ]
-        for include in includes:
-            print('  %s' % include)
+            if '\n#if' in include:
+                include_if += [ f'  {line}'
+                                for line in include.split('\n')
+                                if line.strip() ]
+            else:
+                include_angles += [ f'  {line}'
+                                    for line in include.split('\n')
+                                    if 'include "' not in line and line.strip() ]
+                include_quotes += [ f'  {line}'
+                                    for line in include.split('\n')
+                                    if 'include "' in line and line.strip() ]
+
+        includes = lines_to_multiline(sorted(set(include_angles)))
+        includes += lines_to_multiline(sorted(set(include_quotes)))
+        includes += lines_to_multiline(include_if)
+        print(includes, end='')
 
         # Get link directives.
         links = [ module.getLink()