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