This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] FixNamespaceComments does not understand namespace aliases
ClosedPublic

Authored by MyDeveloperDay on Dec 13 2021, 9:40 AM.

Details

Summary

https://github.com/llvm/llvm-project/issues/35876

Ensure a namespace alias doesn't get incorrectly identifier as a namespace

namespace nn {}
void f() {
  namespace n = nn;
  {
    int i;
    int j;
  } // namespace n=nn;
}

Diff Detail

Event Timeline

MyDeveloperDay created this revision.Dec 13 2021, 9:40 AM
MyDeveloperDay requested review of this revision.Dec 13 2021, 9:40 AM
curdeius added inline comments.Dec 13 2021, 11:26 AM
clang/lib/Format/NamespaceEndCommentsFixer.cpp
27

It seems to be a good place for optional, nope?

180

Maybe just checking previous (non-comment) token to be not a semicolon would be enough?

251

As an alternative, couldn't we avoid computing the name for namespace aliases by searching the next semicolon (don't compute) or l_brace (compute)?

clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
1189

Nit: NamespaceAlias with small 's'.

clang/lib/Format/NamespaceEndCommentsFixer.cpp
59

Should only be equal, right?
Otherwise please add a test for semi.

Look backwards from the { rather than scanning the namespace

MyDeveloperDay marked 3 inline comments as done.Dec 13 2021, 12:35 PM
This revision is now accepted and ready to land.Dec 13 2021, 12:43 PM
owenpan added inline comments.Dec 13 2021, 3:05 PM
clang/lib/Format/NamespaceEndCommentsFixer.cpp
185–189

Something like below will do:
if (AnnotatedLines[StartLineIndex - 1]->endsWith(tok::semi))

use endsWith()

MyDeveloperDay marked 3 inline comments as done.Dec 14 2021, 12:26 AM
owenpan accepted this revision.Dec 14 2021, 12:47 AM
This revision was landed with ongoing or failed builds.Dec 14 2021, 6:53 AM
This revision was automatically updated to reflect the committed changes.