Skip to content

Commit 5d95d27

Browse files
authored
[clang-tidy] Fix support for typedefs in readability-identifier-naming (#66835)
Typedef rename were not properly handled when typedef were used behind pointer, or as a part of function type. Additionally because entire function were passed as an source-range, when function started with macro, such change were not marked for a fix. Removed workaround and used proper TypedefTypeLoc instead. Fixes #55156, #54699
1 parent 33aa095 commit 5d95d27

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp

+5-20
Original file line numberDiff line numberDiff line change
@@ -256,26 +256,6 @@ class RenamerClangTidyVisitor
256256
return true;
257257
}
258258

259-
// Fix type aliases in value declarations.
260-
if (const auto *Value = dyn_cast<ValueDecl>(Decl)) {
261-
if (const Type *TypePtr = Value->getType().getTypePtrOrNull()) {
262-
if (const auto *Typedef = TypePtr->getAs<TypedefType>())
263-
Check->addUsage(Typedef->getDecl(), Value->getSourceRange(), SM);
264-
}
265-
}
266-
267-
// Fix type aliases in function declarations.
268-
if (const auto *Value = dyn_cast<FunctionDecl>(Decl)) {
269-
if (const auto *Typedef =
270-
Value->getReturnType().getTypePtr()->getAs<TypedefType>())
271-
Check->addUsage(Typedef->getDecl(), Value->getSourceRange(), SM);
272-
for (const ParmVarDecl *Param : Value->parameters()) {
273-
if (const TypedefType *Typedef =
274-
Param->getType().getTypePtr()->getAs<TypedefType>())
275-
Check->addUsage(Typedef->getDecl(), Value->getSourceRange(), SM);
276-
}
277-
}
278-
279259
// Fix overridden methods
280260
if (const auto *Method = dyn_cast<CXXMethodDecl>(Decl)) {
281261
if (const CXXMethodDecl *Overridden = getOverrideMethod(Method)) {
@@ -340,6 +320,11 @@ class RenamerClangTidyVisitor
340320
return true;
341321
}
342322

323+
bool VisitTypedefTypeLoc(const TypedefTypeLoc &Loc) {
324+
Check->addUsage(Loc.getTypedefNameDecl(), Loc.getSourceRange(), SM);
325+
return true;
326+
}
327+
343328
bool VisitTagTypeLoc(const TagTypeLoc &Loc) {
344329
Check->addUsage(Loc.getDecl(), Loc.getSourceRange(), SM);
345330
return true;

clang-tools-extra/docs/ReleaseNotes.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ Changes in existing checks
295295
warnings when a type's forward declaration precedes its definition.
296296
Additionally, it now provides appropriate warnings for ``struct`` and
297297
``union`` in C, while also incorporating support for the
298-
``Leading_upper_snake_case`` naming convention.
298+
``Leading_upper_snake_case`` naming convention. The handling of ``typedef``
299+
has been enhanced, particularly within complex types like function pointers
300+
and cases where style checks were omitted when functions started with macros.
299301

300302
- Improved :doc:`readability-implicit-bool-conversion
301303
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take

clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,29 @@ struct forward_declared_as_struct;
729729
class forward_declared_as_struct {
730730
};
731731

732+
namespace pr55156 {
733+
734+
template<typename> struct Wrap;
735+
736+
typedef enum {
737+
VALUE0,
738+
VALUE1,
739+
} ValueType;
740+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: invalid case style for typedef 'ValueType' [readability-identifier-naming]
741+
// CHECK-FIXES: {{^}}} value_type_t;
742+
743+
typedef ValueType (*MyFunPtr)(const ValueType&, Wrap<ValueType>*);
744+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: invalid case style for typedef 'MyFunPtr' [readability-identifier-naming]
745+
// CHECK-FIXES: {{^}}typedef value_type_t (*my_fun_ptr_t)(const value_type_t&, Wrap<value_type_t>*);
746+
747+
#define STATIC_MACRO static
748+
STATIC_MACRO void someFunc(ValueType a_v1, const ValueType& a_v2) {}
749+
// CHECK-FIXES: {{^}}STATIC_MACRO void someFunc(value_type_t a_v1, const value_type_t& a_v2) {}
750+
STATIC_MACRO void someFunc(const ValueType** p_a_v1, ValueType (*p_a_v2)()) {}
751+
// CHECK-FIXES: {{^}}STATIC_MACRO void someFunc(const value_type_t** p_a_v1, value_type_t (*p_a_v2)()) {}
752+
STATIC_MACRO ValueType someFunc() {}
753+
// CHECK-FIXES: {{^}}STATIC_MACRO value_type_t someFunc() {}
754+
STATIC_MACRO void someFunc(MyFunPtr, const MyFunPtr****) {}
755+
// CHECK-FIXES: {{^}}STATIC_MACRO void someFunc(my_fun_ptr_t, const my_fun_ptr_t****) {}
756+
#undef STATIC_MACRO
757+
}

0 commit comments

Comments
 (0)