aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2012-12-21 10:20:02 +0000
committerDaniel Jasper <djasper@google.com>2012-12-21 10:20:02 +0000
commit675d2e34889a1b7e9d4cc659a4cdc33ce0f5b152 (patch)
tree4585a830c9449f68f966642d2a89d565f600957e /lib/Format/Format.cpp
parent98e6b4a65fcc81b1be11f09837e0fc8fac85897b (diff)
Use OperatorPrecedence.h in clang-format
No indented functional changes other than handling more operators correctly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170875 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp42
1 files changed, 14 insertions, 28 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 9a0fa7d31d..d14c1ce5b5 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -19,6 +19,7 @@
#include "clang/Format/Format.h"
#include "UnwrappedLineParser.h"
#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Lex/Lexer.h"
#include <string>
@@ -650,11 +651,12 @@ private:
TokenAnnotation &Annotation = Annotations[i];
const FormatToken &Tok = Line.Tokens[i];
- if (Tok.Tok.is(tok::equal) || Tok.Tok.is(tok::plusequal) ||
- Tok.Tok.is(tok::minusequal) || Tok.Tok.is(tok::starequal) ||
- Tok.Tok.is(tok::slashequal))
+ if (getBinOpPrecedence(Tok.Tok.getKind(), true, true) == prec::Assignment)
AssignmentEncountered = true;
+ if (Annotation.Type != TokenAnnotation::TT_Unknown)
+ continue;
+
if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) {
Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered);
} else if (Tok.Tok.is(tok::minus) || Tok.Tok.is(tok::plus)) {
@@ -663,9 +665,9 @@ private:
Annotation.Type = determineIncrementUsage(i);
} else if (Tok.Tok.is(tok::exclaim)) {
Annotation.Type = TokenAnnotation::TT_UnaryOperator;
- } else if (isBinaryOperator(Line.Tokens[i]))
+ } else if (isBinaryOperator(Line.Tokens[i])) {
Annotation.Type = TokenAnnotation::TT_BinaryOperator;
- else if (Tok.Tok.is(tok::comment)) {
+ } else if (Tok.Tok.is(tok::comment)) {
StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
Tok.Tok.getLength());
if (Data.startswith("//"))
@@ -677,23 +679,8 @@ private:
}
bool isBinaryOperator(const FormatToken &Tok) {
- switch (Tok.Tok.getKind()) {
- case tok::equal:
- case tok::equalequal:
- case tok::exclaimequal:
- case tok::star:
- //case tok::amp:
- case tok::plus:
- case tok::slash:
- case tok::minus:
- case tok::ampamp:
- case tok::pipe:
- case tok::pipepipe:
- case tok::percent:
- return true;
- default:
- return false;
- }
+ // Comma is a binary operator, but does not behave a such wrt. formatting.
+ return getBinOpPrecedence(Tok.Tok.getKind(), true, true) > prec::Comma;
}
TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index,
@@ -796,12 +783,11 @@ private:
if (Right.Tok.is(tok::r_paren) || Right.Tok.is(tok::l_brace) ||
Right.Tok.is(tok::comment) || Right.Tok.is(tok::greater))
return false;
- if (isBinaryOperator(Left) || Right.Tok.is(tok::lessless) ||
- Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
- return true;
- return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || Left.Tok.is(
- tok::semi) || Left.Tok.is(tok::equal) || Left.Tok.is(tok::ampamp) ||
- Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::l_brace) ||
+ return (isBinaryOperator(Left) && Left.Tok.isNot(tok::lessless)) ||
+ Left.Tok.is(tok::comma) || Right.Tok.is(tok::lessless) ||
+ Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period) ||
+ Right.Tok.is(tok::colon) || Left.Tok.is(tok::semi) ||
+ Left.Tok.is(tok::l_brace) ||
(Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren));
}