diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -2686,34 +2686,30 @@ /// Such situations should use the specific attribute parsing functionality. void ParseAttributes(unsigned WhichAttrKinds, ParsedAttributesWithRange &Attrs, - SourceLocation *End = nullptr, LateParsedAttrList *LateAttrs = nullptr); void ParseAttributes(unsigned WhichAttrKinds, ParsedAttributes &Attrs, - SourceLocation *End = nullptr, LateParsedAttrList *LateAttrs = nullptr) { ParsedAttributesWithRange AttrsWithRange(AttrFactory); - ParseAttributes(WhichAttrKinds, AttrsWithRange, End, LateAttrs); + ParseAttributes(WhichAttrKinds, AttrsWithRange, LateAttrs); Attrs.takeAllFrom(AttrsWithRange); } /// \brief Possibly parse attributes based on what syntaxes are desired, /// allowing for the order to vary. bool MaybeParseAttributes(unsigned WhichAttrKinds, ParsedAttributesWithRange &Attrs, - SourceLocation *End = nullptr, LateParsedAttrList *LateAttrs = nullptr) { if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec) || (standardAttributesAllowed() && isCXX11AttributeSpecifier())) { - ParseAttributes(WhichAttrKinds, Attrs, End, LateAttrs); + ParseAttributes(WhichAttrKinds, Attrs, LateAttrs); return true; } return false; } bool MaybeParseAttributes(unsigned WhichAttrKinds, ParsedAttributes &Attrs, - SourceLocation *End = nullptr, LateParsedAttrList *LateAttrs = nullptr) { if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec) || (standardAttributesAllowed() && isCXX11AttributeSpecifier())) { - ParseAttributes(WhichAttrKinds, Attrs, End, LateAttrs); + ParseAttributes(WhichAttrKinds, Attrs, LateAttrs); return true; } return false; @@ -2722,10 +2718,9 @@ void MaybeParseGNUAttributes(Declarator &D, LateParsedAttrList *LateAttrs = nullptr) { if (Tok.is(tok::kw___attribute)) { - ParsedAttributes attrs(AttrFactory); - SourceLocation endLoc; - ParseGNUAttributes(attrs, &endLoc, LateAttrs, &D); - D.takeAttributes(attrs, endLoc); + ParsedAttributesWithRange attrs(AttrFactory); + ParseGNUAttributes(attrs, LateAttrs, &D); + D.takeAttributes(attrs, attrs.Range.getEnd()); } } @@ -2735,11 +2730,10 @@ /// This API is discouraged. Use the version that takes a /// ParsedAttributesWithRange instead. bool MaybeParseGNUAttributes(ParsedAttributes &Attrs, - SourceLocation *EndLoc = nullptr, LateParsedAttrList *LateAttrs = nullptr) { if (Tok.is(tok::kw___attribute)) { ParsedAttributesWithRange AttrsWithRange(AttrFactory); - ParseGNUAttributes(Attrs, EndLoc, LateAttrs); + ParseGNUAttributes(Attrs, LateAttrs); Attrs.takeAllFrom(AttrsWithRange); return true; } @@ -2747,10 +2741,9 @@ } bool MaybeParseGNUAttributes(ParsedAttributesWithRange &Attrs, - SourceLocation *EndLoc = nullptr, LateParsedAttrList *LateAttrs = nullptr) { if (Tok.is(tok::kw___attribute)) { - ParseGNUAttributes(Attrs, EndLoc, LateAttrs); + ParseGNUAttributes(Attrs, LateAttrs); return true; } return false; @@ -2762,16 +2755,14 @@ /// This API is discouraged. Use the version that takes a /// ParsedAttributesWithRange instead. void ParseGNUAttributes(ParsedAttributes &Attrs, - SourceLocation *EndLoc = nullptr, LateParsedAttrList *LateAttrs = nullptr, Declarator *D = nullptr) { ParsedAttributesWithRange AttrsWithRange(AttrFactory); - ParseGNUAttributes(AttrsWithRange, EndLoc, LateAttrs, D); + ParseGNUAttributes(AttrsWithRange, LateAttrs, D); Attrs.takeAllFrom(AttrsWithRange); } void ParseGNUAttributes(ParsedAttributesWithRange &Attrs, - SourceLocation *EndLoc = nullptr, LateParsedAttrList *LateAttrs = nullptr, Declarator *D = nullptr); void ParseGNUAttributeArgs(IdentifierInfo *AttrName, @@ -2800,27 +2791,24 @@ void MaybeParseCXX11Attributes(Declarator &D) { if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) { ParsedAttributesWithRange attrs(AttrFactory); - SourceLocation endLoc; - ParseCXX11Attributes(attrs, &endLoc); - D.takeAttributes(attrs, endLoc); + ParseCXX11Attributes(attrs); + D.takeAttributes(attrs, attrs.Range.getEnd()); } } - bool MaybeParseCXX11Attributes(ParsedAttributes &attrs, - SourceLocation *endLoc = nullptr) { + bool MaybeParseCXX11Attributes(ParsedAttributes &attrs) { if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) { ParsedAttributesWithRange attrsWithRange(AttrFactory); - ParseCXX11Attributes(attrsWithRange, endLoc); + ParseCXX11Attributes(attrsWithRange); attrs.takeAllFrom(attrsWithRange); return true; } return false; } bool MaybeParseCXX11Attributes(ParsedAttributesWithRange &attrs, - SourceLocation *endLoc = nullptr, bool OuterMightBeMessageSend = false) { if (standardAttributesAllowed() && isCXX11AttributeSpecifier(false, OuterMightBeMessageSend)) { - ParseCXX11Attributes(attrs, endLoc); + ParseCXX11Attributes(attrs); return true; } return false; @@ -2838,8 +2826,7 @@ ParseCXX11AttributeSpecifierInternal(Attrs, OpenMPTokens, EndLoc); ReplayOpenMPAttributeTokens(OpenMPTokens); } - void ParseCXX11Attributes(ParsedAttributesWithRange &attrs, - SourceLocation *EndLoc = nullptr); + void ParseCXX11Attributes(ParsedAttributesWithRange &attrs); /// Parses a C++11 (or C2x)-style attribute argument list. Returns true /// if this results in adding an attribute to the ParsedAttributes list. bool ParseCXX11AttributeArgs(IdentifierInfo *AttrName, @@ -2854,25 +2841,23 @@ Sema::AttributeCompletion Completion = Sema::AttributeCompletion::None, const IdentifierInfo *EnclosingScope = nullptr); - void MaybeParseMicrosoftAttributes(ParsedAttributes &attrs, - SourceLocation *endLoc = nullptr) { - if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) - ParseMicrosoftAttributes(attrs, endLoc); + void MaybeParseMicrosoftAttributes(ParsedAttributes &Attrs) { + if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) { + ParsedAttributesWithRange AttrsWithRange(AttrFactory); + ParseMicrosoftAttributes(AttrsWithRange); + Attrs.takeAllFrom(AttrsWithRange); + } } void ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs); - void ParseMicrosoftAttributes(ParsedAttributes &attrs, - SourceLocation *endLoc = nullptr); - bool MaybeParseMicrosoftDeclSpecs(ParsedAttributes &Attrs, - SourceLocation *End = nullptr) { - const auto &LO = getLangOpts(); - if (LO.DeclSpecKeyword && Tok.is(tok::kw___declspec)) { - ParseMicrosoftDeclSpecs(Attrs, End); + void ParseMicrosoftAttributes(ParsedAttributesWithRange &attrs); + bool MaybeParseMicrosoftDeclSpecs(ParsedAttributesWithRange &Attrs) { + if (getLangOpts().DeclSpecKeyword && Tok.is(tok::kw___declspec)) { + ParseMicrosoftDeclSpecs(Attrs); return true; } return false; } - void ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs, - SourceLocation *End = nullptr); + void ParseMicrosoftDeclSpecs(ParsedAttributesWithRange &Attrs); bool ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName, SourceLocation AttrNameLoc, ParsedAttributes &Attrs); diff --git a/clang/include/clang/Sema/DeclSpec.h b/clang/include/clang/Sema/DeclSpec.h --- a/clang/include/clang/Sema/DeclSpec.h +++ b/clang/include/clang/Sema/DeclSpec.h @@ -368,7 +368,7 @@ ExplicitSpecifier FS_explicit_specifier; // attributes. - ParsedAttributes Attrs; + ParsedAttributesWithRange Attrs; // Scope specifier for the type spec, if applicable. CXXScopeSpec TypeScope; diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -105,7 +105,6 @@ void Parser::ParseAttributes(unsigned WhichAttrKinds, ParsedAttributesWithRange &Attrs, - SourceLocation *End, LateParsedAttrList *LateAttrs) { bool MoreToParse; do { @@ -113,11 +112,11 @@ // parsed, loop to ensure all specified attribute combinations are parsed. MoreToParse = false; if (WhichAttrKinds & PAKM_CXX11) - MoreToParse |= MaybeParseCXX11Attributes(Attrs, End); + MoreToParse |= MaybeParseCXX11Attributes(Attrs); if (WhichAttrKinds & PAKM_GNU) - MoreToParse |= MaybeParseGNUAttributes(Attrs, End, LateAttrs); + MoreToParse |= MaybeParseGNUAttributes(Attrs, LateAttrs); if (WhichAttrKinds & PAKM_Declspec) - MoreToParse |= MaybeParseMicrosoftDeclSpecs(Attrs, End); + MoreToParse |= MaybeParseMicrosoftDeclSpecs(Attrs); } while (MoreToParse); } @@ -163,14 +162,11 @@ /// /// We follow the C++ model, but don't allow junk after the identifier. void Parser::ParseGNUAttributes(ParsedAttributesWithRange &Attrs, - SourceLocation *EndLoc, LateParsedAttrList *LateAttrs, Declarator *D) { assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); - SourceLocation StartLoc = Tok.getLocation(), Loc; - - if (!EndLoc) - EndLoc = &Loc; + SourceLocation StartLoc = Tok.getLocation(); + SourceLocation EndLoc = StartLoc; while (Tok.is(tok::kw___attribute)) { SourceLocation AttrTokLoc = ConsumeToken(); @@ -214,7 +210,7 @@ // Handle "parameterized" attributes if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { - ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, nullptr, + ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, &EndLoc, nullptr, SourceLocation(), ParsedAttr::AS_GNU, D); continue; } @@ -247,8 +243,7 @@ SourceLocation Loc = Tok.getLocation(); if (ExpectAndConsume(tok::r_paren)) SkipUntil(tok::r_paren, StopAtSemi); - if (EndLoc) - *EndLoc = Loc; + EndLoc = Loc; // If this was declared in a macro, attach the macro IdentifierInfo to the // parsed attribute. @@ -270,7 +265,7 @@ } } - Attrs.Range = SourceRange(StartLoc, *EndLoc); + Attrs.Range = SourceRange(StartLoc, EndLoc); } /// Determine whether the given attribute has an identifier argument. @@ -750,11 +745,13 @@ /// [MS] extended-decl-modifier-seq: /// extended-decl-modifier[opt] /// extended-decl-modifier extended-decl-modifier-seq -void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs, - SourceLocation *End) { +void Parser::ParseMicrosoftDeclSpecs(ParsedAttributesWithRange &Attrs) { assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled"); assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); + SourceLocation StartLoc = Tok.getLocation(); + SourceLocation EndLoc = StartLoc; + while (Tok.is(tok::kw___declspec)) { ConsumeToken(); BalancedDelimiterTracker T(*this, tok::l_paren); @@ -817,9 +814,10 @@ ParsedAttr::AS_Declspec); } T.consumeClose(); - if (End) - *End = T.getCloseLocation(); + EndLoc = T.getCloseLocation(); } + + Attrs.Range = SourceRange(StartLoc, EndLoc); } void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { @@ -3663,8 +3661,7 @@ // Attributes support. case tok::kw___attribute: case tok::kw___declspec: - ParseAttributes(PAKM_GNU | PAKM_Declspec, DS.getAttributes(), nullptr, - LateAttrs); + ParseAttributes(PAKM_GNU | PAKM_Declspec, DS.getAttributes(), LateAttrs); continue; // Microsoft single token adornments. diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -4513,19 +4513,17 @@ /// /// attribute-specifier-seq: /// attribute-specifier-seq[opt] attribute-specifier -void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs, - SourceLocation *endLoc) { +void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs) { assert(standardAttributesAllowed()); - SourceLocation StartLoc = Tok.getLocation(), Loc; - if (!endLoc) - endLoc = &Loc; + SourceLocation StartLoc = Tok.getLocation(); + SourceLocation EndLoc = StartLoc; do { - ParseCXX11AttributeSpecifier(attrs, endLoc); + ParseCXX11AttributeSpecifier(attrs, &EndLoc); } while (isCXX11AttributeSpecifier()); - attrs.Range = SourceRange(StartLoc, *endLoc); + attrs.Range = SourceRange(StartLoc, EndLoc); } void Parser::DiagnoseAndSkipCXX11Attributes() { @@ -4658,10 +4656,11 @@ /// [MS] ms-attribute-seq: /// ms-attribute[opt] /// ms-attribute ms-attribute-seq -void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, - SourceLocation *endLoc) { +void Parser::ParseMicrosoftAttributes(ParsedAttributesWithRange &Attrs) { assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); + SourceLocation StartLoc = Tok.getLocation(); + SourceLocation EndLoc = StartLoc; do { // FIXME: If this is actually a C++11 attribute, parse it as one. BalancedDelimiterTracker T(*this, tok::l_square); @@ -4681,15 +4680,16 @@ if (Tok.isNot(tok::identifier)) // ']', but also eof break; if (Tok.getIdentifierInfo()->getName() == "uuid") - ParseMicrosoftUuidAttributeArgs(attrs); + ParseMicrosoftUuidAttributeArgs(Attrs); else ConsumeToken(); } T.consumeClose(); - if (endLoc) - *endLoc = T.getCloseLocation(); + EndLoc = T.getCloseLocation(); } while (Tok.is(tok::l_square)); + + Attrs.Range = SourceRange(StartLoc, EndLoc); } void Parser::ParseMicrosoftIfExistsClassDeclaration( diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp --- a/clang/lib/Parse/ParseExprCXX.cpp +++ b/clang/lib/Parse/ParseExprCXX.cpp @@ -1252,7 +1252,7 @@ TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); Actions.PushLambdaScope(); - ParsedAttributes Attr(AttrFactory); + ParsedAttributesWithRange Attr(AttrFactory); if (getLangOpts().CUDA) { // In CUDA code, GNU attributes are allowed to appear immediately after the // "[...]", even if there is no "(...)" before the lambda body. @@ -1355,7 +1355,8 @@ DeclEndLoc = ESpecRange.getEnd(); // Parse attribute-specifier[opt]. - MaybeParseCXX11Attributes(Attr, &DeclEndLoc); + if (MaybeParseCXX11Attributes(Attr)) + DeclEndLoc = Attr.Range.getEnd(); // Parse OpenCL addr space attribute. if (Tok.isOneOf(tok::kw___private, tok::kw___global, tok::kw___local, diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp --- a/clang/lib/Parse/ParsePragma.cpp +++ b/clang/lib/Parse/ParsePragma.cpp @@ -341,7 +341,7 @@ Token &FirstToken) override; /// A pool of attributes that were parsed in \#pragma clang attribute. - ParsedAttributes AttributesForPragmaAttribute; + ParsedAttributesWithRange AttributesForPragmaAttribute; }; struct PragmaMaxTokensHereHandler : public PragmaHandler { @@ -1365,12 +1365,13 @@ namespace { struct PragmaAttributeInfo { enum ActionType { Push, Pop, Attribute }; - ParsedAttributes &Attributes; + ParsedAttributesWithRange &Attributes; ActionType Action; const IdentifierInfo *Namespace = nullptr; ArrayRef Tokens; - PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {} + PragmaAttributeInfo(ParsedAttributesWithRange &Attributes) + : Attributes(Attributes) {} }; #include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc" @@ -1640,7 +1641,7 @@ /*IsReinject=*/false); ConsumeAnnotationToken(); - ParsedAttributes &Attrs = Info->Attributes; + ParsedAttributesWithRange &Attrs = Info->Attributes; Attrs.clearListOnly(); auto SkipToEnd = [this]() { diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -106,7 +106,7 @@ // at the start of the statement. Thus, we're not using MaybeParseAttributes // here because we don't want to allow arbitrary orderings. ParsedAttributesWithRange Attrs(AttrFactory); - MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true); + MaybeParseCXX11Attributes(Attrs, /*MightBeObjCMessageSend*/ true); if (getLangOpts().OpenCL) MaybeParseGNUAttributes(Attrs); @@ -1119,8 +1119,7 @@ ConsumeToken(); ParsedAttributesWithRange attrs(AttrFactory); - MaybeParseCXX11Attributes(attrs, nullptr, - /*MightBeObjCMessageSend*/ true); + MaybeParseCXX11Attributes(attrs, /*MightBeObjCMessageSend*/ true); // If this is the start of a declaration, parse it as such. if (isDeclarationStatement()) { diff --git a/clang/lib/Parse/ParseTentative.cpp b/clang/lib/Parse/ParseTentative.cpp --- a/clang/lib/Parse/ParseTentative.cpp +++ b/clang/lib/Parse/ParseTentative.cpp @@ -1913,7 +1913,7 @@ /*OuterMightBeMessageSend*/true)) return TPResult::True; - ParsedAttributes attrs(AttrFactory); + ParsedAttributesWithRange attrs(AttrFactory); MaybeParseMicrosoftAttributes(attrs); // decl-specifier-seq