diff --git a/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt b/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyLLVMLibcModule + ImplementationInNamespaceCheck.cpp LLVMLibcTidyModule.cpp RestrictSystemLibcHeadersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.h b/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.h @@ -0,0 +1,38 @@ +//===--- ImplementationInNamespaceCheck.h - clang-tidy ----------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVMLIBC_IMPLEMENTATIONINNAMESPACECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVMLIBC_IMPLEMENTATIONINNAMESPACECHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace llvm_libc { + +/// Checks all llvm-libc implementation is within the correct namespace. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/llvmlibc-implementation-in-namespace.html +class ImplementationInNamespaceCheck : public ClangTidyCheck { +public: + ImplementationInNamespaceCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + RequiredNamespace(Options.get("RequiredNamespace", "__llvm_libc")) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + std::string RequiredNamespace; +}; + +} // namespace llvm_libc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVMLIBC_IMPLEMENTATIONINNAMESPACECHECK_H diff --git a/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.cpp b/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.cpp @@ -0,0 +1,56 @@ +//===--- ImplementationInNamespaceCheck.cpp - clang-tidy ------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ImplementationInNamespaceCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace llvm_libc { + +void ImplementationInNamespaceCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + decl(anyOf(hasParent(namespaceDecl(unless(hasName(RequiredNamespace)))), + hasParent(translationUnitDecl())), + unless(namespaceDecl())) + .bind("invalid_namespace"), + this); +} + +void ImplementationInNamespaceCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *MatchedDecl = Result.Nodes.getNodeAs("invalid_namespace"); + if (!Result.SourceManager->isInMainFile(MatchedDecl->getLocation())) + return; + + // Grab enclousing namespace for helpful diagonstics. + const DeclContext *EnclosingDecl = + MatchedDecl->getDeclContext()->getEnclosingNamespaceContext(); + + // If there is no namespace, translation unit is returned. + if (!EnclosingDecl->isNamespace()) { + diag(MatchedDecl->getLocation(), + "%0 is not defined in a namespace, please wrap implentation " + "in '%1' namespace.") + << MatchedDecl->getDeclKindName() << RequiredNamespace; + return; + } + const NamespaceDecl *Namespace = cast(EnclosingDecl); + if (Namespace->getName() != RequiredNamespace) { + diag(MatchedDecl->getLocation(), + "%0 is defined in namespace %1, should be in '%2' namespace.") + << MatchedDecl->getDeclKindName() << Namespace << RequiredNamespace; + } +} + +} // namespace llvm_libc +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/llvmlibc/LLVMLibcTidyModule.cpp b/clang-tools-extra/clang-tidy/llvmlibc/LLVMLibcTidyModule.cpp --- a/clang-tools-extra/clang-tidy/llvmlibc/LLVMLibcTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/llvmlibc/LLVMLibcTidyModule.cpp @@ -9,6 +9,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "ImplementationInNamespaceCheck.h" #include "RestrictSystemLibcHeadersCheck.h" namespace clang { @@ -18,6 +19,8 @@ class LLVMLibcModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck( + "llvmlibc-implementation-in-namespace"); CheckFactories.registerCheck( "llvmlibc-restrict-system-libc-headers"); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -113,6 +113,11 @@ Flags use of the `C` standard library functions ``memset``, ``memcpy`` and ``memcmp`` and similar derivatives on non-trivial types. +- New :doc:`llvmlibc-implementation-in-namespace + ` check. + + Checks all llvm-libc implementation is within the correct namespace. + - New :doc:`llvmlibc-restrict-system-libc-headers ` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -188,6 +188,7 @@ `llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes" `llvm-prefer-register-over-unsigned `_, "Yes" `llvm-twine-local `_, "Yes" + `llvmlibc-implementation-in-namespace `_, `llvmlibc-restrict-system-libc-headers `_, "Yes" `misc-definitions-in-headers `_, "Yes" `misc-misplaced-const `_, diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-implementation-in-namespace.rst b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-implementation-in-namespace.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-implementation-in-namespace.rst @@ -0,0 +1,29 @@ +.. title:: clang-tidy - llvmlibc-implementation-in-namespace + +llvmlibc-implementation-in-namespace +==================================== + +Checks all llvm-libc implementation is within the correct namespace. + +.. code-block:: c++ + + // Correct: implementation inside the correct namespace. + namespace __llvm_libc { + void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {} + } + + // Incorrect: implementation not in a namespace. + void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {} + + // Incorrect: implementation inside an incorrect namespace. + namespace something_else { + void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {} + } + +Options +------- + +.. option:: RequiredNamespace + + The namespace that llvm-libc implementations must be wrapped in. The default + is `__llvm_libc`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-implementation-in-namespace.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-implementation-in-namespace.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-implementation-in-namespace.cpp @@ -0,0 +1,36 @@ +// RUN: %check_clang_tidy %s llvmlibc-implementation-in-namespace %t + +#define MACRO_A "defining macros outside namespace is valid" + +class ClassB; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: CXXRecord is not defined in a namespace, please wrap implentation in '__llvm_libc' namespace. +struct StructC {}; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: CXXRecord is not defined in a namespace, please wrap implentation in '__llvm_libc' namespace. +char *VarD = MACRO_A; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: Var is not defined in a namespace, please wrap implentation in '__llvm_libc' namespace. +typedef int typeE; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: Typedef is not defined in a namespace, please wrap implentation in '__llvm_libc' namespace. +void funcF() {} +// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: Function is not defined in a namespace, please wrap implentation in '__llvm_libc' namespace. + +namespace namespace_g { +class ClassB; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: CXXRecord is defined in namespace 'namespace_g', should be in '__llvm_libc' namespace. +struct StructC {}; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: CXXRecord is defined in namespace 'namespace_g', should be in '__llvm_libc' namespace. +char *VarD = MACRO_A; +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: Var is defined in namespace 'namespace_g', should be in '__llvm_libc' namespace. +typedef int typeE; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: Typedef is defined in namespace 'namespace_g', should be in '__llvm_libc' namespace. +void funcF() {} +// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: Function is defined in namespace 'namespace_g', should be in '__llvm_libc' namespace. +} // namespace namespace_g + +// Wrapped in correct namespace. +namespace __llvm_libc { +class ClassB; +struct StructC {}; +char *VarD = MACRO_A; +typedef int typeE; +void funcF() {} +} // namespace __llvm_libc