Skip to content

Commit fcfc6b5

Browse files
committed
[Clang] Repair the functionrParenEndsCast to make incorrect judgments in template variable cases
1 parent 20cb4ec commit fcfc6b5

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "clang/Basic/SourceManager.h"
1818
#include "clang/Basic/TokenKinds.h"
1919
#include "llvm/ADT/SmallPtrSet.h"
20+
#include "llvm/ADT/SmallVector.h"
21+
#include "llvm/ADT/StringRef.h"
2022
#include "llvm/Support/Debug.h"
2123

2224
#define DEBUG_TYPE "format-token-annotator"
@@ -38,6 +40,9 @@ static bool mustBreakAfterAttributes(const FormatToken &Tok,
3840

3941
namespace {
4042

43+
SmallVector<llvm::StringRef, 100> castIdentifiers{"__type_identity_t",
44+
"remove_reference_t"};
45+
4146
/// Returns \c true if the line starts with a token that can start a statement
4247
/// with an initializer.
4348
static bool startsWithInitStatement(const AnnotatedLine &Line) {
@@ -492,7 +497,8 @@ class AnnotatingParser {
492497
ProbablyFunctionType && CurrentToken->Next &&
493498
(CurrentToken->Next->is(tok::l_paren) ||
494499
(CurrentToken->Next->is(tok::l_square) &&
495-
Line.MustBeDeclaration))) {
500+
(Line.MustBeDeclaration ||
501+
(PrevNonComment && PrevNonComment->isTypeName(LangOpts)))))) {
496502
OpeningParen.setType(OpeningParen.Next->is(tok::caret)
497503
? TT_ObjCBlockLParen
498504
: TT_FunctionTypeLParen);
@@ -2403,7 +2409,8 @@ class AnnotatingParser {
24032409
// not auto operator->() -> xxx;
24042410
Current.setType(TT_TrailingReturnArrow);
24052411
} 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)) {
24072414
// Concept implicit conversion constraint needs to be treated like
24082415
// a trailing return type ... } -> <type>.
24092416
Current.setType(TT_TrailingReturnArrow);
@@ -2472,6 +2479,9 @@ class AnnotatingParser {
24722479
Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
24732480
Current.setType(TT_StringInConcatenation);
24742481
}
2482+
} else if (Current.is(tok::kw_using)) {
2483+
if (Current.Next->Next->Next->isTypeName(LangOpts))
2484+
castIdentifiers.push_back(Current.Next->TokenText);
24752485
} else if (Current.is(tok::l_paren)) {
24762486
if (lParenStartsCppCast(Current))
24772487
Current.setType(TT_CppCastLParen);
@@ -2829,8 +2839,18 @@ class AnnotatingParser {
28292839
IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
28302840
bool ParensCouldEndDecl =
28312841
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+
}
28332852
return true;
2853+
}
28342854

28352855
// At this point, we heuristically assume that there are no casts at the
28362856
// 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 {
39013921
}
39023922

39033923
void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3924+
if (Line.Computed)
3925+
return;
3926+
3927+
Line.Computed = true;
3928+
39043929
for (AnnotatedLine *ChildLine : Line.Children)
39053930
calculateFormattingInformation(*ChildLine);
39063931

@@ -6105,6 +6130,35 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
61056130
return false;
61066131
}
61076132

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+
61086162
if (Left.is(tok::at))
61096163
return false;
61106164
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
@@ -6260,34 +6314,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62606314
return false;
62616315
}
62626316

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-
62916317
// Allow breaking after a trailing annotation, e.g. after a method
62926318
// declaration.
62936319
if (Left.is(TT_TrailingAnnotation)) {

0 commit comments

Comments
 (0)