Skip to content

Commit 48598ef

Browse files
committed
Test typedefs properly
1 parent 3ef4feb commit 48598ef

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ AST_MATCHER_P(clang::TypeLoc, hasType,
3333

3434
AvoidFundamentalIntegerTypesCheck::AvoidFundamentalIntegerTypesCheck(
3535
StringRef Name, ClangTidyContext *Context)
36-
: ClangTidyCheck(Name, Context),
37-
IgnoreTypedefs(Options.get("IgnoreTypedefs", false)) {}
38-
39-
void AvoidFundamentalIntegerTypesCheck::storeOptions(
40-
ClangTidyOptions::OptionMap &Opts) {
41-
Options.store(Opts, "IgnoreTypedefs", IgnoreTypedefs);
42-
}
36+
: ClangTidyCheck(Name, Context) {}
4337

4438
bool AvoidFundamentalIntegerTypesCheck::isFundamentalIntegerType(
4539
const Type *T) const {
@@ -111,16 +105,14 @@ void AvoidFundamentalIntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
111105
fieldDecl().bind("field_decl"),
112106
this);
113107

114-
// Match typedef declarations if not ignoring them
115-
if (!IgnoreTypedefs) {
116-
Finder->addMatcher(
117-
typedefDecl().bind("typedef_decl"),
118-
this);
108+
// Match typedef declarations to check their underlying types
109+
Finder->addMatcher(
110+
typedefDecl().bind("typedef_decl"),
111+
this);
119112

120-
Finder->addMatcher(
121-
typeAliasDecl().bind("alias_decl"),
122-
this);
123-
}
113+
Finder->addMatcher(
114+
typeAliasDecl().bind("alias_decl"),
115+
this);
124116
}
125117

126118
void AvoidFundamentalIntegerTypesCheck::check(
@@ -160,6 +152,12 @@ void AvoidFundamentalIntegerTypesCheck::check(
160152
if (Loc.isInvalid() || QT.isNull())
161153
return;
162154

155+
// Check if the type is already a typedef - if so, don't warn
156+
// since the user is already using a typedef (which is what we want)
157+
if (QT->getAs<TypedefType>()) {
158+
return;
159+
}
160+
163161
const Type *T = QT.getCanonicalType().getTypePtr();
164162
if (!T)
165163
return;
@@ -180,4 +178,4 @@ void AvoidFundamentalIntegerTypesCheck::check(
180178
<< TypeName;
181179
}
182180

183-
} // namespace clang::tidy::modernize
181+
} // namespace clang::tidy::modernize

clang-tools-extra/clang-tidy/modernize/AvoidFundamentalIntegerTypesCheck.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class AvoidFundamentalIntegerTypesCheck : public ClangTidyCheck {
2727
AvoidFundamentalIntegerTypesCheck(StringRef Name, ClangTidyContext *Context);
2828
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2929
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
30-
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
3130
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
3231
return LangOpts.CPlusPlus11;
3332
}
@@ -36,11 +35,10 @@ class AvoidFundamentalIntegerTypesCheck : public ClangTidyCheck {
3635
}
3736

3837
private:
39-
const bool IgnoreTypedefs;
4038
bool isFundamentalIntegerType(const Type *T) const;
4139
bool isSemanticType(const Type *T) const;
4240
};
4341

4442
} // namespace clang::tidy::modernize
4543

46-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDFUNDAMENTALINTEGERTYPESCHECK_H
44+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDFUNDAMENTALINTEGERTYPESCHECK_H

clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-fundamental-integer-types.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// RUN: %check_clang_tidy %s modernize-avoid-fundamental-integer-types %t
22

3+
// Mock fixed-width integer types
4+
// NOLINTBEGIN(modernize-avoid-fundamental-integer-types)
5+
typedef unsigned int uint32_t;
6+
typedef int int32_t;
7+
typedef unsigned long long uint64_t;
8+
typedef long long int64_t;
9+
10+
// Mock standard library semantic types
11+
typedef unsigned long size_t;
12+
typedef long ptrdiff_t;
13+
// NOLINTEND(modernize-avoid-fundamental-integer-types)
14+
315
// Test fundamental integer types that should trigger warnings
416
int global_int = 42;
517
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: avoid using platform-dependent fundamental integer type 'int'; consider using a typedef or fixed-width type instead [modernize-avoid-fundamental-integer-types]
@@ -32,6 +44,16 @@ unsigned char global_unsigned_char = 'c';
3244
bool global_bool = true;
3345
wchar_t global_wchar = L'w';
3446

47+
// Test fixed-width types that should NOT trigger warnings
48+
uint32_t global_uint32 = 42U;
49+
int32_t global_int32 = 42;
50+
uint64_t global_uint64 = 100ULL;
51+
int64_t global_int64 = 100LL;
52+
53+
// Test semantic standard library types that should NOT trigger warnings
54+
size_t global_size = 100;
55+
ptrdiff_t global_ptrdiff = 50;
56+
3557
// Test function parameters
3658
void function_with_int_param(int param) {
3759
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: avoid using platform-dependent fundamental integer type 'int'; consider using a typedef or fixed-width type instead [modernize-avoid-fundamental-integer-types]
@@ -66,6 +88,14 @@ void test_local_variables() {
6688
// These should not trigger warnings
6789
char local_char = 'x';
6890
bool local_bool = false;
91+
92+
// Fixed-width types should not trigger warnings
93+
uint32_t local_uint32 = 42U;
94+
int64_t local_int64 = 100LL;
95+
96+
// Standard library semantic types should not trigger warnings
97+
size_t local_size = 200;
98+
ptrdiff_t local_ptrdiff = 10;
6999
}
70100

71101
// Test struct/class members
@@ -98,11 +128,14 @@ typedef int MyInt;
98128
using MyLong = long;
99129
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: avoid using platform-dependent fundamental integer type 'long'; consider using a typedef or fixed-width type instead [modernize-avoid-fundamental-integer-types]
100130

131+
typedef long long customType;
132+
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: avoid using platform-dependent fundamental integer type 'long long'; consider using a typedef or fixed-width type instead [modernize-avoid-fundamental-integer-types]
133+
101134
// Test template parameters
102135
template<typename T>
103136
void template_function(T param) {}
104137

105138
template<>
106139
void template_function<int>(int param) {
107140
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: avoid using platform-dependent fundamental integer type 'int'; consider using a typedef or fixed-width type instead [modernize-avoid-fundamental-integer-types]
108-
}
141+
}

0 commit comments

Comments
 (0)