rspec/rules/S819/cfamily/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

93 lines
3.5 KiB
Plaintext

== Why is this an issue?
The use of prototypes enables the compiler to check the integrity of function definitions and calls. Without prototypes the compiler is not obliged to pick up certain errors in function calls (e.g. different number of arguments from the function body, mismatch in types of arguments between call and definition). Function interfaces have been shown to be a cause of considerable problems, and therefore this rule is considered very important.
The recommended method of implementing function prototypes for external functions is to declare the function (i.e. give the function prototype) in a header file, and then include the header file in all those code files that need the prototype (see MISRA C 2004, Rule 8.8).
=== Noncompliant code example
[source,cpp]
----
void example() {
fun(); // Noncompliant
}
void fun() {
}
----
=== Compliant solution
[source,cpp]
----
void fun();
void example() {
fun();
}
void fun() {
}
----
== Resources
* MISRA C:2004, 8.1 - Functions shall have prototype declarations and the prototype shall be visible at both the function definition and call
* MISRA C:2012, 17.3 - A function shall not be declared implicitly
* https://wiki.sei.cmu.edu/confluence/x/7NYxBQ[CERT, DCL07-C.] - Include the appropriate type information in function declarators
* https://wiki.sei.cmu.edu/confluence/x/8NUxBQ[CERT, DCL31-C.] - Declare identifiers before using them
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Make the prototype of this function visible at this point.
'''
== Comments And Links
(visible only on this page)
=== is related to: S926
=== on 20 Oct 2014, 14:12:22 Ann Campbell wrote:
\[~samuel.mercier] FYI, I've demoted the 'See Also' heading to h3 to make it a child of the references section rather than a sister
Also, MISRA C:2012, 8.2 is the equivalent rule in the later spec, and RSPEC-926 is related to the 2012 definition. I.e. the 2004 definition does not call for parameter names in prototypes, but the 2012 version does.
=== on 23 Oct 2014, 15:24:31 Samuel Mercier wrote:
\[~ann.campbell.2] the scope of this rule and RSPEC-926 is not exactly the same.
The ultimate goal of this rule is to make sure the type of the arguments are the same when the function is defined and called (although the title is not accurate).
RSPEC-926 requires the name of the arguments to be explicitly written, to give more information to the developer about their usage.
=== on 16 Nov 2014, 02:14:21 Evgeny Mandrikov wrote:
\[~samuel.mercier] IMO the ultimate goal of MISRA C:2004 8.1 (this RSPEC) is to require prototype and not to check the type of arguments. But I agree that goal of MISRA C:2004 16.3 (RSPEC-926) is to require names of the arguments in addition. However RSPEC-926 also refers to MISRA C:2012 8.2, which requires both - prototype and names.
=== on 14 Dec 2014, 23:56:09 Evgeny Mandrikov wrote:
I don't like
____
The provision of a prototype for a function with internal linkage is a good programming practice.
____
because implicit function declaration is allowed only by C90 standard, but disallowed in later standards, thus this is not just a good practice.
=== on 15 Dec 2014, 00:01:35 Evgeny Mandrikov wrote:
Also this is too strong requirement to have a function prototype before actual definition, so title changed to match MISRA C:2012.
=== on 15 Dec 2014, 00:08:02 Evgeny Mandrikov wrote:
There is no references for {cpp}, so removed from list of targeted languages.
endif::env-github,rspecator-view[]