From 7297256a17243a7d0d4b95f0f24e0cdfe9597016 Mon Sep 17 00:00:00 2001 From: Organ1sm Date: Fri, 27 Sep 2024 11:25:58 +0800 Subject: [PATCH] driver: remove unsecessary `isOneof` function update `getPICMode` comment --- src/aro/Driver.zig | 52 ++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/aro/Driver.zig b/src/aro/Driver.zig index 21176149..7a8cbb28 100644 --- a/src/aro/Driver.zig +++ b/src/aro/Driver.zig @@ -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) = .{}, @@ -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")) { @@ -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); @@ -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; @@ -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 @@ -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)) { @@ -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); -}