Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

I noticed that the .gcms from compile-std1.C were sticking around and
confusing other tests; this patch enhances dg-module-cmi to understand
<header> and adds the appropriate directives to the test.

gcc/testsuite/ChangeLog:

        * g++.dg/modules/compile-std1.C: Add dg-module-cmi lines.
        * g++.dg/modules/modules.exp (host_header_path): New.
        (munge_cmi): Factor out...
        (dg-module-cmi): ...from here.
---
 gcc/testsuite/g++.dg/modules/compile-std1.C |  3 ++
 gcc/testsuite/g++.dg/modules/modules.exp    | 54 ++++++++++++++++-----
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/gcc/testsuite/g++.dg/modules/compile-std1.C 
b/gcc/testsuite/g++.dg/modules/compile-std1.C
index a7972be33c1..a03a87569a8 100644
--- a/gcc/testsuite/g++.dg/modules/compile-std1.C
+++ b/gcc/testsuite/g++.dg/modules/compile-std1.C
@@ -1,5 +1,8 @@
 // { dg-additional-options "-fmodules --compile-std-module -g -O" }
 // { dg-do compile { target c++20 } }
+// { dg-module-cmi std }
+// { dg-module-cmi std.compat }
+// { dg-module-cmi <bits/stdc++.h> }
 
 import <bits/stdc++.h>;
 import std;
diff --git a/gcc/testsuite/g++.dg/modules/modules.exp 
b/gcc/testsuite/g++.dg/modules/modules.exp
index 3f2b8f2d576..695bbe62aa2 100644
--- a/gcc/testsuite/g++.dg/modules/modules.exp
+++ b/gcc/testsuite/g++.dg/modules/modules.exp
@@ -54,6 +54,43 @@ global module_cmis
 
 set DEFAULT_REPO "gcm.cache"
 
+# Return the pathname for <HNAME>.
+# On failure this will return garbage that results in the understandable
+# FAIL: g++.dg/modules/compile-std1.C module-cmi <bits/stdcx++.h> 
(gcm.cache/,/ader_path1095619.C:1:10: fatal error: bits/stdcx++.h: No such file 
or directory.gcm)
+# ??? it would be nice to have a simple --print-include-name flag.
+proc host_header_path {hname} {
+    global tool
+    set src header_path[pid].C
+    set f [open $src "w"]
+    puts $f "#include <$hname>"
+    close $f
+    set opts [list "additional_flags=-H" "additional_flags=-fdirectives-only"]
+    set lines [${tool}_target_compile $src /dev/null preprocess $opts]
+    file delete $f
+    # The first line of the -H output is ". /path/to/hname"
+    set newline_idx [expr {[string first "\n" $lines] - 1}]
+    set path [string range $lines 2 $newline_idx]
+    verbose "header_path: $path" 1
+    return $path
+}
+
+# Return the pathname CMI munged like the compiler.
+proc munge_cmi {cmi} {
+    if { [string index $cmi 0] == "/" } {
+       set cmi [string range $cmi 1 end]
+    } else {
+       set cmi ",/$cmi"
+    }
+    set path [file split $cmi]
+    # subst /../ -> /,,/
+    # sadly tcl 8.5 does not have lmap
+    set rplac {}
+    foreach elt $path {lappend rplac [expr {$elt == ".." ? ",," : $elt}]}
+    set cmi [file join {*}$rplac]
+    verbose "munge_cmi: $cmi" 1
+    return $cmi
+}
+
 # Register the module name this produces.
 # dg-module-cmi !?=?NAME WHEN?
 # dg-module-cmi !?{} - header unit
@@ -83,18 +120,11 @@ proc dg-module-cmi { args } {
        if { $name == "" } {
            # get the source file name.  ick!
            upvar prog srcname
-           set cmi "$srcname.gcm"
-           if { [string index $cmi 0] == "/" } {
-               set cmi [string range $cmi 1 end]
-           } else {
-               set cmi ",/$cmi"
-           }
-           set path [file split $cmi]
-           # subst /../ -> /,,/
-           # sadly tcl 8.5 does not have lmap
-           set rplac {}
-           foreach elt $path {lappend rplac [expr {$elt == ".." ? ",," : 
$elt}]}
-           set cmi [file join {*}$rplac]
+           set cmi [munge_cmi "$srcname.gcm"]
+       } elseif { [string index $name 0] == "<"
+                  && [string index $name end] == ">" } {
+           set header "[regsub {<(.*)>} $name {\1}]"
+           set cmi [munge_cmi "[host_header_path $header].gcm"]
        } else {
            set cmi "[regsub : $name -].gcm"
        }

base-commit: 9748d369e2cfc2b8edca9fa81127601aada23a23
-- 
2.52.0

Reply via email to