diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index d95deb570465..2952067c398d 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1256,8 +1256,10 @@ private: IsExpression = true; AnnotatedToken *Previous = Current.Parent; while (Previous != NULL) { - if (Previous->Type == TT_BinaryOperator) + if (Previous->Type == TT_BinaryOperator && + (Previous->is(tok::star) || Previous->is(tok::amp))) { Previous->Type = TT_PointerOrReference; + } Previous = Previous->Parent; } } diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index c21fa0d74e5c..1b39442610f4 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -38,7 +38,10 @@ public: } ~ScopedDeclarationState() { Stack.pop_back(); - Line.MustBeDeclaration = Stack.back(); + if (!Stack.empty()) + Line.MustBeDeclaration = Stack.back(); + else + Line.MustBeDeclaration = true; } private: UnwrappedLine &Line; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index d1af838a730d..806a7eb9c08a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1365,6 +1365,13 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyGoogleFormat("A = new SomeType* [Length]();"); } +TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { + verifyFormat("void f() {\n" + " x[aaaaaaaaa -\n" + " b] = 23;\n" + "}", getLLVMStyleWithColumns(15)); +} + TEST_F(FormatTest, FormatsCasts) { verifyFormat("Type *A = static_cast(P);"); verifyFormat("Type *A = (Type *)P;");