Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement __has_declspec_attribute #514

Merged
merged 4 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -977,10 +977,11 @@ fn fromStringC2X(namespace: ?[]const u8, name: []const u8) ?Tag {
}

fn fromStringDeclspec(name: []const u8) ?Tag {
const normalized = normalize(name);
const decls = @typeInfo(attributes).Struct.decls;
inline for (decls, 0..) |decl, i| {
if (@hasDecl(@field(attributes, decl.name), "declspec")) {
if (mem.eql(u8, @field(attributes, decl.name).declspec, name)) {
if (mem.eql(u8, @field(attributes, decl.name).declspec, normalized)) {
return @enumFromInt(i);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/Preprocessor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ const builtin_macros = struct {
.id = .macro_param_has_attribute,
.source = .generated,
}};
const has_declspec_attribute = [1]RawToken{.{
.id = .macro_param_has_declspec_attribute,
.source = .generated,
}};
const has_warning = [1]RawToken{.{
.id = .macro_param_has_warning,
.source = .generated,
Expand Down Expand Up @@ -173,6 +177,7 @@ fn addBuiltinMacro(pp: *Preprocessor, name: []const u8, is_func: bool, tokens: [

pub fn addBuiltinMacros(pp: *Preprocessor) !void {
try pp.addBuiltinMacro("__has_attribute", true, &builtin_macros.has_attribute);
try pp.addBuiltinMacro("__has_declspec_attribute", true, &builtin_macros.has_declspec_attribute);
try pp.addBuiltinMacro("__has_warning", true, &builtin_macros.has_warning);
try pp.addBuiltinMacro("__has_feature", true, &builtin_macros.has_feature);
try pp.addBuiltinMacro("__has_extension", true, &builtin_macros.has_extension);
Expand Down Expand Up @@ -1212,6 +1217,7 @@ fn reconstructIncludeString(pp: *Preprocessor, param_toks: []const Token) !?[]co
fn handleBuiltinMacro(pp: *Preprocessor, builtin: RawToken.Id, param_toks: []const Token, src_loc: Source.Location) Error!bool {
switch (builtin) {
.macro_param_has_attribute,
.macro_param_has_declspec_attribute,
.macro_param_has_feature,
.macro_param_has_extension,
.macro_param_has_builtin,
Expand All @@ -1238,6 +1244,12 @@ fn handleBuiltinMacro(pp: *Preprocessor, builtin: RawToken.Id, param_toks: []con
const ident_str = pp.expandedSlice(identifier.?);
return switch (builtin) {
.macro_param_has_attribute => Attribute.fromString(.gnu, null, ident_str) != null,
.macro_param_has_declspec_attribute => {
return if (pp.comp.langopts.declspec_attrs)
Attribute.fromString(.declspec, null, ident_str) != null
else
false;
},
.macro_param_has_feature => features.hasFeature(pp.comp, ident_str),
.macro_param_has_extension => features.hasExtension(pp.comp, ident_str),
.macro_param_has_builtin => pp.comp.hasBuiltin(ident_str),
Expand Down Expand Up @@ -1396,6 +1408,7 @@ fn expandFuncMacro(
try buf.append(try pp.makeGeneratedToken(start, .string_literal, tokFromRaw(raw)));
},
.macro_param_has_attribute,
.macro_param_has_declspec_attribute,
.macro_param_has_warning,
.macro_param_has_feature,
.macro_param_has_extension,
Expand Down
3 changes: 3 additions & 0 deletions src/Tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub const Token = struct {
macro_ws,
/// Special token for implementing __has_attribute
macro_param_has_attribute,
/// Special token for implementing __has_declspec_attribute
macro_param_has_declspec_attribute,
/// Special token for implementing __has_warning
macro_param_has_warning,
/// Special token for implementing __has_feature
Expand Down Expand Up @@ -487,6 +489,7 @@ pub const Token = struct {
.stringify_param,
.stringify_va_args,
.macro_param_has_attribute,
.macro_param_has_declspec_attribute,
.macro_param_has_warning,
.macro_param_has_feature,
.macro_param_has_extension,
Expand Down
14 changes: 6 additions & 8 deletions test/cases/declspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ __declspec(aligned(16)) int baz;

_Static_assert(_Alignof(bar) == 16, "wrong alignment");

// #if __has_declspec_attribute(foo)
// #error fail
// #endif
#if __has_declspec_attribute(foo)
#error fail
#endif

// #if !__has_declspec_attribute(align)
// #error fail
// #endif
#if !__has_declspec_attribute(align)
#error fail
#endif

typedef int Int1 __declspec(align(8));
typedef __declspec(align(8)) int Int2;

_Static_assert(_Alignof(Int2) == 8, "");

#define TESTS_SKIPPED 2

#define EXPECTED_ERRORS "declspec.c:7:12: warning: __declspec attribute 'aligned' is not supported [-Wignored-attributes]" \
"declspec.c:19:18: error: 'declspec' attribute not allowed after declarator" \
"declspec.c:19:13: note: this declarator"
8 changes: 8 additions & 0 deletions test/cases/no declspec.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
__declspec(align(4)) int foo;

#if __has_declspec_attribute(noreturn)
#error "__has_declspec_attribute macro should not work without -fdeclspec"
#endif

#if __has_declspec_attribute(x)
#error "__has_declspec_attribute macro should not work without -fdeclspec"
#endif

#define EXPECTED_ERRORS \
"no declspec.c:1:1: error: '__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes" \
Loading