Skip to content

Commit

Permalink
driver: remove unsecessary isOneof function
Browse files Browse the repository at this point in the history
update `getPICMode` comment
  • Loading branch information
Organ1sm committed Sep 27, 2024
1 parent e702d15 commit 7297256
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions src/aro/Driver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ pub const Linker = enum {

const Driver = @This();

const pic_related_options = StaticStringSet.initComptime(.{
.{"-fpic"},
.{"-fno-pic"},
.{"-fPIC"},
.{"-fno-PIC"},
.{"-fpie"},
.{"-fno-pie"},
.{"-fPIE"},
.{"-fno-PIE"},
});

comp: *Compilation,
inputs: std.ArrayListUnmanaged(Source) = .{},
link_objects: std.ArrayListUnmanaged([]const u8) = .{},
Expand Down Expand Up @@ -308,16 +319,6 @@ pub fn parseArgs(
d.comp.langopts.use_native_half_type = true;
} else if (mem.eql(u8, arg, "-fnative-half-arguments-and-returns")) {
d.comp.langopts.allow_half_args_and_returns = true;
} else if (mem.eql(u8, arg, "-fno-pic") or mem.eql(u8, arg, "-fno-PIC") or mem.eql(u8, arg, "-fno-pie") or mem.eql(u8, arg, "-fno-PIE")) {
//
} else if (mem.eql(u8, arg, "-fpic")) {
//
} else if (mem.eql(u8, arg, "-fPIC")) {
//
} else if (mem.eql(u8, arg, "-fpie")) {
//
} else if (mem.eql(u8, arg, "-fPIE")) {
//
} else if (mem.eql(u8, arg, "-fshort-enums")) {
d.comp.langopts.short_enums = true;
} else if (mem.eql(u8, arg, "-fno-short-enums")) {
Expand Down Expand Up @@ -500,7 +501,8 @@ pub fn parseArgs(
try d.comp.addDiagnostic(.{ .tag = .invalid_unwindlib, .extra = .{ .str = unwindlib } }, &.{});
}
} else {
try d.comp.addDiagnostic(.{ .tag = .cli_unknown_arg, .extra = .{ .str = arg } }, &.{});
if (!pic_related_options.has(arg))
try d.comp.addDiagnostic(.{ .tag = .cli_unknown_arg, .extra = .{ .str = arg } }, &.{});
}
} else if (std.mem.endsWith(u8, arg, ".o") or std.mem.endsWith(u8, arg, ".obj")) {
try d.link_objects.append(d.comp.gpa, arg);
Expand Down Expand Up @@ -898,7 +900,10 @@ fn exitWithCleanup(d: *Driver, code: u8) noreturn {
std.process.exit(code);
}

/// This currently just returns the desired settings without considering target defaults / requirements
/// Parses the various -fpic/-fPIC/-fpie/-fPIE arguments.
/// Then, smooshes them together with platform defaults, to decide whether
/// this compile should be using PIC mode or not.
/// Returns a tuple of ( backend.CodeGenOptions.PicLevel, IsPIE).
pub fn getPICMode(d: *Driver, args: []const []const u8) !struct { backend.CodeGenOptions.PicLevel, bool } {
const eqlIgnoreCase = std.ascii.eqlIgnoreCase;

Expand Down Expand Up @@ -975,16 +980,7 @@ pub fn getPICMode(d: *Driver, args: []const []const u8) !struct { backend.CodeGe
// other argument is used. If the last argument is any flavor of the
// '-fno-...' arguments, both PIC and PIE are disabled. Any PIE
// option implicitly enables PIC at the same level.
const lastpic_arg_idx = getLastArg(args, StaticStringSet.initComptime(.{
.{"-fpic"},
.{"-fno-pic"},
.{"-fPIC"},
.{"-fno-PIC"},
.{"-fpie"},
.{"-fno-pie"},
.{"-fPIE"},
.{"-fno-PIE"},
}));
const lastpic_arg_idx = getLastArg(args, pic_related_options);
if (target.os.tag == .windows and
!target_util.isCygwinMinGW(target) and
lastpic_arg_idx != null and
Expand All @@ -1006,10 +1002,10 @@ pub fn getPICMode(d: *Driver, args: []const []const u8) !struct { backend.CodeGe
if (!forced) {
if (lastpic_arg_idx) |idx| {
const arg = args[idx];
if (isOneOf(arg, StaticStringSet.initComptime(.{ .{"-fPIC"}, .{"-fpic"}, .{"-fpie"}, .{"-fPIE"} }))) {
pie = isOneOf(arg, StaticStringSet.initComptime(.{ .{"-fpie"}, .{"-fPIE"} }));
pic = pie or isOneOf(arg, StaticStringSet.initComptime(.{ .{"-fpic"}, .{"-fPIC"} }));
is_piclevel_two = isOneOf(arg, StaticStringSet.initComptime(.{ .{"-fPIE"}, .{"-fPIC"} }));
if (StaticStringSet.initComptime(.{ .{"-fPIC"}, .{"-fpic"}, .{"-fpie"}, .{"-fPIE"} }).has(arg)) {
pie = StaticStringSet.initComptime(.{ .{"-fpie"}, .{"-fPIE"} }).has(arg);
pic = pie or StaticStringSet.initComptime(.{ .{"-fpic"}, .{"-fPIC"} }).has(arg);
is_piclevel_two = StaticStringSet.initComptime(.{ .{"-fPIE"}, .{"-fPIC"} }).has(arg);
} else {
pic, pie = .{ false, false };
if (target_util.isPS(target)) {
Expand Down Expand Up @@ -1125,7 +1121,3 @@ fn getLastArg(args: []const []const u8, options: StaticStringSet) ?usize {
}
return last_index;
}

fn isOneOf(arg: []const u8, options: StaticStringSet) bool {
return options.has(arg);
}

0 comments on commit 7297256

Please sign in to comment.