Description

An unnamed namespace will be unique within each translation unit. Any declarations appearing in an unnamed namespace in a header will refer to a different entity in each translation unit, which is probably not the expected behavior.

Noncompliant Code Example

// Header.hpp
namespace                  // Noncompliant
{
  extern int32_t x;
}
// File1.cpp
#include "Header.hpp"

namespace
{
  int32_t x;
}

void fn_a(void)
{
  x = 42;
}
// File2.cpp
#include "Header.hpp"

namespace
{
  int32_t x;  // this is a different x than in File1.cpp
}

void fn_b(void)
{
  fn_a();                  // Is expected to initialize "x" to 42
  if (x == 42)             // But does not, as there are 2 distinct "x" variables
  {
     // I am NOT an auto link in some source code: S987.
  }
}

Mentioning S987 RSPEC-987 here for testing purposes.

See

  • MISRA C++:2008, 7-3-3 - There shall be no unnamed namespaces in header files.

  • CERT, DCL59-CPP. - Do not define an unnamed namespace in a header file


(visible only on this page)

on 8 Feb 2018, 00:43:04 Thomas Epperson wrote:

The implementation incorrectly flags unnamed namespaces in source files. The specs only refer to unnamed namespaces in HEADER files.

See reported bugs in https://sonarcloud.io/dashboard?id=uglyoldbob_decompiler%3Arestructure