Skip to content

Commit 3c7dab0

Browse files
committed
clang: Fix ambiguous type_info when using clang modules with
-fms-compatibility. We solve this the same way clang solves other builtin records - by ensuring that the builtin is a singleton. Fixes #38400
1 parent b103311 commit 3c7dab0

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
12901290
// Implicitly-declared type 'struct _GUID'.
12911291
mutable TagDecl *MSGuidTagDecl = nullptr;
12921292

1293+
// Implicitly-declared type 'struct type_info'.
1294+
mutable TagDecl *MSTypeInfoTagDecl = nullptr;
1295+
12931296
/// Keep track of CUDA/HIP device-side variables ODR-used by host code.
12941297
/// This does not include extern shared variables used by device host
12951298
/// functions as addresses of shared variables are per warp, therefore
@@ -2381,6 +2384,15 @@ class ASTContext : public RefCountedBase<ASTContext> {
23812384
return getTagDeclType(MSGuidTagDecl);
23822385
}
23832386

2387+
/// Retrieve the implicitly-predeclared 'struct type_info' declaration.
2388+
TagDecl *getMSTypeInfoTagDecl() const {
2389+
// Lazily create this type on demand - it's only needed for MS builds.
2390+
if (!MSTypeInfoTagDecl) {
2391+
MSTypeInfoTagDecl = buildImplicitRecord("type_info");
2392+
}
2393+
return MSTypeInfoTagDecl;
2394+
}
2395+
23842396
/// Return whether a declaration to a builtin is allowed to be
23852397
/// overloaded/redeclared.
23862398
bool canBuiltinBeRedeclared(const FunctionDecl *) const;

clang/include/clang/AST/DeclID.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ enum PredefinedDeclIDs {
7777
/// The internal '__NSConstantString' tag type.
7878
PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
7979

80+
/// The predeclared 'type_info' struct.
81+
PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID,
82+
8083
#define BuiltinTemplate(BTName) PREDEF_DECL##BTName##_ID,
8184
#include "clang/Basic/BuiltinTemplates.inc"
8285

clang/lib/Sema/Sema.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,7 @@ void Sema::Initialize() {
443443
if (getLangOpts().MSVCCompat) {
444444
if (getLangOpts().CPlusPlus &&
445445
IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end())
446-
PushOnScopeChains(
447-
Context.buildImplicitRecord("type_info", TagTypeKind::Class),
448-
TUScope);
446+
PushOnScopeChains(Context.getMSTypeInfoTagDecl(), TUScope);
449447

450448
addImplicitTypedef("size_t", Context.getSizeType());
451449
}

clang/lib/Serialization/ASTReader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8318,6 +8318,9 @@ Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
83188318
NewLoaded = Context.getCFConstantStringTagDecl();
83198319
break;
83208320

8321+
case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8322+
return Context.getMSTypeInfoTagDecl();
8323+
83218324
#define BuiltinTemplate(BTName) \
83228325
case PREDEF_DECL##BTName##_ID: \
83238326
if (Context.Decl##BTName) \

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5618,6 +5618,8 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
56185618
PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
56195619
RegisterPredefDecl(Context.MSGuidTagDecl,
56205620
PREDEF_DECL_BUILTIN_MS_GUID_ID);
5621+
RegisterPredefDecl(Context.MSTypeInfoTagDecl,
5622+
PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID);
56215623
RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
56225624
RegisterPredefDecl(Context.CFConstantStringTypeDecl,
56235625
PREDEF_DECL_CF_CONSTANT_STRING_ID);

0 commit comments

Comments
 (0)