aboutsummaryrefslogtreecommitdiff
path: root/packages/gcc/11.4.0/0010-fixinc-don-t-fix-machine-names-in-__has_include-.PR.patch
blob: 69afa33536218c8f1e64545619b247afd3aaf4a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
From de3f4ee9a5bd2adcb5ff2e1690db2567fda1473c Mon Sep 17 00:00:00 2001
From: Xi Ruoyao <xry111@mengyan1223.wang>
Date: Mon, 28 Jun 2021 13:54:58 +0800
Subject: [PATCH] fixinc: don't "fix" machine names in __has_include(...)
 [PR91085]

fixincludes/

	PR other/91085
	* fixfixes.c (check_has_inc): New static function.
	  (machine_name_fix): Don't replace header names in
	  __has_include(...).
	* inclhack.def (machine_name): Adjust test.
	* tests/base/testing.h: Update.

Upstream: 6bf383c37e6131a8e247e8a0997d55d65c830b6d
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
---
 fixincludes/fixfixes.c           |   45 +++++++++++++++++++++++++++++++++++++--
 fixincludes/inclhack.def         |    3 +-
 fixincludes/tests/base/testing.h |    2 -
 3 files changed, 46 insertions(+), 4 deletions(-)

--- a/fixincludes/fixfixes.c
+++ b/fixincludes/fixfixes.c
@@ -477,6 +477,39 @@
   fputs (text, stdout);
 }
 
+/* Check if the pattern at pos is actually in a "__has_include(...)"
+   directive.  Return the pointer to the ')' of this
+   "__has_include(...)" if it is, NULL otherwise.  */
+static const char *
+check_has_inc (const char *begin, const char *pos, const char *end)
+{
+  static const char has_inc[] = "__has_include";
+  const size_t has_inc_len = sizeof (has_inc) - 1;
+  const char *p;
+
+  for (p = memmem (begin, pos - begin, has_inc, has_inc_len);
+       p != NULL;
+       p = memmem (p, pos - p, has_inc, has_inc_len))
+    {
+      p += has_inc_len;
+      while (p < end && ISSPACE (*p))
+        p++;
+
+      /* "__has_include" may appear as "defined(__has_include)",
+         search for the next appearance then.  */
+      if (*p != '(')
+        continue;
+
+      /* To avoid too much complexity, just hope there is never a
+         ')' in a header name.  */
+      p = memchr (p, ')', end - p);
+      if (p == NULL || p > pos)
+        return p;
+    }
+
+  return NULL;
+}
+
 /* Fix for machine name #ifdefs that are not in the namespace reserved
    by the C standard.  They won't be defined if compiling with -ansi,
    and the headers will break.  We go to some trouble to only change
@@ -524,7 +557,7 @@
       /* If the 'name_pat' matches in between base and limit, we have
          a bogon.  It is not worth the hassle of excluding comments
          because comments on #if/#ifdef lines are rare, and strings on
-         such lines are illegal.
+         such lines are only legal in a "__has_include" directive.
 
          REG_NOTBOL means 'base' is not at the beginning of a line, which
          shouldn't matter since the name_re has no ^ anchor, but let's
@@ -544,8 +577,16 @@
             break;
 
           p = base + match[0].rm_so;
-          base += match[0].rm_eo;
 
+          /* Check if the match is in __has_include(...) (PR 91085). */
+          q = check_has_inc (base, p, limit);
+          if (q) 
+            {
+              base = q + 1;
+              goto again;
+            }
+
+          base += match[0].rm_eo;
           /* One more test: if on the same line we have the same string
              with the appropriate underscores, then leave it alone.
              We want exactly two leading and trailing underscores.  */
--- a/fixincludes/inclhack.def
+++ b/fixincludes/inclhack.def
@@ -3201,7 +3201,8 @@
     c_fix     = machine_name;
 
     test_text = "/* MACH_DIFF: */\n"
-    "#if defined( i386 ) || defined( sparc ) || defined( vax )"
+    "#if defined( i386 ) || defined( sparc ) || defined( vax ) || "
+    "defined( linux ) || __has_include ( <linux.h> )"
     "\n/* no uniform test, so be careful  :-) */";
 };
 
--- a/fixincludes/tests/base/testing.h
+++ b/fixincludes/tests/base/testing.h
@@ -64,7 +64,7 @@
 
 #if defined( MACHINE_NAME_CHECK )
 /* MACH_DIFF: */
-#if defined( i386 ) || defined( sparc ) || defined( vax )
+#if defined( i386 ) || defined( sparc ) || defined( vax ) || defined( linux ) || __has_include ( <linux.h> )
 /* no uniform test, so be careful  :-) */
 #endif  /* MACHINE_NAME_CHECK */