xref: /dragonfly/contrib/gcc-4.7/gcc/cppspec.c (revision ec21d9fb)
1 /* Specific flags and argument handling of the C preprocessor.
2    Copyright (C) 1999, 2007, 2010, 2011 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "gcc.h"
25 #include "opts.h"
26 
27 /* The `cpp' executable installed in $(bindir) and $(cpp_install_dir)
28    is a customized version of the gcc driver.  It forces -E; -S and -c
29    are errors.  It defaults to -x c for files with unrecognized
30    extensions, unless -x options appear in argv, in which case we
31    assume the user knows what they're doing.  If no explicit input is
32    mentioned, it will read stdin.  */
33 
34 /* Suffixes for known sorts of input files.  Note that we do not list
35    files which are normally considered to have been preprocessed already,
36    since the user's expectation is that `cpp' always preprocesses.  */
37 static const char *const known_suffixes[] =
38 {
39   ".c",  ".C",   ".S",   ".m",
40   ".cc", ".cxx", ".cpp", ".cp",  ".c++",
41   ".sx",
42   NULL
43 };
44 
45 /* Filter the command line before processing by the gcc driver proper.  */
46 void
47 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
48 		      unsigned int *in_decoded_options_count,
49 		      int *in_added_libraries ATTRIBUTE_UNUSED)
50 {
51   struct cl_decoded_option *decoded_options = *in_decoded_options;
52   unsigned int argc = *in_decoded_options_count;
53 
54   /* Do we need to read stdin? */
55   int read_stdin = 1;
56 
57   /* Do we need to insert -E? */
58   int need_E = 1;
59 
60   /* Have we seen an input file? */
61   int seen_input = 0;
62 
63   /* Positions to insert -xc, -xassembler-with-cpp, and -o, if necessary.
64      0 means unnecessary.  */
65   unsigned int lang_c_here = 0;
66   unsigned int lang_S_here = 0;
67   unsigned int o_here = 0;
68 
69   /* Do we need to fix up an input file with an unrecognized suffix? */
70   int need_fixups = 1;
71 
72   unsigned int i, j;
73   struct cl_decoded_option *new_decoded_options;
74   unsigned int new_argc;
75   extern int is_cpp_driver;
76 
77   is_cpp_driver = 1;
78 
79   /* First pass.  If we see an -S or -c, barf.  If we see an input file,
80      turn off read_stdin.  If we see a second input file, it is actually
81      the output file.  If we see a third input file, barf.  */
82   for (i = 1; i < argc; i++)
83     {
84       switch (decoded_options[i].opt_index)
85 	{
86 	case OPT_E:
87 	  need_E = 0;
88 	  break;
89 
90 	case OPT_S:
91 	case OPT_c:
92 	  fatal_error ("%qs is not a valid option to the preprocessor",
93 		       decoded_options[i].orig_option_with_args_text);
94 	  return;
95 
96 	case OPT_x:
97 	  need_fixups = 0;
98 	  break;
99 
100 	case OPT_SPECIAL_input_file:
101 	  {
102 	    const char *file = decoded_options[i].arg;
103 
104 	    if (strcmp (file, "-") == 0)
105 	      read_stdin = 0;
106 	    else
107 	      {
108 		seen_input++;
109 		if (seen_input == 3)
110 		  {
111 		    fatal_error ("too many input files");
112 		    return;
113 		  }
114 		else if (seen_input == 2)
115 		  {
116 		    o_here = i;
117 		  }
118 		else
119 		  {
120 		    read_stdin = 0;
121 		    if (need_fixups)
122 		      {
123 			int l = strlen (file);
124 			int known = 0;
125 			const char *const *suff;
126 
127 			for (suff = known_suffixes; *suff; suff++)
128 			  if (!strcmp (*suff, &file[l - strlen(*suff)]))
129 			    {
130 			      known = 1;
131 			      break;
132 			    }
133 
134 			if (! known)
135 			  {
136 			    /* .s files are a special case; we have to
137 			       treat them like .S files so
138 			       -D__ASSEMBLER__ will be in effect.  */
139 			    if (!strcmp (".s", &file[l - 2]))
140 			      lang_S_here = i;
141 			    else
142 			      lang_c_here = i;
143 			  }
144 		      }
145 		  }
146 	      }
147 	  }
148 	  break;
149 	}
150     }
151 
152   /* If we don't need to edit the command line, we can bail early.  */
153 
154   new_argc = argc + need_E + read_stdin + !!lang_c_here + !!lang_S_here;
155 
156   if (new_argc == argc && !o_here)
157     return;
158 
159   new_decoded_options = XNEWVEC (struct cl_decoded_option, new_argc);
160 
161   new_decoded_options[0] = decoded_options[0];
162   j = 1;
163 
164   if (need_E)
165     generate_option (OPT_E, NULL, 1, CL_DRIVER, &new_decoded_options[j++]);
166 
167   for (i = 1; i < argc; i++, j++)
168     {
169       if (i == lang_c_here)
170 	generate_option (OPT_x, "c", 1, CL_DRIVER, &new_decoded_options[j++]);
171       else if (i == lang_S_here)
172 	generate_option (OPT_x, "assembler-with-cpp", 1, CL_DRIVER,
173 			 &new_decoded_options[j++]);
174       else if (i == o_here)
175 	{
176 	  generate_option (OPT_o, decoded_options[i].arg, 1, CL_DRIVER,
177 			   &new_decoded_options[j]);
178 	  continue;
179 	}
180 
181       new_decoded_options[j] = decoded_options[i];
182     }
183 
184   if (read_stdin)
185     generate_option_input_file ("-", &new_decoded_options[j++]);
186 
187   *in_decoded_options_count = new_argc;
188   *in_decoded_options = new_decoded_options;
189 }
190 
191 /* Called before linking.  Returns 0 on success and -1 on failure.  */
192 int lang_specific_pre_link (void)
193 {
194   return 0;  /* Not used for cpp.  */
195 }
196 
197 /* Number of extra output files that lang_specific_pre_link may generate.  */
198 int lang_specific_extra_outfiles = 0;  /* Not used for cpp.  */
199