<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Mar 30, 2014 at 5:40 PM, Kapfhammer <span dir="ltr"><<a href="mailto:kapf@student.ethz.ch" target="_blank" class="cremed">kapf@student.ethz.ch</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">This is a fix for: <a href="http://llvm.org/bugs/show_bug.cgi?id=16500" target="_blank" class="cremed">http://llvm.org/bugs/show_bug.cgi?id=16500</a><br>

<br>
Before: cout << a *b;<br>
After : cout << a * b;<br>
<br>
I tried to be conservative to have as few false positives as possible.<br>
The following lines are still formatted wrong:<br>
<br>
template <int i = a *b><br></div></div></blockquote><div><br></div><div>Yeah, I need to look some more at this one.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">
f(a *b);<br></div></div></blockquote><div><br></div><div>No, this is handled correctly. All you need to do is put it into a context where there can actually be statements, i.e. into a function.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">
a *b + c;</div></div></blockquote><div><br></div><div>Put this as a call argument or onto the RHS of an assignment and it will be formatted correctly. As it, it doesn't make much sense in actual code.</div><div><br></div>
<div>All in all, I think a much easier solution for the mentioned bug would be to treat "<<" similar to how we treat assignments, i.e. if we find a <<, we assume the RHS to be an expression (that's the very first "if" in determineTokenType()).</div>
<div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">
There's also the possibility to be more aggressive, but then the<br>
following existing testcase would have been broken:<br>
<br>
template <class CallbackClass><br>
using MyCallback = void (CallbackClass::*)(SomeObject *Data);<br>
<br>
--<br>
<br>
>From 2717f48825ecb1a24f6fe47400bbd3039d915120 Mon Sep 17 00:00:00 2001<br>
From: Kapfhammer <<a href="mailto:kapf@student.ethz.ch" class="cremed">kapf@student.ethz.ch</a>><br>
Date: Sun, 30 Mar 2014 12:04:17 +0200<br>
Subject: [PATCH] clang-format: Use binary operators as an indicator of for an expression and<br>
 extend the test suite.<br>
<br>
This solves the issue where the star was too often interpreted as a<br>
pointer, e.g "cout<<a*b;" was formatted to "cout << a *b;" instead of<br>
"cout << a * b;".  By marking statements more often an expression, the<br>
function determineStarAmpUsage() is more reliable.<br>
<br>
The test suite was extended.<br>
---<br>
 lib/Format/TokenAnnotator.cpp   | 11 +++++++++++<br>
 unittests/Format/FormatTest.cpp | 15 +++++++++++++++<br>
 2 files changed, 26 insertions(+)<br>
<br>
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp<br>
index 6a0e034..2e1faec 100644<br>
--- a/lib/Format/TokenAnnotator.cpp<br>
+++ b/lib/Format/TokenAnnotator.cpp<br>
@@ -798,6 +798,17 @@ private:<br>
         // function declaration have been found.<br>
         Current.Type = TT_TrailingAnnotation;<br>
       }<br>
+<br>
+      // A binary operator is most likely part of an expression<br>
+      if (Current.Type == TT_BinaryOperator &&<br>
+          !Current.isOneOf(tok::ampamp, tok::less, tok::greater,<br>
+                           tok::greatergreater) &&<br>
+          (!Current.Previous ||<br>
+           Current.Previous->isNot(tok::kw_operator)) &&<br>
+          Contexts.back().CanBeExpression &&<br>
+          !Line.First->isOneOf(tok::kw_template, tok::kw_using)) {<br>
+        Contexts.back().IsExpression = true;<br>
+      }<br>
     }<br>
   }<br>
<br>
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp<br>
index 7332fd0..a4eec3a 100644<br>
--- a/unittests/Format/FormatTest.cpp<br>
+++ b/unittests/Format/FormatTest.cpp<br>
@@ -4610,6 +4610,21 @@ TEST_F(FormatTest, UnderstandsRvalueReferences) {<br>
   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");<br>
 }<br>
<br>
+TEST_F(FormatTest, FormatsMultiplicationOperator) {<br>
+  verifyFormat("operator*(type *a)");<br>
+  verifyFormat("operator<<(type *a)");<br>
+  verifyFormat("cout << (a * b)");<br>
+  verifyFormat("cout << a * b");<br>
+  verifyFormat("x + a * b");<br>
+  verifyFormat("type (*f)(type *a)");<br>
+  verifyFormat("type (&f)(type *a)");<br>
+  verifyFormat("void f(type *a)");<br>
+  verifyFormat("using f = void (*)(a *b)");<br>
+  verifyFormat("template <typename T = void (*)(a *b)");<br>
+  verifyFormat("using f = void (c::*)(a *b)");<br>
+  verifyFormat("template <typename T = void (c::*)(a *b)>");<br>
+}<br>
+<br>
 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {<br>
   verifyFormat("void f() {\n"<br>
                "  x[aaaaaaaaa -\n"<br>
--<br>
1.9.1<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" class="cremed">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</div></div></blockquote></div><br></div></div>