Skip to contentSkip to conversation

gcc-plugins: Fix build with GCC 16.1

PATCH New Jakub Jelinek 1 participant 1 message May 5 Gcc plugins

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gcc-plugins: Fix build with GCC 16.1

GCC is written in C++ for 13 years already, and from the time it was implemented in C and times when it could be built by both C and C++ there were various CONST_CAST* macros. In GCC 16 these macros were removed because one can use C++ const_cast directly.

The kernel gcc-plugins use 2 of those macros though, so when GCC plugins
are enabled, kernel fails to build with
In file included from scripts/gcc-plugins/latent_entropy_plugin.c:78:
scripts/gcc-plugins/gcc-common.h: In function ‘void debug_tree(const_tree)’:
scripts/gcc-plugins/gcc-common.h:417:20: error: ‘CONST_CAST_TREE’ was not declared in this scope; did you mean ‘CONST_CAST_EXPR’?

  417 |         debug_tree(CONST_CAST_TREE(t));
      |                    ^~~~~~~~~~~~~~~
      |                    CONST_CAST_EXPR

scripts/gcc-plugins/gcc-common.h: In function ‘void debug_gimple_stmt(const_gimple_ptr)’:
scripts/gcc-plugins/gcc-common.h:312:47: error: expected primary-expression before ‘,’ token
312 | #define CONST_CAST_GIMPLE(X) CONST_CAST(gimple, (X))
| ^
scripts/gcc-plugins/gcc-common.h:422:27: note: in expansion of macro ‘CONST_CAST_GIMPLE’

  422 |         debug_gimple_stmt(CONST_CAST_GIMPLE(s));
      |                           ^~~~~~~~~~~~~~~~~
scripts/gcc-plugins/gcc-common.h:312:30: error: ‘CONST_CAST’ was not declared in this scope; did you mean ‘CONST_INT’?
  312 | #define CONST_CAST_GIMPLE(X) CONST_CAST(gimple, (X))
      |                              ^~~~~~~~~~
scripts/gcc-plugins/gcc-common.h:422:27: note: in expansion of macro ‘CONST_CAST_GIMPLE’
  422 |         debug_gimple_stmt(CONST_CAST_GIMPLE(s));
      |                           ^~~~~~~~~~~~~~~~~

The following patch fixes it by defining the macros the way they were defined in GCC 15 and earlier for the plugin compatibility layer.

Tested with both GCC 15.2.1 (where it works before/after this patch) and GCC 16.1.1 (where it is fixed with it).

scripts/gcc-plugins/gcc-common.h +6 -0
index 8f1b3500f8e2..0e46215793ba 100644
@@ -304,6 +304,12 @@ static inline void cgraph_call_edge_duplication_hooks(cgraph_edge *cs1, cgraph_e
 	symtab->call_edge_duplication_hooks(cs1, cs2);
 }
 
+
+#if BUILDING_GCC_VERSION >= 16000
+#define CONST_CAST(TYPE, X) (const_cast<TYPE>(X))
+#define CONST_CAST_TREE(X) CONST_CAST(union tree_node *, (X))
+#endif
+
 typedef gimple *gimple_ptr;
 typedef const gimple *const_gimple_ptr;
 #define gimple gimple_ptr