@@ -2286,21 +2286,6 @@ static void DiagnoseNonTriviallyCopyableReason(Sema &SemaRef,
22862286 SemaRef.Diag (D->getLocation (), diag::note_defined_here) << D;
22872287}
22882288
2289- static bool hasMixedAccessSpecifier (const CXXRecordDecl *D) {
2290- AccessSpecifier FirstAccess = AS_none;
2291- for (const FieldDecl *Field : D->fields ()) {
2292-
2293- if (Field->isUnnamedBitField ())
2294- continue ;
2295- AccessSpecifier FieldAccess = Field->getAccess ();
2296- if (FirstAccess == AS_none)
2297- FirstAccess = FieldAccess;
2298- else if (FieldAccess != FirstAccess)
2299- return true ;
2300- }
2301- return false ;
2302- }
2303-
23042289static bool hasMultipleDataBaseClassesWithFields (const CXXRecordDecl *D) {
23052290 int NumBasesWithFields = 0 ;
23062291 for (const CXXBaseSpecifier &Base : D->bases ()) {
@@ -2334,30 +2319,37 @@ static void DiagnoseNonStandardLayoutReason(Sema &SemaRef, SourceLocation Loc,
23342319 << B.getSourceRange ();
23352320 }
23362321 }
2337- if ( hasMixedAccessSpecifier (D)) {
2338- SemaRef. Diag (Loc, diag::note_unsatisfied_trait_reason)
2339- << diag::TraitNotSatisfiedReason::MixedAccess ;
2322+ // Check for mixed access specifiers in fields.
2323+ const FieldDecl *FirstField = nullptr ;
2324+ AccessSpecifier FirstAccess = AS_none ;
23402325
2341- const FieldDecl *FirstField = nullptr ;
2342- AccessSpecifier FirstAcc = AS_none;
2343- for (const FieldDecl *F : D->fields ()) {
2344- if (F->isUnnamedBitField ())
2345- continue ;
2346- if (FirstField == nullptr ) {
2347- FirstField = F;
2348- FirstAcc = F->getAccess ();
2349- } else if (F->getAccess () != FirstAcc) {
2350- SemaRef.Diag (FirstField->getLocation (), diag::note_defined_here)
2351- << FirstField;
2352-
2353- SemaRef.Diag (F->getLocation (), diag::note_unsatisfied_trait_reason)
2354- << diag::TraitNotSatisfiedReason::MixedAccessField
2355- << F // %0: second field
2356- << FirstField; // %1: first field
2357- break ;
2358- }
2326+ for (const FieldDecl *Field : D->fields ()) {
2327+ if (Field->isUnnamedBitField ())
2328+ continue ;
2329+
2330+ // Record the first field we see
2331+ if (!FirstField) {
2332+ FirstField = Field;
2333+ FirstAccess = Field->getAccess ();
2334+ continue ;
2335+ }
2336+
2337+ // Check if the field has a different access specifier than the first one.
2338+ if (Field->getAccess () != FirstAccess) {
2339+ // Emit a diagnostic about mixed access specifiers.
2340+ SemaRef.Diag (Loc, diag::note_unsatisfied_trait_reason)
2341+ << diag::TraitNotSatisfiedReason::MixedAccess;
2342+
2343+ SemaRef.Diag (FirstField->getLocation (), diag::note_defined_here)
2344+ << FirstField;
2345+
2346+ SemaRef.Diag (Field->getLocation (), diag::note_unsatisfied_trait_reason)
2347+ << diag::TraitNotSatisfiedReason::MixedAccessField << Field
2348+ << FirstField;
2349+
2350+ // No need to check further fields, as we already found mixed access.
2351+ return ;
23592352 }
2360- return ;
23612353 }
23622354 if (hasMultipleDataBaseClassesWithFields (D)) {
23632355 SemaRef.Diag (Loc, diag::note_unsatisfied_trait_reason)
0 commit comments