Skip to content

clang: Make the type_info builtin declaration a singleton #151277

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
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ Bug Fixes in This Version

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Fix an ambiguous reference to the builtin `type_info` (available when using
`-fms-compatibility`) with modules. (#GH38400)

Bug Fixes to Attribute Support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
// Implicitly-declared type 'struct _GUID'.
mutable TagDecl *MSGuidTagDecl = nullptr;

// Implicitly-declared type 'struct type_info'.
mutable TagDecl *MSTypeInfoTagDecl = nullptr;

/// Keep track of CUDA/HIP device-side variables ODR-used by host code.
/// This does not include extern shared variables used by device host
/// functions as addresses of shared variables are per warp, therefore
Expand Down Expand Up @@ -2381,6 +2384,15 @@ class ASTContext : public RefCountedBase<ASTContext> {
return getTagDeclType(MSGuidTagDecl);
}

/// Retrieve the implicitly-predeclared 'struct type_info' declaration.
TagDecl *getMSTypeInfoTagDecl() const {
// Lazily create this type on demand - it's only needed for MS builds.
if (!MSTypeInfoTagDecl) {
MSTypeInfoTagDecl = buildImplicitRecord("type_info");
}
return MSTypeInfoTagDecl;
}

/// Return whether a declaration to a builtin is allowed to be
/// overloaded/redeclared.
bool canBuiltinBeRedeclared(const FunctionDecl *) const;
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/AST/DeclID.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ enum PredefinedDeclIDs {
/// The internal '__NSConstantString' tag type.
PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,

/// The predeclared 'type_info' struct.
PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID,

#define BuiltinTemplate(BTName) PREDEF_DECL##BTName##_ID,
#include "clang/Basic/BuiltinTemplates.inc"

Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Sema/Sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,7 @@ void Sema::Initialize() {
if (getLangOpts().MSVCCompat) {
if (getLangOpts().CPlusPlus &&
IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end())
PushOnScopeChains(
Context.buildImplicitRecord("type_info", TagTypeKind::Class),
TUScope);
PushOnScopeChains(Context.getMSTypeInfoTagDecl(), TUScope);

addImplicitTypedef("size_t", Context.getSizeType());
}
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8318,6 +8318,9 @@ Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
NewLoaded = Context.getCFConstantStringTagDecl();
break;

case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
return Context.getMSTypeInfoTagDecl();

#define BuiltinTemplate(BTName) \
case PREDEF_DECL##BTName##_ID: \
if (Context.Decl##BTName) \
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5618,6 +5618,8 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
RegisterPredefDecl(Context.MSGuidTagDecl,
PREDEF_DECL_BUILTIN_MS_GUID_ID);
RegisterPredefDecl(Context.MSTypeInfoTagDecl,
PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID);
RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
RegisterPredefDecl(Context.CFConstantStringTypeDecl,
PREDEF_DECL_CF_CONSTANT_STRING_ID);
Expand Down
15 changes: 15 additions & 0 deletions clang/test/Modules/pr151277.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: split-file %s %t

// RUN: %clang_cc1 -I%t -emit-module -o %t/a.pcm -fmodules %t/module.modulemap -fno-implicit-modules -fmodule-name=a -x c++-header -fms-compatibility
// RUN: %clang_cc1 -I%t -emit-module -o %t/b.pcm -fmodules %t/module.modulemap -fno-implicit-modules -fmodule-name=b -x c++-header -fms-compatibility -fmodule-file=%t/a.pcm

//--- module.modulemap
module a { header "a.h" }
module b { header "b.h" }

//--- a.h
type_info* foo;

//--- b.h
type_info* bar;