Index: clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tidy/google/GoogleTidyModule.cpp +++ clang-tidy/google/GoogleTidyModule.cpp @@ -21,6 +21,7 @@ #include "TodoCommentCheck.h" #include "UnnamedNamespaceInHeaderCheck.h" #include "UsingNamespaceDirectiveCheck.h" +#include "../llvm/NamespaceCommentCheck.h" using namespace clang::ast_matchers; @@ -52,6 +53,8 @@ "google-readability-function"); CheckFactories.registerCheck( "google-readability-todo"); + CheckFactories.registerCheck( + "google-readability-namespace-comments"); } }; Index: clang-tidy/llvm/NamespaceCommentCheck.cpp =================================================================== --- clang-tidy/llvm/NamespaceCommentCheck.cpp +++ clang-tidy/llvm/NamespaceCommentCheck.cpp @@ -25,7 +25,8 @@ "namespace( +([a-zA-Z0-9_]+))? *(\\*/)?$", llvm::Regex::IgnoreCase), ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)), - SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {} + SpacesBeforeComments(Options.get("SpacesBeforeComments", + Name.startswith("google") ? 2u : 1u)) {} void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines); @@ -42,12 +43,10 @@ Sources.getFileID(Loc1) == Sources.getFileID(Loc2); } -std::string getNamespaceComment(const NamespaceDecl *ND, bool InsertLineBreak, - unsigned SpacesBeforeComments) { +std::string getNamespaceComment(const NamespaceDecl *ND, bool InsertLineBreak) { std::string Fix = "// namespace"; if (!ND->isAnonymousNamespace()) - Fix.append(std::string(SpacesBeforeComments, ' ')) - .append(ND->getNameAsString()); + Fix.append(" ").append(ND->getNameAsString()); if (InsertLineBreak) Fix.append("\n"); return Fix; @@ -105,8 +104,7 @@ diag(Loc, "namespace closing comment refers to a wrong namespace '%0'") << NamespaceNameInComment << FixItHint::CreateReplacement( - OldCommentRange, - getNamespaceComment(ND, NeedLineBreak, SpacesBeforeComments)); + OldCommentRange, getNamespaceComment(ND, NeedLineBreak)); return; } @@ -118,9 +116,9 @@ } diag(ND->getLocation(), "namespace not terminated with a closing comment") - << FixItHint::CreateInsertion( - AfterRBrace, - " " + getNamespaceComment(ND, NeedLineBreak, SpacesBeforeComments)); + << FixItHint::CreateInsertion(AfterRBrace, + std::string(SpacesBeforeComments, ' ') + + getNamespaceComment(ND, NeedLineBreak)); } } // namespace tidy Index: test/clang-tidy/google-readability-namespace-comments.cpp =================================================================== --- /dev/null +++ test/clang-tidy/google-readability-namespace-comments.cpp @@ -0,0 +1,15 @@ +// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-readability-namespace-comments %t +// REQUIRES: shell + +// CHECK-MESSAGES: :[[@LINE+2]]:11: warning: namespace not terminated with a closing comment [google-readability-namespace-comments] +// CHECK-MESSAGES: :[[@LINE+2]]:11: warning: namespace not terminated with a closing comment [google-readability-namespace-comments] +namespace n1 { +namespace n2 { + +} +} +// CHECK-FIXES: } // namespace n2 +// CHECK-FIXES: } // namespace n1 + + +namespace short1 { namespace short2 { } }