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