fixtests.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Test to see if a particular fix should be applied to a header file.
  3. Copyright (C) 1997, 1998, 1999, 2000, 2009, 2012
  4. Free Software Foundation, Inc.
  5. = = = = = = = = = = = = = = = = = = = = = = = = =
  6. NOTE TO DEVELOPERS
  7. The routines you write here must work closely with fixincl.c.
  8. Here are the rules:
  9. 1. Every test procedure name must be suffixed with "_test".
  10. These routines will be referenced from inclhack.def, sans the suffix.
  11. 2. Use the "TEST_FOR_FIX_PROC_HEAD()" macro _with_ the "_test" suffix
  12. (I cannot use the ## magic from ANSI C) for defining your entry point.
  13. 3. Put your test name into the FIX_TEST_TABLE
  14. 4. Do not write anything to stdout. It may be closed.
  15. 5. Write to stderr only in the event of a reportable error
  16. In such an event, call "exit(1)".
  17. = = = = = = = = = = = = = = = = = = = = = = = = =
  18. This file is part of GCC.
  19. GCC is free software; you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License as published by
  21. the Free Software Foundation; either version 3, or (at your option)
  22. any later version.
  23. GCC is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. GNU General Public License for more details.
  27. You should have received a copy of the GNU General Public License
  28. along with GCC; see the file COPYING3. If not see
  29. <http://www.gnu.org/licenses/>. */
  30. #include "fixlib.h"
  31. #define _ENV_(v,m,n,t) extern char const * v;
  32. ENV_TABLE
  33. #undef _ENV_
  34. typedef apply_fix_p_t t_test_proc ( tCC* file, tCC* text );
  35. typedef struct {
  36. tCC* test_name;
  37. t_test_proc* test_proc;
  38. } test_entry_t;
  39. #define FIX_TEST_TABLE \
  40. _FT_( "machine_name", machine_name_test ) \
  41. _FT_( "stdc_0_in_system_headers", stdc_0_in_system_headers_test )
  42. #define TEST_FOR_FIX_PROC_HEAD( test ) \
  43. static apply_fix_p_t test ( tCC* fname ATTRIBUTE_UNUSED, \
  44. tCC* text ATTRIBUTE_UNUSED )
  45. TEST_FOR_FIX_PROC_HEAD( machine_name_test )
  46. {
  47. regex_t *label_re, *name_re;
  48. regmatch_t match[2];
  49. tCC *base, *limit;
  50. IGNORE_ARG(fname);
  51. if (!mn_get_regexps (&label_re, &name_re, "machine_name_test"))
  52. return SKIP_FIX;
  53. for (base = text;
  54. xregexec (label_re, base, 2, match, 0) == 0;
  55. base = limit)
  56. {
  57. base += match[0].rm_eo;
  58. /* We're looking at an #if or #ifdef. Scan forward for the
  59. next non-escaped newline. */
  60. limit = base;
  61. do
  62. {
  63. limit++;
  64. limit = strchr (limit, '\n');
  65. if (!limit)
  66. return SKIP_FIX;
  67. }
  68. while (limit[-1] == '\\');
  69. /* If the 'name_pat' matches in between base and limit, we have
  70. a bogon. It is not worth the hassle of excluding comments,
  71. because comments on #if/#ifdef/#ifndef lines are rare,
  72. and strings on such lines are illegal.
  73. REG_NOTBOL means 'base' is not at the beginning of a line, which
  74. shouldn't matter since the name_re has no ^ anchor, but let's
  75. be accurate anyway. */
  76. if (xregexec (name_re, base, 1, match, REG_NOTBOL))
  77. return SKIP_FIX; /* No match in file - no fix needed */
  78. /* Match; is it on the line? */
  79. if (match[0].rm_eo <= limit - base)
  80. return APPLY_FIX; /* Yup */
  81. /* Otherwise, keep looking... */
  82. }
  83. return SKIP_FIX;
  84. }
  85. TEST_FOR_FIX_PROC_HEAD( stdc_0_in_system_headers_test )
  86. {
  87. #ifdef STDC_0_IN_SYSTEM_HEADERS
  88. return (pz_machine == NULL) ? APPLY_FIX : SKIP_FIX;
  89. #else
  90. return APPLY_FIX;
  91. #endif
  92. }
  93. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  94. test for fix selector
  95. THIS IS THE ONLY EXPORTED ROUTINE
  96. */
  97. apply_fix_p_t
  98. run_test( tCC* tname, tCC* fname, tCC* text )
  99. {
  100. #define _FT_(n,p) { n, p },
  101. static test_entry_t test_table[] = { FIX_TEST_TABLE { NULL, NULL }};
  102. #undef _FT_
  103. #define TEST_TABLE_CT (ARRAY_SIZE (test_table)-1)
  104. int ct = TEST_TABLE_CT;
  105. test_entry_t* pte = test_table;
  106. do
  107. {
  108. if (strcmp( pte->test_name, tname ) == 0)
  109. return (*pte->test_proc)( fname, text );
  110. pte++;
  111. } while (--ct > 0);
  112. fprintf( stderr, "fixincludes error: the `%s' fix test is unknown\n",
  113. tname );
  114. exit( 3 );
  115. }