17
17
#include " clang/Basic/SourceManager.h"
18
18
#include " clang/Basic/TokenKinds.h"
19
19
#include " llvm/ADT/SmallPtrSet.h"
20
+ #include " llvm/ADT/SmallVector.h"
21
+ #include " llvm/ADT/StringRef.h"
20
22
#include " llvm/Support/Debug.h"
21
23
22
24
#define DEBUG_TYPE " format-token-annotator"
@@ -38,6 +40,9 @@ static bool mustBreakAfterAttributes(const FormatToken &Tok,
38
40
39
41
namespace {
40
42
43
+ SmallVector<llvm::StringRef, 100 > castIdentifiers{" __type_identity_t" ,
44
+ " remove_reference_t" };
45
+
41
46
// / Returns \c true if the line starts with a token that can start a statement
42
47
// / with an initializer.
43
48
static bool startsWithInitStatement (const AnnotatedLine &Line) {
@@ -492,7 +497,8 @@ class AnnotatingParser {
492
497
ProbablyFunctionType && CurrentToken->Next &&
493
498
(CurrentToken->Next ->is (tok::l_paren) ||
494
499
(CurrentToken->Next ->is (tok::l_square) &&
495
- Line.MustBeDeclaration ))) {
500
+ (Line.MustBeDeclaration ||
501
+ (PrevNonComment && PrevNonComment->isTypeName (LangOpts)))))) {
496
502
OpeningParen.setType (OpeningParen.Next ->is (tok::caret)
497
503
? TT_ObjCBlockLParen
498
504
: TT_FunctionTypeLParen);
@@ -2403,7 +2409,8 @@ class AnnotatingParser {
2403
2409
// not auto operator->() -> xxx;
2404
2410
Current.setType (TT_TrailingReturnArrow);
2405
2411
} else if (Current.is (tok::arrow) && Current.Previous &&
2406
- Current.Previous ->is (tok::r_brace)) {
2412
+ Current.Previous ->is (tok::r_brace) &&
2413
+ Current.Previous ->is (BK_Block)) {
2407
2414
// Concept implicit conversion constraint needs to be treated like
2408
2415
// a trailing return type ... } -> <type>.
2409
2416
Current.setType (TT_TrailingReturnArrow);
@@ -2472,6 +2479,9 @@ class AnnotatingParser {
2472
2479
Current.getNextNonComment ()->isOneOf (tok::comma, tok::r_brace)) {
2473
2480
Current.setType (TT_StringInConcatenation);
2474
2481
}
2482
+ } else if (Current.is (tok::kw_using)) {
2483
+ if (Current.Next ->Next ->Next ->isTypeName (LangOpts))
2484
+ castIdentifiers.push_back (Current.Next ->TokenText );
2475
2485
} else if (Current.is (tok::l_paren)) {
2476
2486
if (lParenStartsCppCast (Current))
2477
2487
Current.setType (TT_CppCastLParen);
@@ -2829,8 +2839,18 @@ class AnnotatingParser {
2829
2839
IsQualifiedPointerOrReference (BeforeRParen, LangOpts);
2830
2840
bool ParensCouldEndDecl =
2831
2841
AfterRParen->isOneOf (tok::equal, tok::semi, tok::l_brace, tok::greater);
2832
- if (ParensAreType && !ParensCouldEndDecl)
2842
+ if (ParensAreType && !ParensCouldEndDecl) {
2843
+ if (BeforeRParen->is (TT_TemplateCloser)) {
2844
+ auto *Prev = BeforeRParen->MatchingParen ->getPreviousNonComment ();
2845
+ if (Prev) {
2846
+ for (auto &name : castIdentifiers)
2847
+ if (Prev->TokenText == name)
2848
+ return true ;
2849
+ return false ;
2850
+ }
2851
+ }
2833
2852
return true ;
2853
+ }
2834
2854
2835
2855
// At this point, we heuristically assume that there are no casts at the
2836
2856
// start of the line. We assume that we have found most cases where there
@@ -3901,6 +3921,11 @@ bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
3901
3921
}
3902
3922
3903
3923
void TokenAnnotator::calculateFormattingInformation (AnnotatedLine &Line) const {
3924
+ if (Line.Computed )
3925
+ return ;
3926
+
3927
+ Line.Computed = true ;
3928
+
3904
3929
for (AnnotatedLine *ChildLine : Line.Children )
3905
3930
calculateFormattingInformation (*ChildLine);
3906
3931
@@ -6105,6 +6130,35 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
6105
6130
return false ;
6106
6131
}
6107
6132
6133
+ // We can break before an r_brace if there was a break after the matching
6134
+ // l_brace, which is tracked by BreakBeforeClosingBrace, or if we are in a
6135
+ // block-indented initialization list.
6136
+ if (Right.is (tok::r_brace)) {
6137
+ return Right.MatchingParen && (Right.MatchingParen ->is (BK_Block) ||
6138
+ (Right.isBlockIndentedInitRBrace (Style)));
6139
+ }
6140
+
6141
+ // We only break before r_paren if we're in a block indented context.
6142
+ if (Right.is (tok::r_paren)) {
6143
+ if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6144
+ !Right.MatchingParen ) {
6145
+ return false ;
6146
+ }
6147
+ auto Next = Right.Next ;
6148
+ if (Next && Next->is (tok::r_paren))
6149
+ Next = Next->Next ;
6150
+ if (Next && Next->is (tok::l_paren))
6151
+ return false ;
6152
+ const FormatToken *Previous = Right.MatchingParen ->Previous ;
6153
+ return !(Previous && (Previous->is (tok::kw_for) || Previous->isIf ()));
6154
+ }
6155
+
6156
+ if (Left.isOneOf (tok::r_paren, TT_TrailingAnnotation) &&
6157
+ Right.is (TT_TrailingAnnotation) &&
6158
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
6159
+ return false ;
6160
+ }
6161
+
6108
6162
if (Left.is (tok::at))
6109
6163
return false ;
6110
6164
if (Left.Tok .getObjCKeywordID () == tok::objc_interface)
@@ -6260,34 +6314,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
6260
6314
return false ;
6261
6315
}
6262
6316
6263
- // We only break before r_brace if there was a corresponding break before
6264
- // the l_brace, which is tracked by BreakBeforeClosingBrace.
6265
- if (Right.is (tok::r_brace)) {
6266
- return Right.MatchingParen && (Right.MatchingParen ->is (BK_Block) ||
6267
- (Right.isBlockIndentedInitRBrace (Style)));
6268
- }
6269
-
6270
- // We only break before r_paren if we're in a block indented context.
6271
- if (Right.is (tok::r_paren)) {
6272
- if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6273
- !Right.MatchingParen ) {
6274
- return false ;
6275
- }
6276
- auto Next = Right.Next ;
6277
- if (Next && Next->is (tok::r_paren))
6278
- Next = Next->Next ;
6279
- if (Next && Next->is (tok::l_paren))
6280
- return false ;
6281
- const FormatToken *Previous = Right.MatchingParen ->Previous ;
6282
- return !(Previous && (Previous->is (tok::kw_for) || Previous->isIf ()));
6283
- }
6284
-
6285
- if (Left.isOneOf (tok::r_paren, TT_TrailingAnnotation) &&
6286
- Right.is (TT_TrailingAnnotation) &&
6287
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
6288
- return false ;
6289
- }
6290
-
6291
6317
// Allow breaking after a trailing annotation, e.g. after a method
6292
6318
// declaration.
6293
6319
if (Left.is (TT_TrailingAnnotation)) {
0 commit comments