Clang warning: 'deprecated' attribute on anonymous namespace ignored [-Wignored-attributes] (warn_deprecated_anonymous_namespace)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: 'deprecated' attribute on anonymous namespace ignored
Type Warning
Category Semantic Issue
Internal Id warn_deprecated_anonymous_namespace
Active by Default Yes
Flags -Wno-attributes (85 elements)
-Wno-ignored-attributes (84 elements)
Internal Message 'deprecated' attribute on anonymous namespace ignored
Regular Expression (?:warning|error|fatal error)\: 'deprecated' attribute on anonymous namespace ignored \[(?:\-Werror,)?\-Wignored\-attributes[^\]]*\]
First Commit 2014-11-14 43f40103f045 [c++1z] Support [[deprecated]] attributes on namespaces. Note that it only applies to situations whe...

Description

The warning is issued by the Clang compiler when the deprecated attribute is applied to an anonymous namespace. In C++17 and later, the deprecated attribute allows marking entities as deprecated, which serves as an indicator that they are expected to be removed or replaced in future versions of the code. However, using this attribute on anonymous namespaces is considered semantically incorrect because anonymous namespaces are not accessible or identifiable outside their translation units, making the deprecation notice practically meaningless. As a result, Clang ignores the deprecated attribute on anonymous namespaces and emits this warning to inform the developer about the misuse of the attribute. The warning aims to guide developers towards correct usage patterns of deprecation attributes and maintain the intended semantics of anonymous namespaces.  
AI Generated

Example

In the following example, an anonymous namespace is marked with the deprecated attribute. Normally, the deprecated attribute is used to indicate that a certain feature or entity should not be used as it may be removed or replaced in a future version. However, applying this attribute to an anonymous namespace doesn't have the intended effect because anonymous namespaces are unique to each translation unit and are not accessible outside of it. Thus, marking an anonymous namespace as deprecated does not serve a practical purpose since its contents are already confined to its translation unit. The compiler generates a specific warning to highlight that the deprecated attribute on an anonymous namespace has been ignored, as there is no meaningful way to deprecate an anonymous namespace that aligns with the attribute's intended use for signaling obsolescence to external users.  
AI Generated


Flags -xc++ -std=c++17

[Try out in Compiler Explorer]

Source
// Deprecated attribute on anonymous namespace
namespace [[deprecated]] { int f() { return 42; } }
Compiler Output
<source>:2:13: warning: 'deprecated' attribute on anonymous namespace ignored [-Wignored-attributes]


Clang Internals (17.0.6)

Git Commit Message

[c++1z] Support [[deprecated]] attributes on namespaces. Note that it only applies to situations where the namespace is mentioned. Thus, use on anonymous namespaces is diagnosed.

llvm-svn: 222054

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDeclAttr.cpp (line 8172)

static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
  if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
    if (NSD->isAnonymousNamespace()) {
      S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaCXX/cxx-deprecated.cpp

  • clang/test/SemaCXX/cxx-deprecated.cpp:3:13: warning: 'deprecated' attribute on anonymous namespace ignored [-Wignored-attributes]