This revision was automatically updated to reflect the committed changes. curdeius marked an inline comment as done. Closed by commit rGe021987273be: [clang-format] Avoid inserting space after C++ casts. (authored by curdeius).
Changed prior to commit: https://reviews.llvm.org/D120140?vs=409959&id=410178#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D120140/new/ https://reviews.llvm.org/D120140 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -10565,6 +10565,13 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("Type *A = static_cast<Type *>(P);"); + verifyFormat("static_cast<Type *>(P);"); + verifyFormat("static_cast<Type &>(Fun)(Args);"); + verifyFormat("static_cast<Type &>(*Fun)(Args);"); + verifyFormat("a = static_cast<Type &>(*Fun)(Args);"); + verifyFormat("const_cast<Type &>(*Fun)(Args);"); + verifyFormat("dynamic_cast<Type &>(*Fun)(Args);"); + verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);"); verifyFormat("Type *A = (Type *)P;"); verifyFormat("Type *A = (vector<Type *, int *>)P;"); verifyFormat("int a = (int)(2.0f);"); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1734,8 +1734,11 @@ else Current.setType(TT_LineComment); } else if (Current.is(tok::r_paren)) { - if (rParenEndsCast(Current)) + if (rParenEndsCast(Current)) { Current.setType(TT_CastRParen); + assert(Current.MatchingParen); + Current.MatchingParen->setType(TT_Unknown); + } if (Current.MatchingParen && Current.Next && !Current.Next->isBinaryOperator() && !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace, @@ -1938,8 +1941,20 @@ // Certain other tokens right before the parentheses are also signals that // this cannot be a cast. + if (LeftOfParens->is(TT_TemplateCloser)) { + if (LeftOfParens->MatchingParen) { + auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment(); + if (Prev && + Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast, + tok::kw_reinterpret_cast, tok::kw_static_cast)) + // FIXME: Maybe we should handle identifiers ending with "_cast", + // e.g. any_cast? + return true; + } + return false; + } if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator, - TT_TemplateCloser, tok::ellipsis)) + tok::ellipsis)) return false; }
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -10565,6 +10565,13 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("Type *A = static_cast<Type *>(P);"); + verifyFormat("static_cast<Type *>(P);"); + verifyFormat("static_cast<Type &>(Fun)(Args);"); + verifyFormat("static_cast<Type &>(*Fun)(Args);"); + verifyFormat("a = static_cast<Type &>(*Fun)(Args);"); + verifyFormat("const_cast<Type &>(*Fun)(Args);"); + verifyFormat("dynamic_cast<Type &>(*Fun)(Args);"); + verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);"); verifyFormat("Type *A = (Type *)P;"); verifyFormat("Type *A = (vector<Type *, int *>)P;"); verifyFormat("int a = (int)(2.0f);"); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1734,8 +1734,11 @@ else Current.setType(TT_LineComment); } else if (Current.is(tok::r_paren)) { - if (rParenEndsCast(Current)) + if (rParenEndsCast(Current)) { Current.setType(TT_CastRParen); + assert(Current.MatchingParen); + Current.MatchingParen->setType(TT_Unknown); + } if (Current.MatchingParen && Current.Next && !Current.Next->isBinaryOperator() && !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace, @@ -1938,8 +1941,20 @@ // Certain other tokens right before the parentheses are also signals that // this cannot be a cast. + if (LeftOfParens->is(TT_TemplateCloser)) { + if (LeftOfParens->MatchingParen) { + auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment(); + if (Prev && + Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast, + tok::kw_reinterpret_cast, tok::kw_static_cast)) + // FIXME: Maybe we should handle identifiers ending with "_cast", + // e.g. any_cast? + return true; + } + return false; + } if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator, - TT_TemplateCloser, tok::ellipsis)) + tok::ellipsis)) return false; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits