[previous] 1504 [next] The object 'name' is only referenced in the translation unit where it is defined.
CMA maintenance checks

This object / function is only used in the same translation unit where it is defined. As this function is not used outside of this translation unit, it would be better practice to reduce its visibility by declaring it static in C or moving it into an unnamed namespace in C++.

Any declarations to the function should be removed, or if they appear in the same source file, made static.

Reducing the visibility of objects and functions ensures that future modifications are in less danger of silently compiling and linking with unintentional behaviour.

For Example:

// TU1.cpp
int sort ()
{
  // implement sort specific to TU1
}

void foo()
{
  sort ();
}

// TU2.cpp
void bar()
{
}

A new library is added with an external function 'Sort', and is to be used in TU2.

Example:

// TU1.cpp
int sort ()
{
  // implement sort specific to TU1
}

void foo()
{
  sort ();
}

// TU2.cpp
int sort (); // typo here.  'S' not 's' in sort.

void bar()
{
  sort ();
}

The code still compiles and links, however the logic is flawed. Restricting the visibility of function 'sort' in TU1 would have resulted in a link error, as sort would no longer be available to other translation units.

For Example:

// TU1.cpp
static int sort ()
{
  // implement sort specific to TU1
}

void foo()
{
  sort ();
}

// TU2.cpp
int sort (); // typo here.  'S' not 's' in sort.

void bar()
{
  sort ();   // OK:        Causes link error, 'sort' is not defined
}


MISRA C:2012 Rules applicable to message 1504:

Rule-8.7  (Advisory) Functions and objects should not be defined with external linkage if they are referenced in only one translation unit


See also:

QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents