Skip to content

[clang-tidy] Fix modernize-use-constraints crash on uses of nonstandard enable_ifs #152938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "UseConstraintsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"

Expand Down Expand Up @@ -78,6 +79,13 @@ matchEnableIfSpecializationImplTypename(TypeLoc TheType) {
if (!TD || TD->getName() != "enable_if")
return std::nullopt;

assert(!TD->getTemplateParameters()->empty() &&
"found template with no template parameters?");
const auto *FirstParam = dyn_cast<NonTypeTemplateParmDecl>(
TD->getTemplateParameters()->getParam(0));
if (!FirstParam || !FirstParam->getType()->isBooleanType())
return std::nullopt;

int NumArgs = SpecializationLoc.getNumArgs();
if (NumArgs != 1 && NumArgs != 2)
return std::nullopt;
Expand Down Expand Up @@ -108,6 +116,13 @@ matchEnableIfSpecializationImplTrait(TypeLoc TheType) {
if (!Specialization->isTypeAlias())
return std::nullopt;

assert(!TD->getTemplateParameters()->empty() &&
"found template with no template parameters?");
const auto *FirstParam = dyn_cast<NonTypeTemplateParmDecl>(
TD->getTemplateParameters()->getParam(0));
if (!FirstParam || !FirstParam->getType()->isBooleanType())
return std::nullopt;

if (const auto *AliasedType =
dyn_cast<DependentNameType>(Specialization->getAliasedType())) {
ElaboratedTypeKeyword Keyword = AliasedType->getKeyword();
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ Changes in existing checks
- Improved :doc:`misc-header-include-cycle
<clang-tidy/checks/misc/header-include-cycle>` check performance.

- Improved :doc:`modernize-use-constraints
<clang-tidy/checks/modernize/use-constraints>` check by fixing a crash on
uses of non-standard ``enable_if`` with a signature different from
``std::enable_if`` (such as ``boost::enable_if``).

- Improved :doc:`modernize-use-designated-initializers
<clang-tidy/checks/modernize/use-designated-initializers>` check to
suggest using designated initializers for aliased aggregate types.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy -std=c++20 %s modernize-use-constraints %t -- -- -fno-delayed-template-parsing
// RUN: %check_clang_tidy -std=c++20-or-later %s modernize-use-constraints %t -- -- -fno-delayed-template-parsing

// NOLINTBEGIN
namespace std {
Expand Down Expand Up @@ -756,3 +756,46 @@ abs(const number<T, ExpressionTemplates> &v) {
}

}

template <typename T>
struct some_type_trait {
static constexpr bool value = true;
};

// Fix-its are offered even for a non-standard enable_if.
namespace nonstd {

template <bool Condition, typename T = void>
struct enable_if : std::enable_if<Condition, T> {};

template <bool Condition, typename T = void>
using enable_if_t = typename enable_if<Condition, T>::type;

}

template <typename T>
typename nonstd::enable_if<some_type_trait<T>::value, void>::type nonstd_enable_if() {}
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use C++20 requires constraints instead of enable_if [modernize-use-constraints]
// CHECK-FIXES: {{^}}void nonstd_enable_if() requires some_type_trait<T>::value {}{{$}}

template <typename T>
nonstd::enable_if_t<some_type_trait<T>::value, void> nonstd_enable_if_t() {}
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use C++20 requires constraints instead of enable_if [modernize-use-constraints]
// CHECK-FIXES: {{^}}void nonstd_enable_if_t() requires some_type_trait<T>::value {}{{$}}

// But only if the non-standard enable_if has the same signature as the standard one.
namespace boost {

template <typename Condition, typename T = void>
struct enable_if : std::enable_if<Condition::value, T> {};

template <typename Condition, typename T = void>
using enable_if_t = typename enable_if<Condition, T>::type;

}

template <typename T>
typename boost::enable_if<some_type_trait<T>, void>::type boost_enable_if() {}

template <typename T>
boost::enable_if_t<some_type_trait<T>, void> boost_enable_if_t() {}