[libc-commits] [libc] d153667 - [libc] Add ability to generate enum types/values to HdrGen.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Fri Feb 28 23:28:56 PST 2020
Author: Siva Chandra Reddy
Date: 2020-02-28T23:27:32-08:00
New Revision: d1536673c68d5010d2a6a3a12a9dd205e8fca2d4
URL: https://github.com/llvm/llvm-project/commit/d1536673c68d5010d2a6a3a12a9dd205e8fca2d4
DIFF: https://github.com/llvm/llvm-project/commit/d1536673c68d5010d2a6a3a12a9dd205e8fca2d4.diff
LOG: [libc] Add ability to generate enum types/values to HdrGen.
A target to generate the std C threads.h file has been added. This
utilizes the new feature added in this change.
Reviewers: phosek
Differential Revision: https://reviews.llvm.org/D75379
Added:
libc/include/threads.h.def
Modified:
libc/config/linux/api.td
libc/config/public_api.td
libc/include/CMakeLists.txt
libc/spec/linux.td
libc/spec/posix.td
libc/spec/spec.td
libc/spec/stdc.td
libc/utils/HdrGen/PublicAPICommand.cpp
Removed:
################################################################################
diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 3d7faa75bf69..ebb4883f4ba5 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -143,3 +143,16 @@ def SignalAPI : PublicAPI<"signal.h"> {
"raise",
];
}
+
+def ThreadsAPI : PublicAPI<"threads.h"> {
+ let Enumerations = [
+ "mtx_plain",
+ "mtx_recursive",
+ "mtx_timed",
+ "thrd_timedout",
+ "thrd_success",
+ "thrd_busy",
+ "thrd_error",
+ "thrd_nomem",
+ ];
+}
diff --git a/libc/config/public_api.td b/libc/config/public_api.td
index 3f409210e05c..b60836901836 100644
--- a/libc/config/public_api.td
+++ b/libc/config/public_api.td
@@ -24,6 +24,7 @@ class PublicAPI<string name> {
string HeaderName = name;
list<MacroDef> Macros = [];
list<TypeDecl> TypeDeclarations = [];
+ list<string> Enumerations = [];
list<string> Structs = [];
list<string> Functions = [];
}
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 1ff9e7829097..612b53196270 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -35,6 +35,14 @@ add_gen_header(
llvm_libc_common_h
)
+add_gen_header(
+ threads_h
+ DEF_FILE threads.h.def
+ GEN_HDR threads.h
+ DEPENDS
+ llvm_libc_common_h
+)
+
add_gen_header(
errno_h
DEF_FILE errno.h.def
diff --git a/libc/include/threads.h.def b/libc/include/threads.h.def
new file mode 100644
index 000000000000..276f78bb35a7
--- /dev/null
+++ b/libc/include/threads.h.def
@@ -0,0 +1,16 @@
+//===---------------- C standard library header threads.h -----------------===//
+//
+// 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 LLVM_LIBC_THREADS_H
+#define LLVM_LIBC_THREADS_H
+
+#include <__llvm-libc-common.h>
+
+%%public_api()
+
+#endif // LLVM_LIBC_THREADS_H
diff --git a/libc/spec/linux.td b/libc/spec/linux.td
index 8903fdaf09b3..29baf8b1e5ee 100644
--- a/libc/spec/linux.td
+++ b/libc/spec/linux.td
@@ -55,16 +55,12 @@ def Linux : StandardSpec<"Linux"> {
Macro<"EL2NSYNC">,
Macro<"EADV">,
Macro<"ECOMM">,
- ],
- [], // Types
- [] // Functions
+ ]
>;
HeaderSpec SysMMan = HeaderSpec<
"sys/mman.h",
- [Macro<"MAP_ANONYMOUS">],
- [], // Types
- [] // Functions
+ [Macro<"MAP_ANONYMOUS">]
>;
HeaderSpec Signal = HeaderSpec<
@@ -106,9 +102,7 @@ def Linux : StandardSpec<"Linux"> {
Macro<"SIGPWR">,
Macro<"SIGSYS">,
Macro<"SIGUNUSED">,
- ],
- [], // Types
- [] // Functions
+ ]
>;
let Headers = [
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index 094eaea04ef5..b2ad036d63b0 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -84,6 +84,7 @@ def POSIX : StandardSpec<"POSIX"> {
Macro<"EXDEV">,
],
[], // Types
+ [], // Enumerations
[] // Functions
>;
@@ -107,6 +108,7 @@ def POSIX : StandardSpec<"POSIX"> {
SizeTType,
OffTType,
],
+ [], // Enumerations
[
FunctionSpec<
"mmap",
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index 61c1f899fda7..2272ef3da368 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -14,6 +14,15 @@ class Struct<string name> : NamedType<name> {
list<Field> Fields;
}
+class EnumNameValue<string name, string value = "__default_enum_value__"> {
+ string Name = name;
+ string Value = value;
+}
+
+class Enum<string name, list<EnumNameValue> enumerations> : NamedType<name> {
+ list<EnumNameValue> Enumerations = enumerations;
+}
+
class PtrType<Type type> : Type {
Type PointeeType = type;
}
@@ -43,6 +52,11 @@ class Macro<string name> {
string Name = name;
}
+class EnumeratedNameValue<string name, string value = "__default__"> {
+ string Name = name;
+ string Value = value;
+}
+
class Annotation {}
class RetValSpec<Type type, list<Annotation> annotations = []> {
@@ -63,13 +77,15 @@ class FunctionSpec<string name, RetValSpec return, list<ArgSpec> args> {
}
class HeaderSpec<string name,
- list<Macro> macros,
- list<Type> types,
- list<FunctionSpec> functions> {
+ list<Macro> macros = [],
+ list<Type> types = [],
+ list<EnumeratedNameValue> enumerations = [],
+ list<FunctionSpec> functions = []> {
string Name = name;
list<FunctionSpec> Functions = functions;
list<Type> Types = types;
list<Macro> Macros = macros;
+ list<EnumeratedNameValue> Enumerations = enumerations;
}
class StandardSpec<string name> {
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 8278e9da6734..0eff0205d74a 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -8,6 +8,14 @@ def StdC : StandardSpec<"stdc"> {
RestrictedPtrType CharRestrictedPtr = RestrictedPtrType<CharType>;
ConstType ConstCharRestrictedPtr = ConstType<CharRestrictedPtr>;
+ NamedType MtxTType = NamedType<"mtx_t">;
+ PtrType MtxTTypePtr = PtrType<MtxTType>;
+ NamedType ThrdStartTType = NamedType<"thrd_start_t">;
+ NamedType ThrdTType = NamedType<"thrd_t">;
+ PtrType ThrdTTypePtr = PtrType<ThrdTType>;
+
+ PtrType IntPtr = PtrType<IntType>;
+
HeaderSpec String = HeaderSpec<
"string.h",
[
@@ -16,6 +24,7 @@ def StdC : StandardSpec<"stdc"> {
[
SizeTType,
],
+ [], // Enumerations
[
FunctionSpec<
"memcpy",
@@ -143,6 +152,7 @@ def StdC : StandardSpec<"stdc"> {
NamedType<"float_t">,
NamedType<"double_t">,
],
+ [], // Enumerations
[
FunctionSpec<"acos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"acosl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
@@ -155,6 +165,7 @@ def StdC : StandardSpec<"stdc"> {
[ // Types
SizeTType,
],
+ [], // Enumerations
[
FunctionSpec<
"snprintf",
@@ -174,9 +185,7 @@ def StdC : StandardSpec<"stdc"> {
Macro<"EDOM">,
Macro<"EILSEQ">,
Macro<"ERANGE">,
- ],
- [], // Types
- [] // Functions
+ ]
>;
HeaderSpec Signal = HeaderSpec<
@@ -197,16 +206,79 @@ def StdC : StandardSpec<"stdc"> {
NamedType<"sigset_t">,
SizeTType,
],
+ [], // Enumerations
[
FunctionSpec<"raise", RetValSpec<IntType>, [ArgSpec<IntType>]>,
]
>;
+ HeaderSpec Threads = HeaderSpec<
+ "threads.h",
+ [], // Macros
+ [
+ MtxTType,
+ ThrdStartTType,
+ ThrdTType,
+ ],
+ [
+ EnumeratedNameValue<"mtx_plain">,
+ EnumeratedNameValue<"mtx_recursive">,
+ EnumeratedNameValue<"mtx_timed">,
+ EnumeratedNameValue<"thrd_timedout">,
+ EnumeratedNameValue<"thrd_success">,
+ EnumeratedNameValue<"thrd_busy">,
+ EnumeratedNameValue<"thrd_error">,
+ EnumeratedNameValue<"thrd_nomem">,
+ ],
+ [
+ FunctionSpec<
+ "mtx_init",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<MtxTTypePtr>,
+ ArgSpec<IntType>,
+ ]
+ >,
+ FunctionSpec<
+ "mtx_lock",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<MtxTTypePtr>,
+ ]
+ >,
+ FunctionSpec<
+ "mtx_unlock",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<MtxTTypePtr>,
+ ]
+ >,
+ FunctionSpec<
+ "thrd_create",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<ThrdTTypePtr>,
+ ArgSpec<ThrdStartTType>,
+ ArgSpec<VoidPtr>,
+ ]
+ >,
+ FunctionSpec<
+ "thrd_join",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<ThrdTTypePtr>,
+ ArgSpec<IntPtr>,
+ ]
+ >
+ ]
+ >;
+
let Headers = [
Errno,
Math,
String,
StdIO,
Signal,
+ Threads,
];
}
diff --git a/libc/utils/HdrGen/PublicAPICommand.cpp b/libc/utils/HdrGen/PublicAPICommand.cpp
index c07283814550..15263e580c14 100644
--- a/libc/utils/HdrGen/PublicAPICommand.cpp
+++ b/libc/utils/HdrGen/PublicAPICommand.cpp
@@ -75,11 +75,13 @@ class APIGenerator {
// Mapping from names to records defining them.
NameToRecordMapping MacroSpecMap;
NameToRecordMapping TypeSpecMap;
+ NameToRecordMapping EnumerationSpecMap;
NameToRecordMapping FunctionSpecMap;
NameToRecordMapping MacroDefsMap;
NameToRecordMapping TypeDeclsMap;
NameSet Structs;
+ NameSet Enumerations;
NameSet Functions;
bool isaNamedType(llvm::Record *Def) { return isa(Def, NamedTypeClass); }
@@ -136,6 +138,13 @@ class APIGenerator {
FunctionSpecMap[std::string(FunctionSpec->getValueAsString("Name"))] =
FunctionSpec;
}
+
+ auto EnumerationSpecList =
+ HeaderSpec->getValueAsListOfDefs("Enumerations");
+ for (llvm::Record *EnumerationSpec : EnumerationSpecList) {
+ EnumerationSpecMap[std::string(
+ EnumerationSpec->getValueAsString("Name"))] = EnumerationSpec;
+ }
}
}
}
@@ -159,6 +168,10 @@ class APIGenerator {
auto FunctionList = PublicAPI->getValueAsListOfStrings("Functions");
for (llvm::StringRef FunctionName : FunctionList)
Functions.insert(std::string(FunctionName));
+
+ auto EnumerationList = PublicAPI->getValueAsListOfStrings("Enumerations");
+ for (llvm::StringRef EnumerationName : EnumerationList)
+ Enumerations.insert(std::string(EnumerationName));
}
void index(llvm::RecordKeeper &Records) {
@@ -211,6 +224,25 @@ class APIGenerator {
OS << '\n';
}
+ if (Enumerations.size() != 0)
+ OS << "enum {" << '\n';
+ for (const auto &Name : Enumerations) {
+ if (EnumerationSpecMap.find(Name) == EnumerationSpecMap.end())
+ llvm::PrintFatalError(
+ Name + " is not listed as an enumeration in any standard spec.\n");
+
+ llvm::Record *EnumerationSpec = EnumerationSpecMap[Name];
+ OS << " " << EnumerationSpec->getValueAsString("Name");
+ auto Value = EnumerationSpec->getValueAsString("Value");
+ if (Value == "__default__") {
+ OS << ",\n";
+ } else {
+ OS << " = " << Value << ",\n";
+ }
+ }
+ if (Enumerations.size() != 0)
+ OS << "};\n\n";
+
OS << "__BEGIN_C_DECLS\n\n";
for (auto &Name : Functions) {
if (FunctionSpecMap.find(Name) == FunctionSpecMap.end())
More information about the libc-commits
mailing list