Main Content

AUTOSAR C++14 Rule A2-8-1

A header file name should reflect the logical entity for which it provides declarations.

Since R2021a

Description

Rule Definition

A header file name should reflect the logical entity for which it provides declarations.

Rationale

A header file name that matches the name of the entity that is declared in that file makes your #include directives clearer and your code more readable.

Polyspace Implementation

Polyspace® checks the header file name against the name of relevant declared types such as class or struct, or namespace names. If the names do not match, Polyspace flags the first character on the first line of the header file.

  • The name comparison is case insensitive. For instance, myheader matches myHeader.

  • The name comparison ignores:

    • The underscore character '_'. For instance, myheader matches my_Header.

    • Prefix characters 'C', 'M', 'T', or suffix character 'T'. The comparison ignores either the prefix or suffix characters, but not both. For instance, myheader matches CmyHeader and myHeader_T, but not CmyHeader_T.

    • The hyphen character '-' in file names. For instance, a file named my-header.h matches a struct named _myHeader.

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

myheader.h

//Noncompliant - header name does not match the class name
#include <memory> 
#include <string>

class myClass
{
	virtual ~myClass()=default;
};

class Player : public myClass
{
	std::string Name;
	int Rank;
};

file.cpp

#include "myheader.h"

int main(){
  return 0;
}

In this example, the name of header file myheader.h is not compliant because it does not match the name of the base class (myClass) declared in that header file.

Check Information

Group: Lexical conventions
Category: Required, Non-automated

Version History

Introduced in R2021a