Skip to content

No longer diagnose __auto_type as the auto extension #134129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025

Conversation

AaronBallman
Copy link
Collaborator

Given:

__auto_type x = 12;
decltype(auto) y = 12;

-Wc++98-compat would diagnose both x and y with:

'auto' type specifier is incompatible with C++98

This patch silences the diagnostic in those cases. decltype(auto) is still diagnosed with:

'decltype(auto)' type specifier is incompatible with C++ standards before C++14

as expected but no longer produces the extraneous diagnostic about use of 'auto'.

Fixes #47900

Given:

  __auto_type x = 12;
  decltype(auto) y = 12;

-Wc++98-compat would diagnose both x and y with:

'auto' type specifier is incompatible with C++98

This patch silences the diagnostic in those cases. decltype(auto) is
still diagnosed with:

'decltype(auto)' type specifier is incompatible with C++ standards
before C++14

as expected but no longer produces the extraneous diagnostic about use
of 'auto'.

Fixes llvm#47900
@AaronBallman AaronBallman added clang Clang issues not falling into any other category c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" quality-of-implementation labels Apr 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 2, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

Given:

__auto_type x = 12;
decltype(auto) y = 12;

-Wc++98-compat would diagnose both x and y with:

'auto' type specifier is incompatible with C++98

This patch silences the diagnostic in those cases. decltype(auto) is still diagnosed with:

'decltype(auto)' type specifier is incompatible with C++ standards before C++14

as expected but no longer produces the extraneous diagnostic about use of 'auto'.

Fixes #47900


Full diff: https://github.com/llvm/llvm-project/pull/134129.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Sema/SemaType.cpp (+12-7)
  • (modified) clang/test/SemaCXX/cxx98-compat.cpp (+10)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7978df0cc71cc..f3738a9608a9f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -315,6 +315,9 @@ Improvements to Clang's diagnostics
 
 - Split diagnosing base class qualifiers from the ``-Wignored-Qualifiers`` diagnostic group into a new ``-Wignored-base-class-qualifiers`` diagnostic group (which is grouped under ``-Wignored-qualifiers``). Fixes #GH131935.
 
+- ``-Wc++98-compat`` no longer diagnoses use of ``__auto_type`` or
+  ``decltype(auto)`` as though it was the extension for ``auto``. (#GH47900)
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 2df961a48c7c3..20240fdaf2bbb 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3376,13 +3376,18 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
     } else if (Auto && D.getContext() != DeclaratorContext::LambdaExpr) {
       // If there was a trailing return type, we already got
       // warn_cxx98_compat_trailing_return_type in the parser.
-      SemaRef.Diag(AutoRange.getBegin(),
-                   D.getContext() == DeclaratorContext::LambdaExprParameter
-                       ? diag::warn_cxx11_compat_generic_lambda
-                   : IsDeducedReturnType
-                       ? diag::warn_cxx11_compat_deduced_return_type
-                       : diag::warn_cxx98_compat_auto_type_specifier)
-          << AutoRange;
+      // If there was a decltype(auto), we already got
+      // warn_cxx11_compat_decltype_auto_type_specifier.
+      unsigned DiagId = 0;
+      if (D.getContext() == DeclaratorContext::LambdaExprParameter)
+        DiagId = diag::warn_cxx11_compat_generic_lambda;
+      else if (IsDeducedReturnType)
+        DiagId = diag::warn_cxx11_compat_deduced_return_type;
+      else if (Auto->getKeyword() == AutoTypeKeyword::Auto)
+        DiagId = diag::warn_cxx98_compat_auto_type_specifier;
+
+      if (DiagId)
+        SemaRef.Diag(AutoRange.getBegin(), DiagId) << AutoRange;
     }
   }
 
diff --git a/clang/test/SemaCXX/cxx98-compat.cpp b/clang/test/SemaCXX/cxx98-compat.cpp
index 43ba208d375cd..8e7acf73923e5 100644
--- a/clang/test/SemaCXX/cxx98-compat.cpp
+++ b/clang/test/SemaCXX/cxx98-compat.cpp
@@ -14,6 +14,16 @@ namespace std {
   };
 }
 
+void test_other_auto_spellings() {
+  __auto_type x = 0; // Ok
+  decltype(auto) y = 0; // expected-warning {{'decltype' type specifier is incompatible with C++98}}
+#ifndef CXX14COMPAT
+  // expected-warning@-2 {{'decltype(auto)' type specifier is a C++14 extension}}
+#else
+  // expected-warning@-4 {{'decltype(auto)' type specifier is incompatible with C++ standards before C++14}}
+#endif
+}
+
 template<typename ...T>  // expected-warning {{variadic templates are incompatible with C++98}}
 class Variadic1 {};
 

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable to me

@AaronBallman AaronBallman merged commit 7febd78 into llvm:main Apr 3, 2025
17 checks passed
@AaronBallman AaronBallman deleted the aballman-auto-type-compat branch April 3, 2025 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category quality-of-implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

-Wc++98-compat thinks __auto_type is auto
3 participants