diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 5e86d193a40be425afb5107ceb6080ab31bafb75..c9836318d76e7c15cf554c9824987561681a27b9 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2005-11-09  Per Bothner  <per@bothner.com>
+	    Uros Bizjak  <uros@kss-loka.si>
+	
+	PR c/24101
+	* toplev.c (process_options): Initialize debug_hooks early
+	in case lang_hooks.post_options ends up calling a debug_hook.
+
 2005-11-08  Jakub Jelinek  <jakub@redhat.com>
 
 	* dwarf2out.c (multiple_reg_loc_descriptor): Don't assume
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 8058bb84e5c003103c4f9871e34f89e52baa0c8a..d0bd89e28e8c1c3db0c2db3c466e920fb2d0b152 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2005-11-09  Uros Bizjak  <uros@kss-loka.si>
+
+	PR c/24101
+	* gcc.dg/pr24101-1.i, gcc.dg/pr24101-2.i: New tests.
+	* gcc.dg/dg.exp: Run main loop also for *.i files.
+
 2005-11-08  Jakub Jelinek  <jakub@redhat.com>
 
 	PR c++/19450
diff --git a/gcc/testsuite/gcc.dg/dg.exp b/gcc/testsuite/gcc.dg/dg.exp
index 00b26d6e394e93f6ff1d98d768157cc38c7e591e..826bcf3f3a5e36e25ecf1194b752b346b10abbca 100644
--- a/gcc/testsuite/gcc.dg/dg.exp
+++ b/gcc/testsuite/gcc.dg/dg.exp
@@ -29,7 +29,7 @@ if ![info exists DEFAULT_CFLAGS] then {
 dg-init
 
 # Main loop.
-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cS\]]] \
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cSi\]]] \
 	"" $DEFAULT_CFLAGS
 
 # All done.
diff --git a/gcc/testsuite/gcc.dg/pr24101-1.i b/gcc/testsuite/gcc.dg/pr24101-1.i
new file mode 100644
index 0000000000000000000000000000000000000000..45c165539b61a12124b1271cbcc558475e213b23
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr24101-1.i
@@ -0,0 +1,4 @@
+/* { dg-do compile } */
+/* { dg-options "-ansi" } */
+
+# 1 "/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.0/include/stddef.h" 1 3 4
diff --git a/gcc/testsuite/gcc.dg/pr24101-2.i b/gcc/testsuite/gcc.dg/pr24101-2.i
new file mode 100644
index 0000000000000000000000000000000000000000..501382da4b680628f67e394b3b7d6f6845a3eee6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr24101-2.i
@@ -0,0 +1,4 @@
+/* { dg-do compile } */
+/* { dg-options "-ansi" } */
+
+#
diff --git a/gcc/toplev.c b/gcc/toplev.c
index eea9ab20ead66dfaab66c11de9095c1aa53f4575..b03e91dc99948bfd4278d113ebf372ad258f4292 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1493,6 +1493,10 @@ general_init (const char *argv0)
 static void
 process_options (void)
 {
+  /* Just in case lang_hooks.post_options ends up calling a debug_hook.
+     This can happen with incorrect pre-processed input. */
+  debug_hooks = &do_nothing_debug_hooks;
+
   /* Allow the front end to perform consistency checks and do further
      initialization based on the command line options.  This hook also
      sets the original filename if appropriate (e.g. foo.i -> foo.c)
@@ -1652,7 +1656,6 @@ process_options (void)
     default_debug_hooks = &vmsdbg_debug_hooks;
 #endif
 
-  debug_hooks = &do_nothing_debug_hooks;
   if (write_symbols == NO_DEBUG)
     ;
 #if defined(DBX_DEBUGGING_INFO)
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog
index 3b14dabf134357cd103a4b338c20e494965f6930..747e45adc72ee538154d042786ef4638af1649ba 100644
--- a/libcpp/ChangeLog
+++ b/libcpp/ChangeLog
@@ -1,3 +1,11 @@
+2005-11-09  Per Bothner  <per@bothner.com>
+	    Uros Bizjak  <uros@kss-loka.si>
+	
+	PR c/24101
+	* init.c (read_original_filename): Temporarily set
+	state.in_directive before calling _cpp_lex_direct for
+	CPP_HASH tokens.
+
 2005-11-03  James E Wilson  <wilson@specifix.com>
 
 	PR preprocessor/24202
diff --git a/libcpp/init.c b/libcpp/init.c
index 3074f96b5c8b9905db5232c0c9e8a43657e6cfd3..bbf2705b46c4cca516609b979ec34fb1cfca4fd8 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -497,8 +497,10 @@ read_original_filename (cpp_reader *pfile)
   token = _cpp_lex_direct (pfile);
   if (token->type == CPP_HASH)
     {
+      pfile->state.in_directive = 1;
       token1 = _cpp_lex_direct (pfile);
       _cpp_backup_tokens (pfile, 1);
+      pfile->state.in_directive = 0;
 
       /* If it's a #line directive, handle it.  */
       if (token1->type == CPP_NUMBER)