From: Manuel Klimek Date: Wed, 23 Jan 2013 14:08:21 +0000 (+0000) Subject: Fixes layouting regression and invalid-read. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a32a7fda316289dca6aac1352fde3caf14a1cb2f;p=clang Fixes layouting regression and invalid-read. Layouting would prevent breaking before + in a[b + c] = d; Regression detected by code review. Also fixes an invalid-read found by the valgrind bot. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173262 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index d95deb5704..2952067c39 100644 --- a/lib/Format/Format.cpp +++ b/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/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index c21fa0d74e..1b39442610 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index d1af838a73..806a7eb9c0 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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;");