Index: clang-tidy/google/CMakeLists.txt =================================================================== --- clang-tidy/google/CMakeLists.txt +++ clang-tidy/google/CMakeLists.txt @@ -6,6 +6,7 @@ ExplicitMakePairCheck.cpp GoogleTidyModule.cpp NamedParameterCheck.cpp + NamespacesCheck.cpp OverloadedUnaryAndCheck.cpp LINK_LIBS Index: clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tidy/google/GoogleTidyModule.cpp +++ clang-tidy/google/GoogleTidyModule.cpp @@ -14,6 +14,7 @@ #include "ExplicitConstructorCheck.h" #include "ExplicitMakePairCheck.h" #include "NamedParameterCheck.h" +#include "NamespacesCheck.h" #include "OverloadedUnaryAndCheck.h" using namespace clang::ast_matchers; @@ -28,6 +29,9 @@ "google-build-explicit-make-pair", new ClangTidyCheckFactory()); CheckFactories.addCheckFactory( + "google-build-namespaces", + new ClangTidyCheckFactory()); + CheckFactories.addCheckFactory( "google-explicit-constructor", new ClangTidyCheckFactory()); CheckFactories.addCheckFactory( Index: clang-tidy/google/NamespacesCheck.h =================================================================== --- /dev/null +++ clang-tidy/google/NamespacesCheck.h @@ -0,0 +1,31 @@ +//===--- NamespacesCheck.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_NAMESPACES_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_NAMESPACES_CHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace build { + +/// \brief Finds anonymous namespaces in headers and using namespace directives. +/// Corresponding cpplint.py check name: 'build/namespaces'. +class NamespacesCheck : 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_NAMESPACES_CHECK_H Index: clang-tidy/google/NamespacesCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/google/NamespacesCheck.cpp @@ -0,0 +1,60 @@ +//===--- NamespacesCheck.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 "NamespacesCheck.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(); +} + +const internal::VariadicDynCastAllOfMatcher + usingDirectiveDecl; + +} // namespace ast_matchers +namespace tidy { +namespace build { + +void NamespacesCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { + Finder->addMatcher( + namespaceDecl(isAnonymousNamespace()).bind("anonymousNamespace"), this); + Finder->addMatcher(usingDirectiveDecl().bind("usingNamespace"), this); +} + +void NamespacesCheck::check(const MatchFinder::MatchResult &Result) { + SourceManager *SM = Result.SourceManager; + if (const auto *N = + Result.Nodes.getNodeAs("anonymousNamespace")) { + SourceLocation Loc = N->getLocStart(); + if (Loc.isValid()) { + // Check for common suffixes only + 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."); + } + } else if (const auto *U = + Result.Nodes.getNodeAs("usingNamespace")) { + SourceLocation Loc = U->getLocStart(); + if (!U->isImplicit() && Loc.isValid()) + diag(Loc, "do not use namespace using-directives. Use using-declarations " + "instead."); + } +} + +} // namespace build +} // namespace tidy +} // namespace clang Index: test/clang-tidy/Inputs/google-namespaces.h =================================================================== --- /dev/null +++ test/clang-tidy/Inputs/google-namespaces.h @@ -0,0 +1,7 @@ +namespace { +int x; +} + +namespace spaaaace { +class core; +} Index: test/clang-tidy/google-member-string-references.cpp =================================================================== --- /dev/null +++ test/clang-tidy/google-member-string-references.cpp @@ -0,0 +1,47 @@ +// RUN: clang-tidy %s -checks='-*,google-runtime-member-string-references' -- | FileCheck %s -implicit-check-not="{{warning|error}}:" + +namespace std { +template + class basic_string {}; + +typedef basic_string string; +} + +class string {}; + + +struct A { + const std::string &s; +// CHECK: :[[@LINE-1]]:3: warning: const string& members are dangerous. It is much better to use alternatives, such as pointers or simple constants. +}; + +struct B { + std::string &s; +}; + +struct C { + const std::string s; +}; + +template +struct D { + D(); + const T &s; +}; + +D d; + +struct AA { + const string &s; +// CHECK: :[[@LINE-1]]:3: warning: const string& members are dangerous. It is much better to use alternatives, such as pointers or simple constants. +}; + +struct BB { + string &s; +}; + +struct CC { + const string s; +}; + +D dd; Index: test/clang-tidy/google-namespaces.cpp =================================================================== --- /dev/null +++ test/clang-tidy/google-namespaces.cpp @@ -0,0 +1,8 @@ +// RUN: clang-tidy %s -checks='-*,google-build-namespaces' -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