[clang-tools-extra] [clang-tidy] Add portability-avoid-platform-specific-fundamental-types (PR #146970)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 4 07:56:04 PDT 2025
================
@@ -0,0 +1,121 @@
+//===--- AvoidPlatformSpecificFundamentalTypesCheck.cpp - clang-tidy
+//---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AvoidPlatformSpecificFundamentalTypesCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::portability {
+
+AvoidPlatformSpecificFundamentalTypesCheck::
+ AvoidPlatformSpecificFundamentalTypesCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+
+void AvoidPlatformSpecificFundamentalTypesCheck::registerMatchers(
+ MatchFinder *Finder) {
+ // Create a matcher for platform-specific fundamental integer types
+ // This should only match direct uses of builtin types, not typedefs
+ auto PlatformSpecificFundamentalType = qualType(allOf(
+ // Must be a builtin type directly (not through typedef)
+ builtinType(),
+ // Only match the specific fundamental integer types we care about
+ anyOf(asString("short"), asString("short int"), asString("signed short"),
+ asString("signed short int"), asString("unsigned short"),
+ asString("unsigned short int"), asString("int"), asString("signed"),
+ asString("signed int"), asString("unsigned"),
+ asString("unsigned int"), asString("long"), asString("long int"),
+ asString("signed long"), asString("signed long int"),
+ asString("unsigned long"), asString("unsigned long int"),
+ asString("long long"), asString("long long int"),
+ asString("signed long long"), asString("signed long long int"),
+ asString("unsigned long long"),
+ asString("unsigned long long int"))));
+
+ // Match variable declarations with platform-specific fundamental integer
+ // types
+ Finder->addMatcher(
+ varDecl(hasType(PlatformSpecificFundamentalType)).bind("var_decl"), this);
+
+ // Match function declarations with platform-specific fundamental integer
+ // return types
+ Finder->addMatcher(
+ functionDecl(returns(PlatformSpecificFundamentalType)).bind("func_decl"),
+ this);
+
+ // Match function parameters with platform-specific fundamental integer types
+ Finder->addMatcher(
+ parmVarDecl(hasType(PlatformSpecificFundamentalType)).bind("param_decl"),
+ this);
+
+ // Match field declarations with platform-specific fundamental integer types
+ Finder->addMatcher(
+ fieldDecl(hasType(PlatformSpecificFundamentalType)).bind("field_decl"),
+ this);
+
+ // Match typedef declarations with platform-specific fundamental underlying
+ // types
+ Finder->addMatcher(
+ typedefDecl(hasUnderlyingType(PlatformSpecificFundamentalType))
+ .bind("typedef_decl"),
+ this);
+
+ // Match type alias declarations with platform-specific fundamental underlying
+ // types
+ Finder->addMatcher(typeAliasDecl(hasType(PlatformSpecificFundamentalType))
+ .bind("alias_decl"),
+ this);
+}
+
+void AvoidPlatformSpecificFundamentalTypesCheck::check(
+ const MatchFinder::MatchResult &Result) {
+ SourceLocation Loc;
+ QualType QT;
+
+ if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var_decl")) {
+ Loc = VD->getLocation();
+ QT = VD->getType();
+ } else if (const auto *FD =
+ Result.Nodes.getNodeAs<FunctionDecl>("func_decl")) {
+ Loc = FD->getLocation();
+ QT = FD->getReturnType();
+ } else if (const auto *PD =
+ Result.Nodes.getNodeAs<ParmVarDecl>("param_decl")) {
+ Loc = PD->getLocation();
+ QT = PD->getType();
+ } else if (const auto *FD = Result.Nodes.getNodeAs<FieldDecl>("field_decl")) {
+ Loc = FD->getLocation();
+ QT = FD->getType();
+ } else if (const auto *TD =
+ Result.Nodes.getNodeAs<TypedefDecl>("typedef_decl")) {
+ Loc = TD->getLocation();
+ QT = TD->getUnderlyingType();
+ } else if (const auto *AD =
+ Result.Nodes.getNodeAs<TypeAliasDecl>("alias_decl")) {
+ Loc = AD->getLocation();
+ QT = AD->getUnderlyingType();
+ } else {
+ return;
+ }
+
+ if (Loc.isInvalid() || QT.isNull())
+ return;
+
+ // Get the type name for the diagnostic
+ std::string TypeName = QT.getAsString();
----------------
EugeneZelenko wrote:
```suggestion
const std::string TypeName = QT.getAsString();
```
https://github.com/llvm/llvm-project/pull/146970
More information about the cfe-commits
mailing list