summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2012-12-23 01:07:46 +0000
committerNico Weber <nicolasweber@gmx.de>2012-12-23 01:07:46 +0000
commit6f372e6533ccc054b96a2e5e68d92c1da532dcbe (patch)
tree0c1a1b255c07468c674f5dbc701e814ede0f3218 /clang
parentcb4c7f4b1803730103bbfa6e74232081d1130274 (diff)
downloadbcm5719-llvm-6f372e6533ccc054b96a2e5e68d92c1da532dcbe.tar.gz
bcm5719-llvm-6f372e6533ccc054b96a2e5e68d92c1da532dcbe.zip
libFormat: Teach the *& usage heuristic that "return" starts a rhs too.
"return a*b;" was formatted as "return a *b;" and is now formatted as "return a * b;". Fixes PR14687 partially. llvm-svn: 170993
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Format/Format.cpp13
-rw-r--r--clang/unittests/Format/FormatTest.cpp3
2 files changed, 11 insertions, 5 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index bb18e9a8515..2a8fbd6e5e9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -716,19 +716,21 @@ public:
private:
void determineTokenTypes() {
- bool AssignmentEncountered = false;
+ bool IsRHS = false;
for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {
TokenAnnotation &Annotation = Annotations[i];
const FormatToken &Tok = Line.Tokens[i];
if (getBinOpPrecedence(Tok.Tok.getKind(), true, true) == prec::Assignment)
- AssignmentEncountered = true;
+ IsRHS = true;
+ else if (Tok.Tok.is(tok::kw_return))
+ IsRHS = true;
if (Annotation.Type != TokenAnnotation::TT_Unknown)
continue;
if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) {
- Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered);
+ Annotation.Type = determineStarAmpUsage(i, IsRHS);
} else if (Tok.Tok.is(tok::minus) || Tok.Tok.is(tok::plus)) {
Annotation.Type = determinePlusMinusUsage(i);
} else if (Tok.Tok.is(tok::minusminus) || Tok.Tok.is(tok::plusplus)) {
@@ -754,12 +756,13 @@ private:
}
TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index,
- bool AssignmentEncountered) {
+ bool IsRHS) {
if (Index == Annotations.size())
return TokenAnnotation::TT_Unknown;
if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) ||
Line.Tokens[Index - 1].Tok.is(tok::comma) ||
+ Line.Tokens[Index - 1].Tok.is(tok::kw_return) ||
Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator)
return TokenAnnotation::TT_UnaryOperator;
@@ -770,7 +773,7 @@ private:
// It is very unlikely that we are going to find a pointer or reference type
// definition on the RHS of an assignment.
- if (AssignmentEncountered)
+ if (IsRHS)
return TokenAnnotation::TT_BinaryOperator;
return TokenAnnotation::TT_PointerOrReference;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f954c14ef73..b316750a0b6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -592,6 +592,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStar) {
verifyFormat("int a = *b * c;");
verifyFormat("int a = b * *c;");
verifyFormat("int main(int argc, char **argv) {\n}");
+ verifyFormat("return 10 * b;");
+ verifyFormat("return *b * *c;");
+ verifyFormat("return a & ~b;");
// FIXME: Is this desired for LLVM? Fix if not.
verifyFormat("A<int *> a;");
OpenPOWER on IntegriCloud