Skip to content

Commit 3a9440b

Browse files
authored
[Sema] Keep attribute lists in the order the attributes were parsed (#162714)
This renames some attribute list related functions, to make callers think about whether they want to append or prepend to the list, instead of defaulting to prepending which is often not the desired behaviour (for the cases where it matters, sometimes we're just adding to an empty list). Then it adjusts some of these calls to append where they were previously prepending. This has the effect of making `err_attributes_are_not_compatible` consistent in emitting diagnostics as `<new-attr> and <existing-attr> are not compatible`, regardless of the syntax used to apply the attributes.
1 parent a1b5e97 commit 3a9440b

File tree

14 files changed

+51
-51
lines changed

14 files changed

+51
-51
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,7 @@ class Parser : public CodeCompletionHandler {
21732173
if (Tok.is(tok::kw___attribute)) {
21742174
ParsedAttributes Attrs(AttrFactory);
21752175
ParseGNUAttributes(Attrs, LateAttrs, &D);
2176-
D.takeAttributes(Attrs);
2176+
D.takeAttributesAppending(Attrs);
21772177
}
21782178
}
21792179

@@ -2272,7 +2272,7 @@ class Parser : public CodeCompletionHandler {
22722272
if (isAllowedCXX11AttributeSpecifier()) {
22732273
ParsedAttributes Attrs(AttrFactory);
22742274
ParseCXX11Attributes(Attrs);
2275-
D.takeAttributes(Attrs);
2275+
D.takeAttributesAppending(Attrs);
22762276
}
22772277
}
22782278

@@ -2292,7 +2292,7 @@ class Parser : public CodeCompletionHandler {
22922292
ParsedAttributes AttrsWithRange(AttrFactory);
22932293
ParseMicrosoftAttributes(AttrsWithRange);
22942294
AttrsParsed = !AttrsWithRange.empty();
2295-
Attrs.takeAllFrom(AttrsWithRange);
2295+
Attrs.takeAllAppendingFrom(AttrsWithRange);
22962296
}
22972297
return AttrsParsed;
22982298
}
@@ -5175,7 +5175,7 @@ class Parser : public CodeCompletionHandler {
51755175
if (Tok.is(tok::colon)) {
51765176
ParsedAttributes Attrs(AttrFactory);
51775177
ParseHLSLAnnotations(Attrs, EndLoc, CouldBeBitField);
5178-
D.takeAttributes(Attrs);
5178+
D.takeAttributesAppending(Attrs);
51795179
return true;
51805180
}
51815181
return false;

clang/include/clang/Sema/DeclSpec.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -835,16 +835,16 @@ class DeclSpec {
835835
/// \endcode
836836
///
837837
void addAttributes(const ParsedAttributesView &AL) {
838-
Attrs.addAll(AL.begin(), AL.end());
838+
Attrs.prepend(AL.begin(), AL.end());
839839
}
840840

841841
bool hasAttributes() const { return !Attrs.empty(); }
842842

843843
ParsedAttributes &getAttributes() { return Attrs; }
844844
const ParsedAttributes &getAttributes() const { return Attrs; }
845845

846-
void takeAttributesFrom(ParsedAttributes &attrs) {
847-
Attrs.takeAllFrom(attrs);
846+
void takeAttributesAppendingingFrom(ParsedAttributes &attrs) {
847+
Attrs.takeAllAppendingFrom(attrs);
848848
}
849849

850850
/// Finish - This does final analysis of the declspec, issuing diagnostics for
@@ -2327,7 +2327,7 @@ class Declarator {
23272327
void AddTypeInfo(const DeclaratorChunk &TI, ParsedAttributes &&attrs,
23282328
SourceLocation EndLoc) {
23292329
DeclTypeInfo.push_back(TI);
2330-
DeclTypeInfo.back().getAttrs().addAll(attrs.begin(), attrs.end());
2330+
DeclTypeInfo.back().getAttrs().prepend(attrs.begin(), attrs.end());
23312331
getAttributePool().takeAllFrom(attrs.getPool());
23322332

23332333
if (!EndLoc.isInvalid())
@@ -2638,17 +2638,17 @@ class Declarator {
26382638
return InventedTemplateParameterList;
26392639
}
26402640

2641-
/// takeAttributes - Takes attributes from the given parsed-attributes
2642-
/// set and add them to this declarator.
2641+
/// takeAttributesAppending - Takes attributes from the given
2642+
/// ParsedAttributes set and add them to this declarator.
26432643
///
26442644
/// These examples both add 3 attributes to "var":
26452645
/// short int var __attribute__((aligned(16),common,deprecated));
26462646
/// short int x, __attribute__((aligned(16)) var
26472647
/// __attribute__((common,deprecated));
26482648
///
26492649
/// Also extends the range of the declarator.
2650-
void takeAttributes(ParsedAttributes &attrs) {
2651-
Attrs.takeAllFrom(attrs);
2650+
void takeAttributesAppending(ParsedAttributes &attrs) {
2651+
Attrs.takeAllAppendingFrom(attrs);
26522652

26532653
if (attrs.Range.getEnd().isValid())
26542654
SetRangeEnd(attrs.Range.getEnd());

clang/include/clang/Sema/ParsedAttr.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -856,19 +856,19 @@ class ParsedAttributesView {
856856
friend class ParsedAttributesView;
857857
};
858858

859-
void addAll(iterator B, iterator E) {
859+
void prepend(iterator B, iterator E) {
860860
AttrList.insert(AttrList.begin(), B.I, E.I);
861861
}
862862

863-
void addAll(const_iterator B, const_iterator E) {
863+
void prepend(const_iterator B, const_iterator E) {
864864
AttrList.insert(AttrList.begin(), B.I, E.I);
865865
}
866866

867-
void addAllAtEnd(iterator B, iterator E) {
867+
void append(iterator B, iterator E) {
868868
AttrList.insert(AttrList.end(), B.I, E.I);
869869
}
870870

871-
void addAllAtEnd(const_iterator B, const_iterator E) {
871+
void append(const_iterator B, const_iterator E) {
872872
AttrList.insert(AttrList.end(), B.I, E.I);
873873
}
874874

@@ -943,18 +943,18 @@ class ParsedAttributes : public ParsedAttributesView {
943943

944944
AttributePool &getPool() const { return pool; }
945945

946-
void takeAllFrom(ParsedAttributes &Other) {
946+
void takeAllPrependingFrom(ParsedAttributes &Other) {
947947
assert(&Other != this &&
948948
"ParsedAttributes can't take attributes from itself");
949-
addAll(Other.begin(), Other.end());
949+
prepend(Other.begin(), Other.end());
950950
Other.clearListOnly();
951951
pool.takeAllFrom(Other.pool);
952952
}
953953

954-
void takeAllAtEndFrom(ParsedAttributes &Other) {
954+
void takeAllAppendingFrom(ParsedAttributes &Other) {
955955
assert(&Other != this &&
956956
"ParsedAttributes can't take attributes from itself");
957-
addAllAtEnd(Other.begin(), Other.end());
957+
append(Other.begin(), Other.end());
958958
Other.clearListOnly();
959959
pool.takeAllFrom(Other.pool);
960960
}

clang/lib/Parse/ParseDecl.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,12 +1934,12 @@ Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
19341934
bool RequireSemi, ForRangeInit *FRI, SourceLocation *DeclSpecStart) {
19351935
// Need to retain these for diagnostics before we add them to the DeclSepc.
19361936
ParsedAttributesView OriginalDeclSpecAttrs;
1937-
OriginalDeclSpecAttrs.addAll(DeclSpecAttrs.begin(), DeclSpecAttrs.end());
1937+
OriginalDeclSpecAttrs.prepend(DeclSpecAttrs.begin(), DeclSpecAttrs.end());
19381938
OriginalDeclSpecAttrs.Range = DeclSpecAttrs.Range;
19391939

19401940
// Parse the common declaration-specifiers piece.
19411941
ParsingDeclSpec DS(*this);
1942-
DS.takeAttributesFrom(DeclSpecAttrs);
1942+
DS.takeAttributesAppendingingFrom(DeclSpecAttrs);
19431943

19441944
ParsedTemplateInfo TemplateInfo;
19451945
DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
@@ -2135,7 +2135,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
21352135
// list. This ensures that we will not attempt to interpret them as statement
21362136
// attributes higher up the callchain.
21372137
ParsedAttributes LocalAttrs(AttrFactory);
2138-
LocalAttrs.takeAllFrom(Attrs);
2138+
LocalAttrs.takeAllPrependingFrom(Attrs);
21392139
ParsingDeclarator D(*this, DS, LocalAttrs, Context);
21402140
if (TemplateInfo.TemplateParams)
21412141
D.setTemplateParameterLists(*TemplateInfo.TemplateParams);
@@ -3462,7 +3462,7 @@ void Parser::ParseDeclarationSpecifiers(
34623462
PA.setInvalid();
34633463
}
34643464

3465-
DS.takeAttributesFrom(attrs);
3465+
DS.takeAttributesAppendingingFrom(attrs);
34663466
}
34673467

34683468
// If this is not a declaration specifier token, we're done reading decl
@@ -3689,7 +3689,7 @@ void Parser::ParseDeclarationSpecifiers(
36893689
if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
36903690
if (!Attrs.empty()) {
36913691
AttrsLastTime = true;
3692-
attrs.takeAllFrom(Attrs);
3692+
attrs.takeAllAppendingFrom(Attrs);
36933693
}
36943694
continue;
36953695
}
@@ -3854,7 +3854,7 @@ void Parser::ParseDeclarationSpecifiers(
38543854
if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
38553855
if (!Attrs.empty()) {
38563856
AttrsLastTime = true;
3857-
attrs.takeAllFrom(Attrs);
3857+
attrs.takeAllAppendingFrom(Attrs);
38583858
}
38593859
continue;
38603860
}
@@ -4463,7 +4463,7 @@ void Parser::ParseDeclarationSpecifiers(
44634463
// take them over and handle them here.
44644464
if (!Attributes.empty()) {
44654465
AttrsLastTime = true;
4466-
attrs.takeAllFrom(Attributes);
4466+
attrs.takeAllAppendingFrom(Attributes);
44674467
}
44684468
continue;
44694469
}
@@ -4830,7 +4830,7 @@ void Parser::ParseLexedCAttribute(LateParsedAttribute &LA, bool EnterScope,
48304830
ConsumeAnyToken();
48314831

48324832
if (OutAttrs) {
4833-
OutAttrs->takeAllFrom(Attrs);
4833+
OutAttrs->takeAllAppendingFrom(Attrs);
48344834
}
48354835
}
48364836

@@ -6122,7 +6122,7 @@ void Parser::ParseTypeQualifierListOpt(
61226122
isAllowedCXX11AttributeSpecifier()) {
61236123
ParsedAttributes Attrs(AttrFactory);
61246124
ParseCXX11Attributes(Attrs);
6125-
DS.takeAttributesFrom(Attrs);
6125+
DS.takeAttributesAppendingingFrom(Attrs);
61266126
}
61276127

61286128
SourceLocation EndLoc;
@@ -7483,7 +7483,7 @@ void Parser::ParseParameterDeclarationClause(
74837483
// Take them so that we only apply the attributes to the first parameter.
74847484
// We have already started parsing the decl-specifier sequence, so don't
74857485
// parse any parameter-declaration pieces that precede it.
7486-
ArgDeclSpecAttrs.takeAllFrom(FirstArgAttrs);
7486+
ArgDeclSpecAttrs.takeAllPrependingFrom(FirstArgAttrs);
74877487
} else {
74887488
// Parse any C++11 attributes.
74897489
MaybeParseCXX11Attributes(ArgDeclAttrs);
@@ -7505,7 +7505,7 @@ void Parser::ParseParameterDeclarationClause(
75057505
DeclSpecContext::DSC_normal,
75067506
/*LateAttrs=*/nullptr, AllowImplicitTypename);
75077507

7508-
DS.takeAttributesFrom(ArgDeclSpecAttrs);
7508+
DS.takeAttributesAppendingingFrom(ArgDeclSpecAttrs);
75097509

75107510
// Parse the declarator. This is "PrototypeContext" or
75117511
// "LambdaExprParameterContext", because we must accept either

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
739739
<< FixItHint::CreateInsertionFromRange(
740740
Tok.getLocation(), CharSourceRange::getTokenRange(Range))
741741
<< FixItHint::CreateRemoval(Range);
742-
Attrs.takeAllFrom(MisplacedAttrs);
742+
Attrs.takeAllPrependingFrom(MisplacedAttrs);
743743
}
744744

745745
// Maybe this is an alias-declaration.
@@ -787,7 +787,7 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
787787
// Parse (optional) attributes.
788788
MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs);
789789
DiagnoseCXX11AttributeExtension(Attrs);
790-
Attrs.addAll(PrefixAttrs.begin(), PrefixAttrs.end());
790+
Attrs.prepend(PrefixAttrs.begin(), PrefixAttrs.end());
791791

792792
if (InvalidDeclarator)
793793
SkipUntil(tok::comma, tok::semi, StopBeforeMatch);
@@ -1948,7 +1948,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
19481948

19491949
// Recover by adding misplaced attributes to the attribute list
19501950
// of the class so they can be applied on the class later.
1951-
attrs.takeAllFrom(Attributes);
1951+
attrs.takeAllAppendingFrom(Attributes);
19521952
}
19531953
}
19541954

@@ -2842,7 +2842,7 @@ Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclaration(
28422842
// decl-specifier-seq:
28432843
// Parse the common declaration-specifiers piece.
28442844
ParsingDeclSpec DS(*this, TemplateDiags);
2845-
DS.takeAttributesFrom(DeclSpecAttrs);
2845+
DS.takeAttributesAppendingingFrom(DeclSpecAttrs);
28462846

28472847
if (MalformedTypeSpec)
28482848
DS.SetTypeSpecError();

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
12441244
break;
12451245
}
12461246

1247-
D.takeAttributes(Attributes);
1247+
D.takeAttributesAppending(Attributes);
12481248
}
12491249

12501250
MultiParseScope TemplateParamScope(*this);

clang/lib/Parse/ParseObjc.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
4343
Parser::DeclGroupPtrTy
4444
Parser::ParseObjCAtDirectives(ParsedAttributes &DeclAttrs,
4545
ParsedAttributes &DeclSpecAttrs) {
46-
DeclAttrs.takeAllFrom(DeclSpecAttrs);
46+
DeclAttrs.takeAllPrependingFrom(DeclSpecAttrs);
4747

4848
SourceLocation AtLoc = ConsumeToken(); // the "@"
4949

@@ -1065,8 +1065,8 @@ void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
10651065

10661066
/// Take all the decl attributes out of the given list and add
10671067
/// them to the given attribute set.
1068-
static void takeDeclAttributes(ParsedAttributesView &attrs,
1069-
ParsedAttributesView &from) {
1068+
static void takeDeclAttributesAppend(ParsedAttributesView &attrs,
1069+
ParsedAttributesView &from) {
10701070
for (auto &AL : llvm::reverse(from)) {
10711071
if (!AL.isUsedAsTypeAttr()) {
10721072
from.remove(&AL);
@@ -1088,10 +1088,10 @@ static void takeDeclAttributes(ParsedAttributes &attrs,
10881088
attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
10891089

10901090
// Now actually move the attributes over.
1091-
takeDeclAttributes(attrs, D.getMutableDeclSpec().getAttributes());
1092-
takeDeclAttributes(attrs, D.getAttributes());
1091+
takeDeclAttributesAppend(attrs, D.getMutableDeclSpec().getAttributes());
1092+
takeDeclAttributesAppend(attrs, D.getAttributes());
10931093
for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1094-
takeDeclAttributes(attrs, D.getTypeObject(i).getAttrs());
1094+
takeDeclAttributesAppend(attrs, D.getTypeObject(i).getAttrs());
10951095
}
10961096

10971097
ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,

clang/lib/Parse/ParseStmt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
718718
// and followed by a semicolon, GCC will reject (it appears to parse the
719719
// attributes as part of a statement in that case). That looks like a bug.
720720
if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
721-
Attrs.takeAllFrom(TempAttrs);
721+
Attrs.takeAllAppendingFrom(TempAttrs);
722722
else {
723723
StmtVector Stmts;
724724
ParsedAttributes EmptyCXX11Attrs(AttrFactory);
@@ -2407,7 +2407,7 @@ StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
24072407
Stmts, StmtCtx, TrailingElseLoc, Attrs, EmptyDeclSpecAttrs,
24082408
PrecedingLabel);
24092409

2410-
Attrs.takeAllFrom(TempAttrs);
2410+
Attrs.takeAllPrependingFrom(TempAttrs);
24112411

24122412
// Start of attribute range may already be set for some invalid input.
24132413
// See PR46336.

clang/lib/Parse/ParseTemplate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclarationAfterTemplate(
196196
ParsingDeclSpec DS(*this, &DiagsFromTParams);
197197
DS.SetRangeStart(DeclSpecAttrs.Range.getBegin());
198198
DS.SetRangeEnd(DeclSpecAttrs.Range.getEnd());
199-
DS.takeAttributesFrom(DeclSpecAttrs);
199+
DS.takeAttributesAppendingingFrom(DeclSpecAttrs);
200200

201201
ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
202202
getDeclSpecContextFromDeclaratorContext(Context));

clang/lib/Parse/Parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
10831083
"expected uninitialised source range");
10841084
DS.SetRangeStart(DeclSpecAttrs.Range.getBegin());
10851085
DS.SetRangeEnd(DeclSpecAttrs.Range.getEnd());
1086-
DS.takeAttributesFrom(DeclSpecAttrs);
1086+
DS.takeAttributesAppendingingFrom(DeclSpecAttrs);
10871087

10881088
ParsedTemplateInfo TemplateInfo;
10891089
MaybeParseMicrosoftAttributes(DS.getAttributes());
@@ -1155,7 +1155,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
11551155
}
11561156

11571157
DS.abort();
1158-
DS.takeAttributesFrom(Attrs);
1158+
DS.takeAttributesAppendingingFrom(Attrs);
11591159

11601160
const char *PrevSpec = nullptr;
11611161
unsigned DiagID;

0 commit comments

Comments
 (0)