This is an archive of the discontinued LLVM Phabricator instance.

clang-format: Fix formatting C++ namespaces with preceding 'inline' or 'export' specifier
ClosedPublic

Authored by melver on Aug 21 2018, 7:15 AM.

Details

Summary

This fixes formatting namespaces with preceding 'inline' and 'export' (Modules TS) specifiers.

This change fixes namespaces not being identified as such with preceding 'inline' or 'export' specifiers.

Motivation: I was experimenting with the Modules TS (-fmodules-ts) and found it would be useful if clang-format would correctly format 'export namespace'. While making the changes, I noticed that similar issues still exist with 'inline namespace', and addressed them as well.

Diff Detail

Event Timeline

melver created this revision.Aug 21 2018, 7:15 AM
owenpan requested changes to this revision.Aug 21 2018, 11:21 AM
owenpan added a subscriber: owenpan.

By the way, I didn't review the test cases.

lib/Format/FormatToken.h
524–525
if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
lib/Format/NamespaceEndCommentsFixer.cpp
127–128

These lines are functionally the same as lines 523-528 in FormatToken.h. Refactor them?

128–129
if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
lib/Format/UnwrappedLineParser.cpp
991–992

Move this case to after the case tok::kw_export below.

1059–1065
  if (!Style.isCpp())
    break;
case tok::kw_inline:
  nextToken();
  if (FormatTok->Tok.is(tok::kw_namespace)) {
    parseNamespace();
    return;
  }
This revision now requires changes to proceed.Aug 21 2018, 11:21 AM
owenpan added inline comments.Aug 21 2018, 1:15 PM
lib/Format/NamespaceEndCommentsFixer.cpp
128–129

Actually, I meant it for line 129 only.

owenpan added inline comments.Aug 21 2018, 7:23 PM
lib/Format/NamespaceEndCommentsFixer.cpp
127–128

I think this is better:

return NamespaceTok->getNamespaceToken();
owenpan added inline comments.Aug 22 2018, 2:43 AM
lib/Format/UnwrappedLineParser.cpp
1059–1065

I forgot to include LLVM_FALLTHROUGH to suppress the warning:

  if (!Style.isCpp())
    break;
  LLVM_FALLTHROUGH;
case tok::kw_inline:
  nextToken();
  if (FormatTok->Tok.is(tok::kw_namespace)) {
    parseNamespace();
    return;
  }
melver updated this revision to Diff 161960.Aug 22 2018, 8:04 AM
melver marked 8 inline comments as done.

Many thanks for the suggestions!

PTAL.

sammccall accepted this revision.Aug 24 2018, 1:29 AM

Nice fix!

lib/Format/Format.cpp
1311–1312

these 3 startswith checks appear 4 times now, you could pull out a helper function startsWithNamespace in FormatToken.h or something like that.
Up to you.

sammccall added inline comments.Aug 24 2018, 1:30 AM
unittests/Format/FormatTest.cpp
7628

you may want to add tests for other modules TS syntax (e.g. non-namespace export decls).
It seems this works well today, but without tests it could regress.

owenpan added inline comments.Aug 24 2018, 2:37 AM
lib/Format/Format.cpp
1311–1312

I missed it. Good catch!

owenpan added inline comments.Aug 24 2018, 2:50 AM
lib/Format/UnwrappedLineFormatter.cpp
533–535

Maybe add a test case (or modify an existing one) for this fix, with a C/C++ style comment before namespace, inline, and/or export?

melver updated this revision to Diff 162653.Aug 27 2018, 4:42 AM
melver marked 4 inline comments as done.

Many thanks! PTAL.

melver added inline comments.Aug 27 2018, 4:42 AM
lib/Format/Format.cpp
1311–1312

Added startsWithNamespace to TokenAnnotator.h.

unittests/Format/FormatTest.cpp
7628

I've added a couple for 'export class' with access specifiers. Otherwise, any other additions should probably be in future non-namespace related patches.

melver marked 2 inline comments as done.Aug 27 2018, 4:43 AM
melver added a comment.Sep 4 2018, 8:27 AM

Awaiting remaining reviewer acceptance.

FYI: I do not have commit commit access -- what is the procedure to commit once diff is accepted?

Many thanks!

Awaiting remaining reviewer acceptance.

FYI: I do not have commit commit access -- what is the procedure to commit once diff is accepted?

Many thanks!

Anyone with commit access can land it for you - I'm happy to do this.
@owenpan any concerns?

owenpan accepted this revision.Sep 4 2018, 11:20 PM

Awaiting remaining reviewer acceptance.

FYI: I do not have commit commit access -- what is the procedure to commit once diff is accepted?

Many thanks!

Anyone with commit access can land it for you - I'm happy to do this.
@owenpan any concerns?

@sammccall Go ahead! :)

This revision is now accepted and ready to land.Sep 4 2018, 11:20 PM
This revision was automatically updated to reflect the committed changes.
melver added a comment.Sep 5 2018, 1:41 AM

Awaiting remaining reviewer acceptance.

FYI: I do not have commit commit access -- what is the procedure to commit once diff is accepted?

Many thanks!

Anyone with commit access can land it for you - I'm happy to do this.
@owenpan any concerns?

Great, many thanks for committing.