mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-01-27 20:06:20 +00:00
[clang-format] Refine structured binding detection
Summary: Revision r356575 had the unfortunate consequence that now clang-format never detects an ObjC call expression after `&&`. This patch tries harder to distinguish between C++17 structured bindings and ObjC call expressions and adds a few regression tests. Reviewers: klimek Reviewed By: klimek Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59774 llvm-svn: 356928
This commit is contained in:
parent
7d3225c4b4
commit
fc67176eec
@ -13,6 +13,7 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "TokenAnnotator.h"
|
#include "TokenAnnotator.h"
|
||||||
|
#include "FormatToken.h"
|
||||||
#include "clang/Basic/SourceManager.h"
|
#include "clang/Basic/SourceManager.h"
|
||||||
#include "llvm/ADT/SmallPtrSet.h"
|
#include "llvm/ADT/SmallPtrSet.h"
|
||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
@ -440,10 +441,11 @@ private:
|
|||||||
Contexts.back().InCSharpAttributeSpecifier;
|
Contexts.back().InCSharpAttributeSpecifier;
|
||||||
|
|
||||||
bool InsideInlineASM = Line.startsWith(tok::kw_asm);
|
bool InsideInlineASM = Line.startsWith(tok::kw_asm);
|
||||||
|
bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
|
||||||
bool StartsObjCMethodExpr =
|
bool StartsObjCMethodExpr =
|
||||||
!InsideInlineASM && !CppArrayTemplates && Style.isCpp() &&
|
!IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
|
||||||
!IsCpp11AttributeSpecifier && Contexts.back().CanBeExpression &&
|
Style.isCpp() && !IsCpp11AttributeSpecifier &&
|
||||||
Left->isNot(TT_LambdaLSquare) &&
|
Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
|
||||||
!CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
|
!CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
|
||||||
(!Parent ||
|
(!Parent ||
|
||||||
Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
|
Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
|
||||||
@ -451,14 +453,12 @@ private:
|
|||||||
Parent->isUnaryOperator() ||
|
Parent->isUnaryOperator() ||
|
||||||
// FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
|
// FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
|
||||||
Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
|
Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
|
||||||
// for (auto && [A,B] : C) && structure binding seen as ObjCMethodExpr
|
(getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
|
||||||
(Parent->isNot(tok::ampamp) &&
|
prec::Unknown));
|
||||||
getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
|
|
||||||
prec::Unknown));
|
|
||||||
bool ColonFound = false;
|
bool ColonFound = false;
|
||||||
|
|
||||||
unsigned BindingIncrease = 1;
|
unsigned BindingIncrease = 1;
|
||||||
if (Left->isCppStructuredBinding(Style)) {
|
if (IsCppStructuredBinding) {
|
||||||
Left->Type = TT_StructuredBindingLSquare;
|
Left->Type = TT_StructuredBindingLSquare;
|
||||||
} else if (Left->is(TT_Unknown)) {
|
} else if (Left->is(TT_Unknown)) {
|
||||||
if (StartsObjCMethodExpr) {
|
if (StartsObjCMethodExpr) {
|
||||||
|
@ -1357,6 +1357,30 @@ TEST_F(FormatTestObjC, DisambiguatesCallsFromCppLambdas) {
|
|||||||
// verifyFormat("x = ([a foo:bar] >> b->c == 'd');");
|
// verifyFormat("x = ([a foo:bar] >> b->c == 'd');");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(FormatTestObjC, DisambiguatesCallsFromStructuredBindings) {
|
||||||
|
verifyFormat("int f() {\n"
|
||||||
|
" if (a && [f arg])\n"
|
||||||
|
" return 0;\n"
|
||||||
|
"}");
|
||||||
|
verifyFormat("int f() {\n"
|
||||||
|
" if (a & [f arg])\n"
|
||||||
|
" return 0;\n"
|
||||||
|
"}");
|
||||||
|
verifyFormat("int f() {\n"
|
||||||
|
" for (auto &[elem] : list)\n"
|
||||||
|
" return 0;\n"
|
||||||
|
"}");
|
||||||
|
verifyFormat("int f() {\n"
|
||||||
|
" for (auto &&[elem] : list)\n"
|
||||||
|
" return 0;\n"
|
||||||
|
"}");
|
||||||
|
verifyFormat(
|
||||||
|
"int f() {\n"
|
||||||
|
" for (auto /**/ const /**/ volatile /**/ && /**/ [elem] : list)\n"
|
||||||
|
" return 0;\n"
|
||||||
|
"}");
|
||||||
|
}
|
||||||
|
|
||||||
} // end namespace
|
} // end namespace
|
||||||
} // end namespace format
|
} // end namespace format
|
||||||
} // end namespace clang
|
} // end namespace clang
|
||||||
|
Loading…
x
Reference in New Issue
Block a user