[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Sat May 4 06:54:37 PDT 2024
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/90830
>From 24cbbd0c87ab2a06381d210da1dff5f966b72773 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 2 May 2024 15:44:45 +0800
Subject: [PATCH] reformat
---
.../clang-tidy/readability/CMakeLists.txt | 1 +
.../readability/ReadabilityTidyModule.cpp | 3 +
.../UnnecessaryExternalLinkageCheck.cpp | 82 +++++++++++++++++++
.../UnnecessaryExternalLinkageCheck.h | 33 ++++++++
clang-tools-extra/docs/ReleaseNotes.rst | 5 ++
.../docs/clang-tidy/checks/list.rst | 1 +
.../unnecessary-external-linkage.rst | 26 ++++++
.../readability/Inputs/mark-static-var/func.h | 3 +
.../readability/Inputs/mark-static-var/var.h | 3 +
.../unnecessary-external-linkage-func.cpp | 30 +++++++
.../unnecessary-external-linkage-var.cpp | 40 +++++++++
11 files changed, 227 insertions(+)
create mode 100644 clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp
create mode 100644 clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.h
create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/unnecessary-external-linkage.rst
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/func.h
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/var.h
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-func.cpp
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-var.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 41065fc8e87859..8f58d9f24ba491 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -23,6 +23,7 @@ add_clang_library(clangTidyReadabilityModule
IdentifierLengthCheck.cpp
IdentifierNamingCheck.cpp
ImplicitBoolConversionCheck.cpp
+ UnnecessaryExternalLinkageCheck.cpp
RedundantInlineSpecifierCheck.cpp
InconsistentDeclarationParameterNameCheck.cpp
IsolateDeclarationCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index d61c0ba39658e5..d389287e8f4909 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -58,6 +58,7 @@
#include "StringCompareCheck.h"
#include "SuspiciousCallArgumentCheck.h"
#include "UniqueptrDeleteReleaseCheck.h"
+#include "UnnecessaryExternalLinkageCheck.h"
#include "UppercaseLiteralSuffixCheck.h"
#include "UseAnyOfAllOfCheck.h"
#include "UseStdMinMaxCheck.h"
@@ -106,6 +107,8 @@ class ReadabilityModule : public ClangTidyModule {
"readability-identifier-naming");
CheckFactories.registerCheck<ImplicitBoolConversionCheck>(
"readability-implicit-bool-conversion");
+ CheckFactories.registerCheck<UnnecessaryExternalLinkageCheck>(
+ "readability-unnecessary-external-linkage");
CheckFactories.registerCheck<MathMissingParenthesesCheck>(
"readability-math-missing-parentheses");
CheckFactories.registerCheck<RedundantInlineSpecifierCheck>(
diff --git a/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp
new file mode 100644
index 00000000000000..4970d3339ef05d
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp
@@ -0,0 +1,82 @@
+//===--- UnnecessaryExternalLinkageCheck.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 "UnnecessaryExternalLinkageCheck.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+#include "clang/Basic/Specifiers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_POLYMORPHIC_MATCHER(isFirstDecl,
+ AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
+ VarDecl)) {
+ return Node.isFirstDecl();
+}
+
+AST_MATCHER(Decl, isInMainFile) {
+ for (const Decl *D : Node.redecls())
+ if (!Finder->getASTContext().getSourceManager().isInMainFile(
+ D->getLocation()))
+ return false;
+ return true;
+}
+
+AST_POLYMORPHIC_MATCHER(isExternStorageClass,
+ AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
+ VarDecl)) {
+ return Node.getStorageClass() == SC_Extern;
+}
+
+} // namespace
+
+void UnnecessaryExternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
+ auto Common = allOf(isFirstDecl(), isInMainFile(),
+ unless(anyOf(
+ // 1. internal linkage
+ isStaticStorageClass(), isInAnonymousNamespace(),
+ // 2. explicit external linkage
+ isExternStorageClass(), isExternC(),
+ // 3. template
+ isExplicitTemplateSpecialization(),
+ clang::ast_matchers::isTemplateInstantiation(),
+ // 4. friend
+ hasAncestor(friendDecl()))));
+ Finder->addMatcher(
+ functionDecl(Common, unless(cxxMethodDecl()), unless(isMain()))
+ .bind("fn"),
+ this);
+ Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this);
+}
+
+static constexpr StringRef Message =
+ "%0 %1 can be internal linkage, "
+ "marking as static or using anonymous namespace can avoid external "
+ "linkage.";
+
+void UnnecessaryExternalLinkageCheck::check(
+ const MatchFinder::MatchResult &Result) {
+ if (const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("fn")) {
+ diag(FD->getLocation(), Message) << "function" << FD;
+ return;
+ }
+ if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var")) {
+ diag(VD->getLocation(), Message) << "variable" << VD;
+ return;
+ }
+ llvm_unreachable("");
+}
+
+} // namespace clang::tidy::readability
diff --git a/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.h b/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.h
new file mode 100644
index 00000000000000..523e271ef20626
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.h
@@ -0,0 +1,33 @@
+//===--- UnnecessaryExternalLinkageCheck.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_READABILITY_UNNECESSARYEXTERNALLINKAGECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_UNNECESSARYEXTERNALLINKAGECHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::readability {
+
+/// Detects variable and function can be marked as static.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/readability/unnecessary-external-linkage.html
+class UnnecessaryExternalLinkageCheck : public ClangTidyCheck {
+public:
+ UnnecessaryExternalLinkageCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ std::optional<TraversalKind> getCheckTraversalKind() const override {
+ return TK_IgnoreUnlessSpelledInSource;
+ }
+};
+
+} // namespace clang::tidy::readability
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_UNNECESSARYEXTERNALLINKAGECHECK_H
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 5956ccb925485c..6269393eb02b8b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -151,6 +151,11 @@ New checks
Enforces consistent style for enumerators' initialization, covering three
styles: none, first only, or all initialized explicitly.
+- New :doc:`readability-unnecessary-external-linkage
+ <clang-tidy/checks/readability/unnecessary-external-linkage>` check.
+
+ Detects variable and function can be marked as static.
+
- New :doc:`readability-math-missing-parentheses
<clang-tidy/checks/readability/math-missing-parentheses>` check.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index 5cdaf9e35b6acd..c50ed21a07abb9 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -390,6 +390,7 @@ Clang-Tidy Checks
:doc:`readability-string-compare <readability/string-compare>`, "Yes"
:doc:`readability-suspicious-call-argument <readability/suspicious-call-argument>`,
:doc:`readability-uniqueptr-delete-release <readability/uniqueptr-delete-release>`, "Yes"
+ :doc:`readability-unnecessary-external-linkage <readability/unnecessary-external-linkage>`,
:doc:`readability-uppercase-literal-suffix <readability/uppercase-literal-suffix>`, "Yes"
:doc:`readability-use-anyofallof <readability/use-anyofallof>`,
:doc:`readability-use-std-min-max <readability/use-std-min-max>`, "Yes"
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/unnecessary-external-linkage.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/unnecessary-external-linkage.rst
new file mode 100644
index 00000000000000..f25c7197cd0da1
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/unnecessary-external-linkage.rst
@@ -0,0 +1,26 @@
+.. title:: clang-tidy - readability-unnecessary-external-linkage
+
+readability-unnecessary-external-linkage
+========================================
+
+Detects variable and function can be marked as static.
+
+Static functions and variables are scoped to a single file. Marking functions
+and variables as static helps to better remove dead code. In addition, it gives
+the compiler more information and can help compiler make more aggressive
+optimizations.
+
+Example:
+
+.. code-block:: c++
+ int v1; // can be marked as static
+
+ void fn1(); // can be marked as static
+
+ namespace {
+ // already in anonymous namespace
+ int v2;
+ void fn2();
+ }
+ // already declared as extern
+ extern int v2;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/func.h b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/func.h
new file mode 100644
index 00000000000000..31c68e9ad0cd92
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/func.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void func_header();
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/var.h b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/var.h
new file mode 100644
index 00000000000000..37e4cfbafff146
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/var.h
@@ -0,0 +1,3 @@
+#pragma once
+
+extern int gloabl_header;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-func.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-func.cpp
new file mode 100644
index 00000000000000..362def250d68c5
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-func.cpp
@@ -0,0 +1,30 @@
+// RUN: %check_clang_tidy %s readability-unnecessary-external-linkage %t -- -- -I%S/Inputs/mark-static-var
+
+#include "func.h"
+
+void func() {}
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func'
+
+template<class T>
+void func_template() {}
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_template'
+
+struct S {
+ void method();
+};
+void S::method() {}
+
+void func_header();
+extern void func_extern();
+static void func_static();
+namespace {
+void func_anonymous_ns();
+} // namespace
+
+int main(int argc, const char*argv[]) {}
+
+extern "C" {
+void func_extern_c_1() {}
+}
+
+extern "C" void func_extern_c_2() {}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-var.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-var.cpp
new file mode 100644
index 00000000000000..68981e04fa5dc8
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-var.cpp
@@ -0,0 +1,40 @@
+// RUN: %check_clang_tidy %s readability-unnecessary-external-linkage %t -- -- -I%S/Inputs/mark-static-var
+
+#include "var.h"
+
+int global;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'global'
+
+template<class T>
+T global_template;
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: variable 'global_template'
+
+int gloabl_header;
+
+extern int global_extern;
+
+static int global_static;
+
+namespace {
+static int global_anonymous_ns;
+namespace NS {
+static int global_anonymous_ns;
+}
+}
+
+static void f(int para) {
+ int local;
+ static int local_static;
+}
+
+struct S {
+ int m1;
+ static int m2;
+};
+int S::m2;
+
+extern "C" {
+int global_in_extern_c_1;
+}
+
+extern "C" int global_in_extern_c_2;
More information about the cfe-commits
mailing list