Skip to content

Commit 2b37803

Browse files
adjust logic
1 parent 03a5fbc commit 2b37803

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

libc/spec/spec.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class FunctionAttr<string style, string attr> {
183183
}
184184
class GnuFunctionAttr<string attr> : FunctionAttr<"gnu", attr> {}
185185
class Cxx11FunctionAttr<string attr, string namespace> : FunctionAttr<"cxx11", attr> {
186-
// The namespace of the attribute, e.g. "gnu" or "clang".
186+
// The namespace of the attribute, e.g. "gnu" or "clang". Empty string means there is no namespace.
187187
string Namespace = namespace;
188188
}
189189
class DeclspecFunctionAttr<string attr> : FunctionAttr<"declspec", attr> {}

libc/utils/HdrGen/PublicAPICommand.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ static void emitAttributeMacroDecls(const AttributeMap &MacroAttr,
122122
});
123123
// 1. If __cplusplus is defined and cxx11 style is provided, define the
124124
// macro using cxx11 version with the following priority:
125-
// 1a. If the attribute is a clang attribute, check for __clang__.
126-
// 2b. If the attribute is a gnu attribute, check for __GNUC__.
125+
// 1a. If there is no namespace (so the macro is supposed to be
126+
// compiler-independent),
127+
// use this version first.
128+
// 1b. If the attribute is a clang attribute, check for __clang__.
129+
// 1c. If the attribute is a gnu attribute, check for __GNUC__.
127130
// 2. Otherwise, if __GNUC__ is defined and gnu
128131
// style is provided, define the macro using gnu version;
129132
// 3. Otherwise, if _MSC_VER is defined and __declspec is provided, define
@@ -139,30 +142,35 @@ static void emitAttributeMacroDecls(const AttributeMap &MacroAttr,
139142
llvm::StringRef Attr = Instance->getValueAsString("Attr");
140143
if (Style == AttributeStyle::Cxx11) {
141144
OS << "#if !defined(" << Macro << ") && defined(__cplusplus)";
142-
if (isAsciiIdentifier(Attr))
143-
OS << " && __has_attribute(" << Attr << ")";
144145
AttributeNamespace Namespace = getAttributeNamespace(Instance);
145146
if (Namespace == AttributeNamespace::Clang)
146147
OS << " && defined(__clang__)\n";
147148
else if (Namespace == AttributeNamespace::Gnu)
148149
OS << " && defined(__GNUC__)\n";
149150
else
150151
OS << '\n';
152+
if (isAsciiIdentifier(Attr) && Namespace != AttributeNamespace::None)
153+
OS << "#if __has_attribute(" << Attr << ")\n";
154+
else
155+
OS << "#if __has_cpp_attribute(" << Attr << ")\n";
151156
OS << "#define " << Macro << " [[";
152157
if (Namespace == AttributeNamespace::Clang)
153158
OS << "clang::";
154159
else if (Namespace == AttributeNamespace::Gnu)
155160
OS << "gnu::";
156161
OS << Attr << "]]\n";
162+
if (isAsciiIdentifier(Attr))
163+
OS << "#endif\n";
157164
OS << "#endif\n";
158165
}
159166
if (Style == AttributeStyle::Gnu) {
160-
OS << "#if !defined(" << Macro << ") && defined(__GNUC__)";
167+
OS << "#if !defined(" << Macro << ") && defined(__GNUC__)\n";
161168
if (isAsciiIdentifier(Attr))
162-
OS << " && __has_attribute(" << Attr << ")";
163-
OS << '\n';
169+
OS << "#if __has_attribute(" << Attr << ")\n";
164170
OS << "#define " << Macro << " __attribute__((";
165171
OS << Attr << "))\n";
172+
if (isAsciiIdentifier(Attr))
173+
OS << "#endif\n";
166174
OS << "#endif\n";
167175
}
168176
if (Style == AttributeStyle::Declspec) {

0 commit comments

Comments
 (0)