[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
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 Date: Thu, 2 May 2024 15:44:45 +0800 Subject: [PATCH 1/3] 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( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-unnecessary-external-linkage"); CheckFactories.registerCheck( "readability-math-missing-parentheses"); CheckFactories.registerCheck( 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 00..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, +
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
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 Date: Thu, 2 May 2024 15:44:45 +0800 Subject: [PATCH 1/3] 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( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-unnecessary-external-linkage"); CheckFactories.registerCheck( "readability-math-missing-parentheses"); CheckFactories.registerCheck( 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 00..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, +
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
carlosgalvezp wrote: > Should I implement auto-fix for this check? As a first step we can have it without auto-fix and add that as a second step once we figure out a good way to do it. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -151,8 +151,8 @@ New checks Enforces consistent style for enumerators' initialization, covering three styles: none, first only, or all initialized explicitly. -- New :doc:`readability-unnecessary-external-linkage - ` check. +- New :doc:`misc-use-internal-linkage EugeneZelenko wrote: Please keep alphabetical order (by check name) in this section. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -1,7 +1,7 @@ -.. title:: clang-tidy - readability-unnecessary-external-linkage +.. title:: clang-tidy - misc-use-internal-linkage -readability-unnecessary-external-linkage - +misc-use-internal-linkage += Detects variable and function can be marked as static. EugeneZelenko wrote: Please synchronize with Release Notes after change there. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -1,4 +1,4 @@ -//===--- UnnecessaryExternalLinkageCheck.cpp - clang-tidy +//===--- UseInternalLinkageCheck.cpp - clang-tidy EugeneZelenko wrote: Please merge into single line. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
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 Date: Thu, 2 May 2024 15:44:45 +0800 Subject: [PATCH 1/2] 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( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-unnecessary-external-linkage"); CheckFactories.registerCheck( "readability-math-missing-parentheses"); CheckFactories.registerCheck( 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 00..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, +
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
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 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( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-unnecessary-external-linkage"); CheckFactories.registerCheck( "readability-math-missing-parentheses"); CheckFactories.registerCheck( 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 00..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, +
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
HerrCai0907 wrote: > * The auto-fix should be configurable to choose `static` or anonymous > namespace. Should I implement auto-fix for this check? Maybe some functions / variables will be marked incorrectly and cause link error because the coder just forget to include the header file but this check mark it as internal linkage. WDYT? @carlosgalvezp @PiotrZSL @EugeneZelenko @SimplyDanny https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -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 + ` check. + + Detects variable and function can be marked as static. SimplyDanny wrote: ```suggestion Detects variables and functions that can be marked as static or moved into an anonymous namespace to enforce internal linkage. ``` https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,80 @@ +//===--- 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 ) { + if (const auto *FD = Result.Nodes.getNodeAs("fn")) { +diag(FD->getLocation(), Message) << "function" << FD; +return; + } + if (const auto *VD = Result.Nodes.getNodeAs("var")) { +diag(VD->getLocation(), Message) << "variable" << VD; +return; + } + llvm_unreachable(""); SimplyDanny wrote: Unnecessary I think. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,80 @@ +//===--- 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; SimplyDanny wrote: Can be replaced by `std::all_of(...)`. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -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. SimplyDanny wrote: ```suggestion the compiler more information and allows for more aggressive optimizations. ``` https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,80 @@ +//===--- 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."; SimplyDanny wrote: A bit shorter: ```suggestion "%0 %1 can be made static or moved into an anonymous " "namespace to enforce internal linkage"; ``` https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff a370d57b9ff9e385e9a51bf6b1d366890f4091cd 1bb5ed2ebd9e1b40fe581b719181681f71f78fa2 -- clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.h clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/func.h clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/var.h clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-func.cpp clang-tools-extra/test/clang-tidy/checkers/readability/unnecessary-external-linkage-var.cpp clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp `` View the diff from clang-format here. ``diff diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index baa4eec704..d389287e8f 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -32,7 +32,6 @@ #include "IsolateDeclarationCheck.h" #include "MagicNumbersCheck.h" #include "MakeMemberFunctionConstCheck.h" -#include "UnnecessaryExternalLinkageCheck.h" #include "MathMissingParenthesesCheck.h" #include "MisleadingIndentationCheck.h" #include "MisplacedArrayIndexCheck.h" @@ -59,6 +58,7 @@ #include "StringCompareCheck.h" #include "SuspiciousCallArgumentCheck.h" #include "UniqueptrDeleteReleaseCheck.h" +#include "UnnecessaryExternalLinkageCheck.h" #include "UppercaseLiteralSuffixCheck.h" #include "UseAnyOfAllOfCheck.h" #include "UseStdMinMaxCheck.h" @@ -107,7 +107,8 @@ public: "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); - CheckFactories.registerCheck("readability-unnecessary-external-linkage"); +CheckFactories.registerCheck( +"readability-unnecessary-external-linkage"); CheckFactories.registerCheck( "readability-math-missing-parentheses"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp index fd302f5ab1..4970d3339e 100644 --- a/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/UnnecessaryExternalLinkageCheck.cpp @@ -1,4 +1,5 @@ -//===--- UnnecessaryExternalLinkageCheck.cpp - clang-tidy -===// +//===--- 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. @@ -65,7 +66,8 @@ static constexpr StringRef Message = "marking as static or using anonymous namespace can avoid external " "linkage."; -void UnnecessaryExternalLinkageCheck::check(const MatchFinder::MatchResult ) { +void UnnecessaryExternalLinkageCheck::check( +const MatchFinder::MatchResult ) { if (const auto *FD = Result.Nodes.getNodeAs("fn")) { diag(FD->getLocation(), Message) << "function" << FD; return; `` https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/90830 >From ec7ddff8aedaa9d42796b5f952ff7ca77465dfd1 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 2 May 2024 15:44:45 +0800 Subject: [PATCH 1/4] [clang-tidy] new check readability-mark-static Add new check readability-mark-static to dectect variable and function can be marked as static. --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/MarkStaticCheck.cpp | 65 +++ .../clang-tidy/readability/MarkStaticCheck.h | 33 ++ .../readability/ReadabilityTidyModule.cpp | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../docs/clang-tidy/checks/list.rst | 1 + .../checks/readability/mark-static.rst| 26 .../readability/Inputs/mark-static-var/func.h | 3 + .../readability/Inputs/mark-static-var/var.h | 3 + .../checkers/readability/mark-static-func.cpp | 24 +++ .../checkers/readability/mark-static-var.cpp | 34 ++ 11 files changed, 197 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/mark-static.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/mark-static-func.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/mark-static-var.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 41065fc8e87859..3e0cbc4d943274 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 + MarkStaticCheck.cpp RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp new file mode 100644 index 00..c8881963071de8 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp @@ -0,0 +1,65 @@ +//===--- MarkStaticCheck.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 "MarkStaticCheck.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) { + return Finder->getASTContext().getSourceManager().isInMainFile( + Node.getLocation()); +} + +AST_POLYMORPHIC_MATCHER(isExternStorageClass, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + return Node.getStorageClass() == SC_Extern; +} + +} // namespace + +void MarkStaticCheck::registerMatchers(MatchFinder *Finder) { + auto Common = + allOf(isFirstDecl(), isInMainFile(), +unless(anyOf(isStaticStorageClass(), isExternStorageClass(), + isInAnonymousNamespace(; + Finder->addMatcher( + functionDecl(Common, unless(cxxMethodDecl()), unless(isMain())) + .bind("fn"), + this); + Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this); +} + +void MarkStaticCheck::check(const MatchFinder::MatchResult ) { + if (const auto *FD = Result.Nodes.getNodeAs("fn")) { +diag(FD->getLocation(), "function %0 can be static") << FD; +return; + } + if (const auto *VD = Result.Nodes.getNodeAs("var")) { +diag(VD->getLocation(), "variable %0 can be static") << VD; +return; + } + llvm_unreachable(""); +} + +} // namespace clang::tidy::readability diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h new file mode 100644 index
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
carlosgalvezp wrote: - I'm not sure this is so much a readability check, I'd put it in `misc`. The main problem that it solves is avoiding ODR violations. I'd call it `misc-prefer-internal-linkage`. - The auto-fix should be configurable to choose `static` or anonymous namespace. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
EugeneZelenko wrote: `readability-use-static`? https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,26 @@ +.. title:: clang-tidy - readability-mark-static + +readability-mark-static +=== + +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. + PiotrZSL wrote: Any of the following names declared at namespace scope have internal linkage: - variables, variable templates(since C++14), functions, or function templates declared static; Looks fine... Still would be nice to allow user select static or anonymous namespace for "auto fix" https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,26 @@ +.. title:: clang-tidy - readability-mark-static + +readability-mark-static +=== + +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. + firewave wrote: Did some research but did not find the case I remembered (it might have variable templates though): - anonymous namespaces do not have external linkage until C++11: https://en.cppreference.com/w/cpp/language/namespace#Unnamed_namespaces / https://en.cppreference.com/w/cpp/language/storage_duration#Linkage - `static` variable templates do not have internal linkage until C++14: https://en.cppreference.com/w/cpp/language/storage_duration#Linkage Interesting tidbit that enumerations have external linkage. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,26 @@ +.. title:: clang-tidy - readability-mark-static + +readability-mark-static +=== + +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. + PiotrZSL wrote: Yes it does, because you cannot get access to such variable/function via extern in separate TU. [(2.3)](https://eel.is/c++draft/basic.link#2.3) When a name has [internal linkage](https://eel.is/c++draft/basic.link#def:linkage,internal), the entity it denotes can be referred to by names from other scopes in the same translation unit[.](https://eel.is/c++draft/basic.link#2.3.sentence-1) The name of an entity that belongs to a [namespace scope](https://eel.is/c++draft/basic.scope.namespace) has internal linkage if it is the name of [(3.1)](https://eel.is/c++draft/basic.link#3.1) a variable, variable template, function, or function template that is explicitly declared static; or https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
PiotrZSL wrote: > Maybe I can provide an option to treat some extension as main file also. Clang-tidy already got those options, check just need to utilize them. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
PiotrZSL wrote: > Isn't this already covered by `-Wmissing-prototypes`? No, because -Wmissing-prototypes will not provide warning, when both declaration and definition is in .cpp https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,26 @@ +.. title:: clang-tidy - readability-mark-static + +readability-mark-static +=== + +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. + firewave wrote: AFAIK `static` doesn't prevent ODR violations (unfortunately I do not have specifics - it is just from experience). You need to use anonymous namespaces for that. Also Clang has a bug where you might still encounter issues although the symbols should be internal (resulting in a crash) #75275. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
firewave wrote: Isn't this already covered by `-Wmissing-prototypes`? https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
HerrCai0907 wrote: > Add support for unity builds (.cpp files included from single .cpp file = > isInMainFile is not sufficient) For unity build, it looks like a project level decision. Then they can ignore this check. Maybe I can provide an option to treat some extension as main file also. https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
https://github.com/PiotrZSL requested changes to this pull request. 1. Wrong check name, maybe readability-unessesary-external-linkage or something, maybe it should even be an performance check [as there it will bring more benefits] (current check suggest more an "static methods"). 2. Add support for unity builds (.cpp files included from single .cpp file = isInMainFile is not sufficient) 3. Diagnostic need to be more detailed 4. When I run into these issues, i got questions from developers why not use anonymous namespaces (maybe it could be configurable, or finall check restrict only to static [I prefer static]) Additionally when those issues were being fixed in project, there were lot of situations where there were forward declaration in header file, but developer forget to include header file in .cpp, this is why diagnostic need to be more detailed, so developer could find issue, instead of adding static. In summary, my main concern is check name & diagnostic, current name is too generic https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,26 @@ +.. title:: clang-tidy - readability-mark-static + +readability-mark-static +=== + +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. + PiotrZSL wrote: Mention other benefits: avoid ODR violations https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
@@ -0,0 +1,65 @@ +//===--- MarkStaticCheck.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 "MarkStaticCheck.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) { + return Finder->getASTContext().getSourceManager().isInMainFile( + Node.getLocation()); +} + +AST_POLYMORPHIC_MATCHER(isExternStorageClass, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + return Node.getStorageClass() == SC_Extern; +} + +} // namespace + +void MarkStaticCheck::registerMatchers(MatchFinder *Finder) { + auto Common = + allOf(isFirstDecl(), isInMainFile(), +unless(anyOf(isStaticStorageClass(), isExternStorageClass(), + isInAnonymousNamespace(; + Finder->addMatcher( + functionDecl(Common, unless(cxxMethodDecl()), unless(isMain())) + .bind("fn"), + this); + Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this); PiotrZSL wrote: Few things are missing here, I wrote same check already for private usage, so let me drop it here (for ideas): ``` Finder->addMatcher(functionDecl(isExpansionInMainFile(), isDefinition(), hasExternalFormalLinkage(), unless(isMain()), unless(isExternC()), unless(isExplicitTemplateSpecialization()), unless(clang::ast_matchers::isTemplateInstantiation()), unless(cxxMethodDecl()), unless(hasAncestor(friendDecl())), unless(hasDeclarationOutsideMainFile()) ).bind("fun"), this); Finder->addMatcher(varDecl(isExpansionInMainFile(), isDefinition(), hasGlobalStorage(), unless(isStaticLocal()), unless(isStaticStorageClass()), hasExternalFormalLinkage(), unless(isExternC()), unless(hasDeclarationOutsideMainFile()), unless(hasVarRedeclaration(anyOf(isStaticStorageClass(), isExternC( ).bind("var"), this); ``` ``` AST_MATCHER(Decl, hasDeclarationOutsideMainFile) { auto = Finder->getASTContext().getSourceManager(); for(const auto* l_it : Node.redecls()) { if (not SourceManager.isInMainFile(SourceManager.getExpansionLoc(l_it->getBeginLoc( { return true; } } return not SourceManager.isInMainFile(SourceManager.getExpansionLoc(Node.getBeginLoc())); } AST_MATCHER_P(VarDecl, hasVarRedeclaration, ast_matchers::internal::Matcher, InnerMatcher) { for(const auto* l_it : Node.redecls()) { if (InnerMatcher.matches(*l_it, Finder, Builder)) { return true; } } return false; } ``` You need also take into account unity builds, something that I didn't took into consideration, when function/variable is in .cpp file, but thats not a a main file. I think there is some API in Clang-tify for that. But at same time, I were focusing more on external linkage: `function %0 has external linkage but it's used only in this source file and has no public declaration, make it internal (anonymous namespace or static) or add public declaration in header file"` https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
https://github.com/PiotrZSL edited https://github.com/llvm/llvm-project/pull/90830 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/90830 >From ec7ddff8aedaa9d42796b5f952ff7ca77465dfd1 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 2 May 2024 15:44:45 +0800 Subject: [PATCH] [clang-tidy] new check readability-mark-static Add new check readability-mark-static to dectect variable and function can be marked as static. --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/MarkStaticCheck.cpp | 65 +++ .../clang-tidy/readability/MarkStaticCheck.h | 33 ++ .../readability/ReadabilityTidyModule.cpp | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../docs/clang-tidy/checks/list.rst | 1 + .../checks/readability/mark-static.rst| 26 .../readability/Inputs/mark-static-var/func.h | 3 + .../readability/Inputs/mark-static-var/var.h | 3 + .../checkers/readability/mark-static-func.cpp | 24 +++ .../checkers/readability/mark-static-var.cpp | 34 ++ 11 files changed, 197 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/mark-static.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/mark-static-func.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/mark-static-var.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 41065fc8e87859..3e0cbc4d943274 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 + MarkStaticCheck.cpp RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp new file mode 100644 index 00..c8881963071de8 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp @@ -0,0 +1,65 @@ +//===--- MarkStaticCheck.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 "MarkStaticCheck.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) { + return Finder->getASTContext().getSourceManager().isInMainFile( + Node.getLocation()); +} + +AST_POLYMORPHIC_MATCHER(isExternStorageClass, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + return Node.getStorageClass() == SC_Extern; +} + +} // namespace + +void MarkStaticCheck::registerMatchers(MatchFinder *Finder) { + auto Common = + allOf(isFirstDecl(), isInMainFile(), +unless(anyOf(isStaticStorageClass(), isExternStorageClass(), + isInAnonymousNamespace(; + Finder->addMatcher( + functionDecl(Common, unless(cxxMethodDecl()), unless(isMain())) + .bind("fn"), + this); + Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this); +} + +void MarkStaticCheck::check(const MatchFinder::MatchResult ) { + if (const auto *FD = Result.Nodes.getNodeAs("fn")) { +diag(FD->getLocation(), "function %0 can be static") << FD; +return; + } + if (const auto *VD = Result.Nodes.getNodeAs("var")) { +diag(VD->getLocation(), "variable %0 can be static") << VD; +return; + } + llvm_unreachable(""); +} + +} // namespace clang::tidy::readability diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h new file mode 100644 index
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
llvmbot wrote: @llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) Changes Add new check readability-mark-static to dectect variable and function can be marked as static. --- Full diff: https://github.com/llvm/llvm-project/pull/90830.diff 11 Files Affected: - (modified) clang-tools-extra/clang-tidy/readability/CMakeLists.txt (+1) - (added) clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp (+65) - (added) clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h (+33) - (modified) clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp (+2) - (modified) clang-tools-extra/docs/ReleaseNotes.rst (+5) - (modified) clang-tools-extra/docs/clang-tidy/checks/list.rst (+1) - (added) clang-tools-extra/docs/clang-tidy/checks/readability/mark-static.rst (+26) - (added) clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/func.h (+3) - (added) clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/mark-static-var/var.h (+3) - (added) clang-tools-extra/test/clang-tidy/checkers/readability/mark-static-func.cpp (+24) - (added) clang-tools-extra/test/clang-tidy/checkers/readability/mark-static-var.cpp (+34) ``diff diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 41065fc8e87859..3e0cbc4d943274 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 + MarkStaticCheck.cpp RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp new file mode 100644 index 00..c8881963071de8 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp @@ -0,0 +1,65 @@ +//===--- MarkStaticCheck.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 "MarkStaticCheck.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) { + return Finder->getASTContext().getSourceManager().isInMainFile( + Node.getLocation()); +} + +AST_POLYMORPHIC_MATCHER(isExternStorageClass, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + return Node.getStorageClass() == SC_Extern; +} + +} // namespace + +void MarkStaticCheck::registerMatchers(MatchFinder *Finder) { + auto Common = + allOf(isFirstDecl(), isInMainFile(), +unless(anyOf(isStaticStorageClass(), isExternStorageClass(), + isInAnonymousNamespace(; + Finder->addMatcher( + functionDecl(Common, unless(cxxMethodDecl()), unless(isMain())) + .bind("fn"), + this); + Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this); +} + +void MarkStaticCheck::check(const MatchFinder::MatchResult ) { + if (const auto *FD = Result.Nodes.getNodeAs("fn")) { +diag(FD->getLocation(), "function %0 can be static") << FD; +return; + } + if (const auto *VD = Result.Nodes.getNodeAs("var")) { +diag(VD->getLocation(), "variable %0 can be static") << VD; +return; + } + llvm_unreachable(""); +} + +} // namespace clang::tidy::readability diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h new file mode 100644 index 00..0af6788d3e3d85 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h @@ -0,0 +1,33 @@ +//===--- MarkStaticCheck.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
[clang-tools-extra] [clang-tidy] new check readability-mark-static (PR #90830)
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/90830 Add new check readability-mark-static to dectect variable and function can be marked as static. >From 7f42888a6c7ee2bdb21c8d84b36b0821d32ffcc7 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 2 May 2024 15:44:45 +0800 Subject: [PATCH] [clang-tidy] new check readability-mark-static Add new check readability-mark-static to dectect variable and function can be marked as static. --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/MarkStaticCheck.cpp | 65 +++ .../clang-tidy/readability/MarkStaticCheck.h | 33 ++ .../readability/ReadabilityTidyModule.cpp | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../docs/clang-tidy/checks/list.rst | 1 + .../checks/readability/mark-static.rst| 26 .../readability/Inputs/mark-static-var/func.h | 3 + .../readability/Inputs/mark-static-var/var.h | 3 + .../checkers/readability/mark-static-func.cpp | 24 +++ .../checkers/readability/mark-static-var.cpp | 34 ++ 11 files changed, 197 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/mark-static.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/mark-static-func.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/mark-static-var.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 41065fc8e87859..3e0cbc4d943274 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 + MarkStaticCheck.cpp RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp new file mode 100644 index 00..c8881963071de8 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.cpp @@ -0,0 +1,65 @@ +//===--- MarkStaticCheck.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 "MarkStaticCheck.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) { + return Finder->getASTContext().getSourceManager().isInMainFile( + Node.getLocation()); +} + +AST_POLYMORPHIC_MATCHER(isExternStorageClass, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + return Node.getStorageClass() == SC_Extern; +} + +} // namespace + +void MarkStaticCheck::registerMatchers(MatchFinder *Finder) { + auto Common = + allOf(isFirstDecl(), isInMainFile(), +unless(anyOf(isStaticStorageClass(), isExternStorageClass(), + isInAnonymousNamespace(; + Finder->addMatcher( + functionDecl(Common, unless(cxxMethodDecl()), unless(isMain())) + .bind("fn"), + this); + Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this); +} + +void MarkStaticCheck::check(const MatchFinder::MatchResult ) { + if (const auto *FD = Result.Nodes.getNodeAs("fn")) { +diag(FD->getLocation(), "function %0 can be static") << FD; +return; + } + if (const auto *VD = Result.Nodes.getNodeAs("var")) { +diag(VD->getLocation(), "variable %0 can be static") << VD; +return; + } + llvm_unreachable(""); +} + +} // namespace clang::tidy::readability diff --git a/clang-tools-extra/clang-tidy/readability/MarkStaticCheck.h