diff options
author | Nico Weber <nicolasweber@gmx.de> | 2019-07-23 17:49:45 +0000 |
---|---|---|
committer | Nico Weber <nicolasweber@gmx.de> | 2019-07-23 17:49:45 +0000 |
commit | 37944130f950ffb8f14cb7fe1a8ba3ca0d9f097c (patch) | |
tree | ea062784f3fb1882b725678fa27d939bbabf871a /clang/lib/Format | |
parent | 93f505942c8091a3ebbf6f08764635f19dc79095 (diff) | |
download | bcm5719-llvm-37944130f950ffb8f14cb7fe1a8ba3ca0d9f097c.tar.gz bcm5719-llvm-37944130f950ffb8f14cb7fe1a8ba3ca0d9f097c.zip |
clang-format: Fix namespace end comments for namespaces with attributes and macros.
Fixes PR39247.
While here, also make C++20 `namespace A::inline B::inline C` nested
inline namespaced definitions work.
Before:
#define DEPRECATE_WOOF [[deprecated("meow")]]
namespace DEPRECATE_WOOF woof {
void f() {}
} // namespace DEPRECATE_WOOFwoof
namespace [[deprecated("meow")]] woof {
void f() {}
} // namespace [[deprecated("meow")]]woof
namespace woof::inline bark {
void f() {}
} // namespace woof::inlinebark
Now:
#define DEPRECATE_WOOF [[deprecated("meow")]]
namespace DEPRECATE_WOOF woof {
void f() {}
} // namespace woof
namespace [[deprecated("meow")]] woof {
void f() {}
} // namespace woof
namespace woof::inline bark {
void f() {}
} // namespace woof::inline bark
(In addition to the fixed namespace end comments, also note the correct
indent of the namespace contents.)
Differential Revision: https://reviews.llvm.org/D65125
llvm-svn: 366831
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/NamespaceEndCommentsFixer.cpp | 16 | ||||
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 9 |
2 files changed, 21 insertions, 4 deletions
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp index d04fc8f115f..98901cff268 100644 --- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -36,7 +36,7 @@ std::string computeName(const FormatToken *NamespaceTok) { const FormatToken *Tok = NamespaceTok->getNextNonComment(); if (NamespaceTok->is(TT_NamespaceMacro)) { // Collects all the non-comment tokens between opening parenthesis - // and closing parenthesis or comma + // and closing parenthesis or comma. assert(Tok && Tok->is(tok::l_paren) && "expected an opening parenthesis"); Tok = Tok->getNextNonComment(); while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) { @@ -44,9 +44,21 @@ std::string computeName(const FormatToken *NamespaceTok) { Tok = Tok->getNextNonComment(); } } else { - // Collects all the non-comment tokens between 'namespace' and '{'. + // 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 '{') and then return all tokens from one + // token before that up until the '{'. + const FormatToken *FirstNSTok = Tok; + while (Tok && !Tok->is(tok::l_brace) && !Tok->is(tok::coloncolon)) { + FirstNSTok = Tok; + Tok = Tok->getNextNonComment(); + } + + Tok = FirstNSTok; while (Tok && !Tok->is(tok::l_brace)) { name += Tok->TokenText; + if (Tok->is(tok::kw_inline)) + name += " "; Tok = Tok->getNextNonComment(); } } diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index b9da698a931..33d0563d942 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1873,8 +1873,13 @@ void UnwrappedLineParser::parseNamespace() { if (InitialToken.is(TT_NamespaceMacro)) { parseParens(); } else { - while (FormatTok->isOneOf(tok::identifier, tok::coloncolon)) - nextToken(); + while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline, + tok::l_square)) { + if (FormatTok->is(tok::l_square)) + parseSquare(); + else + nextToken(); + } } if (FormatTok->Tok.is(tok::l_brace)) { if (ShouldBreakBeforeBrace(Style, InitialToken)) |