Skip to content

Commit 4ab9aec

Browse files
committed
rename to modernize-use-constexpr + review comments
1 parent 16d5a3f commit 4ab9aec

File tree

11 files changed

+170
-164
lines changed

11 files changed

+170
-164
lines changed

clang-tools-extra/clang-tidy/misc/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ set_target_properties(genconfusable PROPERTIES FOLDER "Clang Tools Extra/Sourceg
1919

2020
add_clang_library(clangTidyMiscModule STATIC
2121
ConstCorrectnessCheck.cpp
22-
UseConstexprCheck.cpp
2322
CoroutineHostileRAIICheck.cpp
2423
DefinitionsInHeadersCheck.cpp
2524
ConfusableIdentifierCheck.cpp

clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "UnusedParametersCheck.h"
3232
#include "UnusedUsingDeclsCheck.h"
3333
#include "UseAnonymousNamespaceCheck.h"
34-
#include "UseConstexprCheck.h"
3534
#include "UseInternalLinkageCheck.h"
3635

3736
namespace clang::tidy {
@@ -44,7 +43,6 @@ class MiscModule : public ClangTidyModule {
4443
"misc-confusable-identifiers");
4544
CheckFactories.registerCheck<ConstCorrectnessCheck>(
4645
"misc-const-correctness");
47-
CheckFactories.registerCheck<UseConstexprCheck>("misc-use-constexpr");
4846
CheckFactories.registerCheck<CoroutineHostileRAIICheck>(
4947
"misc-coroutine-hostile-raii");
5048
CheckFactories.registerCheck<DefinitionsInHeadersCheck>(

clang-tools-extra/clang-tidy/modernize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_clang_library(clangTidyModernizeModule STATIC
3030
UnaryStaticAssertCheck.cpp
3131
UseAutoCheck.cpp
3232
UseBoolLiteralsCheck.cpp
33+
UseConstexprCheck.cpp
3334
UseConstraintsCheck.cpp
3435
UseDefaultMemberInitCheck.cpp
3536
UseDesignatedInitializersCheck.cpp

clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "UnaryStaticAssertCheck.h"
3232
#include "UseAutoCheck.h"
3333
#include "UseBoolLiteralsCheck.h"
34+
#include "UseConstexprCheck.h"
3435
#include "UseConstraintsCheck.h"
3536
#include "UseDefaultMemberInitCheck.h"
3637
#include "UseDesignatedInitializersCheck.h"
@@ -76,6 +77,8 @@ class ModernizeModule : public ClangTidyModule {
7677
CheckFactories.registerCheck<MinMaxUseInitializerListCheck>(
7778
"modernize-min-max-use-initializer-list");
7879
CheckFactories.registerCheck<PassByValueCheck>("modernize-pass-by-value");
80+
CheckFactories.registerCheck<UseConstexprCheck>(
81+
"modernize-use-constexpr");
7982
CheckFactories.registerCheck<UseDesignatedInitializersCheck>(
8083
"modernize-use-designated-initializers");
8184
CheckFactories.registerCheck<UseIntegerSignComparisonCheck>(

clang-tools-extra/clang-tidy/misc/UseConstexprCheck.cpp renamed to clang-tools-extra/clang-tidy/modernize/UseConstexprCheck.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
using namespace clang::ast_matchers;
3838

39-
namespace clang::tidy::misc {
39+
namespace clang::tidy::modernize {
4040

4141
namespace {
4242
AST_MATCHER(FunctionDecl, locationPermitsConstexpr) {
@@ -87,14 +87,16 @@ AST_MATCHER(Decl, allRedeclsInSameFile) {
8787
AST_MATCHER(FunctionDecl, isConstexprSpecified) {
8888
return Node.isConstexprSpecified();
8989
}
90+
} // namespace
9091

91-
bool satisfiesConstructorPropertiesUntil20(const CXXConstructorDecl *Ctor,
92-
ASTContext &Ctx) {
92+
static bool
93+
satisfiesConstructorPropertiesUntil20(const CXXConstructorDecl *Ctor,
94+
ASTContext &Ctx) {
9395
const CXXRecordDecl *Rec = Ctor->getParent();
9496
llvm::SmallPtrSet<const RecordDecl *, 8> Bases{};
95-
for (const CXXBaseSpecifier Base : Rec->bases()) {
97+
for (const CXXBaseSpecifier Base : Rec->bases())
9698
Bases.insert(Base.getType()->getAsRecordDecl());
97-
}
99+
98100
llvm::SmallPtrSet<const FieldDecl *, 8> Fields{Rec->field_begin(),
99101
Rec->field_end()};
100102
llvm::SmallPtrSet<const FieldDecl *, 4> Indirects{};
@@ -147,7 +149,7 @@ bool satisfiesConstructorPropertiesUntil20(const CXXConstructorDecl *Ctor,
147149
return true;
148150
}
149151

150-
const Type *unwrapPointee(const Type *T) {
152+
static const Type *unwrapPointee(const Type *T) {
151153
if (!T->isPointerOrReferenceType())
152154
return T;
153155

@@ -163,11 +165,11 @@ const Type *unwrapPointee(const Type *T) {
163165
return T;
164166
}
165167

166-
bool isLiteralType(QualType QT, const ASTContext &Ctx,
167-
const bool ConservativeLiteralType);
168+
static bool isLiteralType(QualType QT, const ASTContext &Ctx,
169+
const bool ConservativeLiteralType);
168170

169-
bool isLiteralType(const Type *T, const ASTContext &Ctx,
170-
const bool ConservativeLiteralType) {
171+
static bool isLiteralType(const Type *T, const ASTContext &Ctx,
172+
const bool ConservativeLiteralType) {
171173
if (!T)
172174
return false;
173175

@@ -211,18 +213,18 @@ bool isLiteralType(const Type *T, const ASTContext &Ctx,
211213
return false;
212214
}
213215

214-
bool isLiteralType(QualType QT, const ASTContext &Ctx,
215-
const bool ConservativeLiteralType) {
216+
static bool isLiteralType(QualType QT, const ASTContext &Ctx,
217+
const bool ConservativeLiteralType) {
216218
return isLiteralType(QT.getTypePtr(), Ctx, ConservativeLiteralType);
217219
}
218220

219-
bool satisfiesProperties11(
221+
static bool satisfiesProperties11(
220222
const FunctionDecl *FDecl, ASTContext &Ctx,
221223
const bool ConservativeLiteralType,
222224
const bool AddConstexprToMethodOfClassWithoutConstexprConstructor) {
223-
if (FDecl->isConstexprSpecified()) {
225+
if (FDecl->isConstexprSpecified())
224226
return true;
225-
}
227+
226228
const LangOptions LO = Ctx.getLangOpts();
227229
const CXXMethodDecl *Method = llvm::dyn_cast<CXXMethodDecl>(FDecl);
228230
if (Method && !Method->isStatic() &&
@@ -373,7 +375,7 @@ bool satisfiesProperties11(
373375

374376
// The only difference between C++14 and C++17 is that `constexpr` lambdas
375377
// can be used in C++17.
376-
bool satisfiesProperties1417(
378+
static bool satisfiesProperties1417(
377379
const FunctionDecl *FDecl, ASTContext &Ctx,
378380
const bool ConservativeLiteralType,
379381
const bool AddConstexprToMethodOfClassWithoutConstexprConstructor) {
@@ -573,13 +575,13 @@ bool satisfiesProperties1417(
573575
return true;
574576
}
575577

576-
bool satisfiesProperties20(
578+
static bool satisfiesProperties20(
577579
const FunctionDecl *FDecl, ASTContext &Ctx,
578580
const bool ConservativeLiteralType,
579581
const bool AddConstexprToMethodOfClassWithoutConstexprConstructor) {
580-
if (FDecl->isConstexprSpecified()) {
582+
if (FDecl->isConstexprSpecified())
581583
return true;
582-
}
584+
583585
const LangOptions LO = Ctx.getLangOpts();
584586
const CXXMethodDecl *Method = llvm::dyn_cast<CXXMethodDecl>(FDecl);
585587
if (Method && !Method->isStatic() &&
@@ -732,12 +734,12 @@ bool satisfiesProperties20(
732734
return true;
733735
}
734736

735-
bool satisfiesProperties2326(
737+
static bool satisfiesProperties2326(
736738
const FunctionDecl *FDecl, ASTContext &Ctx,
737739
const bool AddConstexprToMethodOfClassWithoutConstexprConstructor) {
738-
if (FDecl->isConstexprSpecified()) {
740+
if (FDecl->isConstexprSpecified())
739741
return true;
740-
}
742+
741743
const LangOptions LO = Ctx.getLangOpts();
742744
const CXXMethodDecl *Method = llvm::dyn_cast<CXXMethodDecl>(FDecl);
743745
if (Method && !Method->isStatic() &&
@@ -757,8 +759,7 @@ bool satisfiesProperties2326(
757759
return true;
758760
}
759761

760-
// FIXME: add test for uncalled lambda that throws, and called lambda that
761-
// throws
762+
namespace {
762763
// FIXME: fix CXX23 allowing decomposition decls, but it is only a feature since
763764
// CXX26
764765
AST_MATCHER_P2(FunctionDecl, satisfiesProperties, bool, ConservativeLiteralType,
@@ -943,4 +944,4 @@ void UseConstexprCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
943944
Options.store(Opts, "AddConstexprToMethodOfClassWithoutConstexprConstructor",
944945
AddConstexprToMethodOfClassWithoutConstexprConstructor);
945946
}
946-
} // namespace clang::tidy::misc
947+
} // namespace clang::tidy::modernize

clang-tools-extra/clang-tidy/misc/UseConstexprCheck.h renamed to clang-tools-extra/clang-tidy/modernize/UseConstexprCheck.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USECONSTEXPRCHECK_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USECONSTEXPRCHECK_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USECONSTEXPRCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USECONSTEXPRCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
1313
#include "clang/AST/Decl.h"
1414
#include "llvm/ADT/DenseMap.h"
1515
#include "llvm/ADT/SmallPtrSet.h"
1616

17-
namespace clang::tidy::misc {
17+
namespace clang::tidy::modernize {
1818

1919
/// Find functions and variables that can be declared 'constexpr'.
2020
///
2121
/// For the user-facing documentation see:
22-
/// http://clang.llvm.org/extra/clang-tidy/checks/misc/use-constexpr.html
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-constexpr.html
2323
class UseConstexprCheck : public ClangTidyCheck {
2424
public:
2525
UseConstexprCheck(StringRef Name, ClangTidyContext *Context);
@@ -38,6 +38,6 @@ class UseConstexprCheck : public ClangTidyCheck {
3838
llvm::DenseMap<const VarDecl *, const FunctionDecl *> VariableMapping;
3939
};
4040

41-
} // namespace clang::tidy::misc
41+
} // namespace clang::tidy::modernize
4242

43-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USECONSTEXPRCHECK_H
43+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USECONSTEXPRCHECK_H

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ Changes in existing checks
270270
excluding variables with ``thread_local`` storage class specifier from being
271271
matched.
272272

273+
- Added :doc:`modernize-use-constexpr
274+
<clang-tidy/checks/modernize/use-constexpr>` check that finds functions and
275+
variables that can be declared `constexpr`.
276+
273277
- Improved :doc:`modernize-use-default-member-init
274278
<clang-tidy/checks/modernize/use-default-member-init>` check by matching
275279
arithmetic operations, ``constexpr`` and ``static`` values, and detecting

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ Clang-Tidy Checks
277277
:doc:`misc-unused-parameters <misc/unused-parameters>`, "Yes"
278278
:doc:`misc-unused-using-decls <misc/unused-using-decls>`, "Yes"
279279
:doc:`misc-use-anonymous-namespace <misc/use-anonymous-namespace>`,
280-
:doc:`misc-use-constexpr <misc/use-constexpr>`, "Yes"
281280
:doc:`misc-use-internal-linkage <misc/use-internal-linkage>`, "Yes"
282281
:doc:`modernize-avoid-bind <modernize/avoid-bind>`, "Yes"
283282
:doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`,
@@ -301,6 +300,7 @@ Clang-Tidy Checks
301300
:doc:`modernize-unary-static-assert <modernize/unary-static-assert>`, "Yes"
302301
:doc:`modernize-use-auto <modernize/use-auto>`, "Yes"
303302
:doc:`modernize-use-bool-literals <modernize/use-bool-literals>`, "Yes"
303+
:doc:`modernize-use-constexpr <modernize/use-constexpr>`, "Yes"
304304
:doc:`modernize-use-constraints <modernize/use-constraints>`, "Yes"
305305
:doc:`modernize-use-default-member-init <modernize/use-default-member-init>`, "Yes"
306306
:doc:`modernize-use-designated-initializers <modernize/use-designated-initializers>`, "Yes"

clang-tools-extra/docs/clang-tidy/checks/misc/use-constexpr.rst renamed to clang-tools-extra/docs/clang-tidy/checks/modernize/use-constexpr.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.. title:: clang-tidy - misc-use-constexpr
1+
.. title:: clang-tidy - modernize-use-constexpr
22

3-
misc-use-constexpr
4-
==================
3+
modernize-use-constexpr
4+
=======================
55

66
Find functions and variables that can be declared 'constexpr'.
77

clang-tools-extra/test/clang-tidy/checkers/misc/use-constexpr-cxx20.cpp renamed to clang-tools-extra/test/clang-tidy/checkers/modernize/use-constexpr-cxx20.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++20 %s misc-use-constexpr %t
1+
// RUN: %check_clang_tidy -std=c++20 %s modernize-use-constexpr %t
22

33
namespace std {
44
template <typename T = void>

0 commit comments

Comments
 (0)