cppspec.c 5.3 KB

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