[clang-tools-extra] [clang-tidy] Add readability-use-builtin-literals check (PR #76065)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 5 03:31:45 PST 2024


================
@@ -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(
----------------
PiotrZSL wrote:

`ignoringParenImpCasts` doesn't look to be needed because TK_IgnoreUnlessSpelledInSource is used, and without it all tests still pass. Remove or add tests to cover this.

https://github.com/llvm/llvm-project/pull/76065


More information about the cfe-commits mailing list