Author: Yuki Date: 2026-01-05T02:23:10Z New Revision: 05b8a36f1b542ae903cac11de1bbb26695b7fdac
URL: https://github.com/llvm/llvm-project/commit/05b8a36f1b542ae903cac11de1bbb26695b7fdac DIFF: https://github.com/llvm/llvm-project/commit/05b8a36f1b542ae903cac11de1bbb26695b7fdac.diff LOG: [Clang][Diagnostics] Mention 'import std' in typeid diagnostic (#173236) Previously, the diagnostic only suggested including `<typeinfo>`. Since C++20,the standard library may also be made available via `import std;`. This change updates the diagnostic to mention `import std` as an alternative and adds a test to cover the new wording. Added: clang/test/SemaCXX/typeid-requires-typeinfo.cpp Modified: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaExprCXX.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 6c6a26614ad0e..4f7934b328d9f 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>%select{| or import std}0 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..91967a7a9ff97 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -644,8 +644,10 @@ 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()) { + return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid) + << (getLangOpts().CPlusPlus20 ? 1 : 0)); + } if (!CXXTypeInfoDecl) { IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); @@ -659,7 +661,8 @@ Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); } if (!CXXTypeInfoDecl) - return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); + return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid) + << (getLangOpts().CPlusPlus20 ? 1 : 0)); } if (!getLangOpts().RTTI) { diff --git a/clang/test/SemaCXX/typeid-requires-typeinfo.cpp b/clang/test/SemaCXX/typeid-requires-typeinfo.cpp new file mode 100644 index 0000000000000..ee6efada474b0 --- /dev/null +++ b/clang/test/SemaCXX/typeid-requires-typeinfo.cpp @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify=cxx20 -std=c++20 %s +// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify=cxx17 -std=c++17 %s + +class A{}; + +auto f() { + // cxx20-error@+2 {{you need to include <typeinfo> or import std before using the 'typeid' operator}} + // cxx17-error@+1 {{you need to include <typeinfo> before using the 'typeid' operator}} + return typeid(A); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
