rspec/rules/S960/cfamily/rule.adoc
Fred Tingaud 22b4470f2a
Modify CFamily rules: CPP-4080 Refresh and standardize CppCoreGuidelines references (#3514)
Update all links to C++ Core Guidelines to `e49158a`.

Refresh done using the following script and some manual edits:
db76e34e74/personal/fred-tingaud/rspec/refresh-cppcoreguidelines.py

When re-using this script, be mindful that:
 - it does not cover `shared_content`
 - it does not properly escape inline code in links (e.g., "[=]" or "`mutex`es")
 - it does not change `C++` to `{cpp}` in link titles.

Co-authored-by: Marco Borgeaud <marco.borgeaud@sonarsource.com>
2023-12-20 10:08:18 +01:00

74 lines
2.2 KiB
Plaintext

== Why is this an issue?
It is tempting to treat function-like macros as functions, but the two things work differently. For instance, the use of functions offers parameter type-checking, while the use of macros does not. Additionally, with macros, there is the potential for a macro to be evaluated multiple times. In general, functions offer a safer, more robust mechanism than function-like macros, and that safety usually outweighs the speed advantages offered by macros. Therefore functions should be used instead when possible.
=== Noncompliant code example
[source,cpp]
----
#define CUBE (X) ((X) * (X) * (X)) // Noncompliant
void func(void) {
int i = 2;
int a = CUBE(++i); // Noncompliant. Expands to: int a = ((++i) * (++i) * (++i))
// ...
}
----
=== Compliant solution
[source,cpp]
----
inline int cube(int i) {
return i * i * i;
}
void func(void) {
int i = 2;
int a = cube(++i); // yields 27
// ...
}
----
== Resources
* MISRA C:2004, 19.7 - A function should be used in preference to a function-like macro.
* MISRA {cpp}:2008, 16-0-4 - Function-like macros shall not be defined.
* MISRA C:2012, Dir. 4.9 - A function should be used in preference to a function-like macro where they are interchangeable
* https://wiki.sei.cmu.edu/confluence/x/INcxBQ[CERT, PRE00-C.] - Prefer inline or static functions to function-like macros
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#es31-dont-use-macros-for-constants-or-functions[ES.31: Don't use macros for constants or "functions"]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use an inline or static function instead.
'''
== Comments And Links
(visible only on this page)
=== is related to: S962
=== is related to: S971
=== on 19 Sep 2014, 13:46:13 Freddy Mallet wrote:
@Ann, I would append the following MISRA explanations to the description :
____
While macros can provide a speed advantage over functions, functions provide a safer and more robust mechanism. This is particularly true with respect to the type checking of parameters, and the problem of function-like macros potentially evaluating parameters multiple times.
____
endif::env-github,rspecator-view[]