Skip to content

Commit c66668e

Browse files
committed
Make matchers more specific
1 parent 86787ec commit c66668e

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

clang-tools-extra/clang-tidy/portability/AvoidPlatformSpecificFundamentalTypesCheck.cpp

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,6 @@ using namespace clang::ast_matchers;
1616

1717
namespace clang::tidy::portability {
1818

19-
namespace {
20-
21-
AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
22-
return Node.getBeginLoc().isValid();
23-
}
24-
25-
AST_MATCHER_P(clang::TypeLoc, hasType,
26-
clang::ast_matchers::internal::Matcher<clang::Type>,
27-
InnerMatcher) {
28-
const clang::Type *TypeNode = Node.getTypePtr();
29-
return TypeNode != nullptr &&
30-
InnerMatcher.matches(*TypeNode, Finder, Builder);
31-
}
32-
33-
} // namespace
34-
3519
AvoidPlatformSpecificFundamentalTypesCheck::
3620
AvoidPlatformSpecificFundamentalTypesCheck(StringRef Name,
3721
ClangTidyContext *Context)
@@ -89,22 +73,55 @@ bool AvoidPlatformSpecificFundamentalTypesCheck::isSemanticType(
8973

9074
void AvoidPlatformSpecificFundamentalTypesCheck::registerMatchers(
9175
MatchFinder *Finder) {
92-
// Match variable declarations with fundamental integer types
93-
Finder->addMatcher(varDecl().bind("var_decl"), this);
94-
95-
// Match function declarations with fundamental integer return types
96-
Finder->addMatcher(functionDecl().bind("func_decl"), this);
97-
98-
// Match function parameters with fundamental integer types
99-
Finder->addMatcher(parmVarDecl().bind("param_decl"), this);
100-
101-
// Match field declarations with fundamental integer types
102-
Finder->addMatcher(fieldDecl().bind("field_decl"), this);
103-
104-
// Match typedef declarations to check their underlying types
105-
Finder->addMatcher(typedefDecl().bind("typedef_decl"), this);
106-
107-
Finder->addMatcher(typeAliasDecl().bind("alias_decl"), this);
76+
// Create a matcher for platform-specific fundamental integer types
77+
// This should only match direct uses of builtin types, not typedefs
78+
auto PlatformSpecificFundamentalType = qualType(
79+
allOf(
80+
// Must be a builtin type directly (not through typedef)
81+
builtinType(),
82+
// Only match the specific fundamental integer types we care about
83+
anyOf(
84+
asString("int"),
85+
asString("unsigned int"),
86+
asString("short"),
87+
asString("unsigned short"),
88+
asString("long"),
89+
asString("unsigned long"),
90+
asString("long long"),
91+
asString("unsigned long long")
92+
)
93+
)
94+
);
95+
96+
// Match variable declarations with platform-specific fundamental integer types
97+
Finder->addMatcher(
98+
varDecl(hasType(PlatformSpecificFundamentalType)).bind("var_decl"),
99+
this);
100+
101+
// Match function declarations with platform-specific fundamental integer return types
102+
Finder->addMatcher(
103+
functionDecl(returns(PlatformSpecificFundamentalType)).bind("func_decl"),
104+
this);
105+
106+
// Match function parameters with platform-specific fundamental integer types
107+
Finder->addMatcher(
108+
parmVarDecl(hasType(PlatformSpecificFundamentalType)).bind("param_decl"),
109+
this);
110+
111+
// Match field declarations with platform-specific fundamental integer types
112+
Finder->addMatcher(
113+
fieldDecl(hasType(PlatformSpecificFundamentalType)).bind("field_decl"),
114+
this);
115+
116+
// Match typedef declarations with platform-specific fundamental underlying types
117+
Finder->addMatcher(
118+
typedefDecl(hasUnderlyingType(PlatformSpecificFundamentalType)).bind("typedef_decl"),
119+
this);
120+
121+
// Match type alias declarations with platform-specific fundamental underlying types
122+
Finder->addMatcher(
123+
typeAliasDecl(hasType(PlatformSpecificFundamentalType)).bind("alias_decl"),
124+
this);
108125
}
109126

110127
void AvoidPlatformSpecificFundamentalTypesCheck::check(
@@ -148,24 +165,6 @@ void AvoidPlatformSpecificFundamentalTypesCheck::check(
148165
if (Loc.isInvalid() || QT.isNull())
149166
return;
150167

151-
// Check if the type is already a typedef - if so, don't warn
152-
// since the user is already using a typedef (which is what we want)
153-
if (QT->getAs<TypedefType>()) {
154-
return;
155-
}
156-
157-
const Type *T = QT.getCanonicalType().getTypePtr();
158-
if (!T)
159-
return;
160-
161-
// Skip if not a fundamental integer type
162-
if (!isFundamentalIntegerType(T))
163-
return;
164-
165-
// Skip semantic types
166-
if (isSemanticType(T))
167-
return;
168-
169168
// Get the type name for the diagnostic
170169
std::string TypeName = QT.getAsString();
171170

0 commit comments

Comments
 (0)