Skip to content

[clang-tidy] Fix support for typedefs in readability-identifier-naming #66835

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

Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 5 additions & 20 deletions clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,6 @@ class RenamerClangTidyVisitor
return true;
}

// Fix type aliases in value declarations.
if (const auto *Value = dyn_cast<ValueDecl>(Decl)) {
if (const Type *TypePtr = Value->getType().getTypePtrOrNull()) {
if (const auto *Typedef = TypePtr->getAs<TypedefType>())
Check->addUsage(Typedef->getDecl(), Value->getSourceRange(), SM);
}
}

// Fix type aliases in function declarations.
if (const auto *Value = dyn_cast<FunctionDecl>(Decl)) {
if (const auto *Typedef =
Value->getReturnType().getTypePtr()->getAs<TypedefType>())
Check->addUsage(Typedef->getDecl(), Value->getSourceRange(), SM);
for (const ParmVarDecl *Param : Value->parameters()) {
if (const TypedefType *Typedef =
Param->getType().getTypePtr()->getAs<TypedefType>())
Check->addUsage(Typedef->getDecl(), Value->getSourceRange(), SM);
}
}

// Fix overridden methods
if (const auto *Method = dyn_cast<CXXMethodDecl>(Decl)) {
if (const CXXMethodDecl *Overridden = getOverrideMethod(Method)) {
Expand Down Expand Up @@ -340,6 +320,11 @@ class RenamerClangTidyVisitor
return true;
}

bool VisitTypedefTypeLoc(const TypedefTypeLoc &Loc) {
Check->addUsage(Loc.getTypedefNameDecl(), Loc.getSourceRange(), SM);
return true;
}

bool VisitTagTypeLoc(const TagTypeLoc &Loc) {
Check->addUsage(Loc.getDecl(), Loc.getSourceRange(), SM);
return true;
Expand Down
4 changes: 3 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ Changes in existing checks
warnings when a type's forward declaration precedes its definition.
Additionally, it now provides appropriate warnings for ``struct`` and
``union`` in C, while also incorporating support for the
``Leading_upper_snake_case`` naming convention.
``Leading_upper_snake_case`` naming convention. The handling of ``typedef``
has been enhanced, particularly within complex types like function pointers
and cases where style checks were omitted when functions started with macros.

- Improved :doc:`readability-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,29 @@ struct forward_declared_as_struct;
class forward_declared_as_struct {
};

namespace pr55156 {

template<typename> struct Wrap;

typedef enum {
VALUE0,
VALUE1,
} ValueType;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: invalid case style for typedef 'ValueType' [readability-identifier-naming]
// CHECK-FIXES: {{^}}} value_type_t;

typedef ValueType (*MyFunPtr)(const ValueType&, Wrap<ValueType>*);
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for typedef 'MyFunPtr' [readability-identifier-naming]
// CHECK-FIXES: {{^}}typedef value_type_t (*my_fun_ptr_t)(const value_type_t&, Wrap<value_type_t>*);

#define STATIC_MACRO static
STATIC_MACRO void someFunc(ValueType a_v1, const ValueType& a_v2) {}
// CHECK-FIXES: {{^}}STATIC_MACRO void someFunc(value_type_t a_v1, const value_type_t& a_v2) {}
STATIC_MACRO void someFunc(const ValueType** p_a_v1, ValueType (*p_a_v2)()) {}
// CHECK-FIXES: {{^}}STATIC_MACRO void someFunc(const value_type_t** p_a_v1, value_type_t (*p_a_v2)()) {}
STATIC_MACRO ValueType someFunc() {}
// CHECK-FIXES: {{^}}STATIC_MACRO value_type_t someFunc() {}
STATIC_MACRO void someFunc(MyFunPtr, const MyFunPtr****) {}
// CHECK-FIXES: {{^}}STATIC_MACRO void someFunc(my_fun_ptr_t, const my_fun_ptr_t****) {}
#undef STATIC_MACRO
}