https://gcc.gnu.org/g:2c08ddd3fdd4af794ce66dbabb81ba2e6aee0b7c

commit r15-4342-g2c08ddd3fdd4af794ce66dbabb81ba2e6aee0b7c
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Oct 8 18:26:40 2024 -0400

    libcpp: avoid extra spaces in module preprocessing
    
    Within the compiler, module keywords "import", "module", and "export" that
    are recognized as part of module directives gain an extra trailing space to
    distinguish them from other non-keyword uses of those words in the code.
    But when dumping preprocessed output, printing those spaces creates a
    gratuitous inconsistency with non-modules preprocessing, as revealed by
    several of the g++.dg/modules/cpp* tests if modules are enabled by default
    in C++20 mode.
    
    libcpp/ChangeLog:
    
            * lex.cc (cpp_output_token): Omit terminal space from name.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/cpp-2_c.C: Expect only one space after import.
            * g++.dg/modules/cpp-5_c.C
            * g++.dg/modules/dep-2.C
            * g++.dg/modules/dir-only-2_b.C
            * g++.dg/modules/pr99050_b.C
            * g++.dg/modules/inc-xlate-1_b.H
            * g++.dg/modules/legacy-3_b.H
            * g++.dg/modules/legacy-3_c.H: Likewise.

Diff:
---
 gcc/testsuite/g++.dg/modules/cpp-2_c.C       |  2 +-
 gcc/testsuite/g++.dg/modules/cpp-5_c.C       |  2 +-
 gcc/testsuite/g++.dg/modules/dep-2.C         |  2 +-
 gcc/testsuite/g++.dg/modules/dir-only-2_b.C  | 10 +++++-----
 gcc/testsuite/g++.dg/modules/pr99050_b.C     |  2 +-
 libcpp/lex.cc                                |  7 +++++--
 gcc/testsuite/g++.dg/modules/inc-xlate-1_b.H |  2 +-
 gcc/testsuite/g++.dg/modules/legacy-3_b.H    |  2 +-
 gcc/testsuite/g++.dg/modules/legacy-3_c.H    |  2 +-
 9 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/gcc/testsuite/g++.dg/modules/cpp-2_c.C 
b/gcc/testsuite/g++.dg/modules/cpp-2_c.C
index c6e02b7800ad..2a79031eb345 100644
--- a/gcc/testsuite/g++.dg/modules/cpp-2_c.C
+++ b/gcc/testsuite/g++.dg/modules/cpp-2_c.C
@@ -11,7 +11,7 @@ import nope;
 #endif
 think
 
-// { dg-final { scan-file cpp-2_c.i {cpp-2_c.C"\n\n\n\nmodule  bob;\n#pragma 
GCC unused\nimport  "[^\n]*\./cpp-2_b.H" \[\[ CLOSE ]];\nimport  
"[^\n]*cpp-2_a.H" \[\[ ]];\n} } }
+// { dg-final { scan-file cpp-2_c.i {cpp-2_c.C"\n\n\n\nmodule bob;\n#pragma 
GCC unused\nimport "[^\n]*\./cpp-2_b.H" \[\[ CLOSE ]];\nimport 
"[^\n]*cpp-2_a.H" \[\[ ]];\n} } }
 // { dg-final { scan-file cpp-2_c.i "int i;" } }
 // { dg-final { scan-file-not cpp-2_c.i "import *nope;" } }
 // { dg-final { scan-file cpp-2_c.i "THIS IS STDIO\n" } }
diff --git a/gcc/testsuite/g++.dg/modules/cpp-5_c.C 
b/gcc/testsuite/g++.dg/modules/cpp-5_c.C
index e0a78a516ae3..7f75a228cda7 100644
--- a/gcc/testsuite/g++.dg/modules/cpp-5_c.C
+++ b/gcc/testsuite/g++.dg/modules/cpp-5_c.C
@@ -7,4 +7,4 @@ import "cpp-5_a.H";
 
 Q
 
-// { dg-final { scan-file cpp-5_c.i {\nimport  "[^\n]*cpp-5_a.H";\n\n0\n} } }
+// { dg-final { scan-file cpp-5_c.i {\nimport "[^\n]*cpp-5_a.H";\n\n0\n} } }
diff --git a/gcc/testsuite/g++.dg/modules/dep-2.C 
b/gcc/testsuite/g++.dg/modules/dep-2.C
index 2dccab3554d0..3c869755785a 100644
--- a/gcc/testsuite/g++.dg/modules/dep-2.C
+++ b/gcc/testsuite/g++.dg/modules/dep-2.C
@@ -9,4 +9,4 @@ module m:part;
 // { dg-final { scan-file dep-2.d {\ngcm.cache/m:part\.gcm:| dep-2\.o} } }
 // { dg-final { scan-file dep-2.d {\n\.PHONY: m:part\.c\+\+-module} } }
 
-// { dg-final { scan-file dep-2.i {\nmodule  m:part;\n} } }
+// { dg-final { scan-file dep-2.i {\nmodule m:part;\n} } }
diff --git a/gcc/testsuite/g++.dg/modules/dir-only-2_b.C 
b/gcc/testsuite/g++.dg/modules/dir-only-2_b.C
index 1009ae5b3a26..b1ef9b95e587 100644
--- a/gcc/testsuite/g++.dg/modules/dir-only-2_b.C
+++ b/gcc/testsuite/g++.dg/modules/dir-only-2_b.C
@@ -21,8 +21,8 @@ export module bob;
 
 export import q;
 
-// { dg-final { scan-file dir-only-2_b.i {// a comment\nmodule ;\nfrob} } }
-// { dg-final { scan-file dir-only-2_b.i {frob\nexport\nimport  foo;\nimport 
7;} } }
-// { dg-final { scan-file dir-only-2_b.i {import  
"[^\n]*/dir-only-2_a.H";\nimport  "[^\n]*/dir-only-2_a.H";\nX} } }
-// { dg-final { scan-file dir-only-2_b.i {export  module  bob;\n\nexport  
import  q;} } }
-// { dg-final { scan-file dir-only-2_b.i {import  sing;\n\n\n// comment} } }
+// { dg-final { scan-file dir-only-2_b.i {// a comment\nmodule;\nfrob} } }
+// { dg-final { scan-file dir-only-2_b.i {frob\nexport\nimport foo;\nimport 
7;} } }
+// { dg-final { scan-file dir-only-2_b.i {import 
"[^\n]*/dir-only-2_a.H";\nimport "[^\n]*/dir-only-2_a.H";\nX} } }
+// { dg-final { scan-file dir-only-2_b.i {export module bob;\n\nexport import 
q;} } }
+// { dg-final { scan-file dir-only-2_b.i {import sing;\n\n\n// comment} } }
diff --git a/gcc/testsuite/g++.dg/modules/pr99050_b.C 
b/gcc/testsuite/g++.dg/modules/pr99050_b.C
index 439e216eb166..f2c956313219 100644
--- a/gcc/testsuite/g++.dg/modules/pr99050_b.C
+++ b/gcc/testsuite/g++.dg/modules/pr99050_b.C
@@ -4,4 +4,4 @@
 
 int main () {}
 
-// { dg-final { scan-file pr99050_b.i {import  "[^\n]*99050_a.H" 
\[\[__translated\]\];\n} }  }
+// { dg-final { scan-file pr99050_b.i {import "[^\n]*99050_a.H" 
\[\[__translated\]\];\n} }  }
diff --git a/libcpp/lex.cc b/libcpp/lex.cc
index 40c53181bfc9..f3feadf9352f 100644
--- a/libcpp/lex.cc
+++ b/libcpp/lex.cc
@@ -4411,14 +4411,17 @@ cpp_output_token (const cpp_token *token, FILE *fp)
       {
        size_t i;
        const unsigned char * name = NODE_NAME (token->val.node.node);
-       
-       for (i = 0; i < NODE_LEN (token->val.node.node); i++)
+       unsigned len = NODE_LEN (token->val.node.node);
+
+       for (i = 0; i < len; i++)
          if (name[i] & ~0x7F)
            {
              unsigned char buffer[10];
              i += utf8_to_ucn (buffer, name + i) - 1;
              fwrite (buffer, 1, 10, fp);
            }
+         else if (name[i] == ' ' && i == len - 1)
+           /* Omit terminal space in "export ".  */;
          else
            fputc (NODE_NAME (token->val.node.node)[i], fp);
       }
diff --git a/gcc/testsuite/g++.dg/modules/inc-xlate-1_b.H 
b/gcc/testsuite/g++.dg/modules/inc-xlate-1_b.H
index 50b27efa7ef7..872ebf3de989 100644
--- a/gcc/testsuite/g++.dg/modules/inc-xlate-1_b.H
+++ b/gcc/testsuite/g++.dg/modules/inc-xlate-1_b.H
@@ -18,4 +18,4 @@ extern "C" {
 #endif
 #endif
 
-// { dg-final { scan-file inc-xlate-1_b.i {import  "[^\n]*inc-xlate-1_a.H" 
\[\[__translated\]\];\n} } }
+// { dg-final { scan-file inc-xlate-1_b.i {import "[^\n]*inc-xlate-1_a.H" 
\[\[__translated\]\];\n} } }
diff --git a/gcc/testsuite/g++.dg/modules/legacy-3_b.H 
b/gcc/testsuite/g++.dg/modules/legacy-3_b.H
index 3bf819db6616..47ed31ce4a98 100644
--- a/gcc/testsuite/g++.dg/modules/legacy-3_b.H
+++ b/gcc/testsuite/g++.dg/modules/legacy-3_b.H
@@ -12,5 +12,5 @@ int move (int X = __LINE__); // Capture __LINE__ in a 
non-definition
 // this should not be diverted
 #include "legacy-3.h"
 
-// { dg-final { scan-file legacy-3_b.i {\n# 9 "[^\n]*legacy-3_b.H"\nimport  
"[^\n]*legacy-3_a.H" \[\[__translated\]\];\nint move \(int X = 10\);\n} } }
+// { dg-final { scan-file legacy-3_b.i {\n# 9 "[^\n]*legacy-3_b.H"\nimport 
"[^\n]*legacy-3_a.H" \[\[__translated\]\];\nint move \(int X = 10\);\n} } }
 
diff --git a/gcc/testsuite/g++.dg/modules/legacy-3_c.H 
b/gcc/testsuite/g++.dg/modules/legacy-3_c.H
index 56b817229976..63c9bcf70788 100644
--- a/gcc/testsuite/g++.dg/modules/legacy-3_c.H
+++ b/gcc/testsuite/g++.dg/modules/legacy-3_c.H
@@ -17,7 +17,7 @@ MARK3 __LINE__
 // this should not be translated
 #include "legacy-3.h"
 
-// { dg-final { scan-file legacy-3_c.i {MARK1 8\nimport  "[^\n]*legacy-3_a.H" 
\[\[__translated\]\];\nMARK2 10\n} } }
+// { dg-final { scan-file legacy-3_c.i {MARK1 8\nimport "[^\n]*legacy-3_a.H" 
\[\[__translated\]\];\nMARK2 10\n} } }
 // We should have stopped.
 // { dg-final { scan-file legacy-3_c.i {move} } }
 // { dg-final { scan-file legacy-3_c.i {MARK2 13\n\nMARK3 15\n} } }

Reply via email to