0007-fixinc-don-t-fix-machine-names-in-__has_include-.-PR.patch 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. From de3f4ee9a5bd2adcb5ff2e1690db2567fda1473c Mon Sep 17 00:00:00 2001
  2. From: Xi Ruoyao <xry111@mengyan1223.wang>
  3. Date: Mon, 28 Jun 2021 13:54:58 +0800
  4. Subject: [PATCH] fixinc: don't "fix" machine names in __has_include(...)
  5. [PR91085]
  6. fixincludes/
  7. PR other/91085
  8. * fixfixes.c (check_has_inc): New static function.
  9. (machine_name_fix): Don't replace header names in
  10. __has_include(...).
  11. * inclhack.def (machine_name): Adjust test.
  12. * tests/base/testing.h: Update.
  13. Upstream: 6bf383c37e6131a8e247e8a0997d55d65c830b6d
  14. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  15. ---
  16. fixincludes/fixfixes.c | 45 ++++++++++++++++++++++++++++++--
  17. fixincludes/inclhack.def | 3 ++-
  18. fixincludes/tests/base/testing.h | 2 +-
  19. 3 files changed, 46 insertions(+), 4 deletions(-)
  20. diff --git a/fixincludes/fixfixes.c b/fixincludes/fixfixes.c
  21. index 5b23a8b640d..404b420f302 100644
  22. --- a/fixincludes/fixfixes.c
  23. +++ b/fixincludes/fixfixes.c
  24. @@ -477,6 +477,39 @@ FIX_PROC_HEAD( char_macro_def_fix )
  25. fputs (text, stdout);
  26. }
  27. +/* Check if the pattern at pos is actually in a "__has_include(...)"
  28. + directive. Return the pointer to the ')' of this
  29. + "__has_include(...)" if it is, NULL otherwise. */
  30. +static const char *
  31. +check_has_inc (const char *begin, const char *pos, const char *end)
  32. +{
  33. + static const char has_inc[] = "__has_include";
  34. + const size_t has_inc_len = sizeof (has_inc) - 1;
  35. + const char *p;
  36. +
  37. + for (p = memmem (begin, pos - begin, has_inc, has_inc_len);
  38. + p != NULL;
  39. + p = memmem (p, pos - p, has_inc, has_inc_len))
  40. + {
  41. + p += has_inc_len;
  42. + while (p < end && ISSPACE (*p))
  43. + p++;
  44. +
  45. + /* "__has_include" may appear as "defined(__has_include)",
  46. + search for the next appearance then. */
  47. + if (*p != '(')
  48. + continue;
  49. +
  50. + /* To avoid too much complexity, just hope there is never a
  51. + ')' in a header name. */
  52. + p = memchr (p, ')', end - p);
  53. + if (p == NULL || p > pos)
  54. + return p;
  55. + }
  56. +
  57. + return NULL;
  58. +}
  59. +
  60. /* Fix for machine name #ifdefs that are not in the namespace reserved
  61. by the C standard. They won't be defined if compiling with -ansi,
  62. and the headers will break. We go to some trouble to only change
  63. @@ -524,7 +557,7 @@ FIX_PROC_HEAD( machine_name_fix )
  64. /* If the 'name_pat' matches in between base and limit, we have
  65. a bogon. It is not worth the hassle of excluding comments
  66. because comments on #if/#ifdef lines are rare, and strings on
  67. - such lines are illegal.
  68. + such lines are only legal in a "__has_include" directive.
  69. REG_NOTBOL means 'base' is not at the beginning of a line, which
  70. shouldn't matter since the name_re has no ^ anchor, but let's
  71. @@ -544,8 +577,16 @@ FIX_PROC_HEAD( machine_name_fix )
  72. break;
  73. p = base + match[0].rm_so;
  74. - base += match[0].rm_eo;
  75. + /* Check if the match is in __has_include(...) (PR 91085). */
  76. + q = check_has_inc (base, p, limit);
  77. + if (q)
  78. + {
  79. + base = q + 1;
  80. + goto again;
  81. + }
  82. +
  83. + base += match[0].rm_eo;
  84. /* One more test: if on the same line we have the same string
  85. with the appropriate underscores, then leave it alone.
  86. We want exactly two leading and trailing underscores. */
  87. diff --git a/fixincludes/inclhack.def b/fixincludes/inclhack.def
  88. index 066bef99162..b7ad6982e96 100644
  89. --- a/fixincludes/inclhack.def
  90. +++ b/fixincludes/inclhack.def
  91. @@ -3154,7 +3154,8 @@ fix = {
  92. c_fix = machine_name;
  93. test_text = "/* MACH_DIFF: */\n"
  94. - "#if defined( i386 ) || defined( sparc ) || defined( vax )"
  95. + "#if defined( i386 ) || defined( sparc ) || defined( vax ) || "
  96. + "defined( linux ) || __has_include ( <linux.h> )"
  97. "\n/* no uniform test, so be careful :-) */";
  98. };
  99. diff --git a/fixincludes/tests/base/testing.h b/fixincludes/tests/base/testing.h
  100. index cf95321fb86..8b3accaf04e 100644
  101. --- a/fixincludes/tests/base/testing.h
  102. +++ b/fixincludes/tests/base/testing.h
  103. @@ -64,7 +64,7 @@ BSD43__IOWR('T', 1) /* Some are multi-line */
  104. #if defined( MACHINE_NAME_CHECK )
  105. /* MACH_DIFF: */
  106. -#if defined( i386 ) || defined( sparc ) || defined( vax )
  107. +#if defined( i386 ) || defined( sparc ) || defined( vax ) || defined( linux ) || __has_include ( <linux.h> )
  108. /* no uniform test, so be careful :-) */
  109. #endif /* MACHINE_NAME_CHECK */
  110. --
  111. 2.37.3