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