Pārlūkot izejas kodu

[source-code-locations] - added options to show, obfuscate, and hide source code locations (#5412)

Hayden Gray 2 mēneši atpakaļ
vecāks
revīzija
f72b2b1530
5 mainītis faili ar 107 papildinājumiem un 12 dzēšanām
  1. 8 1
      src/build_settings.cpp
  2. 42 4
      src/check_expr.cpp
  3. 17 3
      src/llvm_backend_const.cpp
  4. 13 1
      src/llvm_backend_general.cpp
  5. 27 3
      src/main.cpp

+ 8 - 1
src/build_settings.cpp

@@ -385,6 +385,13 @@ enum LinkerChoice : i32 {
 	Linker_COUNT,
 };
 
+enum SourceCodeLocationInfo : u8 {
+	SourceCodeLocationInfo_Normal = 0,
+	SourceCodeLocationInfo_Obfuscated = 1,
+	SourceCodeLocationInfo_Filename = 2,
+	SourceCodeLocationInfo_None = 3,
+};
+
 String linker_choices[Linker_COUNT] = {
 	str_lit("default"),
 	str_lit("lld"),
@@ -512,7 +519,7 @@ struct BuildContext {
 
 	bool   dynamic_map_calls;
 
-	bool   obfuscate_source_code_locations;
+	SourceCodeLocationInfo source_code_location_info;
 
 	bool   min_link_libs;
 

+ 42 - 4
src/check_expr.cpp

@@ -8760,23 +8760,52 @@ gb_internal ExprKind check_basic_directive_expr(CheckerContext *c, Operand *o, A
 	String name = bd->name.string;
 	if (name == "file") {
 		String file = get_file_path_string(bd->token.pos.file_id);
-		if (build_context.obfuscate_source_code_locations) {
+		switch (build_context.source_code_location_info) {
+		case SourceCodeLocationInfo_Normal:
+			break;
+		case SourceCodeLocationInfo_Obfuscated:
 			file = obfuscate_string(file, "F");
+			break;
+		case SourceCodeLocationInfo_Filename:
+			file = last_path_element(file);
+			break;
+		case SourceCodeLocationInfo_None:
+			file = str_lit("");
+			break;
 		}
 		o->type = t_untyped_string;
 		o->value = exact_value_string(file);
 	} else if (name == "directory") {
 		String file = get_file_path_string(bd->token.pos.file_id);
 		String path = dir_from_path(file);
-		if (build_context.obfuscate_source_code_locations) {
+		switch (build_context.source_code_location_info) {
+		case SourceCodeLocationInfo_Normal:
+			break;
+		case SourceCodeLocationInfo_Obfuscated:
 			path = obfuscate_string(path, "D");
+			break;
+		case SourceCodeLocationInfo_Filename:
+			path = last_path_element(path);
+			break;
+		case SourceCodeLocationInfo_None:
+			path = str_lit("");
+			break;
 		}
 		o->type = t_untyped_string;
 		o->value = exact_value_string(path);
 	} else if (name == "line") {
 		i32 line = bd->token.pos.line;
-		if (build_context.obfuscate_source_code_locations) {
+		switch (build_context.source_code_location_info) {
+		case SourceCodeLocationInfo_Normal:
+			break;
+		case SourceCodeLocationInfo_Obfuscated:
 			line = obfuscate_i32(line);
+			break;
+		case SourceCodeLocationInfo_Filename:
+			break;
+		case SourceCodeLocationInfo_None:
+			line = 0;
+			break;
 		}
 		o->type = t_untyped_integer;
 		o->value = exact_value_i64(line);
@@ -8787,8 +8816,17 @@ gb_internal ExprKind check_basic_directive_expr(CheckerContext *c, Operand *o, A
 			o->value = exact_value_string(str_lit(""));
 		} else {
 			String p = c->proc_name;
-			if (build_context.obfuscate_source_code_locations) {
+			switch (build_context.source_code_location_info) {
+			case SourceCodeLocationInfo_Normal:
+				break;
+			case SourceCodeLocationInfo_Obfuscated:
 				p = obfuscate_string(p, "P");
+				break;
+			case SourceCodeLocationInfo_Filename:
+				break;
+			case SourceCodeLocationInfo_None:
+				p = str_lit("");
+				break;
 			}
 			o->type = t_untyped_string;
 			o->value = exact_value_string(p);

+ 17 - 3
src/llvm_backend_const.cpp

@@ -292,12 +292,26 @@ gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String cons
 	i32 line   = pos.line;
 	i32 column = pos.column;
 
-	if (build_context.obfuscate_source_code_locations) {
+	switch (build_context.source_code_location_info) {
+	case SourceCodeLocationInfo_Normal:
+		break;
+	case SourceCodeLocationInfo_Obfuscated:
 		file = obfuscate_string(file, "F");
 		procedure = obfuscate_string(procedure, "P");
 
-		line   = obfuscate_i32(line);
-		column = obfuscate_i32(column);
+		line = obfuscate_i32(line);
+		column  = obfuscate_i32(column);
+		break;
+	case SourceCodeLocationInfo_Filename:
+		file = last_path_element(file);
+		break;
+	case SourceCodeLocationInfo_None:
+		file = str_lit("");
+		procedure = str_lit("");
+
+		line = 0;
+		column = 0;
+		break;
 	}
 
 	LLVMValueRef fields[4] = {};

+ 13 - 1
src/llvm_backend_general.cpp

@@ -568,10 +568,22 @@ gb_internal void lb_set_file_line_col(lbProcedure *p, Array<lbValue> arr, TokenP
 	i32 line    = pos.line;
 	i32 col     = pos.column;
 
-	if (build_context.obfuscate_source_code_locations) {
+	switch (build_context.source_code_location_info) {
+	case SourceCodeLocationInfo_Normal:
+		break;
+	case SourceCodeLocationInfo_Obfuscated:
 		file = obfuscate_string(file, "F");
 		line = obfuscate_i32(line);
 		col  = obfuscate_i32(col);
+		break;
+	case SourceCodeLocationInfo_Filename:
+		file = last_path_element(file);
+		break;
+	case SourceCodeLocationInfo_None:
+		file = str_lit("");
+		line = 0;
+		col  = 0;
+		break;
 	}
 
 	arr[0] = lb_find_or_add_entity_string(p->module, file, false);

+ 27 - 3
src/main.cpp

@@ -370,6 +370,7 @@ enum BuildFlagKind {
 	BuildFlag_NoRTTI,
 	BuildFlag_DynamicMapCalls,
 	BuildFlag_ObfuscateSourceCodeLocations,
+	BuildFlag_SourceCodeLocations,
 
 	BuildFlag_Compact,
 	BuildFlag_GlobalDefinitions,
@@ -594,6 +595,7 @@ gb_internal bool parse_build_flags(Array<String> args) {
 	add_flag(&build_flags, BuildFlag_DynamicMapCalls,         str_lit("dynamic-map-calls"),         BuildFlagParam_None,    Command__does_check);
 
 	add_flag(&build_flags, BuildFlag_ObfuscateSourceCodeLocations, str_lit("obfuscate-source-code-locations"), BuildFlagParam_None,    Command__does_build);
+	add_flag(&build_flags, BuildFlag_SourceCodeLocations, 		str_lit("source-code-locations"), 		BuildFlagParam_String,  Command__does_build);
 
 	add_flag(&build_flags, BuildFlag_Short,                   str_lit("short"),                     BuildFlagParam_None,    Command_doc);
 	add_flag(&build_flags, BuildFlag_AllPackages,             str_lit("all-packages"),              BuildFlagParam_None,    Command_doc | Command_test | Command_build);
@@ -1422,7 +1424,23 @@ gb_internal bool parse_build_flags(Array<String> args) {
 							break;
 
 						case BuildFlag_ObfuscateSourceCodeLocations:
-							build_context.obfuscate_source_code_locations = true;
+							gb_printf_err("'-obfuscate-source-code-locations' is now deprecated in favor of '-source-code-locations:obfuscated'\n");
+							build_context.source_code_location_info = SourceCodeLocationInfo_Obfuscated;
+							break;
+
+						case BuildFlag_SourceCodeLocations:
+							if (str_eq_ignore_case(value.value_string, str_lit("normal"))) {
+								build_context.source_code_location_info = SourceCodeLocationInfo_Normal;
+							} else if (str_eq_ignore_case(value.value_string, str_lit("obfuscated"))) {
+								build_context.source_code_location_info = SourceCodeLocationInfo_Obfuscated;
+							} else if (str_eq_ignore_case(value.value_string, str_lit("filename"))) {
+								build_context.source_code_location_info = SourceCodeLocationInfo_Filename;
+							} else if (str_eq_ignore_case(value.value_string, str_lit("none"))) {
+								build_context.source_code_location_info = SourceCodeLocationInfo_None;
+							} else {
+								gb_printf_err("-source-code-locations:<string> options are 'normal', 'obfuscated', 'filename', and 'none'\n");
+								bad_flags = true;
+							}
 							break;
 
 						case BuildFlag_DefaultToNilAllocator:
@@ -2669,8 +2687,14 @@ gb_internal int print_show_help(String const arg0, String command, String option
 		}
 
 
-		if (print_flag("-obfuscate-source-code-locations")) {
-			print_usage_line(2, "Obfuscate the file and procedure strings, and line and column numbers, stored with a 'runtime.Source_Code_Location' value.");
+		if (print_flag("-source-code-locations:<string>")) {
+			print_usage_line(2, "Processes the file and procedure strings, and line and column numbers, stored with a 'runtime.Source_Code_Location' value.");
+			print_usage_line(2, "Available options:");
+				print_usage_line(3, "-source-code-locations:normal");
+				print_usage_line(3, "-source-code-locations:obfuscated");
+				print_usage_line(3, "-source-code-locations:filename");
+				print_usage_line(3, "-source-code-locations:none");
+			print_usage_line(2, "The default is -source-code-locations:normal.");
 		}