krasimir created this revision. Herald added a project: All. krasimir requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
After https://github.com/llvm/llvm-project/commit/b646f0955574c6ad4c156c9db522e46f597cfda9, the added regression test started being formatted as-if the multiplication `*` was a pointer. This adapts the heuristic to distinguish between these two cases. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D129771 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 @@ -10458,6 +10458,9 @@ verifyFormat("class {\n" "}* ptr;", Style); + // Don't confuse a multiplication after a brace-initialized expression with + // a class pointer. + verifyFormat("int i = int{42} * 34;", Style); verifyFormat("struct {\n" "}&& ptr = {};", Style); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2317,7 +2317,15 @@ // After right braces, star tokens are likely to be pointers to struct, // union, or class. // struct {} *ptr; - if (PrevToken->is(tok::r_brace) && Tok.is(tok::star)) + // This by itself is not sufficient to distinguish from multiplication + // following a brace-initialized expression, as in: + // int i = int{42} * 2; + // In the struct case, the part of the struct declaration until the `{` and + // the `}` are put on separate unwrapped lines; in the brace-initialized + // case, the matching `{` is on the same unwrapped line, so check for the + // presence of the matching brace to distinguish between those. + if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) && + PrevToken->MatchingParen == nullptr) return TT_PointerOrReference; // For "} &&"
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -10458,6 +10458,9 @@ verifyFormat("class {\n" "}* ptr;", Style); + // Don't confuse a multiplication after a brace-initialized expression with + // a class pointer. + verifyFormat("int i = int{42} * 34;", Style); verifyFormat("struct {\n" "}&& ptr = {};", Style); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2317,7 +2317,15 @@ // After right braces, star tokens are likely to be pointers to struct, // union, or class. // struct {} *ptr; - if (PrevToken->is(tok::r_brace) && Tok.is(tok::star)) + // This by itself is not sufficient to distinguish from multiplication + // following a brace-initialized expression, as in: + // int i = int{42} * 2; + // In the struct case, the part of the struct declaration until the `{` and + // the `}` are put on separate unwrapped lines; in the brace-initialized + // case, the matching `{` is on the same unwrapped line, so check for the + // presence of the matching brace to distinguish between those. + if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) && + PrevToken->MatchingParen == nullptr) return TT_PointerOrReference; // For "} &&"
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits