diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/google')
4 files changed, 91 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/google/CMakeLists.txt b/clang-tools-extra/clang-tidy/google/CMakeLists.txt index bbe9b4d02d8..16708ed7b43 100644 --- a/clang-tools-extra/clang-tidy/google/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/google/CMakeLists.txt @@ -4,6 +4,7 @@ add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp + GlobalNamesInHeadersCheck.cpp GoogleTidyModule.cpp IntegerTypesCheck.cpp MemsetZeroLengthCheck.cpp diff --git a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp new file mode 100644 index 00000000000..d8303d51897 --- /dev/null +++ b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp @@ -0,0 +1,53 @@ +//===--- GlobalNamesInHeadersCheck.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 "GlobalNamesInHeadersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace readability { + +void +GlobalNamesInHeadersCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { + Finder->addMatcher( + decl(anyOf(usingDecl(), usingDirectiveDecl()), + hasDeclContext(translationUnitDecl())).bind("using_decl"), + this); +} + +void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) { + const auto *D = Result.Nodes.getNodeAs<Decl>("using_decl"); + // If it comes from a macro, we'll assume it is fine. + if (D->getLocStart().isMacroID()) + return; + + // Ignore if it comes from the "main" file ... + if (Result.SourceManager->isInMainFile( + Result.SourceManager->getExpansionLoc(D->getLocStart()))) { + // unless that file is a header. + StringRef Filename = Result.SourceManager->getFilename( + Result.SourceManager->getSpellingLoc(D->getLocStart())); + + if (!Filename.endswith(".h")) + return; + } + + diag(D->getLocStart(), + "using declarations in the global namespace in headers are prohibited"); +} + +} // namespace readability +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h new file mode 100644 index 00000000000..07f79c5692f --- /dev/null +++ b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h @@ -0,0 +1,34 @@ +//===--- GlobalNamesInHeadersCheck.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_GLOBAL_NAMES_IN_HEADERS_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_GLOBAL_NAMES_IN_HEADERS_CHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace readability { + +// Flag global namespace pollution in header files. +// Right now it only triggers on using declarations and directives. +class GlobalNamesInHeadersCheck : public ClangTidyCheck { +public: + GlobalNamesInHeadersCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace readability +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_GLOBAL_NAMES_IN_HEADERS_CHECK_H + diff --git a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp index 29d94a987e5..4009f46e722 100644 --- a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp @@ -17,6 +17,7 @@ #include "AvoidCStyleCastsCheck.h" #include "ExplicitConstructorCheck.h" #include "ExplicitMakePairCheck.h" +#include "GlobalNamesInHeadersCheck.h" #include "IntegerTypesCheck.h" #include "MemsetZeroLengthCheck.h" #include "NamedParameterCheck.h" @@ -58,6 +59,8 @@ public: "google-readability-todo"); CheckFactories.registerCheck<readability::BracesAroundStatementsCheck>( "google-readability-braces-around-statements"); + CheckFactories.registerCheck<readability::GlobalNamesInHeadersCheck>( + "google-global-names-in-headers"); CheckFactories.registerCheck<readability::FunctionSizeCheck>( "google-readability-function-size"); CheckFactories.registerCheck<readability::NamespaceCommentCheck>( |