This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix FixNamespaceComments when BraceWrapping AfterNamespace is true.
ClosedPublic

Authored by curdeius on Sep 15 2017, 7:48 AM.

Details

Summary

NamespaceEndCommentsFixer did not fix namespace comments when the brace opening the namespace was not on the same line as the "namespace" keyword.
It occurs in Allman, GNU and Linux styles and whenever BraceWrapping.AfterNamespace is true.

Before:

namespace a
{
void f();
void g();
}

After:

namespace a
{
void f();
void g();
} // namespace a

Event Timeline

curdeius created this revision.Sep 15 2017, 7:48 AM
curdeius edited the summary of this revision. (Show Details)Sep 15 2017, 7:50 AM
djasper edited reviewers, added: krasimir; removed: djasper.Sep 18 2017, 2:10 AM
krasimir edited edge metadata.Sep 18 2017, 8:54 AM

Could you please move the test that adds a namespace end comment to unittests/Format/NamespaceEndCommentsFixerTest.cpp?

Could you please move the test that adds a namespace end comment to unittests/Format/NamespaceEndCommentsFixerTest.cpp?

That's what I wanted to do initially, but the very same tests there passed surprisingly. I think that there are some differences related to "messing up" the code between both test suites.
I'll have another look at it however.

I confirm what I observed before: when invoking tests in unittests/Format/NamespaceEndCommentsFixerTest.cpp, the const AnnotatedLine *line parameter in getNamespaceToken gets one big line that includes both namespace and { (something like namespace\n{\n..., whereas in tests invoked from unittests/Format/FormatTests.cpp, there is a separate line with namespace\n and another one with {\n.

I haven't looked at what provokes this behavior, but I imagine that there are additional passes in FormatTests.

This is how you could add a test in NamespaceEndCommentsFixerTest.cpp:

TEST_F(NamespaceEndCommentsFixerTest, FixesNamespaceCommentsInAllmanStyle) {
  FormatStyle AllmanStyle = getLLVMStyle();
  AllmanStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
  EXPECT_EQ("namespace a\n"
            "{\n"
            "void f();\n"
            "void g();\n"
            "}// namespace a\n",
            fixNamespaceEndComments("namespace a\n"
                                    "{\n"
                                    "void f();\n"
                                    "void g();\n"
                                    "}\n",
                                    AllmanStyle));
}

That's precisely what I've written, but, as I'd said before, such tests pass already without any modification in NamespaceEndCommentsFixer.

Any thoughts on this?

krasimir accepted this revision.Sep 26 2017, 9:53 PM

Looks good!

I must have gotten confused with my test suggestion.
Do you have commit access, or should I commit this for you?

This revision is now accepted and ready to land.Sep 26 2017, 9:53 PM
curdeius closed this revision.Sep 27 2017, 12:53 AM