init.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /* CPP Library.
  2. Copyright (C) 1986-2015 Free Software Foundation, Inc.
  3. Contributed by Per Bothner, 1994-95.
  4. Based on CCCP program by Paul Rubin, June 1986
  5. Adapted to ANSI C, Richard Stallman, Jan 1987
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "cpplib.h"
  20. #include "internal.h"
  21. #include "mkdeps.h"
  22. #include "localedir.h"
  23. #include "filenames.h"
  24. #ifndef ENABLE_CANONICAL_SYSTEM_HEADERS
  25. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  26. #define ENABLE_CANONICAL_SYSTEM_HEADERS 1
  27. #else
  28. #define ENABLE_CANONICAL_SYSTEM_HEADERS 0
  29. #endif
  30. #endif
  31. static void init_library (void);
  32. static void mark_named_operators (cpp_reader *, int);
  33. static void read_original_filename (cpp_reader *);
  34. static void read_original_directory (cpp_reader *);
  35. static void post_options (cpp_reader *);
  36. /* If we have designated initializers (GCC >2.7) these tables can be
  37. initialized, constant data. Otherwise, they have to be filled in at
  38. runtime. */
  39. #if HAVE_DESIGNATED_INITIALIZERS
  40. #define init_trigraph_map() /* Nothing. */
  41. #define TRIGRAPH_MAP \
  42. __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
  43. #define END };
  44. #define s(p, v) [p] = v,
  45. #else
  46. #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
  47. static void init_trigraph_map (void) { \
  48. unsigned char *x = _cpp_trigraph_map;
  49. #define END }
  50. #define s(p, v) x[p] = v;
  51. #endif
  52. TRIGRAPH_MAP
  53. s('=', '#') s(')', ']') s('!', '|')
  54. s('(', '[') s('\'', '^') s('>', '}')
  55. s('/', '\\') s('<', '{') s('-', '~')
  56. END
  57. #undef s
  58. #undef END
  59. #undef TRIGRAPH_MAP
  60. /* A set of booleans indicating what CPP features each source language
  61. requires. */
  62. struct lang_flags
  63. {
  64. char c99;
  65. char cplusplus;
  66. char extended_numbers;
  67. char extended_identifiers;
  68. char c11_identifiers;
  69. char std;
  70. char digraphs;
  71. char uliterals;
  72. char rliterals;
  73. char user_literals;
  74. char binary_constants;
  75. char digit_separators;
  76. char trigraphs;
  77. };
  78. static const struct lang_flags lang_defaults[] =
  79. { /* c99 c++ xnum xid c11 std digr ulit rlit udlit bincst digsep trig */
  80. /* GNUC89 */ { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
  81. /* GNUC99 */ { 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0 },
  82. /* GNUC11 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0 },
  83. /* STDC89 */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
  84. /* STDC94 */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
  85. /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
  86. /* STDC11 */ { 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
  87. /* GNUCXX */ { 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
  88. /* CXX98 */ { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
  89. /* GNUCXX11 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0 },
  90. /* CXX11 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1 },
  91. /* GNUCXX14 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0 },
  92. /* CXX14 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  93. /* GNUCXX1Z */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0 },
  94. /* CXX1Z */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
  95. /* ASM */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  96. };
  97. /* Sets internal flags correctly for a given language. */
  98. void
  99. cpp_set_lang (cpp_reader *pfile, enum c_lang lang)
  100. {
  101. const struct lang_flags *l = &lang_defaults[(int) lang];
  102. CPP_OPTION (pfile, lang) = lang;
  103. CPP_OPTION (pfile, c99) = l->c99;
  104. CPP_OPTION (pfile, cplusplus) = l->cplusplus;
  105. CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
  106. CPP_OPTION (pfile, extended_identifiers) = l->extended_identifiers;
  107. CPP_OPTION (pfile, c11_identifiers) = l->c11_identifiers;
  108. CPP_OPTION (pfile, std) = l->std;
  109. CPP_OPTION (pfile, digraphs) = l->digraphs;
  110. CPP_OPTION (pfile, uliterals) = l->uliterals;
  111. CPP_OPTION (pfile, rliterals) = l->rliterals;
  112. CPP_OPTION (pfile, user_literals) = l->user_literals;
  113. CPP_OPTION (pfile, binary_constants) = l->binary_constants;
  114. CPP_OPTION (pfile, digit_separators) = l->digit_separators;
  115. CPP_OPTION (pfile, trigraphs) = l->trigraphs;
  116. }
  117. /* Initialize library global state. */
  118. static void
  119. init_library (void)
  120. {
  121. static int initialized = 0;
  122. if (! initialized)
  123. {
  124. initialized = 1;
  125. _cpp_init_lexer ();
  126. /* Set up the trigraph map. This doesn't need to do anything if
  127. we were compiled with a compiler that supports C99 designated
  128. initializers. */
  129. init_trigraph_map ();
  130. #ifdef ENABLE_NLS
  131. (void) bindtextdomain (PACKAGE, LOCALEDIR);
  132. #endif
  133. }
  134. }
  135. /* Initialize a cpp_reader structure. */
  136. cpp_reader *
  137. cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
  138. struct line_maps *line_table)
  139. {
  140. cpp_reader *pfile;
  141. /* Initialize this instance of the library if it hasn't been already. */
  142. init_library ();
  143. pfile = XCNEW (cpp_reader);
  144. memset (&pfile->base_context, 0, sizeof (pfile->base_context));
  145. cpp_set_lang (pfile, lang);
  146. CPP_OPTION (pfile, warn_multichar) = 1;
  147. CPP_OPTION (pfile, discard_comments) = 1;
  148. CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
  149. CPP_OPTION (pfile, tabstop) = 8;
  150. CPP_OPTION (pfile, operator_names) = 1;
  151. CPP_OPTION (pfile, warn_trigraphs) = 2;
  152. CPP_OPTION (pfile, warn_endif_labels) = 1;
  153. CPP_OPTION (pfile, cpp_warn_c90_c99_compat) = -1;
  154. CPP_OPTION (pfile, cpp_warn_deprecated) = 1;
  155. CPP_OPTION (pfile, cpp_warn_long_long) = 0;
  156. CPP_OPTION (pfile, dollars_in_ident) = 1;
  157. CPP_OPTION (pfile, warn_dollars) = 1;
  158. CPP_OPTION (pfile, warn_variadic_macros) = 1;
  159. CPP_OPTION (pfile, warn_builtin_macro_redefined) = 1;
  160. /* By default, track locations of tokens resulting from macro
  161. expansion. The '2' means, track the locations with the highest
  162. accuracy. Read the comments for struct
  163. cpp_options::track_macro_expansion to learn about the other
  164. values. */
  165. CPP_OPTION (pfile, track_macro_expansion) = 2;
  166. CPP_OPTION (pfile, warn_normalize) = normalized_C;
  167. CPP_OPTION (pfile, warn_literal_suffix) = 1;
  168. CPP_OPTION (pfile, canonical_system_headers)
  169. = ENABLE_CANONICAL_SYSTEM_HEADERS;
  170. CPP_OPTION (pfile, ext_numeric_literals) = 1;
  171. CPP_OPTION (pfile, warn_date_time) = 0;
  172. /* Default CPP arithmetic to something sensible for the host for the
  173. benefit of dumb users like fix-header. */
  174. CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
  175. CPP_OPTION (pfile, char_precision) = CHAR_BIT;
  176. CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
  177. CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
  178. CPP_OPTION (pfile, unsigned_char) = 0;
  179. CPP_OPTION (pfile, unsigned_wchar) = 1;
  180. CPP_OPTION (pfile, bytes_big_endian) = 1; /* does not matter */
  181. /* Default to no charset conversion. */
  182. CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
  183. CPP_OPTION (pfile, wide_charset) = 0;
  184. /* Default the input character set to UTF-8. */
  185. CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
  186. /* A fake empty "directory" used as the starting point for files
  187. looked up without a search path. Name cannot be '/' because we
  188. don't want to prepend anything at all to filenames using it. All
  189. other entries are correct zero-initialized. */
  190. pfile->no_search_path.name = (char *) "";
  191. /* Initialize the line map. */
  192. pfile->line_table = line_table;
  193. /* Initialize lexer state. */
  194. pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
  195. /* Set up static tokens. */
  196. pfile->avoid_paste.type = CPP_PADDING;
  197. pfile->avoid_paste.val.source = NULL;
  198. pfile->eof.type = CPP_EOF;
  199. pfile->eof.flags = 0;
  200. /* Create a token buffer for the lexer. */
  201. _cpp_init_tokenrun (&pfile->base_run, 250);
  202. pfile->cur_run = &pfile->base_run;
  203. pfile->cur_token = pfile->base_run.base;
  204. /* Initialize the base context. */
  205. pfile->context = &pfile->base_context;
  206. pfile->base_context.c.macro = 0;
  207. pfile->base_context.prev = pfile->base_context.next = 0;
  208. /* Aligned and unaligned storage. */
  209. pfile->a_buff = _cpp_get_buff (pfile, 0);
  210. pfile->u_buff = _cpp_get_buff (pfile, 0);
  211. /* Initialize table for push_macro/pop_macro. */
  212. pfile->pushed_macros = 0;
  213. /* Do not force token locations by default. */
  214. pfile->forced_token_location_p = NULL;
  215. /* The expression parser stack. */
  216. _cpp_expand_op_stack (pfile);
  217. /* Initialize the buffer obstack. */
  218. obstack_specify_allocation (&pfile->buffer_ob, 0, 0, xmalloc, free);
  219. _cpp_init_files (pfile);
  220. _cpp_init_hashtable (pfile, table);
  221. return pfile;
  222. }
  223. /* Set the line_table entry in PFILE. This is called after reading a
  224. PCH file, as the old line_table will be incorrect. */
  225. void
  226. cpp_set_line_map (cpp_reader *pfile, struct line_maps *line_table)
  227. {
  228. pfile->line_table = line_table;
  229. }
  230. /* Free resources used by PFILE. Accessing PFILE after this function
  231. returns leads to undefined behavior. Returns the error count. */
  232. void
  233. cpp_destroy (cpp_reader *pfile)
  234. {
  235. cpp_context *context, *contextn;
  236. struct def_pragma_macro *pmacro;
  237. tokenrun *run, *runn;
  238. int i;
  239. free (pfile->op_stack);
  240. while (CPP_BUFFER (pfile) != NULL)
  241. _cpp_pop_buffer (pfile);
  242. free (pfile->out.base);
  243. if (pfile->macro_buffer)
  244. {
  245. free (pfile->macro_buffer);
  246. pfile->macro_buffer = NULL;
  247. pfile->macro_buffer_len = 0;
  248. }
  249. if (pfile->deps)
  250. deps_free (pfile->deps);
  251. obstack_free (&pfile->buffer_ob, 0);
  252. _cpp_destroy_hashtable (pfile);
  253. _cpp_cleanup_files (pfile);
  254. _cpp_destroy_iconv (pfile);
  255. _cpp_free_buff (pfile->a_buff);
  256. _cpp_free_buff (pfile->u_buff);
  257. _cpp_free_buff (pfile->free_buffs);
  258. for (run = &pfile->base_run; run; run = runn)
  259. {
  260. runn = run->next;
  261. free (run->base);
  262. if (run != &pfile->base_run)
  263. free (run);
  264. }
  265. for (context = pfile->base_context.next; context; context = contextn)
  266. {
  267. contextn = context->next;
  268. free (context);
  269. }
  270. if (pfile->comments.entries)
  271. {
  272. for (i = 0; i < pfile->comments.count; i++)
  273. free (pfile->comments.entries[i].comment);
  274. free (pfile->comments.entries);
  275. }
  276. if (pfile->pushed_macros)
  277. {
  278. do
  279. {
  280. pmacro = pfile->pushed_macros;
  281. pfile->pushed_macros = pmacro->next;
  282. free (pmacro->name);
  283. free (pmacro);
  284. }
  285. while (pfile->pushed_macros);
  286. }
  287. free (pfile);
  288. }
  289. /* This structure defines one built-in identifier. A node will be
  290. entered in the hash table under the name NAME, with value VALUE.
  291. There are two tables of these. builtin_array holds all the
  292. "builtin" macros: these are handled by builtin_macro() in
  293. macro.c. Builtin is somewhat of a misnomer -- the property of
  294. interest is that these macros require special code to compute their
  295. expansions. The value is a "cpp_builtin_type" enumerator.
  296. operator_array holds the C++ named operators. These are keywords
  297. which act as aliases for punctuators. In C++, they cannot be
  298. altered through #define, and #if recognizes them as operators. In
  299. C, these are not entered into the hash table at all (but see
  300. <iso646.h>). The value is a token-type enumerator. */
  301. struct builtin_macro
  302. {
  303. const uchar *const name;
  304. const unsigned short len;
  305. const unsigned short value;
  306. const bool always_warn_if_redefined;
  307. };
  308. #define B(n, t, f) { DSC(n), t, f }
  309. static const struct builtin_macro builtin_array[] =
  310. {
  311. B("__TIMESTAMP__", BT_TIMESTAMP, false),
  312. B("__TIME__", BT_TIME, false),
  313. B("__DATE__", BT_DATE, false),
  314. B("__FILE__", BT_FILE, false),
  315. B("__BASE_FILE__", BT_BASE_FILE, false),
  316. B("__LINE__", BT_SPECLINE, true),
  317. B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true),
  318. B("__COUNTER__", BT_COUNTER, true),
  319. B("__has_attribute", BT_HAS_ATTRIBUTE, true),
  320. B("__has_cpp_attribute", BT_HAS_ATTRIBUTE, true),
  321. /* Keep builtins not used for -traditional-cpp at the end, and
  322. update init_builtins() if any more are added. */
  323. B("_Pragma", BT_PRAGMA, true),
  324. B("__STDC__", BT_STDC, true),
  325. };
  326. #undef B
  327. struct builtin_operator
  328. {
  329. const uchar *const name;
  330. const unsigned short len;
  331. const unsigned short value;
  332. };
  333. #define B(n, t) { DSC(n), t }
  334. static const struct builtin_operator operator_array[] =
  335. {
  336. B("and", CPP_AND_AND),
  337. B("and_eq", CPP_AND_EQ),
  338. B("bitand", CPP_AND),
  339. B("bitor", CPP_OR),
  340. B("compl", CPP_COMPL),
  341. B("not", CPP_NOT),
  342. B("not_eq", CPP_NOT_EQ),
  343. B("or", CPP_OR_OR),
  344. B("or_eq", CPP_OR_EQ),
  345. B("xor", CPP_XOR),
  346. B("xor_eq", CPP_XOR_EQ)
  347. };
  348. #undef B
  349. /* Mark the C++ named operators in the hash table. */
  350. static void
  351. mark_named_operators (cpp_reader *pfile, int flags)
  352. {
  353. const struct builtin_operator *b;
  354. for (b = operator_array;
  355. b < (operator_array + ARRAY_SIZE (operator_array));
  356. b++)
  357. {
  358. cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
  359. hp->flags |= flags;
  360. hp->is_directive = 0;
  361. hp->directive_index = b->value;
  362. }
  363. }
  364. /* Helper function of cpp_type2name. Return the string associated with
  365. named operator TYPE. */
  366. const char *
  367. cpp_named_operator2name (enum cpp_ttype type)
  368. {
  369. const struct builtin_operator *b;
  370. for (b = operator_array;
  371. b < (operator_array + ARRAY_SIZE (operator_array));
  372. b++)
  373. {
  374. if (type == b->value)
  375. return (const char *) b->name;
  376. }
  377. return NULL;
  378. }
  379. void
  380. cpp_init_special_builtins (cpp_reader *pfile)
  381. {
  382. const struct builtin_macro *b;
  383. size_t n = ARRAY_SIZE (builtin_array);
  384. if (CPP_OPTION (pfile, traditional))
  385. n -= 2;
  386. else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
  387. || CPP_OPTION (pfile, std))
  388. n--;
  389. for (b = builtin_array; b < builtin_array + n; b++)
  390. {
  391. if (b->value == BT_HAS_ATTRIBUTE
  392. && (CPP_OPTION (pfile, lang) == CLK_ASM
  393. || pfile->cb.has_attribute == NULL))
  394. continue;
  395. cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
  396. hp->type = NT_MACRO;
  397. hp->flags |= NODE_BUILTIN;
  398. if (b->always_warn_if_redefined)
  399. hp->flags |= NODE_WARN;
  400. hp->value.builtin = (enum cpp_builtin_type) b->value;
  401. }
  402. }
  403. /* Read the builtins table above and enter them, and language-specific
  404. macros, into the hash table. HOSTED is true if this is a hosted
  405. environment. */
  406. void
  407. cpp_init_builtins (cpp_reader *pfile, int hosted)
  408. {
  409. cpp_init_special_builtins (pfile);
  410. if (!CPP_OPTION (pfile, traditional)
  411. && (! CPP_OPTION (pfile, stdc_0_in_system_headers)
  412. || CPP_OPTION (pfile, std)))
  413. _cpp_define_builtin (pfile, "__STDC__ 1");
  414. if (CPP_OPTION (pfile, cplusplus))
  415. {
  416. if (CPP_OPTION (pfile, lang) == CLK_CXX1Z
  417. || CPP_OPTION (pfile, lang) == CLK_GNUCXX1Z)
  418. _cpp_define_builtin (pfile, "__cplusplus 201500L");
  419. else if (CPP_OPTION (pfile, lang) == CLK_CXX14
  420. || CPP_OPTION (pfile, lang) == CLK_GNUCXX14)
  421. _cpp_define_builtin (pfile, "__cplusplus 201402L");
  422. else if (CPP_OPTION (pfile, lang) == CLK_CXX11
  423. || CPP_OPTION (pfile, lang) == CLK_GNUCXX11)
  424. _cpp_define_builtin (pfile, "__cplusplus 201103L");
  425. else
  426. _cpp_define_builtin (pfile, "__cplusplus 199711L");
  427. }
  428. else if (CPP_OPTION (pfile, lang) == CLK_ASM)
  429. _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
  430. else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
  431. _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
  432. else if (CPP_OPTION (pfile, lang) == CLK_STDC11
  433. || CPP_OPTION (pfile, lang) == CLK_GNUC11)
  434. _cpp_define_builtin (pfile, "__STDC_VERSION__ 201112L");
  435. else if (CPP_OPTION (pfile, c99))
  436. _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
  437. if (CPP_OPTION (pfile, uliterals)
  438. && !(CPP_OPTION (pfile, cplusplus)
  439. && (CPP_OPTION (pfile, lang) == CLK_GNUCXX
  440. || CPP_OPTION (pfile, lang) == CLK_CXX98)))
  441. {
  442. _cpp_define_builtin (pfile, "__STDC_UTF_16__ 1");
  443. _cpp_define_builtin (pfile, "__STDC_UTF_32__ 1");
  444. }
  445. if (hosted)
  446. _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
  447. else
  448. _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
  449. if (CPP_OPTION (pfile, objc))
  450. _cpp_define_builtin (pfile, "__OBJC__ 1");
  451. }
  452. /* Sanity-checks are dependent on command-line options, so it is
  453. called as a subroutine of cpp_read_main_file (). */
  454. #if ENABLE_CHECKING
  455. static void sanity_checks (cpp_reader *);
  456. static void sanity_checks (cpp_reader *pfile)
  457. {
  458. cppchar_t test = 0;
  459. size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
  460. /* Sanity checks for assumptions about CPP arithmetic and target
  461. type precisions made by cpplib. */
  462. test--;
  463. if (test < 1)
  464. cpp_error (pfile, CPP_DL_ICE, "cppchar_t must be an unsigned type");
  465. if (CPP_OPTION (pfile, precision) > max_precision)
  466. cpp_error (pfile, CPP_DL_ICE,
  467. "preprocessor arithmetic has maximum precision of %lu bits;"
  468. " target requires %lu bits",
  469. (unsigned long) max_precision,
  470. (unsigned long) CPP_OPTION (pfile, precision));
  471. if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
  472. cpp_error (pfile, CPP_DL_ICE,
  473. "CPP arithmetic must be at least as precise as a target int");
  474. if (CPP_OPTION (pfile, char_precision) < 8)
  475. cpp_error (pfile, CPP_DL_ICE, "target char is less than 8 bits wide");
  476. if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
  477. cpp_error (pfile, CPP_DL_ICE,
  478. "target wchar_t is narrower than target char");
  479. if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
  480. cpp_error (pfile, CPP_DL_ICE,
  481. "target int is narrower than target char");
  482. /* This is assumed in eval_token() and could be fixed if necessary. */
  483. if (sizeof (cppchar_t) > sizeof (cpp_num_part))
  484. cpp_error (pfile, CPP_DL_ICE,
  485. "CPP half-integer narrower than CPP character");
  486. if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
  487. cpp_error (pfile, CPP_DL_ICE,
  488. "CPP on this host cannot handle wide character constants over"
  489. " %lu bits, but the target requires %lu bits",
  490. (unsigned long) BITS_PER_CPPCHAR_T,
  491. (unsigned long) CPP_OPTION (pfile, wchar_precision));
  492. }
  493. #else
  494. # define sanity_checks(PFILE)
  495. #endif
  496. /* This is called after options have been parsed, and partially
  497. processed. */
  498. void
  499. cpp_post_options (cpp_reader *pfile)
  500. {
  501. int flags;
  502. sanity_checks (pfile);
  503. post_options (pfile);
  504. /* Mark named operators before handling command line macros. */
  505. flags = 0;
  506. if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
  507. flags |= NODE_OPERATOR;
  508. if (CPP_OPTION (pfile, warn_cxx_operator_names))
  509. flags |= NODE_DIAGNOSTIC | NODE_WARN_OPERATOR;
  510. if (flags != 0)
  511. mark_named_operators (pfile, flags);
  512. }
  513. /* Setup for processing input from the file named FNAME, or stdin if
  514. it is the empty string. Return the original filename
  515. on success (e.g. foo.i->foo.c), or NULL on failure. */
  516. const char *
  517. cpp_read_main_file (cpp_reader *pfile, const char *fname)
  518. {
  519. if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
  520. {
  521. if (!pfile->deps)
  522. pfile->deps = deps_init ();
  523. /* Set the default target (if there is none already). */
  524. deps_add_default_target (pfile->deps, fname);
  525. }
  526. pfile->main_file
  527. = _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0, false);
  528. if (_cpp_find_failed (pfile->main_file))
  529. return NULL;
  530. _cpp_stack_file (pfile, pfile->main_file, false);
  531. /* For foo.i, read the original filename foo.c now, for the benefit
  532. of the front ends. */
  533. if (CPP_OPTION (pfile, preprocessed))
  534. {
  535. read_original_filename (pfile);
  536. fname =
  537. ORDINARY_MAP_FILE_NAME
  538. ((LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table)));
  539. }
  540. return fname;
  541. }
  542. /* For preprocessed files, if the first tokens are of the form # NUM.
  543. handle the directive so we know the original file name. This will
  544. generate file_change callbacks, which the front ends must handle
  545. appropriately given their state of initialization. */
  546. static void
  547. read_original_filename (cpp_reader *pfile)
  548. {
  549. const cpp_token *token, *token1;
  550. /* Lex ahead; if the first tokens are of the form # NUM, then
  551. process the directive, otherwise back up. */
  552. token = _cpp_lex_direct (pfile);
  553. if (token->type == CPP_HASH)
  554. {
  555. pfile->state.in_directive = 1;
  556. token1 = _cpp_lex_direct (pfile);
  557. _cpp_backup_tokens (pfile, 1);
  558. pfile->state.in_directive = 0;
  559. /* If it's a #line directive, handle it. */
  560. if (token1->type == CPP_NUMBER
  561. && _cpp_handle_directive (pfile, token->flags & PREV_WHITE))
  562. {
  563. read_original_directory (pfile);
  564. return;
  565. }
  566. }
  567. /* Backup as if nothing happened. */
  568. _cpp_backup_tokens (pfile, 1);
  569. }
  570. /* For preprocessed files, if the tokens following the first filename
  571. line is of the form # <line> "/path/name//", handle the
  572. directive so we know the original current directory. */
  573. static void
  574. read_original_directory (cpp_reader *pfile)
  575. {
  576. const cpp_token *hash, *token;
  577. /* Lex ahead; if the first tokens are of the form # NUM, then
  578. process the directive, otherwise back up. */
  579. hash = _cpp_lex_direct (pfile);
  580. if (hash->type != CPP_HASH)
  581. {
  582. _cpp_backup_tokens (pfile, 1);
  583. return;
  584. }
  585. token = _cpp_lex_direct (pfile);
  586. if (token->type != CPP_NUMBER)
  587. {
  588. _cpp_backup_tokens (pfile, 2);
  589. return;
  590. }
  591. token = _cpp_lex_direct (pfile);
  592. if (token->type != CPP_STRING
  593. || ! (token->val.str.len >= 5
  594. && IS_DIR_SEPARATOR (token->val.str.text[token->val.str.len-2])
  595. && IS_DIR_SEPARATOR (token->val.str.text[token->val.str.len-3])))
  596. {
  597. _cpp_backup_tokens (pfile, 3);
  598. return;
  599. }
  600. if (pfile->cb.dir_change)
  601. {
  602. char *debugdir = (char *) alloca (token->val.str.len - 3);
  603. memcpy (debugdir, (const char *) token->val.str.text + 1,
  604. token->val.str.len - 4);
  605. debugdir[token->val.str.len - 4] = '\0';
  606. pfile->cb.dir_change (pfile, debugdir);
  607. }
  608. }
  609. /* This is called at the end of preprocessing. It pops the last
  610. buffer and writes dependency output.
  611. Maybe it should also reset state, such that you could call
  612. cpp_start_read with a new filename to restart processing. */
  613. void
  614. cpp_finish (cpp_reader *pfile, FILE *deps_stream)
  615. {
  616. /* Warn about unused macros before popping the final buffer. */
  617. if (CPP_OPTION (pfile, warn_unused_macros))
  618. cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
  619. /* lex.c leaves the final buffer on the stack. This it so that
  620. it returns an unending stream of CPP_EOFs to the client. If we
  621. popped the buffer, we'd dereference a NULL buffer pointer and
  622. segfault. It's nice to allow the client to do worry-free excess
  623. cpp_get_token calls. */
  624. while (pfile->buffer)
  625. _cpp_pop_buffer (pfile);
  626. if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
  627. && deps_stream)
  628. {
  629. deps_write (pfile->deps, deps_stream, 72);
  630. if (CPP_OPTION (pfile, deps.phony_targets))
  631. deps_phony_targets (pfile->deps, deps_stream);
  632. }
  633. /* Report on headers that could use multiple include guards. */
  634. if (CPP_OPTION (pfile, print_include_names))
  635. _cpp_report_missing_guards (pfile);
  636. }
  637. static void
  638. post_options (cpp_reader *pfile)
  639. {
  640. /* -Wtraditional is not useful in C++ mode. */
  641. if (CPP_OPTION (pfile, cplusplus))
  642. CPP_OPTION (pfile, cpp_warn_traditional) = 0;
  643. /* Permanently disable macro expansion if we are rescanning
  644. preprocessed text. Read preprocesed source in ISO mode. */
  645. if (CPP_OPTION (pfile, preprocessed))
  646. {
  647. if (!CPP_OPTION (pfile, directives_only))
  648. pfile->state.prevent_expansion = 1;
  649. CPP_OPTION (pfile, traditional) = 0;
  650. }
  651. if (CPP_OPTION (pfile, warn_trigraphs) == 2)
  652. CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
  653. if (CPP_OPTION (pfile, traditional))
  654. {
  655. CPP_OPTION (pfile, trigraphs) = 0;
  656. CPP_OPTION (pfile, warn_trigraphs) = 0;
  657. }
  658. }