diff options
author | Paul Hoad <mydeveloperday@gmail.com> | 2019-10-04 14:16:59 +0000 |
---|---|---|
committer | Paul Hoad <mydeveloperday@gmail.com> | 2019-10-04 14:16:59 +0000 |
commit | ba12cec21f55cae6aaae0b57d08cab0de9358d51 (patch) | |
tree | de2195aa365975198c19ea6e3aef899089a1eb77 /clang/lib/Format/TokenAnnotator.cpp | |
parent | f44ca7f6eba468e146b0096a91dd9931b556aa86 (diff) | |
download | bcm5719-llvm-ba12cec21f55cae6aaae0b57d08cab0de9358d51.tar.gz bcm5719-llvm-ba12cec21f55cae6aaae0b57d08cab0de9358d51.zip |
[clang-format] [PR43531] clang-format damages "alternative representations" for operators
Summary:
https://bugs.llvm.org/show_bug.cgi?id=43531
Fix for clang-format incorrectly handles "alternative operators" as described by https://en.cppreference.com/w/cpp/language/operator_alternative
compl = ~
not = !
these are unary operators, and clang-format will remove the space between them and a numeric constant
this incorrectly formats the following code
```
int a compl 5;
int a not 5;
```
into:
```
int a compl5;
int a not5;
```
The code adds FIXME unit tests for "alternative token" representations for {} [] and # as defined by the same link, which would require a more detailed change to the FormatTokenLexer
Reviewers: klimek, reuk, owenpan, mitchell-stellar, STL_MSFT
Reviewed By: mitchell-stellar
Subscribers: cfe-commits
Tags: #clang-format, #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D68332
llvm-svn: 373750
Diffstat (limited to 'clang/lib/Format/TokenAnnotator.cpp')
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 50e3d056b83..0c417238213 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2900,9 +2900,19 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return false; return true; } - if (Left.is(TT_UnaryOperator)) + if (Left.is(TT_UnaryOperator)) { + // The alternative operators for ~ and ! are "compl" and "not". + // If they are used instead, we do not want to combine them with + // the token to the right, unless that is a left paren. + if (!Right.is(tok::l_paren)) { + if (Left.is(tok::exclaim) && Left.TokenText == "not") + return true; + if (Left.is(tok::tilde) && Left.TokenText == "compl") + return true; + } return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) || Right.is(TT_BinaryOperator); + } // If the next token is a binary operator or a selector name, we have // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly. |