================ @@ -0,0 +1,161 @@ +//===--- UseBuiltinLiteralsCheck.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 "UseBuiltinLiteralsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include <optional> + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +namespace { + +using RuleOnStd = bool (*)(const LangStandard &LS); + +struct Replacement { + Replacement(StringRef Seq, const RuleOnStd Std = nullptr) + : Seq(Seq), Std(Std) {} + bool operator()(const LangOptions &LO) const { + return Std ? Std(LangStandard::getLangStandardForKind(LO.LangStd)) : true; + } + StringRef Seq; + RuleOnStd Std; +}; + +} // namespace + +static const llvm::Regex CharRegex("^(u8|u|U|L)?"); +static const llvm::StringMap<Replacement> CharPrefix({ + {"char", {""}}, + {"char8_t", {"u8"}}, + {"char16_t", {"u"}}, + {"char32_t", {"U"}}, + {"wchar_t", {"L"}}, +}); + +static const llvm::Regex + IntRegex("(([uU]?[lL]{0,2})|([lL]{0,2}[uU]?)|([uU]?[zZ]?)|([zZ]?[uU]?))?$"); +static const llvm::StringMap<Replacement> IntSuffix({ + {"int", {""}}, + {"unsigned int", {"u"}}, + {"long", {"L"}}, + {"unsigned long", {"uL"}}, + {"long long", {"LL"}}, + {"unsigned long long", {"uLL"}}, + {"size_t", {"uz", [](const auto &LS) { return LS.isCPlusPlus23(); }}}, + {"std::size_t", {"uz", [](const auto &LS) { return LS.isCPlusPlus23(); }}}, +}); + +static const llvm::Regex FloatRegex( + "([fF]|[lL]|([fF]16)|([fF]32)|([fF]64)|([fF]128)|((bf|BF)16))?$"); +static const llvm::StringMap<Replacement> FloatSuffix({ + {"double", {""}}, + {"float", {"f"}}, + {"long double", {"L"}}, + {"std::float16_t", {"f16"}}, + {"std::float32_t", {"f32"}}, + {"std::float64_t", {"f64"}}, + {"std::float128_t", {"f128"}}, + {"std::bfloat16_t", {"bf16"}}, + {"float16_t", {"f16"}}, + {"float32_t", {"f32"}}, + {"float64_t", {"f64"}}, + {"float128_t", {"f128"}}, + {"bfloat16_t", {"bf16"}}, +}); + +void UseBuiltinLiteralsCheck::registerMatchers(MatchFinder *Finder) { + static const auto Literal = has(ignoringParenImpCasts( + expr(anyOf(characterLiteral().bind("char"), integerLiteral().bind("int"), + floatLiteral().bind("float"))) + .bind("lit"))); + Finder->addMatcher( + traverse(TK_IgnoreUnlessSpelledInSource, + explicitCastExpr(anyOf(Literal, has(initListExpr(Literal)))) + .bind("expr")), ---------------- PiotrZSL wrote:
change this "expr" into "cast", as later in check method is not so clear what it refers. https://github.com/llvm/llvm-project/pull/76065 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits