|
15 | 15 | #include "llvm/ADT/StringRef.h"
|
16 | 16 | #include "llvm/Support/SourceMgr.h"
|
17 | 17 | #include "llvm/TableGen/Record.h"
|
| 18 | +#include <llvm/ADT/STLExtras.h> |
18 | 19 |
|
19 | 20 | // Text blocks for macro definitions and type decls can be indented to
|
20 | 21 | // suit the surrounding tablegen listing. We need to dedent such blocks
|
@@ -49,7 +50,7 @@ namespace llvm_libc {
|
49 | 50 |
|
50 | 51 | void writeAPIFromIndex(APIIndexer &G,
|
51 | 52 | std::vector<std::string> EntrypointNameList,
|
52 |
| - llvm::raw_ostream &OS) { |
| 53 | + llvm::raw_ostream &OS, AttributeStyle PreferedStyle) { |
53 | 54 | for (auto &Pair : G.MacroDefsMap) {
|
54 | 55 | const std::string &Name = Pair.first;
|
55 | 56 | if (G.MacroSpecMap.find(Name) == G.MacroSpecMap.end())
|
@@ -102,6 +103,62 @@ void writeAPIFromIndex(APIIndexer &G,
|
102 | 103 | llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
|
103 | 104 | llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
|
104 | 105 |
|
| 106 | + auto GetStyle = [](llvm::Record *Instance) { |
| 107 | + auto Style = Instance->getValueAsString("Style"); |
| 108 | + if (Style == "gnu") |
| 109 | + return AttributeStyle::Gnu; |
| 110 | + if (Style == "c23") |
| 111 | + return AttributeStyle::C23; |
| 112 | + if (Style == "declspec") |
| 113 | + return AttributeStyle::Declspec; |
| 114 | + return AttributeStyle::None; |
| 115 | + }; |
| 116 | + |
| 117 | + if (PreferedStyle != AttributeStyle::None) { |
| 118 | + auto Attributes = FunctionSpec->getValueAsListOfDefs("Attributes"); |
| 119 | + llvm::SmallVector<llvm::Record *> Attrs; |
| 120 | + for (auto *Attr : Attributes) { |
| 121 | + auto Instances = Attr->getValueAsListOfDefs("Instances"); |
| 122 | + for (auto *Instance : Instances) { |
| 123 | + if (GetStyle(Instance) == PreferedStyle) { |
| 124 | + Attrs.push_back(Instance); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (Attrs.size() != 0) { |
| 130 | + if (PreferedStyle == AttributeStyle::Gnu) { |
| 131 | + OS << "__attribute__(("; |
| 132 | + llvm::interleaveComma(Attrs, OS, [&](llvm::Record *Instance) { |
| 133 | + OS << Instance->getValueAsString("Attr"); |
| 134 | + }); |
| 135 | + OS << ")) "; |
| 136 | + } |
| 137 | + |
| 138 | + if (PreferedStyle == AttributeStyle::C23) { |
| 139 | + OS << "__attribute__(("; |
| 140 | + llvm::interleaveComma(Attrs, OS, [&](llvm::Record *Instance) { |
| 141 | + auto Namespace = Instance->getValueAsString("Namespace"); |
| 142 | + if (Namespace != "") |
| 143 | + OS << Namespace << "::"; |
| 144 | + OS << Instance->getValueAsString("Attr"); |
| 145 | + }); |
| 146 | + OS << ")) "; |
| 147 | + } |
| 148 | + |
| 149 | + if (PreferedStyle == AttributeStyle::Declspec) { |
| 150 | + OS << "__declspec("; |
| 151 | + llvm::interleave( |
| 152 | + Attrs.begin(), Attrs.end(), |
| 153 | + [&](llvm::Record *Instance) { |
| 154 | + OS << Instance->getValueAsString("Attr"); |
| 155 | + }, |
| 156 | + [&]() { OS << ' '; }); |
| 157 | + OS << ") "; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
105 | 162 | OS << G.getTypeAsString(ReturnType) << " " << Name << "(";
|
106 | 163 |
|
107 | 164 | auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
|
@@ -134,12 +191,24 @@ void PublicAPICommand::run(llvm::raw_ostream &OS, const ArgVector &Args,
|
134 | 191 | llvm::StringRef StdHeader,
|
135 | 192 | llvm::RecordKeeper &Records,
|
136 | 193 | const Command::ErrorReporter &Reporter) const {
|
137 |
| - if (Args.size() != 0) { |
138 |
| - Reporter.printFatalError("public_api command does not take any arguments."); |
| 194 | + if (Args.size() > 1) { |
| 195 | + Reporter.printFatalError( |
| 196 | + "public_api command does not take more than one arguments."); |
| 197 | + } |
| 198 | + |
| 199 | + AttributeStyle PreferedStyle = AttributeStyle::Gnu; |
| 200 | + |
| 201 | + for (auto &arg : Args) { |
| 202 | + if (arg == "prefer-c23-attributes") { |
| 203 | + PreferedStyle = AttributeStyle::C23; |
| 204 | + } |
| 205 | + if (arg == "prefer-no-attributes") { |
| 206 | + PreferedStyle = AttributeStyle::None; |
| 207 | + } |
139 | 208 | }
|
140 | 209 |
|
141 | 210 | APIIndexer G(StdHeader, Records);
|
142 |
| - writeAPIFromIndex(G, EntrypointNameList, OS); |
| 211 | + writeAPIFromIndex(G, EntrypointNameList, OS, PreferedStyle); |
143 | 212 | }
|
144 | 213 |
|
145 | 214 | } // namespace llvm_libc
|
0 commit comments