Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -847,6 +847,7 @@ unsigned CommaCount = 0; while (CurrentToken) { if (CurrentToken->is(tok::r_brace)) { + Scopes.pop_back(); assert(OpeningBrace.Optional == CurrentToken->Optional); OpeningBrace.MatchingParen = CurrentToken; CurrentToken->MatchingParen = &OpeningBrace; @@ -1146,6 +1147,27 @@ if (Previous && Previous->getType() != TT_DictLiteral) Previous->setType(TT_SelectorName); } + switch (Tok->getType()) { + case TT_FunctionLBrace: + case TT_LambdaLBrace: + Scopes.push_back(ScopeType::Function); + break; + case TT_ClassLBrace: + case TT_StructLBrace: + case TT_UnionLBrace: + Scopes.push_back(ScopeType::Class); + break; + case TT_EnumLBrace: + case TT_ControlStatementLBrace: + case TT_ElseLBrace: + case TT_BracedListLBrace: + case TT_CompoundRequirementLBrace: + case TT_ObjCBlockLBrace: + case TT_RecordLBrace: + case TT_RequiresExpressionLBrace: + default: + Scopes.push_back(ScopeType::Other); + } if (!parseBrace()) return false; break; @@ -1176,6 +1198,7 @@ case tok::r_square: return false; case tok::r_brace: + Scopes.pop_back(); // Lines can start with '}'. if (Tok->Previous) return false; @@ -1643,6 +1666,17 @@ } ContextType = Unknown; }; + enum class ScopeType : std::int8_t { + // Not contained within scope block. + None, + // Contained in class declaration/definition. + Class, + // Contained within function definition. + Function, + // Contained within other scope block (loop, if/else, etc). + Other, + }; + /// Puts a new \c Context onto the stack \c Contexts for the lifetime /// of each instance. struct ScopedContextCreator { @@ -2446,6 +2480,25 @@ if (IsExpression && !Contexts.back().CaretFound) return TT_BinaryOperator; + // Opeartors at class scope are likely pointer or reference members. + if (Scopes.back() == ScopeType::Class) + return TT_PointerOrReference; + + // It's more likely that & represents operator& than an uninitialized + // reference. + if (Tok.is(tok::amp) && (PrevToken && PrevToken->Tok.isAnyIdentifier()) && + (!PrevToken->getPreviousNonComment() || + PrevToken->getPreviousNonComment()->isOneOf(tok::amp, tok::period, + tok::arrow, tok::arrowstar, + tok::periodstar)) && + (NextToken && NextToken->Tok.isAnyIdentifier()) && + (NextToken->getNextNonComment() && + (NextToken->getNextNonComment()->isOneOf( + tok::amp, tok::semi, tok::period, tok::arrow, tok::arrowstar, + tok::periodstar)))) { + return TT_BinaryOperator; + } + return TT_PointerOrReference; } @@ -2476,6 +2529,7 @@ } SmallVector Contexts; + static SmallVector Scopes; const FormatStyle &Style; AnnotatedLine &Line; @@ -2722,6 +2776,9 @@ FormatToken *Current; }; +SmallVector AnnotatingParser::Scopes = { + AnnotatingParser::ScopeType::None}; + } // end anonymous namespace void TokenAnnotator::setCommentLineLevels( @@ -2875,6 +2932,7 @@ return true; if (Next->Next == Next->MatchingParen) return true; // Empty parentheses. + // If there is an &/&& after the r_paren, this is likely a function. if (Next->MatchingParen->Next && Next->MatchingParen->Next->is(TT_PointerOrReference)) { @@ -3661,6 +3719,7 @@ } if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) return !Left.Children.empty(); // No spaces in "{}". + if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) || (Right.is(tok::r_brace) && Right.MatchingParen && Right.MatchingParen->isNot(BK_Block))) { @@ -4855,6 +4914,7 @@ return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None; if (Right.is(Keywords.kw_as)) return false; // must not break before as in 'x as type' casts + if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) { // extends and infer can appear as keywords in conditional types: // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types 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,48 @@ ASSERT_EQ(Tokens.size(), 17u) << Tokens; EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference); EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_PointerOrReference); + + 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 & 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) {