Main Content

AUTOSAR C++14 Rule A2-10-5

An identifier name of a function with static storage duration or a non-member object with external or internal linkage should not be reused

Since R2020b

Description

Rule Definition

An identifier name of a function with static storage duration or a non-member object with external or internal linkage should not be reused.

Rationale

Objects with static storage duration remain available during the entire execution of the program. These include:

  • Non-member objects with external linkage that can be referred to from any of the translation units of your project.

  • Objects declared with the static class specifier. These objects have internal linkage and can be referred to from any scope within their translation unit.

If you reuse the name of an identifier, you might mistake one identifier for the other.

The rule does not apply to objects with no linkage, for instance function local static objects, since the identifiers of those objects cannot be referred to from outside of their scope.

Polyspace Implementation

  • When you reuse identifiers, Polyspace® flags the last use of the identifier if they are in the same translation unit. If the identifiers are in separate files, the identifier in the last file path by alphabetical order is flagged.

  • If you declare a function in a namespace with the static class specifier and reuse the function identifier to declare a non-static function in another namespace, Polyspace flags the identifier of the static function. For instance, in this code snippet, the identifier func is reused in namespace NS_2 but it is flagged in namespace NS_1.

    namespace NS_1 {
        static void func(void); // Polyspace flags this use of "func".
    };
    
    namespace NS_2 {
        void func(void); //"func" identifier reused but this is not a static function.
    }
    
    

  • Polyspace flags the identifier of a global variable if you reuse the identifier for a local variable.

  • Polyspace does not flag the reuse of an identifier for global functions and their arguments that are declared without the static class specifier.

The checker is not raised on unused code such as

  • Noninstantiated templates

  • Uncalled static or extern functions

  • Uncalled and undefined local functions

  • Unused types and variables

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

file1.cpp

#include <cstdint>

namespace first_namespace
{
	static std::int32_t global_var = 0; //Noncompliant - identifier reused

}
static std::int32_t file_var = 10; //Compliant - identifier not reused

file2.cpp

;

#include <cstdint>

namespace first_namespace
{
	static std::int32_t global_var = 0;  // identifier reused
	static std::int16_t module_var = 20; // Compliant - identifier not reused
}




namespace second_namespace
{

	void globalfunc(int argument) // non-static global function and arguments do not raise violation
	{
		int local_var; // local variable
		static std::int16_t local_static; // Object with no linkage
	}
	std::int16_t globalvar_reusedinlocal; 
	std::int16_t globalvar_notreused; // Compliant, identifier not reused
	void foo(){
		++globalvar_reusedinlocal;
		++globalvar_notreused;
	}
};

namespace third_namespace
{

	void globalfunc(int argument) // non-static global function and arguments do not raise violation
	{
		static std::int16_t local_static; // Object with no linkage
		int local_var; // local variable
		int globalvar_reusedinlocal; // Non-compliant, identifier reused in local variable
		++globalvar_reusedinlocal;
	}

};

In this example, global_var is declared with the static class specifier in source file file1.cpp. This identifier is reused in source file file2.cpp. In the same file, globalvar_reusedinlocal is declared in second_namespace and has external linkage. This declaration is non-compliant because the identifier is reused for the local variable in globalfunc.

Check Information

Group: Lexical conventions
Category: Advisory, Automated

Version History

Introduced in R2020b