Skip to content

Commit 126630e

Browse files
add checks for namespaces
1 parent 9eef290 commit 126630e

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

libc/utils/HdrGen/PublicAPICommand.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ void writeAPIFromIndex(APIIndexer &G,
111111
return AttributeStyle::Declspec;
112112
};
113113

114+
auto GetNamespace = [](llvm::Record *Instance) {
115+
auto Namespace = Instance->getValueAsString("Namespace");
116+
// Empty namespace is likely to be most standard-compliant.
117+
if (Namespace == "")
118+
return AttributeNamespace::None;
119+
// Dispatch clang version before gnu version.
120+
if (Namespace == "clang")
121+
return AttributeNamespace::Clang;
122+
return AttributeNamespace::Gnu;
123+
};
124+
114125
for (auto &[Macro, Attr] : MacroAttr) {
115126
auto Instances = Attr->getValueAsListOfDefs("Instances");
116127
llvm::SmallVector<std::pair<AttributeStyle, llvm::Record *>> Styles;
@@ -122,14 +133,26 @@ void writeAPIFromIndex(APIIndexer &G,
122133
return {Style, Instance};
123134
});
124135
// Effectively sort on the first field
125-
std::sort(Styles.begin(), Styles.end());
136+
std::sort(Styles.begin(), Styles.end(), [&](auto &a, auto &b) {
137+
if (a.first == AttributeStyle::Cxx11 && b.first == AttributeStyle::Cxx11)
138+
return GetNamespace(a.second) < GetNamespace(b.second);
139+
return a.first < b.first;
140+
});
126141
for (auto &[Style, Instance] : Styles) {
127142
if (Style == AttributeStyle::Cxx11) {
128-
OS << "#if !defined(" << Macro << ") && defined(__cplusplus)\n";
143+
OS << "#if !defined(" << Macro << ") && defined(__cplusplus)";
144+
auto Namespace = GetNamespace(Instance);
145+
if (Namespace == AttributeNamespace::Clang)
146+
OS << " && defined(__clang__)\n";
147+
else if (Namespace == AttributeNamespace::Gnu)
148+
OS << " && defined(__GNUC__)\n";
149+
else
150+
OS << '\n';
129151
OS << "#define " << Macro << " [[";
130-
auto Namespace = Instance->getValueAsString("Namespace");
131-
if (Namespace != "")
132-
OS << Namespace << "::";
152+
if (Namespace == AttributeNamespace::Clang)
153+
OS << "clang::";
154+
else if (Namespace == AttributeNamespace::Gnu)
155+
OS << "gnu::";
133156
OS << Instance->getValueAsString("Attr") << "]]\n";
134157
OS << "#endif\n";
135158
}

libc/utils/HdrGen/PublicAPICommand.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class RecordKeeper;
2626
namespace llvm_libc {
2727

2828
enum class AttributeStyle { Cxx11 = 0, Gnu = 1, Declspec = 2 };
29+
enum class AttributeNamespace { None = 0, Clang = 1, Gnu = 2 };
2930

3031
class PublicAPICommand : public Command {
3132
private:

0 commit comments

Comments
 (0)