[clang] [RFC][clang][Support] Extract type persing function. [NFCI] (PR #82797)
fpetrogalli wrote: I have added for feedback all people involved in https://github.com/llvm/llvm-project/pull/68324 https://github.com/llvm/llvm-project/pull/82797 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [RFC][clang][Support] Extract type persing function. [NFCI] (PR #82797)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Francesco Petrogalli (fpetrogalli) Changes I wanted to get a sense if people are OK for me to provide unit tests in `clang/unittests/Support` for the the parsing function I factored out from `clang-tblgen` into `clangSupport`. The refactoring would look like this patch - I haven't added any actual tests because I didn't want to keep working on this if people have strong arguments against it. FWIW, I believe that this refactoring will make it easier to handle new types over time. Pleas note that this is an RFC - I am not asking for a fully detailed review, just for some feedback on the idea. --- Full diff: https://github.com/llvm/llvm-project/pull/82797.diff 4 Files Affected: - (added) clang/include/clang/Support/BuiltinsUtils.h (+24) - (modified) clang/lib/Support/CMakeLists.txt (+1) - (added) clang/lib/Support/ClangBuiltinsUtils.cpp (+81) - (modified) clang/utils/TableGen/ClangBuiltinsEmitter.cpp (+2-72) ``diff diff --git a/clang/include/clang/Support/BuiltinsUtils.h b/clang/include/clang/Support/BuiltinsUtils.h new file mode 100644 index 00..2ee26f28127ce9 --- /dev/null +++ b/clang/include/clang/Support/BuiltinsUtils.h @@ -0,0 +1,24 @@ +//===--- BuiltinsUtils.h - clang Builtins Utils -*- C++ -*-===// +// +// 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 +// +//===--===// +#ifndef CLANG_SUPPORT_BUILTINSUTILS_H +#define CLANG_SUPPORT_BUILTINSUTILS_H + +#include "llvm/ADT/StringRef.h" +#include +namespace llvm { +class SMLoc; +} +namespace clang { + +/// Parse builtins prototypes according to the rules in +/// clang/include/clang/Basic/Builtins.def +void ParseBuiltinType(llvm::StringRef T, llvm::StringRef Substitution, + std::string , llvm::SMLoc *Loc); + +} // namespace clang +#endif // CLANG_SUPPORT_BUILTINSUTILS_H diff --git a/clang/lib/Support/CMakeLists.txt b/clang/lib/Support/CMakeLists.txt index 8ea5620052ed84..93959eb8565b6b 100644 --- a/clang/lib/Support/CMakeLists.txt +++ b/clang/lib/Support/CMakeLists.txt @@ -11,6 +11,7 @@ set(LLVM_LINK_COMPONENTS set(clangSupport_sources RISCVVIntrinsicUtils.cpp + ClangBuiltinsUtils.cpp ) add_clang_library(clangSupport ${clangSupport_sources}) diff --git a/clang/lib/Support/ClangBuiltinsUtils.cpp b/clang/lib/Support/ClangBuiltinsUtils.cpp new file mode 100644 index 00..523378322cdf1b --- /dev/null +++ b/clang/lib/Support/ClangBuiltinsUtils.cpp @@ -0,0 +1,81 @@ +#include "clang/Support/BuiltinsUtils.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/Support/SMLoc.h" +#include "llvm/TableGen/Error.h" + +void clang::ParseBuiltinType(llvm::StringRef T, llvm::StringRef Substitution, + std::string , llvm::SMLoc *Loc) { + assert(Loc); + T = T.trim(); + if (T.consume_back("*")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "*"; + } else if (T.consume_back("const")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "C"; + } else if (T.consume_back("volatile")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "D"; + } else if (T.consume_back("restrict")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "R"; + } else if (T.consume_back("&")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "&"; + } else if (T.consume_front("long")) { +Type += "L"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("unsigned")) { +Type += "U"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("_Complex")) { +Type += "X"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("_Constant")) { +Type += "I"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("T")) { +if (Substitution.empty()) + llvm::PrintFatalError(*Loc, "Not a template"); +ParseBuiltinType(Substitution, Substitution, Type, Loc); + } else { +auto ReturnTypeVal = llvm::StringSwitch(T) + .Case("__builtin_va_list_ref", "A") + .Case("__builtin_va_list", "a") + .Case("__float128", "LLd") + .Case("__fp16", "h") + .Case("__int128_t", "LLLi") + .Case("_Float16", "x") + .Case("bool", "b") + .Case("char", "c") + .Case("constant_CFString", "F") + .Case("double", "d") + .Case("FILE", "P") + .Case("float", "f") + .Case("id", "G")
[clang] [RFC][clang][Support] Extract type persing function. [NFCI] (PR #82797)
https://github.com/fpetrogalli created https://github.com/llvm/llvm-project/pull/82797 I wanted to get a sense if people are OK for me to provide unit tests in `clang/unittests/Support` for the the parsing function I factored out from `clang-tblgen` into `clangSupport`. The refactoring would look like this patch - I haven't added any actual tests because I didn't want to keep working on this if people have strong arguments against it. FWIW, I believe that this refactoring will make it easier to handle new types over time. Pleas note that this is an RFC - I am not asking for a fully detailed review, just for some feedback on the idea. >From e9e5ec780fbc366c4619ae860c9069ee91c77b44 Mon Sep 17 00:00:00 2001 From: Francesco Petrogalli Date: Fri, 23 Feb 2024 17:53:30 +0100 Subject: [PATCH] [RFC][clang][Support] Extract type persing function. [NFCI] --- clang/include/clang/Support/BuiltinsUtils.h | 24 ++ clang/lib/Support/CMakeLists.txt | 1 + clang/lib/Support/ClangBuiltinsUtils.cpp | 81 +++ clang/utils/TableGen/ClangBuiltinsEmitter.cpp | 74 + 4 files changed, 108 insertions(+), 72 deletions(-) create mode 100644 clang/include/clang/Support/BuiltinsUtils.h create mode 100644 clang/lib/Support/ClangBuiltinsUtils.cpp diff --git a/clang/include/clang/Support/BuiltinsUtils.h b/clang/include/clang/Support/BuiltinsUtils.h new file mode 100644 index 00..2ee26f28127ce9 --- /dev/null +++ b/clang/include/clang/Support/BuiltinsUtils.h @@ -0,0 +1,24 @@ +//===--- BuiltinsUtils.h - clang Builtins Utils -*- C++ -*-===// +// +// 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 +// +//===--===// +#ifndef CLANG_SUPPORT_BUILTINSUTILS_H +#define CLANG_SUPPORT_BUILTINSUTILS_H + +#include "llvm/ADT/StringRef.h" +#include +namespace llvm { +class SMLoc; +} +namespace clang { + +/// Parse builtins prototypes according to the rules in +/// clang/include/clang/Basic/Builtins.def +void ParseBuiltinType(llvm::StringRef T, llvm::StringRef Substitution, + std::string , llvm::SMLoc *Loc); + +} // namespace clang +#endif // CLANG_SUPPORT_BUILTINSUTILS_H diff --git a/clang/lib/Support/CMakeLists.txt b/clang/lib/Support/CMakeLists.txt index 8ea5620052ed84..93959eb8565b6b 100644 --- a/clang/lib/Support/CMakeLists.txt +++ b/clang/lib/Support/CMakeLists.txt @@ -11,6 +11,7 @@ set(LLVM_LINK_COMPONENTS set(clangSupport_sources RISCVVIntrinsicUtils.cpp + ClangBuiltinsUtils.cpp ) add_clang_library(clangSupport ${clangSupport_sources}) diff --git a/clang/lib/Support/ClangBuiltinsUtils.cpp b/clang/lib/Support/ClangBuiltinsUtils.cpp new file mode 100644 index 00..523378322cdf1b --- /dev/null +++ b/clang/lib/Support/ClangBuiltinsUtils.cpp @@ -0,0 +1,81 @@ +#include "clang/Support/BuiltinsUtils.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/Support/SMLoc.h" +#include "llvm/TableGen/Error.h" + +void clang::ParseBuiltinType(llvm::StringRef T, llvm::StringRef Substitution, + std::string , llvm::SMLoc *Loc) { + assert(Loc); + T = T.trim(); + if (T.consume_back("*")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "*"; + } else if (T.consume_back("const")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "C"; + } else if (T.consume_back("volatile")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "D"; + } else if (T.consume_back("restrict")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "R"; + } else if (T.consume_back("&")) { +ParseBuiltinType(T, Substitution, Type, Loc); +Type += "&"; + } else if (T.consume_front("long")) { +Type += "L"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("unsigned")) { +Type += "U"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("_Complex")) { +Type += "X"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("_Constant")) { +Type += "I"; +ParseBuiltinType(T, Substitution, Type, Loc); + } else if (T.consume_front("T")) { +if (Substitution.empty()) + llvm::PrintFatalError(*Loc, "Not a template"); +ParseBuiltinType(Substitution, Substitution, Type, Loc); + } else { +auto ReturnTypeVal = llvm::StringSwitch(T) + .Case("__builtin_va_list_ref", "A") + .Case("__builtin_va_list", "a") + .Case("__float128", "LLd") + .Case("__fp16", "h") + .Case("__int128_t", "LLLi") + .Case("_Float16", "x") + .Case("bool", "b") +