Main Content

AUTOSAR C++14 Rule A16-2-3

An include directive shall be added explicitly for every symbol used in a file

Since R2021b

Description

Rule Definition

An include directive shall be added explicitly for every symbol used in a file.

Rationale

To avoid a compilation failure, declare every symbol, macro, and data type before use and include their associated header files in the source file.

Polyspace Implementation

Polyspace® reports a violation when a source file contains a symbol, macro, or data type and the header file in which the symbol, macro, or data type is defined in is not included in the same source file.

Polyspace does not report a violation if the header file containing the required include directive shares a name with the source file.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, Polyspace reports a violation of the coding rule because of the use of initializer_list in the declaration of the function printMembers and the use of int32_t in the for loop. In both cases, the source code does not explicitly include the header files which define the class template (initializer_list) and the type (int32_t).

Note that in the case of initializer_list, the header <string> implicitly includes the header <intializer_list>, but Polyspace still reports a violation because the header <intializer_list> is not included explicitly in the source code.

#include <iostream>
#include <ostream>
#include <string>

void printMembers(std::initializer_list<int32_t> myList) //Noncompliant
{
        std::cout << "Printing list members...\n";
        for (int32_t member : myList)                    //Noncompliant
        {
                std::cout << member << "\n";
        }
}

int main()
{
        std::string msg = "Hello world : ";
        int32_t num = 5;
        std::cout << msg << num << "\n";

        std::initializer_list<int32_t> numList = {1, 2, 3, 4, 5};
        printMembers(numList);
        return 0;
}

Check Information

Group: Preprocessing directives
Category: Required, Non-automated

Version History

Introduced in R2021b