Clang warning: 'register' storage class specifier is deprecated and incompatible with C++17 [-Wdeprecated-register] (warn_deprecated_register)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: 'register' storage class specifier is deprecated and incompatible with C++1z
Type Warning
Category Deprecations
Internal Id warn_deprecated_register
Active by Default Yes
Flags -Wno-c++17-compat (45 elements)
-Wno-c++17-compat-pedantic (89 elements)
-Wno-c++1z-compat (45 elements)
-Wno-deprecated (43 elements)
-Wno-deprecated-register (1 element)
-Wno-register (2 elements)
Internal Message 'register' storage class specifier is deprecated and incompatible with C++1z
Regular Expression (?:warning|error|fatal error)\: 'register' storage class specifier is deprecated and incompatible with C\+\+17 \[(?:\-Werror,)?\-Wdeprecated\-register[^\]]*\]
First Commit 2013-06-13 8ca78a16f4a5 Add -Wdeprecated warnings and fixits for things deprecated in C++11:

Description

The warning is issued by the Clang compiler when the register storage class specifier is used in C++ code. This specifier was deprecated in C++11 and made incompatible with the C++17 standard, reflecting the language's continuous evolution and the removal of outdated features. The register keyword was originally intended to suggest that a variable should be stored in a processor register for faster access. However, modern compilers are now highly optimized to autonomously make such decisions, rendering the manual specification of register both unnecessary and potentially misleading. Consequently, its use can lead to compatibility issues with C++17 and later versions, which is why this warning is generated.  
AI Generated

Example

In the following example, a variable x is declared with the register storage class specifier within the main function of a C++ program. The specifier register is intended to suggest that the variable x should be stored in a processor register for potentially faster access. This declaration demonstrates the use of register in its simplest form, assigning an initial value to a single integer variable. However, this usage triggers a warning because the register storage class specifier is deprecated in C++17. This indicates that its use is outdated and not compatible with the C++17 standard and subsequent versions, reflecting the evolution of C++ and the compiler's capability to make optimization decisions autonomously.  
AI Generated


Flags -xc++ -std=c++11

[Try out in Compiler Explorer]

Source
int main() {
  // Deprecated in C++17
  register int x = 0;
  return x;
}
Compiler Output
<source>:3:3: warning: 'register' storage class specifier is deprecated and incompatible with C++17 [-Wdeprecated-register]


Clang Internals (17.0.6)

Git Commit Message

Add -Wdeprecated warnings and fixits for things deprecated in C++11:
 - 'register' storage class
 - dynamic exception specifications

Only the former check is enabled by default for now (the latter might be quite noisy).

llvm-svn: 183881

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/SemaDecl.cpp (line 7496)

NamedDecl *Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, TypeSourceInfo *TInfo, LookupResult &Previous, MultiTemplateParamsArg TemplateParamLists, bool &AddToScope, ArrayRef<BindingDecl *> Bindings) {
  // ...
  if (getLangOpts().CPlusPlus11 && SCSpec == DeclSpec::SCS_register && !D.getAsmLabel() && !getSourceManager().isInSystemMacro(D.getDeclSpec().getStorageClassSpecLoc())) {
    // ...
    Diag(D.getDeclSpec().getStorageClassSpecLoc(), getLangOpts().CPlusPlus17 ? diag::ext_register_storage_class : diag::warn_deprecated_register) << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());

clang/lib/Sema/SemaDecl.cpp (line 14738)

/// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator()
/// to introduce parameters into function prototype scope.
Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) {
  // ...
  if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
    // ...
    // In C++11, the 'register' storage class specifier is deprecated.
    // In C++17, it is not allowed, but we tolerate it as an extension.
    if (getLangOpts().CPlusPlus11) {
      Diag(DS.getStorageClassSpecLoc(), getLangOpts().CPlusPlus17 ? diag::ext_register_storage_class : diag::warn_deprecated_register) << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());

Triggered in Clang Tests

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

clang/test/SemaCXX/attr-cxx0x.cpp

  • clang/test/SemaCXX/attr-cxx0x.cpp:15:14: warning: 'register' storage class specifier is deprecated and incompatible with C++17 [-Wdeprecated-register]