[clang-tools-extra] 8683f5d - [clang-tidy] Add check callee-namespace.
Paula Toth via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 28 17:23:08 PDT 2020
Author: Paula Toth
Date: 2020-04-28T17:22:23-07:00
New Revision: 8683f5de535274398a82ed0a2c9f05648dc43f41
URL: https://github.com/llvm/llvm-project/commit/8683f5de535274398a82ed0a2c9f05648dc43f41
DIFF: https://github.com/llvm/llvm-project/commit/8683f5de535274398a82ed0a2c9f05648dc43f41.diff
LOG: [clang-tidy] Add check callee-namespace.
Summary:
This check will ensure that all calls to functions resolve to one inside the `__llvm_libc` namespace.
This is done to ensure that if we include a public header then we don't accidentally call into the a function within the global namespace.
Reviewers: aaron.ballman, njames93
Reviewed By: aaron.ballman
Subscribers: Eugene.Zelenko, mgorny, xazax.hun, cfe-commits, sivachandra
Tags: #clang-tools-extra, #libc-project, #clang
Differential Revision: https://reviews.llvm.org/D78890
Added:
clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp
clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.h
clang-tools-extra/docs/clang-tidy/checks/llvmlibc-callee-namespace.rst
clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp
Modified:
clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt
clang-tools-extra/clang-tidy/llvmlibc/LLVMLibcTidyModule.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt b/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt
index d7965e126b72..9d4edb2abf1a 100644
--- a/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
)
add_clang_library(clangTidyLLVMLibcModule
+ CalleeNamespaceCheck.cpp
ImplementationInNamespaceCheck.cpp
LLVMLibcTidyModule.cpp
RestrictSystemLibcHeadersCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp b/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp
new file mode 100644
index 000000000000..fbc6762a44e7
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp
@@ -0,0 +1,56 @@
+//===-- 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
diff --git a/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.h b/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.h
new file mode 100644
index 000000000000..b35c6011f088
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.h
@@ -0,0 +1,38 @@
+//===-- 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
diff --git a/clang-tools-extra/clang-tidy/llvmlibc/LLVMLibcTidyModule.cpp b/clang-tools-extra/clang-tidy/llvmlibc/LLVMLibcTidyModule.cpp
index 8c5a7ac20161..61cfe2953664 100644
--- 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 "CalleeNamespaceCheck.h"
#include "ImplementationInNamespaceCheck.h"
#include "RestrictSystemLibcHeadersCheck.h"
@@ -19,6 +20,8 @@ namespace llvm_libc {
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>(
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 011962ea1d05..97ef4271d615 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -113,6 +113,11 @@ New checks
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.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index a3c695dd3311..c3d047f30292 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -188,6 +188,7 @@ Clang-Tidy Checks
`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"
diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-callee-namespace.rst b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-callee-namespace.rst
new file mode 100644
index 000000000000..63c1398881b4
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-callee-namespace.rst
@@ -0,0 +1,24 @@
+.. 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
diff --git a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp
new file mode 100644
index 000000000000..b7fee5dd7e0b
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp
@@ -0,0 +1,42 @@
+// 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
More information about the cfe-commits
mailing list