From 33fc3db9a16f8174bb0fcabc5b323c1f8b48cf4c Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Mon, 22 Sep 2014 10:41:39 +0000 Subject: Add NamespaceCommentCheck to the Google module. Summary: 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. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D5410 llvm-svn: 218240 --- clang-tools-extra/clang-tidy/llvm/CMakeLists.txt | 2 +- .../clang-tidy/llvm/LLVMTidyModule.cpp | 4 +- .../clang-tidy/llvm/NamespaceCommentCheck.cpp | 127 --------------------- .../clang-tidy/llvm/NamespaceCommentCheck.h | 39 ------- 4 files changed, 3 insertions(+), 169 deletions(-) delete mode 100644 clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.cpp delete mode 100644 clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.h (limited to 'clang-tools-extra/clang-tidy/llvm') diff --git a/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt b/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt index 420745d4cb5..360e9308e90 100644 --- a/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt @@ -4,7 +4,6 @@ add_clang_library(clangTidyLLVMModule HeaderGuardCheck.cpp IncludeOrderCheck.cpp LLVMTidyModule.cpp - NamespaceCommentCheck.cpp TwineLocalCheck.cpp LINK_LIBS @@ -13,6 +12,7 @@ add_clang_library(clangTidyLLVMModule clangBasic clangLex clangTidy + clangTidyReadability clangTidyUtils clangTooling ) diff --git a/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp b/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp index 1eb82c58f59..73add5e8f5a 100644 --- a/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp @@ -12,7 +12,7 @@ #include "../ClangTidyModuleRegistry.h" #include "HeaderGuardCheck.h" #include "IncludeOrderCheck.h" -#include "NamespaceCommentCheck.h" +#include "../readability/NamespaceCommentCheck.h" #include "TwineLocalCheck.h" namespace clang { @@ -23,7 +23,7 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck("llvm-header-guard"); CheckFactories.registerCheck("llvm-include-order"); - CheckFactories.registerCheck( + CheckFactories.registerCheck( "llvm-namespace-comment"); CheckFactories.registerCheck("llvm-twine-local"); } diff --git a/clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.cpp b/clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.cpp deleted file mode 100644 index 897c60a9203..00000000000 --- a/clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.cpp +++ /dev/null @@ -1,127 +0,0 @@ -//===--- NamespaceCommentCheck.cpp - clang-tidy ---------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "NamespaceCommentCheck.h" -#include "clang/AST/ASTContext.h" -#include "clang/ASTMatchers/ASTMatchers.h" -#include "clang/Lex/Lexer.h" -#include "llvm/ADT/StringExtras.h" - -using namespace clang::ast_matchers; - -namespace clang { -namespace tidy { - -NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name, - ClangTidyContext *Context) - : ClangTidyCheck(Name, Context), - NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" - "namespace( +([a-zA-Z0-9_]+))? *(\\*/)?$", - llvm::Regex::IgnoreCase), - ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)), - SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {} - -void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { - Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines); - Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments); -} - -void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher(namespaceDecl().bind("namespace"), this); -} - -bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, - SourceLocation Loc2) { - return Loc1.isFileID() && Loc2.isFileID() && - Sources.getFileID(Loc1) == Sources.getFileID(Loc2); -} - -std::string getNamespaceComment(const NamespaceDecl *ND, bool InsertLineBreak, - unsigned SpacesBeforeComments) { - std::string Fix = "// namespace"; - if (!ND->isAnonymousNamespace()) - Fix.append(std::string(SpacesBeforeComments, ' ')) - .append(ND->getNameAsString()); - if (InsertLineBreak) - Fix.append("\n"); - return Fix; -} - -void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) { - const NamespaceDecl *ND = Result.Nodes.getNodeAs("namespace"); - const SourceManager &Sources = *Result.SourceManager; - - if (!locationsInSameFile(Sources, ND->getLocStart(), ND->getRBraceLoc())) - return; - - // Don't require closing comments for namespaces spanning less than certain - // number of lines. - unsigned StartLine = Sources.getSpellingLineNumber(ND->getLocStart()); - unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc()); - if (EndLine - StartLine + 1 <= ShortNamespaceLines) - return; - - // Find next token after the namespace closing brace. - SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1); - SourceLocation Loc = AfterRBrace; - Token Tok; - // Skip whitespace until we find the next token. - while (Lexer::getRawToken(Loc, Tok, Sources, Result.Context->getLangOpts())) { - Loc = Loc.getLocWithOffset(1); - } - if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc)) - return; - - bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine; - // If we insert a line comment before the token in the same line, we need - // to insert a line break. - bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof); - - // Try to find existing namespace closing comment on the same line. - if (Tok.is(tok::comment) && NextTokenIsOnSameLine) { - StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength()); - SmallVector Groups; - if (NamespaceCommentPattern.match(Comment, &Groups)) { - StringRef NamespaceNameInComment = Groups.size() >= 6 ? Groups[5] : ""; - - // Check if the namespace in the comment is the same. - if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) || - ND->getNameAsString() == NamespaceNameInComment) { - // FIXME: Maybe we need a strict mode, where we always fix namespace - // comments with different format. - return; - } - - // Otherwise we need to fix the comment. - NeedLineBreak = Comment.startswith("/*"); - CharSourceRange OldCommentRange = CharSourceRange::getCharRange( - SourceRange(Loc, Loc.getLocWithOffset(Tok.getLength()))); - diag(Loc, "namespace closing comment refers to a wrong namespace '%0'") - << NamespaceNameInComment - << FixItHint::CreateReplacement( - OldCommentRange, - getNamespaceComment(ND, NeedLineBreak, SpacesBeforeComments)); - return; - } - - // This is not a recognized form of a namespace closing comment. - // Leave line comment on the same line. Move block comment to the next line, - // as it can be multi-line or there may be other tokens behind it. - if (Comment.startswith("//")) - NeedLineBreak = false; - } - - diag(ND->getLocation(), "namespace not terminated with a closing comment") - << FixItHint::CreateInsertion( - AfterRBrace, - " " + getNamespaceComment(ND, NeedLineBreak, SpacesBeforeComments)); -} - -} // namespace tidy -} // namespace clang diff --git a/clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.h b/clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.h deleted file mode 100644 index f1b92e7b63b..00000000000 --- a/clang-tools-extra/clang-tidy/llvm/NamespaceCommentCheck.h +++ /dev/null @@ -1,39 +0,0 @@ -//===--- NamespaceCommentCheck.h - clang-tidy -------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_NAMESPACE_COMMENT_CHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_NAMESPACE_COMMENT_CHECK_H - -#include "../ClangTidy.h" -#include "llvm/Support/Regex.h" - -namespace clang { -namespace tidy { - -/// \brief Checks that long namespaces have a closing comment. -/// -/// see: http://llvm.org/docs/CodingStandards.html#namespace-indentation -class NamespaceCommentCheck : public ClangTidyCheck { -public: - NamespaceCommentCheck(StringRef Name, ClangTidyContext *Context); - void registerMatchers(ast_matchers::MatchFinder *Finder) override; - void check(const ast_matchers::MatchFinder::MatchResult &Result) override; - -private: - void storeOptions(ClangTidyOptions::OptionMap &Options) override; - - llvm::Regex NamespaceCommentPattern; - const unsigned ShortNamespaceLines; - const unsigned SpacesBeforeComments; -}; - -} // namespace tidy -} // namespace clang - -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_NAMESPACE_COMMENT_CHECK_H -- cgit v1.2.1