xref: /aosp_15_r20/external/pigweed/pw_preprocessor/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_preprocessor:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker---------------
4*61c4878aSAndroid Build Coastguard Workerpw_preprocessor
5*61c4878aSAndroid Build Coastguard Worker---------------
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_preprocessor
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerThe preprocessor module provides various helpful preprocessor macros.
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard WorkerCompatibility
12*61c4878aSAndroid Build Coastguard Worker=============
13*61c4878aSAndroid Build Coastguard WorkerC and C++
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard WorkerHeaders
16*61c4878aSAndroid Build Coastguard Worker=======
17*61c4878aSAndroid Build Coastguard WorkerThe preprocessor module provides several headers.
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Workerpw_preprocessor/apply.h
20*61c4878aSAndroid Build Coastguard Worker---------------------------------
21*61c4878aSAndroid Build Coastguard WorkerDefines general macro to support macro expansion. Includes the following macro:
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard WorkerAPI Reference
24*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^
25*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_APPLY
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Workerpw_preprocessor/arguments.h
28*61c4878aSAndroid Build Coastguard Worker---------------------------------
29*61c4878aSAndroid Build Coastguard WorkerDefines macros for handling variadic arguments to function-like macros. Macros
30*61c4878aSAndroid Build Coastguard Workerinclude the following:
31*61c4878aSAndroid Build Coastguard Worker
32*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_DELEGATE_BY_ARG_COUNT(name, ...)
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker  Selects and invokes a macro based on the number of arguments provided. Expands
35*61c4878aSAndroid Build Coastguard Worker  to ``<name><arg_count>(...)``. For example,
36*61c4878aSAndroid Build Coastguard Worker  ``PW_DELEGATE_BY_ARG_COUNT(foo_, 1, 2, 3)`` expands to ``foo_3(1, 2, 3)``.
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker  This example shows how ``PW_DELEGATE_BY_ARG_COUNT`` could be used to log a
39*61c4878aSAndroid Build Coastguard Worker  customized message based on the number of arguments provided.
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker  .. code-block:: cpp
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker     #define ARG_PRINT(...)  PW_DELEGATE_BY_ARG_COUNT(_ARG_PRINT, __VA_ARGS__)
44*61c4878aSAndroid Build Coastguard Worker     #define _ARG_PRINT0(a)        LOG_INFO("nothing!")
45*61c4878aSAndroid Build Coastguard Worker     #define _ARG_PRINT1(a)        LOG_INFO("1 arg: %s", a)
46*61c4878aSAndroid Build Coastguard Worker     #define _ARG_PRINT2(a, b)     LOG_INFO("2 args: %s, %s", a, b)
47*61c4878aSAndroid Build Coastguard Worker     #define _ARG_PRINT3(a, b, c)  LOG_INFO("3 args: %s, %s, %s", a, b, c)
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard Worker  When used, ``ARG_PRINT`` expands to the ``_ARG_PRINT#`` macro corresponding
50*61c4878aSAndroid Build Coastguard Worker  to the number of arguments.
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker  .. code-block:: cpp
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker     ARG_PRINT();               // Outputs: nothing!
55*61c4878aSAndroid Build Coastguard Worker     ARG_PRINT("a");            // Outputs: 1 arg: a
56*61c4878aSAndroid Build Coastguard Worker     ARG_PRINT("a", "b");       // Outputs: 2 args: a, b
57*61c4878aSAndroid Build Coastguard Worker     ARG_PRINT("a", "b", "c");  // Outputs: 3 args: a, b, c
58*61c4878aSAndroid Build Coastguard Worker
59*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_COMMA_ARGS(...)
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard Worker  Expands to a comma followed by the arguments if any arguments are provided.
62*61c4878aSAndroid Build Coastguard Worker  Otherwise, expands to nothing. If the final argument is empty, it is omitted.
63*61c4878aSAndroid Build Coastguard Worker  This is useful when passing ``__VA_ARGS__`` to a variadic function or template
64*61c4878aSAndroid Build Coastguard Worker  parameter list, since it removes the extra comma when no arguments are
65*61c4878aSAndroid Build Coastguard Worker  provided. ``PW_COMMA_ARGS`` must NOT be used when invoking a macro from
66*61c4878aSAndroid Build Coastguard Worker  another macro.
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard Worker  For example. ``PW_COMMA_ARGS(1, 2, 3)``, expands to ``, 1, 2, 3``, while
69*61c4878aSAndroid Build Coastguard Worker  ``PW_COMMA_ARGS()`` expands to nothing. ``PW_COMMA_ARGS(1, 2, )`` expands to
70*61c4878aSAndroid Build Coastguard Worker  ``, 1, 2``.
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Workerpw_preprocessor/boolean.h
73*61c4878aSAndroid Build Coastguard Worker-------------------------
74*61c4878aSAndroid Build Coastguard WorkerDefines macros for boolean logic on literal 1s and 0s. This is useful for
75*61c4878aSAndroid Build Coastguard Workersituations when a literal is needed to build the name of a function or macro.
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard Workerpw_preprocessor/compiler.h
78*61c4878aSAndroid Build Coastguard Worker--------------------------
79*61c4878aSAndroid Build Coastguard WorkerMacros for compiler-specific features, such as attributes or builtins.
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard WorkerModifying compiler diagnostics
82*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
83*61c4878aSAndroid Build Coastguard Worker``pw_preprocessor/compiler.h`` provides macros for enabling or disabling
84*61c4878aSAndroid Build Coastguard Workercompiler diagnostics (warnings or errors) for sections of code.
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard Worker:c:macro:`PW_MODIFY_DIAGNOSTICS_PUSH` and :c:macro:`PW_MODIFY_DIAGNOSTICS_POP`
87*61c4878aSAndroid Build Coastguard Workerare used to turn off or on diagnostics (warnings or errors) for a section of
88*61c4878aSAndroid Build Coastguard Workercode. Use :c:macro:`PW_MODIFY_DIAGNOSTICS_PUSH`, use
89*61c4878aSAndroid Build Coastguard Worker:c:macro:`PW_MODIFY_DIAGNOSTIC` as many times as needed, then use
90*61c4878aSAndroid Build Coastguard Worker:c:macro:`PW_MODIFY_DIAGNOSTICS_POP` to restore the previous settings.
91*61c4878aSAndroid Build Coastguard Worker
92*61c4878aSAndroid Build Coastguard Worker.. code-block:: c
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker   PW_MODIFY_DIAGNOSTICS_PUSH();
95*61c4878aSAndroid Build Coastguard Worker   PW_MODIFY_DIAGNOSTIC(ignored, "-Wunused-variable");
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker   static int this_variable_is_never_used;
98*61c4878aSAndroid Build Coastguard Worker
99*61c4878aSAndroid Build Coastguard Worker   PW_MODIFY_DIAGNOSTICS_POP();
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard Worker.. tip::
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker  :c:macro:`PW_MODIFY_DIAGNOSTIC` and related macros should rarely be used.
104*61c4878aSAndroid Build Coastguard Worker  Whenever possible, fix the underlying issues about which the compiler is
105*61c4878aSAndroid Build Coastguard Worker  warning, rather than silencing the diagnostics.
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard Worker.. _module-pw_preprocessor-integer-overflow:
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard WorkerInteger with Overflow Checking
110*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111*61c4878aSAndroid Build Coastguard Worker``pw_preprocessor/compiler.h`` provides macros for performing arithmetic
112*61c4878aSAndroid Build Coastguard Workeroperations and checking whether it overflowed.
113*61c4878aSAndroid Build Coastguard Worker
114*61c4878aSAndroid Build Coastguard Worker- :c:macro:`PW_ADD_OVERFLOW`
115*61c4878aSAndroid Build Coastguard Worker- :c:macro:`PW_SUB_OVERFLOW`
116*61c4878aSAndroid Build Coastguard Worker- :c:macro:`PW_MUL_OVERFLOW`
117*61c4878aSAndroid Build Coastguard Worker
118*61c4878aSAndroid Build Coastguard WorkerAPI Reference
119*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^
120*61c4878aSAndroid Build Coastguard Worker.. doxygengroup:: pw_preprocessor_compiler
121*61c4878aSAndroid Build Coastguard Worker   :content-only:
122*61c4878aSAndroid Build Coastguard Worker
123*61c4878aSAndroid Build Coastguard Workerpw_preprocessor/concat.h
124*61c4878aSAndroid Build Coastguard Worker------------------------
125*61c4878aSAndroid Build Coastguard WorkerDefines the ``PW_CONCAT(...)`` macro, which expands its arguments if they are
126*61c4878aSAndroid Build Coastguard Workermacros and token pastes the results. This can be used for building names of
127*61c4878aSAndroid Build Coastguard Workerclasses, variables, macros, etc.
128*61c4878aSAndroid Build Coastguard Worker
129*61c4878aSAndroid Build Coastguard Workerpw_preprocessor/util.h
130*61c4878aSAndroid Build Coastguard Worker----------------------
131*61c4878aSAndroid Build Coastguard WorkerGeneral purpose, useful macros.
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker* ``PW_ARRAY_SIZE(array)`` -- calculates the size of a C array
134*61c4878aSAndroid Build Coastguard Worker* ``PW_STRINGIFY(...)`` -- expands its arguments as macros and converts them to
135*61c4878aSAndroid Build Coastguard Worker  a string literal
136*61c4878aSAndroid Build Coastguard Worker* ``PW_EXTERN_C`` -- declares a name to be ``extern "C"`` in C++; expands to
137*61c4878aSAndroid Build Coastguard Worker  nothing in C
138*61c4878aSAndroid Build Coastguard Worker* ``PW_EXTERN_C_START`` / ``PW_EXTERN_C_END`` -- declares an ``extern "C" { }``
139*61c4878aSAndroid Build Coastguard Worker  block in C++; expands to nothing in C
140*61c4878aSAndroid Build Coastguard Worker
141*61c4878aSAndroid Build Coastguard WorkerZephyr
142*61c4878aSAndroid Build Coastguard Worker======
143*61c4878aSAndroid Build Coastguard WorkerTo enable ``pw_preprocessor`` for Zephyr add ``CONFIG_PIGWEED_PREPROCESSOR=y``
144*61c4878aSAndroid Build Coastguard Workerto the project's configuration.
145