Hi,

In traditional CPP mode (-save-temps, -no-integrated-cpp, etc.), the 
compilation directory is conveyed to cc1 using a line such as:

# <line> "/path/name//"

This string literal can contain escape sequences, for instance, if the 
original source file was compiled in "/tmp/a\b", then this line will be:

# <line> "/tmp/a\\b/"

So reading the compilation directory must decode escape sequences. This last 
part is currently missing and this patch implements it.

The test added to gcc.dg/cpp/ is a .i file modeled on gcc.dg/cpp/pr36674.i, 
but it turns out that the latter was probably never exercised.

Tested on x86-64/Linux, OK for the mainline?


2025-10-10  Pierre Marie de Rodat  <[email protected]>

libcpp/
        * init.c (read_original_directory): Attempt to decode escape
        sequences with cpp_interpret_string_notranslate.


2025-10-10  Eric Botcazou <[email protected]>

gcc/testsuite/
        * gcc.dg/cpp/cpp.exp: Process .i files instead of .s files.
        * gcc.dg/cpp/pr36674.i: Pass -Wno-implicit-int.
        * gcc.dg/cpp/escape-3.i: New test.

-- 
Eric Botcazou
diff --git a/gcc/testsuite/gcc.dg/cpp/cpp.exp b/gcc/testsuite/gcc.dg/cpp/cpp.exp
index 7a359c050e4..21ca1a320b6 100644
--- a/gcc/testsuite/gcc.dg/cpp/cpp.exp
+++ b/gcc/testsuite/gcc.dg/cpp/cpp.exp
@@ -4,12 +4,12 @@
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation; either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
@@ -36,11 +36,11 @@ if ![info exists DEFAULT_CFLAGS] then {
 dg-init
 
 # Main loop.
-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.{c,S} ]] \
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.{c,i} ]] \
 	"" $DEFAULT_CFLAGS
 
 # C/C++ common tests.
-dg-runtest [lsort [glob -nocomplain $srcdir/c-c++-common/cpp/*.{c,S} ]] \
+dg-runtest [lsort [glob -nocomplain $srcdir/c-c++-common/cpp/*.{c,i} ]] \
 	" -Wc++-compat " ""
 
 
diff --git a/gcc/testsuite/gcc.dg/cpp/pr36674.i b/gcc/testsuite/gcc.dg/cpp/pr36674.i
index 9362d5a4080..f4362877239 100644
--- a/gcc/testsuite/gcc.dg/cpp/pr36674.i
+++ b/gcc/testsuite/gcc.dg/cpp/pr36674.i
@@ -1,6 +1,6 @@
 /* PR cpp/36674  #include location is offset by one row in errors from preprocessed files */
 /* { dg-do compile } */
-/* { dg-options "-fshow-column" } */
+/* { dg-options "-fshow-column -Wno-implicit-int" } */
 # 1 "gcc/testsuite/gcc.dg/pr36674.c"
 # 1 "<built-in>"
 # 1 "<command-line>"
diff --git a/libcpp/init.cc b/libcpp/init.cc
index eb495e26eff..1743ee7a2cb 100644
--- a/libcpp/init.cc
+++ b/libcpp/init.cc
@@ -893,11 +893,34 @@ read_original_directory (cpp_reader *pfile)
 
       if (pfile->cb.dir_change)
 	{
-	  /* Smash the string directly, it's dead at this point  */
-	  char *smashy = (char *)text;
-	  smashy[len - 3] = 0;
+	  cpp_string s = { 0, 0 };
+	  const char *debugdir_slashslash;
+	  unsigned int debugdir_slashslash_len;
+
+	  /* If we fail to decode escape sequences in the string literal, fall
+	     back onto the literal itself, manually removing the opening and
+	     closing quotes (").  */
+	  if (cpp_interpret_string_notranslate (pfile, &string->val.str, 1, &s,
+						CPP_STRING))
+	    {
+	      /* At this point, the trailing NUL byte in S is included in its
+		 length, so take it out.  */
+	      debugdir_slashslash = (const char *) s.text;
+	      debugdir_slashslash_len = s.len - 1;
+	    }
+	  else
+	    {
+	      debugdir_slashslash = (const char *) string->val.str.text + 1;
+	      debugdir_slashslash_len = string->val.str.len - 2;
+	    }
+
+	  /* Strip the trailing double slash.  */
+	  unsigned debugdir_len = debugdir_slashslash_len - 2;
+	  char *debugdir = (char *) alloca (debugdir_len + 1);
+	  memcpy (debugdir, debugdir_slashslash, debugdir_len);
+	  debugdir[debugdir_len] = '\0';
 
-	  pfile->cb.dir_change (pfile, smashy + 1);
+	  pfile->cb.dir_change (pfile, debugdir);
 	}
 
       /* We should be at EOL.  */
# 0 "escape-3.c"
# 0 "/some\\directory//"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "escape-3.c"

/* { dg-do compile } */
/* { dg-options "-g1" } */

int foo (int a, int b)
{
   return a + b;
}

/* Test for "/some\\directory" instead of "/some\\\\directory" */
/* { dg-final { scan-assembler "/some\\\\\\\\directory" } } */

Reply via email to