From 7a578a8725f8fd7d92fcbbac14841ea7e8d0870f Mon Sep 17 00:00:00 2001 From: zhangxiaohua Date: Sun, 25 Aug 2024 23:08:53 +0800 Subject: [PATCH 157/157] Enable macro-use-commandline Signed-off-by: zhangxiaohua --- gcc/c-family/c-opts.cc | 4 +++ gcc/c-family/c.opt | 4 +++ gcc/doc/cppopts.texi | 4 +++ gcc/doc/invoke.texi | 1 + .../gcc.dg/cpp/macro-use-cmdline-1.c | 26 ++++++++++++++ .../gcc.dg/cpp/macro-use-cmdline-2.c | 34 +++++++++++++++++++ libcpp/include/cpplib.h | 3 ++ libcpp/init.cc | 1 + libcpp/macro.cc | 16 ++++++++- 9 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-1.c create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-2.c diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index 5134f6128..744b54dc3 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -527,6 +527,10 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value, cpp_opts->track_macro_expansion = 2; break; + case OPT_fmacro_use_commandline: + cpp_opts->macro_use_commandline = 1; + break; + case OPT_fexec_charset_: cpp_opts->narrow_charset = arg; break; diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 07da40ef4..a36c27f07 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -2012,6 +2012,10 @@ ftrack-macro-expansion= C ObjC C++ ObjC++ JoinedOrMissing RejectNegative UInteger -ftrack-macro-expansion=<0|1|2> Track locations of tokens coming from macro expansion and display them in error messages. +fmacro-use-commandline +C ObjC C++ ObjC++ JoinedOrMissing RejectNegative UInteger +Preferentially use options from the commandline. + fpretty-templates C++ ObjC++ Var(flag_pretty_templates) Init(1) Do not pretty-print template specializations as the template signature followed by the arguments. diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi index c0a92b370..8c8a81eac 100644 --- a/gcc/doc/cppopts.texi +++ b/gcc/doc/cppopts.texi @@ -277,6 +277,10 @@ correct column numbers in warnings or errors, even if tabs appear on the line. If the value is less than 1 or greater than 100, the option is ignored. The default is 8. +@item -fmacro-use-commandline +@opindex fmacro-use-commandline +Preferentially use options from the command line. + @item -ftrack-macro-expansion@r{[}=@var{level}@r{]} @opindex ftrack-macro-expansion Track locations of tokens across macro expansions. This allows the diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index bdd8b9429..2ff7d860d 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -630,6 +630,7 @@ Objective-C and Objective-C++ Dialects}. -fexec-charset=@var{charset} -fextended-identifiers @gol -finput-charset=@var{charset} -flarge-source-files @gol -fmacro-prefix-map=@var{old}=@var{new} -fmax-include-depth=@var{depth} @gol +-fmacro-use-commandline @gol -fno-canonical-system-headers -fpch-deps -fpch-preprocess @gol -fpreprocessed -ftabstop=@var{width} -ftrack-macro-expansion @gol -fwide-exec-charset=@var{charset} -fworking-directory @gol diff --git a/gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-1.c b/gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-1.c new file mode 100644 index 000000000..f85d9c268 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-1.c @@ -0,0 +1,26 @@ +/* + { dg-options "-fmacro-use-commandline -DTEST_MACRO=1 -DTEST_MACRO=20" } + { dg-do compile } + { dg-do run } +*/ + +/* { dg-warning "-:redefined" "redef TEST_MACRO" { target *-*-* } 0 } + { dg-message "-:previous" "prev def TEST_MACRO" { target *-*-* } 0 } +*/ + +#if DEBUG +extern int puts (const char *); +#else +#define puts(X) +#endif +extern void abort (void); + +#define err(str) do { puts(str); abort(); } while (0) + +int main (int argc, char *argv[]) +{ + int macroValue = TEST_MACRO; + if (macroValue != 20) + err("macroValue"); + return 0; +} \ No newline at end of file diff --git a/gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-2.c b/gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-2.c new file mode 100644 index 000000000..99d92d1e4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/macro-use-cmdline-2.c @@ -0,0 +1,34 @@ +/* + { dg-options "-fmacro-use-commandline -DTEST_MACRO=1" } + { dg-do compile } + { dg-do run } +*/ + +#define TEST_MACRO 300 +#define TEST_MACRO_1 400 +/* + { dg-warning "-:redefined" "redef TEST_MACRO" { target *-*-* } 7 } + { dg-message "-:previous" "prev def TEST_MACRO" { target *-*-* } 0 } +*/ + +#if DEBUG +extern int puts (const char *); +#else +#define puts(X) +#endif + +extern void abort (void); + +#define err(str) do { puts(str); abort(); } while (0) + +int main (int argc, char *argv[]) +{ + int macroValue = TEST_MACRO; + if (macroValue != 1) + err("macroValue"); + + int macroValue1 = TEST_MACRO_1; + if (macroValue1 != 400) + err("macroValue1"); + return 0; +} \ No newline at end of file diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 3eba6f74b..c6101ca01 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -471,6 +471,9 @@ struct cpp_options consumes the highest amount of memory. */ unsigned char track_macro_expansion; + /* Use the options on the command line first. */ + unsigned char macro_use_commandline; + /* Nonzero means handle C++ alternate operator names. */ unsigned char operator_names; diff --git a/libcpp/init.cc b/libcpp/init.cc index f4ab83d21..47be60a36 100644 --- a/libcpp/init.cc +++ b/libcpp/init.cc @@ -215,6 +215,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table, cpp_options::track_macro_expansion to learn about the other values. */ CPP_OPTION (pfile, track_macro_expansion) = 2; + CPP_OPTION (pfile, macro_use_commandline) = 0; CPP_OPTION (pfile, warn_normalize) = normalized_C; CPP_OPTION (pfile, warn_literal_suffix) = 1; CPP_OPTION (pfile, canonical_system_headers) diff --git a/libcpp/macro.cc b/libcpp/macro.cc index 8ebf360c0..aa9e4ffa6 100644 --- a/libcpp/macro.cc +++ b/libcpp/macro.cc @@ -3852,7 +3852,21 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node) node->value.macro->line, 0, "this is the location of the previous definition"); } - _cpp_free_definition (node); +#define LOCATION_FROM_LINEMAP 0 +#define MIN_LINE_OF_MACRO_BEEN_OVERRIDDEN 96 +#define MAX_LINE_OF_MACRO_BEEN_OVERRIDDEN 128 + if (CPP_OPTION (pfile, macro_use_commandline) + && node->value.macro->line >= MIN_LINE_OF_MACRO_BEEN_OVERRIDDEN + && node->value.macro->line <= MAX_LINE_OF_MACRO_BEEN_OVERRIDDEN + && pfile->forced_token_location == LOCATION_FROM_LINEMAP) + { + cpp_pedwarning_with_line (pfile, CPP_W_NONE, + node->value.macro->line, 0, + "use the previous definition from commandline"); + return false; + } + else + _cpp_free_definition (node); } /* Enter definition in hash table. */ -- 2.33.0