================ @@ -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; ---------------- EugeneZelenko wrote:
Isn't next conditional statement doing the same? https://github.com/llvm/llvm-project/pull/146970 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits