Index: clang-tidy/readability/NamespaceCommentCheck.h =================================================================== --- clang-tidy/readability/NamespaceCommentCheck.h +++ clang-tidy/readability/NamespaceCommentCheck.h @@ -34,6 +34,7 @@ llvm::Regex NamespaceCommentPattern; const unsigned ShortNamespaceLines; const unsigned SpacesBeforeComments; + llvm::SmallVector Ends; }; } // namespace readability Index: clang-tidy/readability/NamespaceCommentCheck.cpp =================================================================== --- clang-tidy/readability/NamespaceCommentCheck.cpp +++ clang-tidy/readability/NamespaceCommentCheck.cpp @@ -23,7 +23,7 @@ ClangTidyContext *Context) : ClangTidyCheck(Name, Context), NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" - "namespace( +([a-zA-Z0-9_]+))?\\.? *(\\*/)?$", + "namespace( +([a-zA-Z0-9_:]+))?\\.? *(\\*/)?$", llvm::Regex::IgnoreCase), ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)), SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {} @@ -56,6 +56,14 @@ return Fix; } +static std::string getNamespaceComment(const std::string &s, bool InsertLineBreak) { + std::string Fix = "// namespacen "; + Fix.append(s); + if (InsertLineBreak) + Fix.append("\n"); + return Fix; +} + void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) { const auto *ND = Result.Nodes.getNodeAs("namespace"); const SourceManager &Sources = *Result.SourceManager; @@ -74,6 +82,34 @@ SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1); SourceLocation Loc = AfterRBrace; Token Tok; + SourceLocation LBracketLocation = ND->getLocation(); + auto NestedNamespaceBegin = LBracketLocation; + + // Currently for nested namepsace (n1::n2::...) the AST matcher will match foo + // then bar instead of a single match. So if we got a nested namespace we have + // to skip the next ones + for (const auto &i : Ends) { + if (Sources.isBeforeInTranslationUnit(NestedNamespaceBegin, i)) { + return; + } + } + + while (Lexer::getRawToken(LBracketLocation, Tok, Sources, getLangOpts()) || + !Tok.is(tok::l_brace)) { + LBracketLocation = LBracketLocation.getLocWithOffset(1); + } + + auto TextRange = + Lexer::getAsCharRange(SourceRange(NestedNamespaceBegin, LBracketLocation), + Sources, getLangOpts()); + auto NestedNamespaceName = + Lexer::getSourceText(TextRange, Sources, getLangOpts()).rtrim(); + bool IsNested = NestedNamespaceName.contains(':'); + + if (IsNested) { + Ends.push_back(LBracketLocation); + } + // Skip whitespace until we find the next token. while (Lexer::getRawToken(Loc, Tok, Sources, getLangOpts()) || Tok.is(tok::semi)) { @@ -98,10 +134,13 @@ StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : ""; StringRef Anonymous = Groups.size() > 3 ? Groups[3] : ""; - // Check if the namespace in the comment is the same. - if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) || - (ND->getNameAsString() == NamespaceNameInComment && - Anonymous.empty())) { + // C++17 nested namespace + if (IsNested && NestedNamespaceName == NamespaceNameInComment) { + return; + } // Check if the namespace in the comment is the same. + else if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) || + (ND->getNameAsString() == NamespaceNameInComment && + Anonymous.empty())) { // FIXME: Maybe we need a strict mode, where we always fix namespace // comments with different format. return; @@ -131,13 +170,16 @@ std::string NamespaceName = ND->isAnonymousNamespace() ? "anonymous namespace" - : ("namespace '" + ND->getNameAsString() + "'"); + : ("namespace '" + IsNested ? NestedNamespaceName + : ND->getNameAsString() + "'"); diag(AfterRBrace, Message) - << NamespaceName << FixItHint::CreateReplacement( - CharSourceRange::getCharRange(OldCommentRange), - std::string(SpacesBeforeComments, ' ') + - getNamespaceComment(ND, NeedLineBreak)); + << NamespaceName + << FixItHint::CreateReplacement( + CharSourceRange::getCharRange(OldCommentRange), + std::string(SpacesBeforeComments, ' ') + + (IsNested ? getNamespaceComment(NestedNamespaceName, NeedLineBreak) + : getNamespaceComment(ND, NeedLineBreak))); diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note) << NamespaceName; }