How can I write a macro for pragma section directives?

To define input sections on C/C++ source code level so-called pragma section directives are used, e.g., for AutoSAR applications. Several compiler vendors provide a different syntax for it. To ensure the portability of your source code you should use macros to encapsulate pragma section directives.

Example

#define BEGIN_CODE_SECTION(sec) CODE_SECTION(clang section text=#sec)
#define CODE_SECTION(sec) _Pragma(#sec)
#define END_SECTION CODE_SECTION(clang section text="")

BEGIN_CODE_SECTION(.myfunc)
int add3(int foo3)
{
	for (int i = 0; i < 10; i++) {}
	return foo3+3;
}
END_SECTION

_Pragma is defined in the standard C99. The # indicates that the parameter is passed as a string. The preprocessor will replace the above statement by:

#pragma clang section text=".myfunc"
int add3(int foo3)
{
	for (int i = 0; i < 10; i++) {}
	return foo3+3;
}
#pragma clang section text=""

Example output section

SECTIONS
{
    .myfunc :
	{
		*(*.myfunc*)
	} > int_flash0
}