zequanwu created this revision. zequanwu added reviewers: thakis, MyDeveloperDay, owenpan, klimek, sammccall. Herald added a project: All. zequanwu requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Example: $ cat a.cpp namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) { void test() {} } $ clang-format a.cpp namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) { void test() {} }// namespace my_namespace::yeahAPI_AVAILABLE(macos(10.15)) After: $ clang-format a.cpp namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) { void test() {} }// namespace my_namespace::yeah Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D121269 Files: clang/lib/Format/NamespaceEndCommentsFixer.cpp clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp =================================================================== --- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -189,6 +189,26 @@ "int i;\n" "int j;\n" "}")); + EXPECT_EQ("#define M(x) x##x\n" + "namespace A M(x) {\n" + "int i;\n" + "int j;\n" + "}// namespace A M(x)", + fixNamespaceEndComments("#define M(x) x##x\n" + "namespace A M(x) {\n" + "int i;\n" + "int j;\n" + "}")); + EXPECT_EQ("#define M(x) x##x\n" + "namespace A::B M(x) {\n" + "int i;\n" + "int j;\n" + "}// namespace A::B", + fixNamespaceEndComments("#define M(x) x##x\n" + "namespace A::B M(x) {\n" + "int i;\n" + "int j;\n" + "}")); EXPECT_EQ("inline namespace A {\n" "int i;\n" "int j;\n" Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp =================================================================== --- clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -55,30 +55,72 @@ Tok = Tok->getNextNonComment(); } - // Use the string after `namespace` as a name candidate until `{` or `::` or - // `(`. If the name is empty, use the candicate. std::string FirstNSName; // For `namespace [[foo]] A::B::inline C {` or // `namespace MACRO1 MACRO2 A::B::inline C {`, returns "A::B::inline C". // Peek for the first '::' (or '{' or '(')) and then return all tokens from // one token before that up until the '{'. A '(' might be a macro with // arguments. - const FormatToken *FirstNSTok = Tok; + const FormatToken *FirstNSTok = nullptr; while (Tok && !Tok->isOneOf(tok::l_brace, tok::coloncolon, tok::l_paren)) { - FirstNSName += FirstNSTok->TokenText; + if (FirstNSTok) + FirstNSName += FirstNSTok->TokenText; FirstNSTok = Tok; Tok = Tok->getNextNonComment(); } - Tok = FirstNSTok; - while (Tok && !Tok->is(tok::l_brace)) { + bool IsPrevColoncolon = false; + bool HasColoncolon = false; + bool IsPrevInline = false; + bool NameFinished = false; + if (FirstNSTok) + Tok = FirstNSTok; + FirstNSTok = nullptr; + // Add everything from '(' to ')'. + auto AddMacro = [&name](const FormatToken *Tok) { + if (!Tok->is(tok::l_paren)) + return Tok; name += Tok->TokenText; - if (Tok->is(tok::kw_inline)) - name += " "; + Tok = Tok->getNextNonComment(); + for (int NestLevel = 1; Tok && NestLevel > 0;) { + if (Tok->is(tok::l_paren)) + ++NestLevel; + else if (Tok->is(tok::r_paren)) + --NestLevel; + name += Tok->TokenText; + Tok = Tok->getNextNonComment(); + } + return Tok; + }; + // If we found '::' in name, then it's the name. Otherwise, we can't tell + // which one is name. For example, `namespace A B {`. + while (Tok && !Tok->is(tok::l_brace)) { + if (FirstNSTok) { + if (!IsPrevInline && HasColoncolon && !IsPrevColoncolon) { + if (FirstNSTok->is(tok::l_paren)) { + FirstNSTok = Tok = AddMacro(FirstNSTok); + continue; + } + if (!FirstNSTok->is(tok::coloncolon)) { + NameFinished = true; + break; + } + } + name += FirstNSTok->TokenText; + IsPrevColoncolon = FirstNSTok->is(tok::coloncolon); + HasColoncolon |= IsPrevColoncolon; + if (FirstNSTok->is(tok::kw_inline)) { + name += " "; + IsPrevInline = true; + } + } + FirstNSTok = Tok; Tok = Tok->getNextNonComment(); } - if (name.empty()) - name = FirstNSName; + if (!NameFinished && FirstNSTok && !FirstNSTok->is(tok::l_brace)) + name += FirstNSTok->TokenText; + if (!FirstNSName.empty() && !HasColoncolon) + name = FirstNSName + " " + name; } return name; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits