[libc-commits] [libc] 59eb542 - [libc] Let header generator generate the type header inclusion boiler plate.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Mon Jan 24 15:25:28 PST 2022


Author: Siva Chandra Reddy
Date: 2022-01-24T23:25:19Z
New Revision: 59eb542f6070a017f0d407df908aa53639feb0ea

URL: https://github.com/llvm/llvm-project/commit/59eb542f6070a017f0d407df908aa53639feb0ea
DIFF: https://github.com/llvm/llvm-project/commit/59eb542f6070a017f0d407df908aa53639feb0ea.diff

LOG: [libc] Let header generator generate the type header inclusion boiler plate.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D117855

Added: 
    

Modified: 
    libc/config/linux/api.td
    libc/config/public_api.td
    libc/utils/HdrGen/PublicAPICommand.cpp
    libc/utils/LibcTableGenUtil/APIIndexer.cpp
    libc/utils/LibcTableGenUtil/APIIndexer.h

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 48cd1c1113485..77e22287fe765 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -6,46 +6,6 @@ include "spec/llvm_libc_ext.td"
 include "spec/posix.td"
 include "spec/stdc.td"
 
-// TODO: Eliminate all TypeDecl specializations. Since we define all public
-// types in their own self contained header files, the header generator can
-// produce the boiler plate which pulls in the type definitions.
-
-def SizeT : TypeDecl<"size_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/size_t.h>
-  }];
-}
-
-def SSizeT : TypeDecl<"ssize_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/ssize_t.h>
-  }];
-}
-
-def StructTm: TypeDecl<"struct tm"> {
-  let Decl = [{
-    #include <llvm-libc-types/struct_tm.h>
-  }];
-}
-
-def TimeT: TypeDecl<"time_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/time_t.h>
-  }];
-}
-
-def OffT : TypeDecl<"off_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/off_t.h>
-  }];
-}
-
-def FILE : TypeDecl<"FILE"> {
-  let Decl = [{
-    #include <llvm-libc-types/FILE.h>
-  }];
-}
-
 def AssertMacro : MacroDef<"assert"> {
   let Defn = [{
     #undef assert
@@ -102,16 +62,8 @@ def AssertAPI : PublicAPI<"assert.h"> {
 def CTypeAPI : PublicAPI<"ctype.h"> {
 }
 
-def IMaxDivT : TypeDecl<"imaxdiv_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/imaxdiv_t.h>
-  }];
-}
-
 def IntTypesAPI : PublicAPI<"inttypes.h"> {
-  let TypeDeclarations = [
-    IMaxDivT,
-  ];
+  let Types = ["imaxdiv_t"];
 }
 
 def MathErrHandlingMacro : MacroDef<"math_errhandling"> {
@@ -146,18 +98,6 @@ def IsNanMacro : MacroDef<"isnan"> {
   }];
 }
 
-def FloatT : TypeDecl<"float_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/float_t.h>
-  }];
-}
-
-def DoubleT : TypeDecl<"double_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/double_t.h>
-  }];
-}
-
 def MathAPI : PublicAPI<"math.h"> {
   let Macros = [
     SimpleMacroDef<"MATH_ERRNO", "1">,
@@ -174,22 +114,7 @@ def MathAPI : PublicAPI<"math.h"> {
     IsInfMacro,
     IsNanMacro,
   ];
-  let TypeDeclarations = [
-    DoubleT,
-    FloatT,
-  ];
-}
-
-def FEnvT : TypeDecl<"fenv_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/fenv_t.h>
-  }];
-}
-
-def FExceptT : TypeDecl<"fexcept_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/fexcept_t.h>
-  }];
+  let Types = ["double_t", "float_t"];
 }
 
 def FenvAPI: PublicAPI<"fenv.h"> {
@@ -208,16 +133,11 @@ def FenvAPI: PublicAPI<"fenv.h"> {
 
     SimpleMacroDef<"FE_DFL_ENV", "((fenv_t *)-1)">,
   ];
-  let TypeDeclarations = [
-    FEnvT,
-    FExceptT,
-  ];
+  let Types = ["fenv_t", "fexcept_t"];
 }
 
 def StringAPI : PublicAPI<"string.h"> {
-  let TypeDeclarations = [
-    SizeT,
-  ];
+  let Types = ["size_t"];
 
   let Macros = [
     NullMacro,
@@ -225,66 +145,22 @@ def StringAPI : PublicAPI<"string.h"> {
 }
 
 def StdIOAPI : PublicAPI<"stdio.h"> {
-  let TypeDeclarations = [
-    SizeT,
-    FILE,
-  ];
-}
-
-def DivT : TypeDecl<"div_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/div_t.h>
-  }];
-}
-
-def LDivT : TypeDecl<"ldiv_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/ldiv_t.h>
-  }];
-}
-
-def LLDivT : TypeDecl<"lldiv_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/lldiv_t.h>
-  }];
-}
-
-def BSearchCompareTDefn : TypeDecl<"__bsearchcompare_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/__bsearchcompare_t.h>
-  }];
-}
-
-def QSortCompareTDefn : TypeDecl<"__qsortcompare_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/__qsortcompare_t.h>
-  }];
+  let Types = ["size_t", "FILE"];
 }
 
 def StdlibAPI : PublicAPI<"stdlib.h"> {
-  let TypeDeclarations = [
-    DivT,
-    LDivT,
-    LLDivT,
-    SizeT,
-    BSearchCompareTDefn,
-    QSortCompareTDefn,
+  let Types = [
+    "div_t",
+    "ldiv_t",
+    "lldiv_t",
+    "size_t",
+    "__bsearchcompare_t",
+    "__qsortcompare_t"
   ];
 }
 
 def TimeAPI : PublicAPI<"time.h"> {
-  let TypeDeclarations = [
-    StructTm,
-    TimeT,
-  ];
-
-  let Functions = [
-    "asctime",
-    "asctime_r",
-    "gmtime",
-    "gmtime_r",
-    "mktime",
-  ];
+  let Types = ["time_t", "struct tm"];
 }
 
 def ErrnoAPI : PublicAPI<"errno.h"> {
@@ -307,6 +183,7 @@ def ErrnoAPI : PublicAPI<"errno.h"> {
 }
 
 def SysMManAPI : PublicAPI<"sys/mman.h"> {
+  let Types = ["off_t", "size_t"];
   let Macros = [
     SimpleMacroDef<"PROT_NONE", "0">,
     SimpleMacroDef<"PROT_READ", "1">,
@@ -326,65 +203,10 @@ def SysMManAPI : PublicAPI<"sys/mman.h"> {
     // TODO: Add other MAP_* macros used by Linux.
   ];
 
-  let TypeDeclarations = [
-    SizeT,
-    OffT,
-  ];
-}
-
-def StructSigactionDefn : TypeDecl<"struct sigaction"> {
-  let Decl = [{
-    #include <llvm-libc-types/struct_sigaction.h>
-  }];
-}
-
-def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/__sighandler_t.h>
-  }];
 }
 
 def SignalAPI : PublicAPI<"signal.h"> {
-  let TypeDeclarations = [
-    StructSigactionDefn,
-    SighandlerTDefn,
-  ];
-}
-
-def OnceFlag : TypeDecl<"once_flag"> {
-  let Decl = [{
-    #include <llvm-libc-types/once_flag.h>
-  }];
-}
-
-def MtxT : TypeDecl<"mtx_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/mtx_t.h>
-  }];
-}
-
-def CndT : TypeDecl<"cnd_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/cnd_t.h>
-  }];
-}
-
-def ThrdT : TypeDecl<"thrd_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/thrd_t.h>
-  }];
-}
-
-def ThreadStartT : TypeDecl<"thrd_start_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/thrd_start_t.h>
-  }];
-}
-
-def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
-  let Decl = [{
-    #include <llvm-libc-types/__call_once_func_t.h>
-  }];
+  let Types = ["struct sigaction", "__sighandler_t"];
 }
 
 def ThreadsAPI : PublicAPI<"threads.h"> {
@@ -392,13 +214,13 @@ def ThreadsAPI : PublicAPI<"threads.h"> {
     SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
   ];
 
-  let TypeDeclarations = [
-    OnceFlag,
-    CallOnceFuncT,
-    MtxT,
-    CndT,
-    ThrdT,
-    ThreadStartT,
+  let Types = [
+    "__call_once_func_t",
+    "once_flag",
+    "cnd_t",
+    "mtx_t",
+    "thrd_t",
+    "thrd_start_t",
   ];
 
   let Enumerations = [
@@ -414,8 +236,5 @@ def ThreadsAPI : PublicAPI<"threads.h"> {
 }
 
 def UniStdAPI : PublicAPI<"unistd.h"> {
-  let TypeDeclarations = [
-    SSizeT,
-    SizeT,
-  ];
+  let Types = ["size_t", "ssize_t"];
 }

diff  --git a/libc/config/public_api.td b/libc/config/public_api.td
index b60836901836a..6d2f534f5a593 100644
--- a/libc/config/public_api.td
+++ b/libc/config/public_api.td
@@ -1,10 +1,5 @@
 include "spec/spec.td"
 
-class TypeDecl<string name> {
-  string Name = name;
-  string Decl = "";
-}
-
 class MacroDef<string name> {
   string Name = name;
   string Defn = "";
@@ -23,7 +18,7 @@ class MacroDefineIfNot<string name, string value> : MacroDef<name> {
 class PublicAPI<string name> {
   string HeaderName = name;
   list<MacroDef> Macros = [];
-  list<TypeDecl> TypeDeclarations = [];
+  list<string> Types = [];
   list<string> Enumerations = [];
   list<string> Structs = [];
   list<string> Functions = [];

diff  --git a/libc/utils/HdrGen/PublicAPICommand.cpp b/libc/utils/HdrGen/PublicAPICommand.cpp
index 5285b0daac7d5..8f036f7a6d56e 100644
--- a/libc/utils/HdrGen/PublicAPICommand.cpp
+++ b/libc/utils/HdrGen/PublicAPICommand.cpp
@@ -10,6 +10,7 @@
 
 #include "utils/LibcTableGenUtil/APIIndexer.h"
 
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/SourceMgr.h"
@@ -38,6 +39,12 @@ static void dedentAndWrite(llvm::StringRef Text, llvm::raw_ostream &OS) {
   }
 }
 
+static std::string getTypeHdrName(const std::string &Name) {
+  llvm::SmallVector<llvm::StringRef> Parts;
+  llvm::SplitString(llvm::StringRef(Name), Parts);
+  return llvm::join(Parts.begin(), Parts.end(), "_");
+}
+
 namespace llvm_libc {
 
 void writeAPIFromIndex(APIIndexer &G,
@@ -54,16 +61,12 @@ void writeAPIFromIndex(APIIndexer &G,
     OS << '\n';
   }
 
-  for (auto &Pair : G.TypeDeclsMap) {
-    const std::string &Name = Pair.first;
-    if (G.TypeSpecMap.find(Name) == G.TypeSpecMap.end())
-      llvm::PrintFatalError(Name + " not found in any standard spec.\n");
-
-    llvm::Record *TypeDecl = Pair.second;
-    dedentAndWrite(TypeDecl->getValueAsString("Decl"), OS);
-
-    OS << '\n';
+  for (auto &TypeName : G.RequiredTypes) {
+    if (G.TypeSpecMap.find(TypeName) == G.TypeSpecMap.end())
+      llvm::PrintFatalError(TypeName + " not found in any standard spec.\n");
+    OS << "#include <llvm-libc-types/" << getTypeHdrName(TypeName) << ".h>\n";
   }
+  OS << '\n';
 
   if (G.Enumerations.size() != 0)
     OS << "enum {" << '\n';

diff  --git a/libc/utils/LibcTableGenUtil/APIIndexer.cpp b/libc/utils/LibcTableGenUtil/APIIndexer.cpp
index 16aef5880bffe..fd3f53c4150fc 100644
--- a/libc/utils/LibcTableGenUtil/APIIndexer.cpp
+++ b/libc/utils/LibcTableGenUtil/APIIndexer.cpp
@@ -120,9 +120,9 @@ void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
   for (llvm::Record *MacroDef : MacroDefList)
     MacroDefsMap[std::string(MacroDef->getValueAsString("Name"))] = MacroDef;
 
-  auto TypeDeclList = PublicAPI->getValueAsListOfDefs("TypeDeclarations");
-  for (llvm::Record *TypeDecl : TypeDeclList)
-    TypeDeclsMap[std::string(TypeDecl->getValueAsString("Name"))] = TypeDecl;
+  auto TypeList = PublicAPI->getValueAsListOfStrings("Types");
+  for (llvm::StringRef TypeName : TypeList)
+    RequiredTypes.insert(std::string(TypeName));
 
   auto StructList = PublicAPI->getValueAsListOfStrings("Structs");
   for (llvm::StringRef StructName : StructList)

diff  --git a/libc/utils/LibcTableGenUtil/APIIndexer.h b/libc/utils/LibcTableGenUtil/APIIndexer.h
index 7b4d62a38c615..cbb7ac2e66dcf 100644
--- a/libc/utils/LibcTableGenUtil/APIIndexer.h
+++ b/libc/utils/LibcTableGenUtil/APIIndexer.h
@@ -63,10 +63,10 @@ class APIIndexer {
   NameToRecordMapping EnumerationSpecMap;
   NameToRecordMapping FunctionSpecMap;
   NameToRecordMapping MacroDefsMap;
-  NameToRecordMapping TypeDeclsMap;
 
   std::unordered_map<std::string, std::string> FunctionToHeaderMap;
 
+  NameSet RequiredTypes;
   NameSet Structs;
   NameSet Enumerations;
   NameSet Functions;


        


More information about the libc-commits mailing list