@@ -3205,6 +3205,17 @@ void ASTWriter::WriteType(QualType T) {
3205
3205
// Declaration Serialization
3206
3206
// ===----------------------------------------------------------------------===//
3207
3207
3208
+ static bool IsInternalDeclFromFileContext (const Decl *D) {
3209
+ auto *ND = dyn_cast<NamedDecl>(D);
3210
+ if (!ND)
3211
+ return false ;
3212
+
3213
+ if (!D->getDeclContext ()->getRedeclContext ()->isFileContext ())
3214
+ return false ;
3215
+
3216
+ return ND->getFormalLinkage () == Linkage::Internal;
3217
+ }
3218
+
3208
3219
// / Write the block containing all of the declaration IDs
3209
3220
// / lexically declared within the given DeclContext.
3210
3221
// /
@@ -3225,6 +3236,15 @@ uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3225
3236
if (DoneWritingDeclsAndTypes && !wasDeclEmitted (D))
3226
3237
continue ;
3227
3238
3239
+ // We don't need to write decls with internal linkage into reduced BMI.
3240
+ // If such decls gets emitted due to it get used from inline functions,
3241
+ // the program illegal. However, there are too many use of static inline
3242
+ // functions in the global module fragment and it will be breaking change
3243
+ // to forbid that. So we have to allow to emit such declarations from GMF.
3244
+ if (GeneratingReducedBMI && !D->isFromExplicitGlobalModule () &&
3245
+ IsInternalDeclFromFileContext (D))
3246
+ continue ;
3247
+
3228
3248
KindDeclPairs.push_back (D->getKind ());
3229
3249
KindDeclPairs.push_back (GetDeclRef (D).get ());
3230
3250
}
@@ -3886,6 +3906,13 @@ class ASTDeclContextNameLookupTrait {
3886
3906
!Writer.wasDeclEmitted (DeclForLocalLookup))
3887
3907
continue ;
3888
3908
3909
+ // Try to avoid writing internal decls to reduced BMI.
3910
+ // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
3911
+ if (Writer.isGeneratingReducedBMI () &&
3912
+ !DeclForLocalLookup->isFromExplicitGlobalModule () &&
3913
+ IsInternalDeclFromFileContext (DeclForLocalLookup))
3914
+ continue ;
3915
+
3889
3916
DeclIDs.push_back (Writer.GetDeclRef (DeclForLocalLookup));
3890
3917
}
3891
3918
return std::make_pair (Start, DeclIDs.size ());
@@ -4257,6 +4284,12 @@ uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4257
4284
if (DoneWritingDeclsAndTypes && !wasDeclEmitted (ND))
4258
4285
continue ;
4259
4286
4287
+ // We don't need to force emitting internal decls into reduced BMI.
4288
+ // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4289
+ if (GeneratingReducedBMI && !ND->isFromExplicitGlobalModule () &&
4290
+ IsInternalDeclFromFileContext (ND))
4291
+ continue ;
4292
+
4260
4293
GetDeclRef (ND);
4261
4294
}
4262
4295
}
@@ -4917,8 +4950,7 @@ void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
4917
4950
// is ill-formed. However, in practice, there are a lot of projects
4918
4951
// uses `static inline` in the headers. So we can't get rid of all
4919
4952
// static entities in reduced BMI now.
4920
- if (auto *ND = dyn_cast<NamedDecl>(D);
4921
- ND && ND->getFormalLinkage () == Linkage::Internal)
4953
+ if (IsInternalDeclFromFileContext (D))
4922
4954
continue ;
4923
4955
}
4924
4956
0 commit comments