diff mbox series

[preprocessor] Popping "" file names

Message ID 8a248825-7241-c984-b1fc-28b02df3631e@acm.org
State New
Headers show
Series [preprocessor] Popping "" file names | expand

Commit Message

Nathan Sidwell Aug. 30, 2019, 1:29 p.m. UTC
On the modules branch, I needed a bunch of tests checking the 
interaction of #include nesting and the declarations therein.  Doing 
this with the current testsuite infrastructure is quite awkward.

Using
   # <digits> "name" [12]
directives works just fine, /except/ in the unnesting case.  There we 
need "name" to match the name of the main file.  But we don't know that, 
because it's /some/directory/src/gcc/..../mytest.C

Hence this change to the directive.  when unnesting, if the given 
filename is "", we simply use the filename we already expect to be 
popped to.

I've wanted this occasionally in the past, but my current requirements 
pushed me to implement this.

1) if the popped-to filename is actually "", the right thing happens 
anyway.  So existing sources using this form of line control continue to 
be accepted.

2) the IS_MAIN_FILE test about mismatches isn't needed -- if we're in 
the main file, we'll have a NULL 'from' linemap pointer.

3) We do not document this form of the line control.  Only the 
standard-mandated '#line' form, and that doesn't permit the trailing 
push/pop/system-header flags.

I intend to commit this next week, to allow for comments.

nathan
diff mbox series

Patch

2019-08-30  Nathan Sidwell  <nathan@acm.org>

	New # semantics for popping to "" name.
	libcpp/
	* directives.c (do_linemarker): Popping to "" name means get the
	name from the include stack..

Index: libcpp/directives.c
===================================================================
--- libcpp/directives.c	(revision 275032)
+++ libcpp/directives.c	(working copy)
@@ -1085,7 +1085,15 @@  do_linemarker (cpp_reader *pfile)
       const line_map_ordinary *from
 	= linemap_included_from_linemap (line_table, map);
-      if (MAIN_FILE_P (map)
-	  || (from
-	      && filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0))
+
+      if (!from)
+	/* Not nested.  */;
+      else if (!new_file[0])
+	/* Leaving to "" means fill in the popped-to name.  */
+	new_file = ORDINARY_MAP_FILE_NAME (from);
+      else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0)
+	/* It's the wrong name, Grommit!  */
+	from = NULL;
+
+      if (!from)
 	{
 	  cpp_warning (pfile, CPP_W_NONE,
@@ -1095,4 +1103,5 @@  do_linemarker (cpp_reader *pfile)
 	}
     }
+
   /* Compensate for the increment in linemap_add that occurs in
      _cpp_do_file_change.  We're currently at the start of the line
Index: gcc/testsuite/c-c++-common/cpp/line-1.c
===================================================================
--- gcc/testsuite/c-c++-common/cpp/line-1.c	(revision 0)
+++ gcc/testsuite/c-c++-common/cpp/line-1.c	(working copy)
@@ -0,0 +1,20 @@ 
+/* { dg-do preprocess } */
+/* { dg-additional-options -Wno-pedantic } */
+
+main-1 __FILE__
+
+# 7 "inner.h" 1
+inner-1 __FILE__
+# 9 "inside.h" 1
+inside-1 __FILE__
+# 11 "" 2
+inner-2 __FILE__
+#13 "" 2
+main-2 __FILE__
+
+
+/* { dg-final { scan-file line-1.i "main-1 \"\[^\n]*line-1.c\"\n" } } */
+/* { dg-final { scan-file line-1.i "main-2 \"\[^\n]*line-1.c\"\n" } } */
+/* { dg-final { scan-file line-1.i "inner-1 \"inner.h\"\n" } } */
+/* { dg-final { scan-file line-1.i "inner-2 \"inner.h\"\n" } } */
+/* { dg-final { scan-file line-1.i "inside-1 \"inside.h\"\n" } } */