Index: clang/lib/Format/TokenAnnotator.h =================================================================== --- clang/lib/Format/TokenAnnotator.h +++ clang/lib/Format/TokenAnnotator.h @@ -178,9 +178,18 @@ // FIXME: Can/should this be done in the UnwrappedLineParser? void setCommentLineLevels(SmallVectorImpl &Lines) const; - void annotate(AnnotatedLine &Line) const; + void annotate(AnnotatedLine &Line); void calculateFormattingInformation(AnnotatedLine &Line) const; + enum class ScopeType : int8_t { + // Contained in class declaration/definition. + Class, + // Contained within function definition. + Function, + // Contained within other scope block (loop, if/else, etc). + Other, + }; + private: /// Calculate the penalty for splitting before \c Tok. unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok, @@ -220,6 +229,8 @@ const FormatStyle &Style; const AdditionalKeywords &Keywords; + + SmallVector Scopes; }; } // end namespace format Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -111,9 +111,10 @@ class AnnotatingParser { public: AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line, - const AdditionalKeywords &Keywords) + const AdditionalKeywords &Keywords, + SmallVector &TrackedScopes) : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false), - Keywords(Keywords) { + Keywords(Keywords), Scopes(TrackedScopes) { Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); resetTokenMetadata(); } @@ -847,6 +848,8 @@ unsigned CommaCount = 0; while (CurrentToken) { if (CurrentToken->is(tok::r_brace)) { + assert(!Scopes.empty()); + Scopes.pop_back(); assert(OpeningBrace.Optional == CurrentToken->Optional); OpeningBrace.MatchingParen = CurrentToken; CurrentToken->MatchingParen = &OpeningBrace; @@ -1146,6 +1149,19 @@ if (Previous && Previous->getType() != TT_DictLiteral) Previous->setType(TT_SelectorName); } + switch (Tok->getType()) { + case TT_FunctionLBrace: + case TT_LambdaLBrace: + Scopes.push_back(TokenAnnotator::ScopeType::Function); + break; + case TT_ClassLBrace: + case TT_StructLBrace: + case TT_UnionLBrace: + Scopes.push_back(TokenAnnotator::ScopeType::Class); + break; + default: + Scopes.push_back(TokenAnnotator::ScopeType::Other); + } if (!parseBrace()) return false; break; @@ -1176,6 +1192,9 @@ case tok::r_square: return false; case tok::r_brace: + // Handle unbalanced braces. + if (!Scopes.empty()) + Scopes.pop_back(); // Lines can start with '}'. if (Tok->Previous) return false; @@ -2446,6 +2465,28 @@ if (IsExpression && !Contexts.back().CaretFound) return TT_BinaryOperator; + // Opeartors at class scope are likely pointer or reference members. + if (!Scopes.empty() && Scopes.back() == TokenAnnotator::ScopeType::Class) + return TT_PointerOrReference; + + // Tokens that indicate member access or chained operator& use. + auto IsChainedOperatorAmpOrMember = [](const FormatToken *token) { + return token->isOneOf(tok::amp, tok::period, tok::arrow, tok::arrowstar, + tok::periodstar); + }; + + // It's more likely that & represents operator& than an uninitialized + // reference. + if (Tok.is(tok::amp) && (PrevToken && PrevToken->Tok.isAnyIdentifier()) && + (!PrevToken->getPreviousNonComment() || + IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment())) && + (NextToken && NextToken->Tok.isAnyIdentifier()) && + (NextToken->getNextNonComment() && + (IsChainedOperatorAmpOrMember(NextToken->getNextNonComment()) || + NextToken->getNextNonComment()->is(tok::semi)))) { + return TT_BinaryOperator; + } + return TT_PointerOrReference; } @@ -2483,6 +2524,8 @@ bool AutoFound; const AdditionalKeywords &Keywords; + SmallVector &Scopes; + // Set of "<" tokens that do not open a template parameter list. If parseAngle // determines that a specific token can't be a template opener, it will make // same decision irrespective of the decisions for tokens leading up to it. @@ -2763,11 +2806,11 @@ return Result; } -void TokenAnnotator::annotate(AnnotatedLine &Line) const { +void TokenAnnotator::annotate(AnnotatedLine &Line) { for (auto &Child : Line.Children) annotate(*Child); - AnnotatingParser Parser(Style, Line, Keywords); + AnnotatingParser Parser(Style, Line, Keywords, Scopes); Line.Type = Parser.parseLine(); // With very deep nesting, ExpressionParser uses lots of stack and the Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -11267,6 +11267,13 @@ verifyFormat("int operator()(T (&&)[N]) { return 1; }"); verifyFormat("int operator()(T (&)[N]) { return 0; }"); + + verifyFormat("val1 & val2;"); + verifyFormat("val1 & val2 & val3;"); + verifyFormat("class c {\n" + " void func(type &a) { a & member; }\n" + " anotherType &member;\n" + "}"); } TEST_F(FormatTest, UnderstandsAttributes) { Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -175,6 +175,73 @@ ASSERT_EQ(Tokens.size(), 17u) << Tokens; EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference); EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_PointerOrReference); + + Tokens = annotate("Type1 &val1 = val2;"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_PointerOrReference); + + Tokens = annotate("Type1 *val1 = &val2;"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::star, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[4], tok::amp, TT_UnaryOperator); + + Tokens = annotate("val1 & val2;"); + ASSERT_EQ(Tokens.size(), 5u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator); + + Tokens = annotate("val1 & val2.member;"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator); + + Tokens = annotate("val1 & val2.*member;"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator); + + Tokens = annotate("val1.*member & val2;"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator); + + Tokens = annotate("val1 & val2->*member;"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator); + + Tokens = annotate("val1->member & val2;"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator); + + Tokens = annotate("val1 & val2 & val3;"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator); + + Tokens = annotate("val1 & val2 // comment\n" + " & val3;"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[4], tok::amp, TT_BinaryOperator); + + Tokens = + annotate("val1 & val2.member & val3.member() & val4 & val5->member;"); + ASSERT_EQ(Tokens.size(), 19u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[5], tok::amp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[11], tok::amp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[13], tok::amp, TT_BinaryOperator); + + Tokens = annotate("class c {\n" + " void func(type &a) { a & member; }\n" + " anotherType &member;\n" + "}"); + ASSERT_EQ(Tokens.size(), 22u) << Tokens; + EXPECT_TOKEN(Tokens[7], tok::amp, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[12], tok::amp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[17], tok::amp, TT_PointerOrReference); + + Tokens = annotate("struct S {\n" + " auto Mem = C & D;\n" + "}"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[7], tok::amp, TT_BinaryOperator); } TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {