#! /bin/sh -e

# DP: Backport from trunk: -fdirectives-only (when preprocessing,
# DP: handle directives, but do not expand macros).

dir=
if [ $# -eq 3 -a "$2" = '-d' ]; then
    pdir="-d $3"
    dir="$3/"
elif [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch)
        patch $pdir -f --no-backup-if-mismatch -p0 < $0
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p0 < $0
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

Backport to 4.1, revert libcpp/macro.c

gcc/

2007-07-30  Ollie Wild  <aaw@google.com>

	* c-ppoutput.c (print_lines_directives_only): New function.
	(scan_translation_unit_directives_only): New function.
	(preprocess_file): Add call to scan_translation_unit_directives_only.
	* c-opts.c (c_common_handle_option): Add OPT_fdirectives_only.
	(sanitize_cpp_opts): Add default flag_dump_macros setting for
	-fdirectives-only.  Add errors for -fdirectives-only conflict with
	-Wunused-macros and -traditional.
	(finish_options): Add builtin macro initialization for
	-fdirectives-only + -fpreprocessed.
	* c.opt (fdirectives-only): New.
	* doc/cppopts.texi (fdirectives-only): New.

2007-08-23  Ollie Wild  <aaw@google.com> 
 
	* c-opts.c (c_common_handle_option): Support -fno-directives-only. 
	* gcc.c (default_compilers): Add -fno-directives-only to 
	@assembler-with-cpp. 

libcpp/

2007-07-30  Ollie Wild  <aaw@google.com>

	* directives-only.c: New file.
	* internal.h (struct _cpp_dir_only_callbacks): New.
	(_cpp_preprocess_dir_only): New function.
	* directives.c (_cpp_handle_directive): Check directives_only before
	disabling execution of indented directives.
	* files.c (_cpp_stack_file): Add directives_only check.
	* include/cpplib.h (struct cpp_options): Add directives_only.
	(cpp_init_special_builtins): New function.
	* init.c (cpp_init_special_builtins): New function.
	(cpp_init_builtins): Move builtin_array initialization to
	cpp_init_special_builtins.
	(post_options): Check directives_only before setting
	pfile->state.prevent_expansion = 1.
	* macro.c (_cpp_builtin_macro_text): Print an error if __COUNTER__
	is expanded inside a directive while -fdirectives-only is enabled.
	* Makefile.in (libcpp_a_OBJS): Add directives-only.o.
	(libcpp_a_SOURCES): Add directives-only.c.

gcc/testsuite/

2007-07-30  Ollie Wild  <aaw@google.com>

	* gcc.dg/cpp/dir-only-1.c: New test.
	* gcc.dg/cpp/dir-only-1.h: New file.
	* gcc.dg/cpp/dir-only-2.c: New test.
	* gcc.dg/cpp/dir-only-3.c: New test.
	* gcc.dg/cpp/dir-only-3a.h: New file.
	* gcc.dg/cpp/dir-only-3b.h: New file.
	* gcc.dg/cpp/dir-only-4.c: New test.
	* gcc.dg/cpp/dir-only-5.c: New test.
	* gcc.dg/cpp/dir-only-6.c: New test.

2007-05-22  Janis Johnson  <janis187@us.ibm.com>
	    Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

	* lib/gcc-dg.exp(process_message): New proc.
	(dg-message): New test directive.


Index: gcc/testsuite/lib/gcc-dg.exp
===================================================================
--- gcc/testsuite/lib/gcc-dg.exp	(revision 124951)
+++ gcc/testsuite/lib/gcc-dg.exp	(revision 124952)
@@ -559,4 +559,40 @@
     }
 }
 
+# Modify the regular expression saved by a DejaGnu message directive to
+# include a prefix and to force the expression to match a single line.
+# MSGPROC is the procedure to call.
+# MSGPREFIX is the prefix to prepend.
+# DGARGS is the original argument list.
+
+proc process-message { msgproc msgprefix dgargs } {
+    upvar dg-messages dg-messages
+
+    # Process the dg- directive, including adding the regular expression
+    # to the new message entry in dg-messages.
+    set msgcnt [llength ${dg-messages}]
+    catch { eval $msgproc $dgargs }
+
+    # If the target expression wasn't satisfied there is no new message.
+    if { [llength ${dg-messages}] == $msgcnt } {
+	return;
+    }
+
+    # Prepend the message prefix to the regular expression and make
+    # it match a single line.
+    set newentry [lindex ${dg-messages} end]
+    set expmsg [lindex $newentry 2]
+    set expmsg "$msgprefix\[^\n]*$expmsg"
+    set newentry [lreplace $newentry 2 2 $expmsg]
+    set dg-messages [lreplace ${dg-messages} end end $newentry]
+    verbose "process-message:\n${dg-messages}" 2
+}
+
+# Look for messages that don't have standard prefixes.
+
+proc dg-message { args } {
+    upvar dg-messages dg-messages
+    process-message dg-warning "" $args
+}
+
 set additional_prunes ""
Index: gcc/testsuite/gcc.dg/cpp/dir-only-1.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-1.c	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-1.c	(revision 0)
@@ -0,0 +1,73 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options -fdirectives-only } */
+
+/* Tests scan_translation_unit_directives_only()'s handling of corner cases. */
+
+/* Ignore directives inside block comments...
+#error directive inside block comment
+*/
+
+// Escaped newline doesn't terminate line comment \
+#error directive inside line comment
+
+/* A comment canot start inside a string. */
+const char *c1 = "/*";
+#define NOT_IN_COMMENT
+const char *c2 = "*/";
+#ifndef NOT_IN_COMMENT
+#error Comment started inside a string literal
+#endif
+
+/* Escaped newline handling. */
+int i; \
+#error ignored escaped newline
+  \
+  \
+#define BOL
+#ifndef BOL
+#error escaped newline did not preserve beginning of line
+#endif
+
+/* Handles \\ properly at the end of a string. */
+"string ends in \\"/*
+#error Missed string terminator.
+*/
+
+/* Handles macro expansion in preprocessing directives. */
+#define HEADER "dir-only-1.h"
+#include HEADER
+#ifndef GOT_HEADER
+#error Failed to include header.
+#endif
+
+/\
+*
+#define IN_COMMENT
+*/
+#ifdef IN_COMMENT
+#error Escaped newline breaks block comment initiator.
+#endif
+
+/*
+*\
+/
+#define NOT_IN_COMMENT2
+/**/
+#ifndef NOT_IN_COMMENT2
+#error Escaped newline breaks block comment terminator.
+#endif
+
+/* Test escaped newline inside character escape sequence. */
+"\\
+\"/*
+#error Missed string terminator
+*/
+
+/* Block comments don't mask trailing preprocessing
+   directive. */ #define NOT_MASKED
+#ifndef NOT_MASKED
+#error Comment masks trailing directive.
+#endif
Index: gcc/testsuite/gcc.dg/cpp/dir-only-1.h
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-1.h	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-1.h	(revision 0)
@@ -0,0 +1,3 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+#define GOT_HEADER
Index: gcc/testsuite/gcc.dg/cpp/dir-only-2.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-2.c	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-2.c	(revision 0)
@@ -0,0 +1,12 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fpreprocessed -fdirectives-only -DNOT_SET" } */
+
+/* Tests -fdirectives-only + -fpreprocessed. */
+
+/* Check this is not defined. */
+#ifdef NOT_SET
+#error Command line macro not disabled.
+#endif
Index: gcc/testsuite/gcc.dg/cpp/dir-only-3.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-3.c	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-3.c	(revision 0)
@@ -0,0 +1,13 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fdirectives-only -H" } */
+/* { dg-message "dir-only-3a\.h\n\[^\n\]*dir-only-3b\.h\n\[^\n\]*dir-only-3a\.h\n" "include guard check" { target *-*-* } 0 } */
+
+/* Tests include guards. */
+
+#include "dir-only-3a.h"
+#include "dir-only-3b.h"
+#include "dir-only-3b.h"
+#include "dir-only-3a.h"
Index: gcc/testsuite/gcc.dg/cpp/dir-only-4.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-4.c	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-4.c	(revision 0)
@@ -0,0 +1,6 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fdirectives-only -Wunused-macros" } */
+/* { dg-error "-fdirectives-only is incompatible with -Wunused_macros\n" "-Wunused-macros check" { target *-*-* } 0 } */
Index: gcc/testsuite/gcc.dg/cpp/dir-only-3a.h
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-3a.h	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-3a.h	(revision 0)
@@ -0,0 +1,8 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+extern int outside_guard
+
+#ifndef DIR_ONLY_3A_H
+#define DIR_ONLY_3A_H
+#endif
Index: gcc/testsuite/gcc.dg/cpp/dir-only-5.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-5.c	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-5.c	(revision 0)
@@ -0,0 +1,6 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fdirectives-only -traditional" } */
+/* { dg-error "-fdirectives-only is incompatible with -traditional\n" "-traditional check" { target *-*-* } 0 } */
Index: gcc/testsuite/gcc.dg/cpp/dir-only-3b.h
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-3b.h	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-3b.h	(revision 0)
@@ -0,0 +1,9 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+#ifndef DIR_ONLY_3B_H
+#define DIR_ONLY_3B_H
+
+extern int inside guard;
+
+#endif
Index: gcc/testsuite/gcc.dg/cpp/dir-only-6.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/dir-only-6.c	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/dir-only-6.c	(revision 0)
@@ -0,0 +1,7 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options -fdirectives-only } */
+
+/* { dg-error "unterminated comment" }
Index: gcc/c.opt
===================================================================
--- gcc/c.opt	(revision 127319)
+++ gcc/c.opt	(working copy)
@@ -477,6 +477,10 @@
 C++ ObjC++
 Inline member functions by default
 
+fdirectives-only
+C ObjC C++ ObjC++
+Preprocess directives only.
+
 fdollars-in-identifiers
 C ObjC C++ ObjC++
 Permit '$' as an identifier character
Index: gcc/c-opts.c
===================================================================
--- gcc/c-opts.c	(revision 127319)
+++ gcc/c-opts.c	(working copy)
@@ -599,6 +599,10 @@
 	disable_builtin_function (arg);
       break;
 
+    case OPT_fdirectives_only:
+      cpp_opts->directives_only = 1;
+      break;
+
     case OPT_fdollars_in_identifiers:
       cpp_opts->dollars_in_ident = value;
       break;
@@ -1289,6 +1293,11 @@
   if (flag_dump_macros == 'M')
     flag_no_output = 1;
 
+  /* By default, -fdirectives-only implies -dD.  This allows subsequent phases
+     to perform proper macro expansion.  */
+  if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
+    flag_dump_macros = 'D';
+
   /* Disable -dD, -dN and -dI if normal output is suppressed.  Allow
      -dM since at least glibc relies on -M -dM to work.  */
   /* Also, flag_no_output implies flag_no_line_commands, always.  */
@@ -1319,6 +1328,14 @@
      actually output the current directory?  */
   if (flag_working_directory == -1)
     flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
+
+  if (cpp_opts->directives_only)
+    {
+      if (warn_unused_macros)
+	error ("-fdirectives-only is incompatible with -Wunused_macros");
+      if (cpp_opts->traditional)
+	error ("-fdirectives-only is incompatible with -traditional");
+    }
 }
 
 /* Add include path with a prefix at the front of its name.  */
@@ -1399,6 +1416,8 @@
 	    }
 	}
     }
+  else if (cpp_opts->directives_only)
+    cpp_init_special_builtins (parse_in);
 
   include_cursor = 0;
   push_command_line_include ();
Index: gcc/c-ppoutput.c
===================================================================
--- gcc/c-ppoutput.c	(revision 127319)
+++ gcc/c-ppoutput.c	(working copy)
@@ -41,6 +41,8 @@
 
 /* General output routines.  */
 static void scan_translation_unit (cpp_reader *);
+static void print_lines_directives_only (int, const void *, size_t);
+static void scan_translation_unit_directives_only (cpp_reader *);
 static void scan_translation_unit_trad (cpp_reader *);
 static void account_for_newlines (const unsigned char *, size_t);
 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
@@ -75,6 +77,9 @@
     }
   else if (cpp_get_options (pfile)->traditional)
     scan_translation_unit_trad (pfile);
+  else if (cpp_get_options (pfile)->directives_only
+	   && !cpp_get_options (pfile)->preprocessed)
+    scan_translation_unit_directives_only (pfile);
   else
     scan_translation_unit (pfile);
 
@@ -179,6 +184,26 @@
     }
 }
 
+static void
+print_lines_directives_only (int lines, const void *buf, size_t size)
+{
+  print.src_line += lines;
+  fwrite (buf, 1, size, print.outf);
+}
+
+/* Writes out the preprocessed file, handling spacing and paste
+   avoidance issues.  */
+static void
+scan_translation_unit_directives_only (cpp_reader *pfile)
+{
+  struct _cpp_dir_only_callbacks cb;
+
+  cb.print_lines = print_lines_directives_only;
+  cb.maybe_print_line = maybe_print_line;
+
+  _cpp_preprocess_dir_only (pfile, &cb);
+}
+
 /* Adjust print.src_line for newlines embedded in output.  */
 static void
 account_for_newlines (const unsigned char *str, size_t len)
Index: libcpp/directives.c
===================================================================
--- libcpp/directives.c	(revision 127319)
+++ libcpp/directives.c	(working copy)
@@ -427,8 +427,13 @@
 	 does not cause '#define foo bar' to get executed when
 	 compiled with -save-temps, we recognize directives in
 	 -fpreprocessed mode only if the # is in column 1.  macro.c
-	 puts a space in front of any '#' at the start of a macro.  */
+	 puts a space in front of any '#' at the start of a macro.
+	 
+	 We exclude the -fdirectives-only case because macro expansion
+	 has not been performed yet, and block comments can cause spaces
+	 to preceed the directive.  */
       if (CPP_OPTION (pfile, preprocessed)
+	  && !CPP_OPTION (pfile, directives_only)
 	  && (indented || !(dir->flags & IN_I)))
 	{
 	  skip = 0;
Index: libcpp/Makefile.in
===================================================================
--- libcpp/Makefile.in	(revision 127319)
+++ libcpp/Makefile.in	(working copy)
@@ -69,14 +69,14 @@
 
 ALL_CFLAGS = $(CFLAGS) $(WARN_CFLAGS) $(INCLUDES) $(CPPFLAGS)
 
-libcpp_a_OBJS = charset.o directives.o errors.o expr.o files.o \
-	identifiers.o init.o lex.o line-map.o macro.o mkdeps.o \
-	pch.o symtab.o traditional.o
+libcpp_a_OBJS = charset.o directives.o directives-only.o errors.o \
+	expr.o files.o identifiers.o init.o lex.o line-map.o macro.o \
+	mkdeps.o pch.o symtab.o traditional.o
 makedepend_OBJS = makedepend.o
 
-libcpp_a_SOURCES = charset.c directives.c errors.c expr.c files.c \
-	identifiers.c init.c lex.c line-map.c macro.c mkdeps.c \
-	pch.c symtab.c traditional.c
+libcpp_a_SOURCES = charset.c directives.c directives-only.c errors.c \
+	expr.c files.c identifiers.c init.c lex.c line-map.c macro.c \
+	mkdeps.c pch.c symtab.c traditional.c
 
 all: libcpp.a makedepend$(EXEEXT) $(USED_CATALOGS)
 
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp/include/cpplib.h	(revision 127319)
+++ libcpp/include/cpplib.h	(working copy)
@@ -438,6 +438,9 @@
 
   /* True means error callback should be used for diagnostics.  */
   bool client_diagnostic;
+
+  /* True disables tokenization outside of preprocessing directives. */
+  bool directives_only;
 };
 
 /* Callback for header lookup for HEADER, which is the name of a
@@ -640,6 +643,10 @@
    too.  If there was an error opening the file, it returns NULL.  */
 extern const char *cpp_read_main_file (cpp_reader *, const char *);
 
+/* Set up built-ins with special behavior.  Use cpp_init_builtins()
+   instead unless your know what you are doing.  */
+extern void cpp_init_special_builtins (cpp_reader *);
+
 /* Set up built-ins like __FILE__.  */
 extern void cpp_init_builtins (cpp_reader *, int);
 
Index: libcpp/files.c
===================================================================
--- libcpp/files.c	(revision 127319)
+++ libcpp/files.c	(working copy)
@@ -722,7 +722,8 @@
 
   /* Stack the buffer.  */
   buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size,
-			    CPP_OPTION (pfile, preprocessed));
+			    CPP_OPTION (pfile, preprocessed)
+			    && !CPP_OPTION (pfile, directives_only));
   buffer->file = file;
   buffer->sysp = sysp;
 
Index: libcpp/init.c
===================================================================
--- libcpp/init.c	(revision 127319)
+++ libcpp/init.c	(working copy)
@@ -346,11 +346,8 @@
     }
 }
 
-/* Read the builtins table above and enter them, and language-specific
-   macros, into the hash table.  HOSTED is true if this is a hosted
-   environment.  */
 void
-cpp_init_builtins (cpp_reader *pfile, int hosted)
+cpp_init_special_builtins (cpp_reader *pfile)
 {
   const struct builtin *b;
   size_t n = ARRAY_SIZE (builtin_array);
@@ -359,10 +356,7 @@
     n -= 2;
   else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
 	   || CPP_OPTION (pfile, std))
-    {
-      n--;
-      _cpp_define_builtin (pfile, "__STDC__ 1");
-    }
+    n--;
 
   for (b = builtin_array; b < builtin_array + n; b++)
     {
@@ -371,7 +365,21 @@
       hp->flags |= NODE_BUILTIN | NODE_WARN;
       hp->value.builtin = (enum builtin_type) b->value;
     }
+}
 
+/* Read the builtins table above and enter them, and language-specific
+   macros, into the hash table.  HOSTED is true if this is a hosted
+   environment.  */
+void
+cpp_init_builtins (cpp_reader *pfile, int hosted)
+{
+  cpp_init_special_builtins (pfile);
+
+  if (!CPP_OPTION (pfile, traditional)
+      && (! CPP_OPTION (pfile, stdc_0_in_system_headers)
+	  || CPP_OPTION (pfile, std)))
+    _cpp_define_builtin (pfile, "__STDC__ 1");
+
   if (CPP_OPTION (pfile, cplusplus))
     _cpp_define_builtin (pfile, "__cplusplus 1");
   else if (CPP_OPTION (pfile, lang) == CLK_ASM)
@@ -618,7 +626,8 @@
      preprocessed text.  Read preprocesed source in ISO mode.  */
   if (CPP_OPTION (pfile, preprocessed))
     {
-      pfile->state.prevent_expansion = 1;
+      if (!CPP_OPTION (pfile, directives_only))
+	pfile->state.prevent_expansion = 1;
       CPP_OPTION (pfile, traditional) = 0;
     }
 
Index: libcpp/directives-only.c
===================================================================
--- libcpp/directives-only.c	(revision 0)
+++ libcpp/directives-only.c	(revision 0)
@@ -0,0 +1,240 @@
+/* CPP Library - directive only preprocessing for distributed compilation.
+   Copyright (C) 2007
+   Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, 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 this program; if not, write to the Free Software
+Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#include "config.h"
+#include "system.h"
+#include "cpplib.h"
+#include "internal.h"
+
+/* DO (Directive only) flags. */
+#define DO_BOL		 (1 << 0) /* At the beginning of a logical line. */
+#define DO_STRING	 (1 << 1) /* In a string constant. */
+#define DO_CHAR		 (1 << 2) /* In a character constant. */
+#define DO_BLOCK_COMMENT (1 << 3) /* In a block comment. */
+#define DO_LINE_COMMENT	 (1 << 4) /* In a single line "//-style" comment. */
+
+#define DO_LINE_SPECIAL (DO_STRING | DO_CHAR | DO_LINE_COMMENT)
+#define DO_SPECIAL	(DO_LINE_SPECIAL | DO_BLOCK_COMMENT)
+
+/* Writes out the preprocessed file, handling spacing and paste
+   avoidance issues.  */
+void
+_cpp_preprocess_dir_only (cpp_reader *pfile,
+			  const struct _cpp_dir_only_callbacks *cb)
+{
+  struct cpp_buffer *buffer;
+  const unsigned char *cur, *base, *next_line, *rlimit;
+  cppchar_t c, last_c;
+  unsigned flags;
+  int lines, col;
+  source_location loc;
+
+ restart:
+  /* Buffer initialization ala _cpp_clean_line(). */
+  buffer = pfile->buffer;
+  buffer->cur_note = buffer->notes_used = 0;
+  buffer->cur = buffer->line_base = buffer->next_line;
+  buffer->need_line = false;
+
+  /* This isn't really needed.  It prevents a compiler warning, though. */
+  loc = pfile->line_table->highest_line;
+
+  /* Scan initialization. */
+  next_line = cur = base = buffer->cur;
+  rlimit = buffer->rlimit;
+  flags = DO_BOL;
+  lines = 0;
+  col = 1;
+
+  for (last_c = '\n', c = *cur; cur < rlimit; last_c = c, c = *++cur, ++col)
+    {
+      /* Skip over escaped newlines. */
+      if (__builtin_expect (c == '\\', false))
+	{
+	  const unsigned char *tmp = cur + 1;
+
+	  while (is_nvspace (*tmp) && tmp < rlimit)
+	    tmp++;
+	  if (*tmp == '\r')
+	    tmp++;
+	  if (*tmp == '\n' && tmp < rlimit)
+	    {
+	      CPP_INCREMENT_LINE (pfile, 0);
+	      lines++;
+	      col = 0;
+	      cur = tmp;
+	      c = last_c;
+	      continue;
+	    }
+	}
+
+      if (__builtin_expect (last_c == '#', false) && !(flags & DO_SPECIAL))
+	{
+	  if (c != '#' && (flags & DO_BOL))
+	  {
+	    struct line_maps *line_table;
+
+	    if (!pfile->state.skipping && next_line != base)
+	      cb->print_lines (lines, base, next_line - base);
+
+	    /* Prep things for directive handling. */
+	    buffer->next_line = cur;
+	    buffer->need_line = true;
+	    _cpp_get_fresh_line (pfile);
+
+	    /* Ensure proper column numbering for generated error messages. */
+	    buffer->line_base -= col - 1;
+
+	    _cpp_handle_directive (pfile, 0 /* ignore indented */);
+
+	    /* Sanitize the line settings.  Duplicate #include's can mess
+	       things up. */
+	    line_table = pfile->line_table;
+	    line_table->highest_location = line_table->highest_line;
+
+	    /* The if block prevents us from outputing line information when
+	       the file ends with a directive and no newline.  Note that we
+	       must use pfile->buffer, not buffer. */
+	    if (pfile->buffer->next_line < pfile->buffer->rlimit)
+	      cb->maybe_print_line (pfile->line_table->highest_line);
+
+	    goto restart;
+	  }
+
+	  flags &= ~DO_BOL;
+	  pfile->mi_valid = false;
+	}
+      else if (__builtin_expect (last_c == '/', false) \
+	       && !(flags & DO_SPECIAL) && c != '*' && c != '/')
+	{
+	  /* If a previous slash is not starting a block comment, clear the
+	     DO_BOL flag.  */
+	  flags &= ~DO_BOL;
+	  pfile->mi_valid = false;
+	}
+
+      switch (c)
+	{
+	case '/':
+	  if ((flags & DO_BLOCK_COMMENT) && last_c == '*')
+	    {
+	      flags &= ~DO_BLOCK_COMMENT;
+	      c = 0;
+	    }
+	  else if (!(flags & DO_SPECIAL) && last_c == '/')
+	    flags |= DO_LINE_COMMENT;
+	  else if (!(flags & DO_SPECIAL))
+	    /* Mark the position for possible error reporting. */
+	    LINEMAP_POSITION_FOR_COLUMN (loc, pfile->line_table, col);
+
+	  break;
+
+	case '*':
+	  if (!(flags & DO_SPECIAL))
+	    {
+	      if (last_c == '/')
+		flags |= DO_BLOCK_COMMENT;
+	      else
+		{
+		  flags &= ~DO_BOL;
+		  pfile->mi_valid = false;
+		}
+	    }
+
+	  break;
+
+	case '\'':
+	case '"':
+	  {
+	    unsigned state = (c == '"') ? DO_STRING : DO_CHAR;
+
+	    if (!(flags & DO_SPECIAL))
+	      {
+		flags |= state;
+		flags &= ~DO_BOL;
+		pfile->mi_valid = false;
+	      }
+	    else if ((flags & state) && last_c != '\\')
+	      flags &= ~state;
+
+	    break;
+	  }
+
+	case '\\':
+	  {
+	    if ((flags & (DO_STRING | DO_CHAR)) && last_c == '\\')
+	      c = 0;
+
+	    if (!(flags & DO_SPECIAL))
+	      {
+		flags &= ~DO_BOL;
+		pfile->mi_valid = false;
+	      }
+
+	    break;
+	  }
+
+	case '\n':
+	  CPP_INCREMENT_LINE (pfile, 0);
+	  lines++;
+	  col = 0;
+	  flags &= ~DO_LINE_SPECIAL;
+	  if (!(flags & DO_SPECIAL))
+	    flags |= DO_BOL;
+	  break;
+
+	case '#':
+	  next_line = cur;
+	  /* Don't update DO_BOL yet. */
+	  break;
+
+	case ' ': case '\t': case '\f': case '\v': case '\0':
+	  break;
+
+	default:
+	  if (!(flags & DO_SPECIAL))
+	    {
+	      flags &= ~DO_BOL;
+	      pfile->mi_valid = false;
+	    }
+	  break;
+	}
+    }
+
+  if (flags & DO_BLOCK_COMMENT)
+    cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0, "unterminated comment");
+
+  if (!pfile->state.skipping && cur != base)
+    {
+      /* If the file was not newline terminated, add rlimit, which is
+         guaranteed to point to a newline, to the end of our range.  */
+      if (cur[-1] != '\n')
+	{
+	  cur++;
+	  CPP_INCREMENT_LINE (pfile, 0);
+	  lines++;
+	}
+
+      cb->print_lines (lines, base, cur - base);
+    }
+
+  _cpp_pop_buffer (pfile);
+  if (pfile->buffer)
+    goto restart;
+}
Index: libcpp/internal.h
===================================================================
--- libcpp/internal.h	(revision 127319)
+++ libcpp/internal.h	(working copy)
@@ -550,6 +550,17 @@
 				 unsigned int, unsigned int);
 extern void _cpp_pop_buffer (cpp_reader *);
 
+/* In directives.c */
+struct _cpp_dir_only_callbacks
+{
+  /* Called to print a block of lines. */
+  void (*print_lines) (int, const void *, size_t);
+  void (*maybe_print_line) (source_location);
+};
+
+extern void _cpp_preprocess_dir_only (cpp_reader *,
+				      const struct _cpp_dir_only_callbacks *);
+
 /* In traditional.c.  */
 extern bool _cpp_scan_out_logical_line (cpp_reader *, cpp_macro *);
 extern bool _cpp_read_logical_line_trad (cpp_reader *);

diff --git a/gcc/c-opts.c b/gcc/c-opts.c
--- gcc/c-opts.c
+++ gcc/c-opts.c
@@ -619,7 +619,7 @@ c_common_handle_option (size_t scode, const char *arg, int value)
       break;
 
     case OPT_fdirectives_only:
-      cpp_opts->directives_only = 1;
+      cpp_opts->directives_only = value;
       break;
 
     case OPT_fdollars_in_identifiers:
diff --git a/gcc/gcc.c b/gcc/gcc.c
--- gcc/gcc.c
+++ gcc/gcc.c
@@ -1025,12 +1025,12 @@ static const struct compiler default_compilers[] =
   {".S", "@assembler-with-cpp", 0, 1, 0},
   {"@assembler-with-cpp",
 #ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
-   "%(trad_capable_cpp) -lang-asm %(cpp_options)\
+   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
        as %(asm_debug) %(asm_options) %|.s %A }}}}"
 #else
-   "%(trad_capable_cpp) -lang-asm %(cpp_options)\
+   "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
        as %(asm_debug) %(asm_options) %m.s %A }}}}"
