[clang] [llvm] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/PiotrZSL closed https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/PiotrZSL updated https://github.com/llvm/llvm-project/pull/73069 >From 286c4445f8cba6ea2f49fb9e8f732f04ebdb6c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20detect?= =?UTF-8?q?=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 132 + .../RedundantInlineSpecifierCheck.h | 42 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 32 .../redundant-inline-specifier.cpp| 137 ++ 8 files changed, 353 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index fa571d5dd7650d..1d15228da69451 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -22,6 +22,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index f769752c5de5fa..521dacd6f9df3e 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -41,6 +41,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -99,6 +100,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 00..0e8d17d4442478 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,132 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +namespace { +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +AST_POLYMORPHIC_MATCHER_P(isInternalLinkage, + AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +
[clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 286c4445f8cba6ea2f49fb9e8f732f04ebdb6c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20detect?= =?UTF-8?q?=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 132 + .../RedundantInlineSpecifierCheck.h | 42 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 32 .../redundant-inline-specifier.cpp| 137 ++ 8 files changed, 353 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index fa571d5dd7650d1..1d15228da694510 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -22,6 +22,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index f769752c5de5fa7..521dacd6f9df3e2 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -41,6 +41,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -99,6 +100,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 000..0e8d17d4442478c --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,132 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +namespace { +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +AST_POLYMORPHIC_MATCHER_P(isInternalLinkage, + AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin, =?utf-8?q?F=C3=A9lix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/EugeneZelenko approved this pull request. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/PiotrZSL approved this pull request. LGTM. I think this could be merged. I was thinking, maybe with current scope of check name readability-redundant-inline could be sufficient. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 01/13] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20de?= =?UTF-8?q?tect=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a4617..811310db8c721a 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e641432060..3ce7bfecaecba6 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 00..e73b570df75915 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,113 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation Loc = RangeLocation.getBegin(); + if (Loc.isMacroID()) +return std::nullopt; + + Token FirstToken; + Lexer::getRawToken(Loc, FirstToken, Sources, LangOpts, true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), isDefaulted(), + isInAnonymousNamespace(), felix642 wrote: Adding a parameter to differentiate "implicit" from "redundant" inline declarations seems like a good idea. Some users might be using it to hint the optimizer and might want to keep it if it's not implicit. I will add the parameter "StrictMode" suggested by @PiotrZSL which will be an extra condition to flag the following cases if they are not constexpr: - templates - functions declared in an anonymous namespace - functions marked as static https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/FalcoGer edited https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/FalcoGer edited https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/FalcoGer edited https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,113 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation Loc = RangeLocation.getBegin(); + if (Loc.isMacroID()) +return std::nullopt; + + Token FirstToken; + Lexer::getRawToken(Loc, FirstToken, Sources, LangOpts, true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), isDefaulted(), + isInAnonymousNamespace(), FalcoGer wrote: According to [cppref](https://en.cppreference.com/w/cpp/language/inline), static and anon namespace variables are implicitly inline when they are also constexpr. It doesn't say anything about static functions, probably because a static, inline function makes no sense. Statically declared functions are only visible in their TU, so inline would be redundant. Inline only allows redefinition across multiple TUs. Not sure if modules change any of that. So technically it's pointless, not implicit. Same with templates. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,113 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation Loc = RangeLocation.getBegin(); + if (Loc.isMacroID()) +return std::nullopt; + + Token FirstToken; + Lexer::getRawToken(Loc, FirstToken, Sources, LangOpts, true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), isDefaulted(), + isInAnonymousNamespace(), PiotrZSL wrote: To be honest I'm not so sure about anonymous namespace, static keyword, and templates. Looks like in those cases those functions are inline only due to optimizations, not because they "inline". This is more visible in GCC when you use attribute: ```inline int __attribute__((always_inline)) fn2(int i) ``` if you change inline to static, or put this in anonymous namespace you get warning: `warning: 'always_inline' function might not be inlinable [-Wattributes]` Best would be add "StrictMode" option to check, and when set to true then emit warning also for templates and static and anonymous namespace, and when set to false, emit it only for class members and constexpr. Please investigate it more. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,113 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, PiotrZSL wrote: Put this matcher into anonymous namespace to avoid ODR issues https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,113 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional PiotrZSL wrote: You don't need std::optional for SourceLocation, SourceLocation got method isValid/isInvalid use them for checking. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,141 @@ +// RUN: %check_clang_tidy %s readability-redundant-inline-specifier -std=c++17 %t + +template inline T f() PiotrZSL wrote: according to C++ insights this inline is not redundant, please verify. Wont inline keyword force change here of linkage to internal ? Maybe this should also be checked. Simply because if i call this function from one file, and then i use forward declaration in other file, then without inline keyword code may still compile and symbol will be emitted. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 01/12] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20de?= =?UTF-8?q?tect=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a461..811310db8c721 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e64143206..3ce7bfecaecba 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 0..e73b570df7591 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 01/11] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20de?= =?UTF-8?q?tect=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a4617..811310db8c721a 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e641432060..3ce7bfecaecba6 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 00..e73b570df75915 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,110 @@ +// RUN: %check_clang_tidy %s readability-redundant-inline-specifier %t + +template inline T f() +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: function 'f' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: template T f() +{ +return T{}; +} + +template <> inline double f() = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'f' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: template <> double f() = delete; + +inline int g(float a) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:1: warning: function 'g' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return static_cast(a - 5.F); +} + +inline int g(double) = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: function 'g' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: int g(double) = delete; + +class C +{ + public: +inline C& operator=(const C&) = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'operator=' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: C& operator=(const C&) = delete; + +constexpr inline C& operator=(int a); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function 'operator=' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr C& operator=(int a); + +inline C() {} +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'C' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: C() {} + +constexpr inline C(int); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function 'C' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr C(int); + +inline int Get42() const { return 42; } +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'Get42' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: int Get42() const { return 42; } + +static inline constexpr int C_STATIC = 42; +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'C_STATIC' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: static constexpr int C_STATIC = 42; + +static constexpr int C_STATIC_2 = 42; +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: variable 'C_STATIC_2' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +}; + +constexpr inline int Get42() { return 42; } +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: function 'Get42' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr int Get42() { return 42; } + + +static constexpr inline int NAMESPACE_STATIC = 42; +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:18: warning: variable 'NAMESPACE_STATIC' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] + +inline static int fn0(int i) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:1: warning: function 'fn0' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return i - 1; +} + +static constexpr inline int fn1(int i) +// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function 'fn1' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: static constexpr int fn1(int i) +{ +return i - 1; +} + +namespace +{ +inline int fn2(int i) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: function 'fn2' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return i - 1; +} + +inline constexpr int fn3(int i) +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'fn3' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr int fn3(int i) +{ +return i - 1; +} +} + +namespace ns +{ +inline int fn4(int i) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: function 'fn4' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return i - 1; +} + +inline constexpr int fn5(int i) +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'fn5' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr int fn5(int i) +{ +return i - 1; +} +} + +auto fn6 = [](){}; +//CHECK-MESSAGES-NOT: :[[@LINE-1]]:1: warning: function 'operator()' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] + felix642 wrote: The current implementation does not raise any warnings if the inline
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 01/10] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20de?= =?UTF-8?q?tect=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a46173..811310db8c721a6 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e6414320600..3ce7bfecaecba64 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 000..e73b570df759153 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 1/9] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20dete?= =?UTF-8?q?ct=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a46173..811310db8c721a6 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e6414320600..3ce7bfecaecba64 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 000..e73b570df759153 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 1/8] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20dete?= =?UTF-8?q?ct=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a46173..811310db8c721a6 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e6414320600..3ce7bfecaecba64 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 000..e73b570df759153 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,25 @@ +.. title:: clang-tidy - readability-redundant-inline-specifier + +readability-redundant-inline-specifier +== + +Checks for instances of the ``inline`` keyword in code where it is redundant felix642 wrote: Right, I was looking at the wrong file hence why I was confused. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 1/7] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20dete?= =?UTF-8?q?ct=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a46173..811310db8c721a6 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e6414320600..3ce7bfecaecba64 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 000..e73b570df759153 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), felix642 wrote: Same here, it should now match on functions in anonymous namespace. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl() + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInlineSpecified(), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), felix642 wrote: I've adjusted the matcher to match on variables in anonymous namespace. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl() + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInlineSpecified(), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), +hasAncestor(recordDecl( + .bind("var_decl"), + this); + + Finder->addMatcher( + functionTemplateDecl(has(functionDecl(isInlineSpecified( PiotrZSL wrote: Please add both those as an tests, and we can see. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), felix642 wrote: From what I can see default methods are implicitly inlined : https://cppinsights.io/s/fb9b0f8e. I've adjusted my matcher to take them into consideration. Let me know if it's ok with you. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl() + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInlineSpecified(), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), +hasAncestor(recordDecl( + .bind("var_decl"), + this); + + Finder->addMatcher( + functionTemplateDecl(has(functionDecl(isInlineSpecified( felix642 wrote: Well, I think that it's fair to emit a warning if either a forward declaration of a function definition has a redundant inline specifier. In both case we want to suggest to remove it don't we ? https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), PiotrZSL wrote: maybe methods that are = default also should be ignored ? https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl() + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInlineSpecified(), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), +hasAncestor(recordDecl( + .bind("var_decl"), + this); + + Finder->addMatcher( + functionTemplateDecl(has(functionDecl(isInlineSpecified( PiotrZSL wrote: what about code like this: ``` template inline T f(); template T f() { return T{}; } ``` do we want to spam a warning for a forward declaration ? or ``` template T f(); template inline T f() { return T{}; } ``` Wont warning be emitted here a forward declaration ? https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), PiotrZSL wrote: what if function is in anonymous namespace https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl() + .bind("fun_decl"), + this); + + Finder->addMatcher( PiotrZSL wrote: You could check LangOpts if inline variables are enabled, and do not register this matcher at all if not. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,110 @@ +// RUN: %check_clang_tidy %s readability-redundant-inline-specifier %t + +template inline T f() +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: function 'f' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: template T f() +{ +return T{}; +} + +template <> inline double f() = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'f' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: template <> double f() = delete; + +inline int g(float a) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:1: warning: function 'g' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return static_cast(a - 5.F); +} + +inline int g(double) = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: function 'g' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: int g(double) = delete; + +class C +{ + public: +inline C& operator=(const C&) = delete; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'operator=' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: C& operator=(const C&) = delete; + +constexpr inline C& operator=(int a); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function 'operator=' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr C& operator=(int a); + +inline C() {} +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'C' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: C() {} + +constexpr inline C(int); +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function 'C' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr C(int); + +inline int Get42() const { return 42; } +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'Get42' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: int Get42() const { return 42; } + +static inline constexpr int C_STATIC = 42; +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'C_STATIC' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: static constexpr int C_STATIC = 42; + +static constexpr int C_STATIC_2 = 42; +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: variable 'C_STATIC_2' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +}; + +constexpr inline int Get42() { return 42; } +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: function 'Get42' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr int Get42() { return 42; } + + +static constexpr inline int NAMESPACE_STATIC = 42; +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:18: warning: variable 'NAMESPACE_STATIC' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] + +inline static int fn0(int i) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:1: warning: function 'fn0' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return i - 1; +} + +static constexpr inline int fn1(int i) +// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: function 'fn1' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: static constexpr int fn1(int i) +{ +return i - 1; +} + +namespace +{ +inline int fn2(int i) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: function 'fn2' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return i - 1; +} + +inline constexpr int fn3(int i) +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'fn3' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr int fn3(int i) +{ +return i - 1; +} +} + +namespace ns +{ +inline int fn4(int i) +// CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: function 'fn4' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +{ +return i - 1; +} + +inline constexpr int fn5(int i) +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'fn5' has inline specifier but is implicitly inlined [readability-redundant-inline-specifier] +// CHECK-FIXES: constexpr int fn5(int i) +{ +return i - 1; +} +} + +auto fn6 = [](){}; +//CHECK-MESSAGES-NOT: :[[@LINE-1]]:1: warning: function 'operator()' has inline specifier
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,109 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Token.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +AST_POLYMORPHIC_MATCHER(isInlineSpecified, +AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, +VarDecl)) { + if (const auto *FD = dyn_cast()) +return FD->isInlineSpecified(); + if (const auto *VD = dyn_cast()) +return VD->isInlineSpecified(); + llvm_unreachable("Not a valid polymorphic type"); +} + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + Token FirstToken; + Lexer::getRawToken(RangeLocation.getBegin(), FirstToken, Sources, LangOpts, + true); + std::optional CurrentToken = FirstToken; + while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() && + CurrentToken->isNot(tok::eof)) { +if (CurrentToken->is(tok::raw_identifier) && +CurrentToken->getRawIdentifier() == "inline") + return CurrentToken->getLocation(); + +CurrentToken = +Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), isInlineSpecified(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl() + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInlineSpecified(), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), PiotrZSL wrote: what about variables in anonymous namespace ? what if variable already got internal linkage ? https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin, =?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/EugeneZelenko edited https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if (CurrentToken.getRawIdentifier() == "inline") { +return CurrentToken.getLocation(); + } +} +CurrentLocation = CurrentToken.getEndLoc(); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()), + unless(hasAncestor(lambdaExpr())), isInline(), felix642 wrote: Using `TK_IgnoreUnlessSpelledInSource` instead of `isImplicit()` seems to have to solved this issue. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/73069 From 89281ccb5354e3d6349d10e6f9446194d2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= Date: Thu, 16 Nov 2023 22:03:15 -0500 Subject: [PATCH 1/5] =?UTF-8?q?[clang-tidy]=C2=A0Added=20check=20to=20dete?= =?UTF-8?q?ct=20redundant=20inline=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks find usages of the inline keywork where it is already implicitly defined by the compiler and suggests it's removal. Fixes #72397 --- .../clang-tidy/readability/CMakeLists.txt | 1 + .../readability/ReadabilityTidyModule.cpp | 3 + .../RedundantInlineSpecifierCheck.cpp | 99 .../RedundantInlineSpecifierCheck.h | 36 ++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../docs/clang-tidy/checks/list.rst | 1 + .../redundant-inline-specifier.rst| 34 ++ .../redundant-inline-specifier.cpp| 110 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- 9 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/readability/redundant-inline-specifier.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-inline-specifier.cpp diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 5452c2d48a46173..811310db8c721a6 100644 --- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyReadabilityModule IdentifierLengthCheck.cpp IdentifierNamingCheck.cpp ImplicitBoolConversionCheck.cpp + RedundantInlineSpecifierCheck.cpp InconsistentDeclarationParameterNameCheck.cpp IsolateDeclarationCheck.cpp MagicNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp index b8e6e6414320600..3ce7bfecaecba64 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -39,6 +39,7 @@ #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" +#include "RedundantInlineSpecifierCheck.h" #include "RedundantMemberInitCheck.h" #include "RedundantPreprocessorCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -93,6 +94,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); +CheckFactories.registerCheck( +"readability-redundant-inline-specifier"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp new file mode 100644 index 000..e73b570df759153 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/RedundantInlineSpecifierCheck.cpp @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if (CurrentToken.getRawIdentifier() == "inline") { +return CurrentToken.getLocation(); + } +} +CurrentLocation = CurrentToken.getEndLoc(); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()), + unless(hasAncestor(lambdaExpr())), isInline(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl()), + unless(hasAncestor(cxxConstructorDecl()) + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInline(), unless(isImplicit()), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), felix642 wrote: The description of this matcher says : >Matches variable/function declarations that have "static" storage >class specifier ("static" keyword) written in the source. From what I understand, it needs to have the "static" keyword written in the source. Is there a better matcher that I could use in that case? https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?F=C3=A9lix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,35 @@ +.. title:: clang-tidy - readability-redundant-inline-specifier + +readability-redundant-inline-specifier +== + +Checks for instances of the `inline` keyword in code where it is redundant +and recommends its removal. + +Examples: + +.. code-block:: c++ + + constexpr inline void f() {} + +In the example abvove the keyword `inline` is redundant since constexpr EugeneZelenko wrote: Please use double back-ticks for language constructs. Same in other places. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?F=C3=A9lix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,35 @@ +.. title:: clang-tidy - readability-redundant-inline-specifier + +readability-redundant-inline-specifier +== + +Checks for instances of the `inline` keyword in code where it is redundant EugeneZelenko wrote: Please synchronize with statement in Release Notes. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?F=C3=A9lix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/EugeneZelenko requested changes to this pull request. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: https://github.com/EugeneZelenko edited https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
@@ -0,0 +1,35 @@ +.. title:: clang-tidy - readability-redundant-inline-specifier + +readability-redundant-inline-specifier +== + +Checks for instances of the `inline` keyword in code where it is redundant +and recommends its removal. + +Examples: + +.. code-block:: c++ + + constexpr inline void f() {} + +In the example abvove the keyword `inline` is redundant since constexpr +functions are implicitly inlined bjosv wrote: ```suggestion In the example above the keyword `inline` is redundant since constexpr functions are implicitly inlined. ``` https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/bjosv edited https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: PiotrZSL wrote: > @PiotrZSL could you use the "Start review" button to avoid spamming people > with dozens of E-Mails for a single review? @philnik777 If you prefer not to receive notifications, simply unsubscribe. Specially if you consider such notifications a "spam". https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if (CurrentToken.getRawIdentifier() == "inline") { +return CurrentToken.getLocation(); + } +} +CurrentLocation = CurrentToken.getEndLoc(); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()), + unless(hasAncestor(lambdaExpr())), isInline(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl()), + unless(hasAncestor(cxxConstructorDecl()) + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInline(), unless(isImplicit()), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), +hasAncestor(recordDecl( + .bind("var_decl"), + this); + + Finder->addMatcher( + functionTemplateDecl(has(functionDecl(isInline(.bind("templ_decl"), + this); +} + +template +void RedundantInlineSpecifierCheck::handleMatchedDecl( +const T *MatchedDecl, const SourceManager , +const MatchFinder::MatchResult , const char *Message) { + if (auto Loc = getInlineTokenLocation(MatchedDecl->getSourceRange(), Sources, +Result.Context->getLangOpts())) +diag(*Loc, Message) << MatchedDecl << FixItHint::CreateRemoval(*Loc); +} + +void RedundantInlineSpecifierCheck::check( +const MatchFinder::MatchResult ) { + const SourceManager = *Result.SourceManager; + + if (const auto *MatchedDecl = + Result.Nodes.getNodeAs("fun_decl")) { +handleMatchedDecl( +MatchedDecl, Sources, Result, +"Function %0 has inline specifier but is implicitly inlined"); PiotrZSL wrote: start description with a small letter https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if (CurrentToken.getRawIdentifier() == "inline") { +return CurrentToken.getLocation(); + } +} +CurrentLocation = CurrentToken.getEndLoc(); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()), + unless(hasAncestor(lambdaExpr())), isInline(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl()), + unless(hasAncestor(cxxConstructorDecl()) + .bind("fun_decl"), + this); + + Finder->addMatcher( + varDecl(isInline(), unless(isImplicit()), + anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())), PiotrZSL wrote: isStaticStorageClass it's not necessary a "static" in code. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if (CurrentToken.getRawIdentifier() == "inline") { +return CurrentToken.getLocation(); + } +} +CurrentLocation = CurrentToken.getEndLoc(); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()), + unless(hasAncestor(lambdaExpr())), isInline(), + anyOf(isConstexpr(), isDeleted(), + allOf(isDefinition(), hasAncestor(recordDecl()), + unless(hasAncestor(cxxConstructorDecl()) PiotrZSL wrote: I'm not sure about that cxxConstructorDecl https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if (CurrentToken.getRawIdentifier() == "inline") { +return CurrentToken.getLocation(); + } +} +CurrentLocation = CurrentToken.getEndLoc(); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()), + unless(hasAncestor(lambdaExpr())), isInline(), PiotrZSL wrote: i think that with lambdas we may need to do something else, like exclude those lambda invokers, issue is that someone can put class definition inside lambda. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, + true) && + CurrentLocation < RangeLocation.getEnd() && + CurrentToken.isNot(tok::eof)) { +if (CurrentToken.is(tok::raw_identifier)) { + if (CurrentToken.getRawIdentifier() == "inline") { +return CurrentToken.getLocation(); + } +} +CurrentLocation = CurrentToken.getEndLoc(); + } + return std::nullopt; +} + +void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()), PiotrZSL wrote: use TK_IgnoreUnlessSpelledInSource instead of unless(isImplicit())) https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
=?utf-8?q?Félix-Antoine?= Constantin Message-ID: In-Reply-To: @@ -0,0 +1,99 @@ +//===--- RedundantInlineSpecifierCheck.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 "RedundantInlineSpecifierCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" + +#include "../utils/LexerUtils.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::optional +getInlineTokenLocation(SourceRange RangeLocation, const SourceManager , + const LangOptions ) { + SourceLocation CurrentLocation = RangeLocation.getBegin(); + Token CurrentToken; + while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts, PiotrZSL wrote: I think that there is something like findNextToken in LexerUtils, take a look. https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)
https://github.com/felix642 edited https://github.com/llvm/llvm-project/pull/73069 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits