@@ -111,6 +111,17 @@ void writeAPIFromIndex(APIIndexer &G,
111
111
return AttributeStyle::Declspec;
112
112
};
113
113
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
+
114
125
for (auto &[Macro, Attr] : MacroAttr) {
115
126
auto Instances = Attr->getValueAsListOfDefs (" Instances" );
116
127
llvm::SmallVector<std::pair<AttributeStyle, llvm::Record *>> Styles;
@@ -122,14 +133,26 @@ void writeAPIFromIndex(APIIndexer &G,
122
133
return {Style, Instance};
123
134
});
124
135
// 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
+ });
126
141
for (auto &[Style, Instance] : Styles) {
127
142
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 ' ;
129
151
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::" ;
133
156
OS << Instance->getValueAsString (" Attr" ) << " ]]\n " ;
134
157
OS << " #endif\n " ;
135
158
}
0 commit comments