Hi djasper,
This uses a bit hacky way to set the defaults for the spaces before
comments, but it's also one of the simplest ways. Fixed a bug with how the
SpacesBeforeComments option was used.
http://reviews.llvm.org/D5410
Files:
clang-tidy/google/GoogleTidyModule.cpp
clang-tidy/llvm/NamespaceCommentCheck.cpp
test/clang-tidy/google-readability-namespace-comments.cpp
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<readability::TodoCommentCheck>(
"google-readability-todo");
+ CheckFactories.registerCheck<NamespaceCommentCheck>(
+ "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 { } }
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits