Author: d0k Date: Wed Jul 16 09:16:56 2014 New Revision: 213153 URL: http://llvm.org/viewvc/llvm-project?rev=213153&view=rev Log: [clang-tidy] Add namespaces checkers.
This change contains of two checkers that warn about 1. anonymous namespaces in header files. 2. 'using namespace' directives everywhere. Differential Revision: http://reviews.llvm.org/D4523 Added: clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h clang-tools-extra/trunk/test/clang-tidy/Inputs/google-namespaces.h clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp Modified: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Modified: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt?rev=213153&r1=213152&r2=213153&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt Wed Jul 16 09:16:56 2014 @@ -8,6 +8,8 @@ add_clang_library(clangTidyGoogleModule NamedParameterCheck.cpp OverloadedUnaryAndCheck.cpp StringReferenceMemberCheck.cpp + UnnamedNamespaceInHeaderCheck.cpp + UsingNamespaceDirectiveCheck.cpp LINK_LIBS clangAST Modified: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp?rev=213153&r1=213152&r2=213153&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Wed Jul 16 09:16:56 2014 @@ -16,6 +16,8 @@ #include "NamedParameterCheck.h" #include "OverloadedUnaryAndCheck.h" #include "StringReferenceMemberCheck.h" +#include "UnnamedNamespaceInHeaderCheck.h" +#include "UsingNamespaceDirectiveCheck.h" using namespace clang::ast_matchers; @@ -29,6 +31,12 @@ public: "google-build-explicit-make-pair", new ClangTidyCheckFactory<build::ExplicitMakePairCheck>()); CheckFactories.addCheckFactory( + "google-build-namespaces", + new ClangTidyCheckFactory<build::UnnamedNamespaceInHeaderCheck>()); + CheckFactories.addCheckFactory( + "google-build-using-namespace", + new ClangTidyCheckFactory<build::UsingNamespaceDirectiveCheck>()); + CheckFactories.addCheckFactory( "google-explicit-constructor", new ClangTidyCheckFactory<ExplicitConstructorCheck>()); CheckFactories.addCheckFactory( Added: clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp?rev=213153&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp (added) +++ clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp Wed Jul 16 09:16:56 2014 @@ -0,0 +1,51 @@ +//===--- UnnamedNamespaceInHeaderCheck.cpp - clang-tidy ---------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UnnamedNamespaceInHeaderCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/AST/ASTContext.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace ast_matchers { +AST_MATCHER(NamespaceDecl, isAnonymousNamespace) { + return Node.isAnonymousNamespace(); +} +} // namespace ast_matchers + +namespace tidy { +namespace build { + +void UnnamedNamespaceInHeaderCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + Finder->addMatcher( + namespaceDecl(isAnonymousNamespace()).bind("anonymousNamespace"), this); +} + +void +UnnamedNamespaceInHeaderCheck::check(const MatchFinder::MatchResult &Result) { + SourceManager *SM = Result.SourceManager; + const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace"); + SourceLocation Loc = N->getLocStart(); + if (!Loc.isValid()) + return; + + // Look if we're inside a header, check for common suffixes only. + // TODO: Allow configuring the set of file extensions. + StringRef FileName = SM->getPresumedLoc(Loc).getFilename(); + if (FileName.endswith(".h") || FileName.endswith(".hh") || + FileName.endswith(".hpp") || FileName.endswith(".hxx")) + diag(Loc, "do not use unnamed namespaces in header files."); +} + +} // namespace build +} // namespace tidy +} // namespace clang Added: clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h?rev=213153&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h (added) +++ clang-tools-extra/trunk/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h Wed Jul 16 09:16:56 2014 @@ -0,0 +1,32 @@ +//===--- UnnamedNamespaceInHeaderCheck.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_GOOGLE_UNNAMED_NAMESPACE_IN_HEADER_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_UNNAMED_NAMESPACE_IN_HEADER_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace build { + +/// \brief Finds anonymous namespaces in headers. +/// +/// Corresponding cpplint.py check name: 'build/namespaces'. +class UnnamedNamespaceInHeaderCheck : public ClangTidyCheck { +public: + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace build +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_UNNAMED_NAMESPACE_IN_HEADER_H Added: clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp?rev=213153&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp (added) +++ clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.cpp Wed Jul 16 09:16:56 2014 @@ -0,0 +1,41 @@ +//===--- UsingNamespaceDirectiveCheck.cpp - clang-tidy ----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UsingNamespaceDirectiveCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/AST/ASTContext.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace build { + +void UsingNamespaceDirectiveCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + Finder->addMatcher(usingDirectiveDecl().bind("usingNamespace"), this); +} + +void +UsingNamespaceDirectiveCheck::check(const MatchFinder::MatchResult &Result) { + const auto *U = Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace"); + SourceLocation Loc = U->getLocStart(); + if (U->isImplicit() || !Loc.isValid()) + return; + + diag(Loc, "do not use namespace using-directives. Use using-declarations " + "instead."); + // TODO: We could suggest a list of using directives replacing the using + // namespace directive. +} + +} // namespace build +} // namespace tidy +} // namespace clang Added: clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h?rev=213153&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h (added) +++ clang-tools-extra/trunk/clang-tidy/google/UsingNamespaceDirectiveCheck.h Wed Jul 16 09:16:56 2014 @@ -0,0 +1,32 @@ +//===--- UsingNamespaceDirectiveCheck.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_GOOGLE_USING_NAMESPACE_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_USING_NAMESPACE_CHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace build { + +/// \brief Finds using namespace directives. +/// +/// Corresponding cpplint.py check name: 'build/namespaces'. +class UsingNamespaceDirectiveCheck : public ClangTidyCheck { +public: + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace build +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_USING_NAMESPACE_CHECK_H Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/google-namespaces.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/google-namespaces.h?rev=213153&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/Inputs/google-namespaces.h (added) +++ clang-tools-extra/trunk/test/clang-tidy/Inputs/google-namespaces.h Wed Jul 16 09:16:56 2014 @@ -0,0 +1,7 @@ +namespace { +int x; +} + +namespace spaaaace { +class core; +} Added: clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp?rev=213153&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp (added) +++ clang-tools-extra/trunk/test/clang-tidy/google-namespaces.cpp Wed Jul 16 09:16:56 2014 @@ -0,0 +1,8 @@ +// RUN: clang-tidy %s -checks='-*,google-build-namespaces,google-build-using-namespace' -header-filter='.*' -- | FileCheck %s -implicit-check-not="{{warning|error}}:" +#include "Inputs/google-namespaces.h" +// CHECK: warning: do not use unnamed namespaces in header files. + +using namespace spaaaace; +// CHECK: :[[@LINE-1]]:1: warning: do not use namespace using-directives. Use using-declarations instead. + +using spaaaace::core; // no-warning _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
