https://github.com/AltriaSuki updated https://github.com/llvm/llvm-project/pull/173236
>From e3b21647a8f9facebe13ecd4236cd3f7a72f2b3c Mon Sep 17 00:00:00 2001 From: feilun <[email protected]> Date: Mon, 22 Dec 2025 18:04:49 +0800 Subject: [PATCH 1/2] [Clang][Diagnostics] Mention 'import std' in typeid diagnostic --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +- clang/test/SemaCXX/typeid-requires-typeinfo.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 clang/test/SemaCXX/typeid-requires-typeinfo.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 49eee0c2fa617..a8bff4d789b38 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -8429,7 +8429,7 @@ def err_bad_dynamic_cast_not_polymorphic : Error<"%0 is not polymorphic">; // Other C++ expressions def err_need_header_before_typeid : Error< - "you need to include <typeinfo> before using the 'typeid' operator">; + "you need to include <typeinfo> or import std before using the 'typeid' operator">; def err_need_header_before_placement_new : Error< "no matching %0 function for non-allocating placement new expression; " "include <new>">; diff --git a/clang/test/SemaCXX/typeid-requires-typeinfo.cpp b/clang/test/SemaCXX/typeid-requires-typeinfo.cpp new file mode 100644 index 0000000000000..ec8773b438c28 --- /dev/null +++ b/clang/test/SemaCXX/typeid-requires-typeinfo.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++17 %s + +class A{}; + +auto f() { + return typeid(A); +} + +// CHECK: error: you need to include <typeinfo> or import std before using the 'typeid' operator \ No newline at end of file >From e34e4c6de922f6289487d890a11a143086ccaa52 Mon Sep 17 00:00:00 2001 From: feilun <[email protected]> Date: Thu, 25 Dec 2025 10:35:06 +0800 Subject: [PATCH 2/2] [Clang][Diagnostics] Suggest 'import std' in typeid diagnostic for C++20+ In C++20 and later, when typeid is used without including <typeinfo>, the diagnostic now suggests either including <typeinfo> or importing std, since modules are available in C++20. For C++17 and earlier versions, the diagnostic continues to only suggest including <typeinfo>, as modules are not available in those standards. Close #172966 --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 +++- clang/lib/Sema/SemaExprCXX.cpp | 8 ++++++-- clang/test/SemaCXX/typeid-requires-typeinfo.cpp | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index eb6d0d211524c..a4dbb07ec2de5 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -8428,8 +8428,10 @@ def err_bad_dynamic_cast_not_ptr : Error<"cannot use dynamic_cast to convert fro def err_bad_dynamic_cast_not_polymorphic : Error<"%0 is not polymorphic">; // Other C++ expressions -def err_need_header_before_typeid : Error< +def err_need_header_or_std_before_typeid : Error< "you need to include <typeinfo> or import std before using the 'typeid' operator">; +def err_need_header_before_typeid : Error< + "you need to include <typeinfo> before using the 'typeid' operator">; def err_need_header_before_placement_new : Error< "no matching %0 function for non-allocating placement new expression; " "include <new>">; diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 965ad55465db7..977f3216b8520 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -644,8 +644,12 @@ Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, } // Find the std::type_info type. - if (!getStdNamespace()) - return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); + if (!getStdNamespace()){ + auto DiagID = getLangOpts().CPlusPlus20 + ? diag::err_need_header_or_std_before_typeid + : diag::err_need_header_before_typeid; + return ExprError(Diag(OpLoc, DiagID)); + } if (!CXXTypeInfoDecl) { IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); diff --git a/clang/test/SemaCXX/typeid-requires-typeinfo.cpp b/clang/test/SemaCXX/typeid-requires-typeinfo.cpp index ec8773b438c28..dc20c6b3dcb14 100644 --- a/clang/test/SemaCXX/typeid-requires-typeinfo.cpp +++ b/clang/test/SemaCXX/typeid-requires-typeinfo.cpp @@ -1,3 +1,4 @@ +// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++20 %s // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify -std=c++17 %s class A{}; @@ -6,4 +7,5 @@ auto f() { return typeid(A); } -// CHECK: error: you need to include <typeinfo> or import std before using the 'typeid' operator \ No newline at end of file +// cxx20-error@-2 {{you need to include <typeinfo> or import std before using the 'typeid' operator}} +// cxx17-error@-2 {{you need to include <typeinfo> before using the 'typeid' operator}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
