)
add_clang_library(clangTidyLLVMLibcModule
+ CalleeNamespaceCheck.cpp
ImplementationInNamespaceCheck.cpp
LLVMLibcTidyModule.cpp
RestrictSystemLibcHeadersCheck.cpp
--- /dev/null
+//===-- CalleeNamespaceCheck.cpp ------------------------------------------===//
+//
+// 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 "CalleeNamespaceCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace llvm_libc {
+
+// Gets the outermost namespace of a DeclContext, right under the Translation
+// Unit.
+const DeclContext *getOutermostNamespace(const DeclContext *Decl) {
+ const DeclContext *Parent = Decl->getParent();
+ if (Parent && Parent->isTranslationUnit())
+ return Decl;
+ return getOutermostNamespace(Parent);
+}
+
+void CalleeNamespaceCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ declRefExpr(to(functionDecl().bind("func"))).bind("use-site"), this);
+}
+
+void CalleeNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *UsageSiteExpr = Result.Nodes.getNodeAs<DeclRefExpr>("use-site");
+ const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func");
+
+ // Ignore compiler builtin functions.
+ if (FuncDecl->getBuiltinID() != 0)
+ return;
+
+ // If the outermost namespace of the function is __llvm_libc, we're good.
+ const auto *NS = dyn_cast<NamespaceDecl>(getOutermostNamespace(FuncDecl));
+ if (NS && NS->getName() == "__llvm_libc")
+ return;
+
+ diag(UsageSiteExpr->getBeginLoc(), "%0 must resolve to a function declared "
+ "within the '__llvm_libc' namespace")
+ << FuncDecl;
+
+ diag(FuncDecl->getLocation(), "resolves to this declaration",
+ clang::DiagnosticIDs::Note);
+}
+
+} // namespace llvm_libc
+} // namespace tidy
+} // namespace clang
--- /dev/null
+//===-- CalleeNamespaceCheck.h ----------------------------------*- 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_CALLEENAMESPACECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVMLIBC_CALLEENAMESPACECHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace llvm_libc {
+
+/// Checks all calls resolve to functions within __llvm_libc namespace.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/llvmlibc-callee-namespace.html
+class CalleeNamespaceCheck : public ClangTidyCheck {
+public:
+ CalleeNamespaceCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+
+ bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+ return LangOpts.CPlusPlus;
+ }
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace llvm_libc
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVMLIBC_CALLEENAMESPACECHECK_H
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
+#include "CalleeNamespaceCheck.h"
#include "ImplementationInNamespaceCheck.h"
#include "RestrictSystemLibcHeadersCheck.h"
class LLVMLibcModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+ CheckFactories.registerCheck<CalleeNamespaceCheck>(
+ "llvmlibc-callee-namespace");
CheckFactories.registerCheck<ImplementationInNamespaceCheck>(
"llvmlibc-implementation-in-namespace");
CheckFactories.registerCheck<RestrictSystemLibcHeadersCheck>(
Flags use of the `C` standard library functions ``memset``, ``memcpy`` and
``memcmp`` and similar derivatives on non-trivial types.
+- New :doc:`llvmlibc-callee-namespace
+ <clang-tidy/checks/llvmlibc-callee-namespace>` check.
+
+ Checks all calls resolve to functions within ``__llvm_libc`` namespace.
+
- New :doc:`llvmlibc-implementation-in-namespace
<clang-tidy/checks/llvmlibc-implementation-in-namespace>` check.
`llvm-prefer-isa-or-dyn-cast-in-conditionals <llvm-prefer-isa-or-dyn-cast-in-conditionals.html>`_, "Yes"
`llvm-prefer-register-over-unsigned <llvm-prefer-register-over-unsigned.html>`_, "Yes"
`llvm-twine-local <llvm-twine-local.html>`_, "Yes"
+ `llvmlibc-callee-namespace <llvmlibc-calle-namespace.html>`_,
`llvmlibc-implementation-in-namespace <llvmlibc-implementation-in-namespace.html>`_,
`llvmlibc-restrict-system-libc-headers <llvmlibc-restrict-system-libc-headers.html>`_, "Yes"
`misc-definitions-in-headers <misc-definitions-in-headers.html>`_, "Yes"
--- /dev/null
+.. title:: clang-tidy - llvmlibc-callee-namespace
+
+llvmlibc-callee-namespace
+====================================
+
+Checks all calls resolve to functions within ``__llvm_libc`` namespace.
+
+.. code-block:: c++
+
+ namespace __llvm_libc {
+
+ // Allow calls with the fully qualified name.
+ __llvm_libc::strlen("hello");
+
+ // Allow calls to compiler provided functions.
+ (void)__builtin_abs(-1);
+
+ // Bare calls are allowed as long as they resolve to the correct namespace.
+ strlen("world");
+
+ // Disallow calling into functions in the global namespace.
+ ::strlen("!");
+
+ } // namespace __llvm_libc
--- /dev/null
+// RUN: %check_clang_tidy %s llvmlibc-callee-namespace %t
+
+namespace __llvm_libc {
+namespace nested {
+void nested_func() {}
+} // namespace nested
+void libc_api_func() {}
+} // namespace __llvm_libc
+
+// Emulate a function from the public headers like string.h
+void libc_api_func() {}
+
+namespace __llvm_libc {
+void Test() {
+ // Allow calls with the fully qualified name.
+ __llvm_libc::libc_api_func();
+ __llvm_libc::nested::nested_func();
+ void (*qualifiedPtr)(void) = __llvm_libc::libc_api_func;
+ qualifiedPtr();
+
+ // Should not trigger on compiler provided function calls.
+ (void)__builtin_abs(-1);
+
+ // Bare calls are allowed as long as they resolve to the correct namespace.
+ libc_api_func();
+ nested::nested_func();
+ void (*barePtr)(void) = __llvm_libc::libc_api_func;
+ barePtr();
+
+ // Disallow calling into global namespace for implemented entrypoints.
+ ::libc_api_func();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'libc_api_func' must resolve to a function declared within the '__llvm_libc' namespace
+ // CHECK-MESSAGES: :11:6: note: resolves to this declaration
+
+ // Disallow indirect references to functions in global namespace.
+ void (*badPtr)(void) = ::libc_api_func;
+ badPtr();
+ // CHECK-MESSAGES: :[[@LINE-2]]:26: warning: 'libc_api_func' must resolve to a function declared within the '__llvm_libc' namespace
+ // CHECK-MESSAGES: :11:6: note: resolves to this declaration
+}
+
+} // namespace __llvm_libc