Skip to content

Commit

Permalink
Merge pull request #24 from ehaas/predefined-macros
Browse files Browse the repository at this point in the history
Update predefined macros
  • Loading branch information
Vexu authored Sep 16, 2021
2 parents b1a7d5d + 8afe7c3 commit f69069d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const assert = std.debug.assert;
const mem = std.mem;
const Allocator = mem.Allocator;
const EpochSeconds = std.time.epoch.EpochSeconds;
const Diagnostics = @import("Diagnostics.zig");
const LangOpts = @import("LangOpts.zig");
const Preprocessor = @import("Preprocessor.zig");
Expand Down Expand Up @@ -52,6 +53,31 @@ pub fn deinit(comp: *Compilation) void {
if (comp.builtin_header_path) |some| comp.gpa.free(some);
}

fn generateDateAndTime(w: anytype) !void {
const timestamp = std.math.clamp(std.time.timestamp(), 0, std.math.maxInt(i64));
const epoch_seconds = EpochSeconds{ .secs = @intCast(u64, timestamp) };
const epoch_day = epoch_seconds.getEpochDay();
const day_seconds = epoch_seconds.getDaySeconds();
const year_day = epoch_day.calculateYearDay();

const month_day = year_day.calculateMonthDay();

const MonthNames = [_][]const u8{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
std.debug.assert(std.time.epoch.Month.jan.numeric() == 1);

const monthName = MonthNames[month_day.month.numeric() - 1];
try w.print("#define __DATE__ \"{s} {d: >2} {d}\"\n", .{
monthName,
month_day.day_index + 1,
year_day.year,
});
try w.print("#define __TIME__ \"{d:0>2}:{d:0>2}:{d:0>2}\"\n", .{
day_seconds.getHoursIntoDay(),
day_seconds.getMinutesIntoHour(),
day_seconds.getSecondsIntoMinute(),
});
}

/// Generate builtin macros that will be available to each source file.
pub fn generateBuiltinMacros(comp: *Compilation) !Source {
var buf = std.ArrayList(u8).init(comp.gpa);
Expand All @@ -62,13 +88,17 @@ pub fn generateBuiltinMacros(comp: *Compilation) !Source {
++ @import("lib.zig").version_str ++ "\"\n" ++
\\#define __STDC__ 1
\\#define __STDC_HOSTED__ 1
\\#define __STDC_VERSION__ 201710L
\\#define __STDC_NO_ATOMICS__ 1
\\#define __STDC_NO_COMPLEX__ 1
\\#define __STDC_NO_THREADS__ 1
\\#define __STDC_NO_VLA__ 1
\\
);
try generateDateAndTime(buf.writer());

if (comp.langopts.standard.StdCVersionMacro()) |stdc_version| {
try buf.writer().print("#define __STDC_VERSION__ {s}\n", .{stdc_version});
}

switch (comp.target.os.tag) {
.linux => try buf.appendSlice(
Expand Down
13 changes: 13 additions & 0 deletions src/LangOpts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ const Standard = enum {
pub fn isExplicitGNU(standard: Standard) bool {
return standard.isGNU() and standard != .default;
}

/// Value reported by __STDC_VERSION__ macro
pub fn StdCVersionMacro(standard: Standard) ?[]const u8 {
return switch (standard) {
.c89, .gnu89 => null,
.iso9899 => "199409L",
.c99, .gnu99 => "199901L",
.c11, .gnu11 => "201112L",
.default, .c17, .gnu17 => "201710L",
// todo: update once finalized; this currently matches clang
.c2x, .gnu2x => "201710L",
};
}
};

standard: Standard = .default,
Expand Down
6 changes: 6 additions & 0 deletions test/cases/predefined macros.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//std=c99
void foo(void) {
_Static_assert(__STDC_VERSION__ == 199901, "__STDC_VERSION__ is incorrect");
(void)__DATE__;
(void)__TIME__;
}
8 changes: 7 additions & 1 deletion test/runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ pub fn main() !void {

try comp.defineSystemIncludes();

const builtin_macros = try comp.generateBuiltinMacros();
const test_runner_macros = blk: {
const duped_path = try gpa.dupe(u8, "<test_runner>");
errdefer comp.gpa.free(duped_path);
Expand Down Expand Up @@ -108,6 +107,13 @@ pub fn main() !void {
}
}

const builtin_macros = try comp.generateBuiltinMacros();
defer {
_ = comp.sources.swapRemove(builtin_macros.path);
gpa.free(builtin_macros.path);
gpa.free(builtin_macros.buf);
}

const case = std.mem.sliceTo(std.fs.path.basename(path), '.');
var case_node = root_node.start(case, 0);
case_node.activate();
Expand Down

0 comments on commit f69069d

Please sign in to comment.