aboutsummaryrefslogtreecommitdiff
path: root/clang/lib
diff options
context:
space:
mode:
authorJonathan Coe <jbcoe@google.com>2020-03-11 12:57:29 +0000
committerJonathan Coe <jbcoe@google.com>2020-03-11 12:58:52 +0000
commit1fb9c29833ab88c4a3d6fda9911117839754d998 (patch)
tree782d8aa8b062da90c8bd9f528b0afbe2938655c9 /clang/lib
parent5c917bd9a7de8fc45401da00cd27661b429887e9 (diff)
downloadllvm-1fb9c29833ab88c4a3d6fda9911117839754d998.zip
llvm-1fb9c29833ab88c4a3d6fda9911117839754d998.tar.gz
llvm-1fb9c29833ab88c4a3d6fda9911117839754d998.tar.bz2
[clang-format] Improved identification of C# nullables
Summary: Allow `?` inside C# generics. Do not mistake casts like `(Type?)` as conditional operators. Reviewers: krasimir Subscribers: cfe-commits, MyDeveloperDay Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D75983
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp17
1 files changed, 9 insertions, 8 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index cf481a1d..d546a9f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -131,7 +131,7 @@ private:
}
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
(CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
- Style.Language != FormatStyle::LK_Proto &&
+ !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
Style.Language != FormatStyle::LK_TextProto))
return false;
// If a && or || is found and interpreted as a binary operator, this set
@@ -1013,12 +1013,13 @@ private:
Style.Language == FormatStyle::LK_JavaScript)
break;
if (Style.isCSharp()) {
- // `Type? name;` and `Type? name =` can only be nullable types.
+ // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
+ // nullable types.
// Line.MustBeDeclaration will be true for `Type? name;`.
- if (!Contexts.back().IsExpression &&
- (Line.MustBeDeclaration ||
- (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
- Tok->Next->Next->is(tok::equal)))) {
+ if ((!Contexts.back().IsExpression && Line.MustBeDeclaration) ||
+ (Tok->Next && Tok->Next->isOneOf(tok::r_paren, tok::greater)) ||
+ (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
+ Tok->Next->Next->is(tok::equal))) {
Tok->Type = TT_CSharpNullable;
break;
}
@@ -2969,9 +2970,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
if (Right.is(TT_CSharpNullable))
return false;
- // Require space after ? in nullable types.
+ // Require space after ? in nullable types except in generics and casts.
if (Left.is(TT_CSharpNullable))
- return true;
+ return !Right.isOneOf(TT_TemplateCloser, tok::r_paren);
// No space before or after '?.'.
if (Left.is(TT_CSharpNullConditional) || Right.is(TT_CSharpNullConditional))