linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Masahiro Yamada <masahiroy@kernel.org>
To: linux-kbuild@vger.kernel.org
Cc: Masahiro Yamada <masahiroy@kernel.org>, linux-kernel@vger.kernel.org
Subject: [PATCH 03/10] kconfig: refactor conf_write_symbol()
Date: Fri,  1 Oct 2021 14:32:46 +0900	[thread overview]
Message-ID: <20211001053253.1223316-3-masahiroy@kernel.org> (raw)
In-Reply-To: <20211001053253.1223316-1-masahiroy@kernel.org>

I do not think 'struct conf_printer' is so useful.

Add simple functions, print_symbol_for_*() to write out one symbol.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/kconfig/confdata.c | 136 ++++++++++++++++---------------------
 1 file changed, 57 insertions(+), 79 deletions(-)

diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
index ed1bb8ba971b..ce11e7442d12 100644
--- a/scripts/kconfig/confdata.c
+++ b/scripts/kconfig/confdata.c
@@ -11,6 +11,7 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -159,10 +160,6 @@ static int conf_touch_dep(const char *name)
 	return 0;
 }
 
-struct conf_printer {
-	void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
-};
-
 static void conf_warning(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
 
@@ -629,91 +626,52 @@ static void conf_write_heading(FILE *fp, const struct comment_style *cs)
  * This printer is used when generating the resulting configuration after
  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
  * passing a non-NULL argument to the printer.
- *
  */
-static void
-kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
-{
-
-	switch (sym->type) {
-	case S_BOOLEAN:
-	case S_TRISTATE:
-		if (*value == 'n') {
-			bool skip_unset = (arg != NULL);
-
-			if (!skip_unset)
-				fprintf(fp, "# %s%s is not set\n",
-				    CONFIG_, sym->name);
-			return;
-		}
-		break;
-	default:
-		break;
-	}
-
-	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
-}
+enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
 
-static struct conf_printer kconfig_printer_cb =
+static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
+			   bool escape_string)
 {
-	.print_symbol = kconfig_print_symbol,
-};
+	const char *val;
+	char *escaped = NULL;
 
-/*
- * Header printer
- *
- * This printer is used when generating the `include/generated/autoconf.h' file.
- */
-static void
-header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
-{
+	if (sym->type == S_UNKNOWN)
+		return;
 
-	switch (sym->type) {
-	case S_BOOLEAN:
-	case S_TRISTATE: {
-		const char *suffix = "";
+	val = sym_get_string_value(sym);
 
-		switch (*value) {
-		case 'n':
-			break;
-		case 'm':
-			suffix = "_MODULE";
-			/* fall through */
-		default:
-			fprintf(fp, "#define %s%s%s 1\n",
-			    CONFIG_, sym->name, suffix);
-		}
-		break;
+	if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
+	    output_n != OUTPUT_N && *val == 'n') {
+		if (output_n == OUTPUT_N_AS_UNSET)
+			fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
+		return;
 	}
-	case S_HEX: {
-		const char *prefix = "";
 
-		if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
-			prefix = "0x";
-		fprintf(fp, "#define %s%s %s%s\n",
-		    CONFIG_, sym->name, prefix, value);
-		break;
-	}
-	case S_STRING:
-	case S_INT:
-		fprintf(fp, "#define %s%s %s\n",
-		    CONFIG_, sym->name, value);
-		break;
-	default:
-		break;
+	if (sym->type == S_STRING && escape_string) {
+		escaped = sym_escape_string_value(val);
+		val = escaped;
 	}
 
+	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
+
+	free(escaped);
 }
 
-static struct conf_printer header_printer_cb =
+static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
 {
-	.print_symbol = header_print_symbol,
-};
+	__print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
+}
+
+static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
+{
+	__print_symbol(fp, sym, OUTPUT_N_NONE, true);
+}
 
-static void conf_write_symbol(FILE *fp, struct symbol *sym,
-			      struct conf_printer *printer, void *printer_arg)
+static void print_symbol_for_c(FILE *fp, struct symbol *sym)
 {
 	const char *val;
+	const char *sym_suffix = "";
+	const char *val_prefix = "";
 	char *escaped = NULL;
 
 	if (sym->type == S_UNKNOWN)
@@ -721,12 +679,32 @@ static void conf_write_symbol(FILE *fp, struct symbol *sym,
 
 	val = sym_get_string_value(sym);
 
-	if (sym->type == S_STRING) {
+	switch (sym->type) {
+	case S_BOOLEAN:
+	case S_TRISTATE:
+		switch (*val) {
+		case 'n':
+			return;
+		case 'm':
+			sym_suffix = "_MODULE";
+			/* fall through */
+		default:
+			val = "1";
+		}
+		break;
+	case S_HEX:
+		if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
+			val_prefix = "0x";
+		break;
+	case S_STRING:
 		escaped = sym_escape_string_value(val);
 		val = escaped;
+	default:
+		break;
 	}
 
-	printer->print_symbol(fp, sym, val, printer_arg);
+	fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
+		val_prefix, val);
 
 	free(escaped);
 }
@@ -787,7 +765,7 @@ int conf_write_defconfig(const char *filename)
 						goto next_menu;
 				}
 			}
-			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
+			print_symbol_for_dotconfig(out, sym);
 		}
 next_menu:
 		if (menu->list != NULL) {
@@ -874,7 +852,7 @@ int conf_write(const char *name)
 				need_newline = false;
 			}
 			sym->flags |= SYMBOL_WRITTEN;
-			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
+			print_symbol_for_dotconfig(out, sym);
 		}
 
 next:
@@ -1060,8 +1038,8 @@ int conf_write_autoconf(int overwrite)
 			continue;
 
 		/* write symbols to auto.conf and autoconf.h */
-		conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
-		conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
+		print_symbol_for_autoconf(out, sym);
+		print_symbol_for_c(out_h, sym);
 	}
 	fclose(out);
 	fclose(out_h);
-- 
2.30.2


  parent reply	other threads:[~2021-10-01  5:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-01  5:32 [PATCH 01/10] kconfig: remove 'const' from the return type of sym_escape_string_value() Masahiro Yamada
2021-10-01  5:32 ` [PATCH 02/10] kconfig: refactor conf_write_heading() Masahiro Yamada
2021-10-01  5:32 ` Masahiro Yamada [this message]
2021-10-28  5:16   ` [PATCH 03/10] kconfig: refactor conf_write_symbol() Boris Kolpackov
2021-11-05  8:46     ` Masahiro Yamada
2021-11-05 11:40       ` Boris Kolpackov
2021-10-01  5:32 ` [PATCH 04/10] kconfig: refactor listnewconfig code Masahiro Yamada
2021-10-01  5:32 ` [PATCH 05/10] kconfig: move sym_escape_string_value() to confdata.c Masahiro Yamada
2021-10-01  5:32 ` [PATCH 06/10] kconfig: add conf_get_autoheader_name() Masahiro Yamada
2021-10-01  5:32 ` [PATCH 07/10] kconfig: refactor conf_write_autoconf() Masahiro Yamada
2021-10-01  5:32 ` [PATCH 08/10] kconfig: refactor conf_write_dep() Masahiro Yamada
2021-10-01  5:32 ` [PATCH 09/10] kconfig: refactor conf_touch_dep() Masahiro Yamada
2021-10-01  5:32 ` [PATCH 10/10] [for next only] kconfig: generate include/generated/rustc_cfg Masahiro Yamada
2021-10-01 11:28   ` n.schier
2021-10-02  8:59     ` Masahiro Yamada
2021-10-05 15:37 ` [PATCH 01/10] kconfig: remove 'const' from the return type of sym_escape_string_value() Masahiro Yamada

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211001053253.1223316-3-masahiroy@kernel.org \
    --to=masahiroy@kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).