]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libcpp: avoid extra spaces in module preprocessing
authorJason Merrill <jason@redhat.com>
Tue, 8 Oct 2024 22:26:40 +0000 (18:26 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 14 Oct 2024 22:42:18 +0000 (18:42 -0400)
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.

gcc/testsuite/g++.dg/modules/cpp-2_c.C
gcc/testsuite/g++.dg/modules/cpp-5_c.C
gcc/testsuite/g++.dg/modules/dep-2.C
gcc/testsuite/g++.dg/modules/dir-only-2_b.C
gcc/testsuite/g++.dg/modules/inc-xlate-1_b.H
gcc/testsuite/g++.dg/modules/legacy-3_b.H
gcc/testsuite/g++.dg/modules/legacy-3_c.H
gcc/testsuite/g++.dg/modules/pr99050_b.C
libcpp/lex.cc

index c6e02b7800ad658eb77cb3c82f9ff6bce8b3d416..2a79031eb34562015f6207c0f9bb66a91d0f3f42 100644 (file)
@@ -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" } }
index e0a78a516ae34ed62066b007bc564b7503555d66..7f75a228cda70eaf753107539ce31750a3c17e9c 100644 (file)
@@ -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} } }
index 2dccab3554d0a9a0450bc76ace725280a17f90e6..3c869755785a5ec36f583a378f7f34075e7db13a 100644 (file)
@@ -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} } }
index 1009ae5b3a263b2038ec0004f282af2e8bf4c3a3..b1ef9b95e5877e56feba758f0068bb7f015eb4a3 100644 (file)
@@ -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} } }
index 50b27efa7ef7a5a54d4c4b6b8b9e3ddc8cbe8189..872ebf3de98979b538fe0c85f6c84b6857c96886 100644 (file)
@@ -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} } }
index 3bf819db66162607870406b1c92af70a272b502c..47ed31ce4a986d893aa3c36d29959f2e857ca33d 100644 (file)
@@ -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} } }
 
index 56b81722997645a8e7363db011d5ea20d2355e75..63c9bcf70788720c21ccfc7153aee09cf63c53b8 100644 (file)
@@ -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} } }
index 439e216eb166cb28f16ee49e5f272a144d674989..f2c956313219ffa08171fe170248ff240842ef0f 100644 (file)
@@ -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} }  }
index 40c53181bfc9c704c85e141c1a7918b0f800ad84..f3feadf9352facc153403b9150d0634be24252e8 100644 (file)
@@ -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);
       }