]> granicus.if.org Git - clang/commitdiff
Fixes layouting regression and invalid-read.
authorManuel Klimek <klimek@google.com>
Wed, 23 Jan 2013 14:08:21 +0000 (14:08 +0000)
committerManuel Klimek <klimek@google.com>
Wed, 23 Jan 2013 14:08:21 +0000 (14:08 +0000)
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

lib/Format/Format.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp

index d95deb570465293faed72fad015aed9611ef3ba0..2952067c398db5eb714fe83cc7588dcd8ddbf819 100644 (file)
@@ -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;
       }
     }
index c21fa0d74e5c50da3ea92c0ad0964a4bff3780a3..1b39442610f4030df5ebb801b11671159f021104 100644 (file)
@@ -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;
index d1af838a730dfcc61e46c7885d0a4b197ddc2dcc..806a7eb9c08ade0d7a06fb5fbbc2f15a64390754 100644 (file)
@@ -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<Type *>(P);");
   verifyFormat("Type *A = (Type *)P;");