* [pushed 1/3] selftests: split out make_fndecl from selftest.h to its own header @ 2024-05-28 20:07 David Malcolm 2024-05-28 20:07 ` [pushed 2/3] libcpp: move label_text " David Malcolm 2024-05-28 20:07 ` [pushed 3/3] diagnostics: consolidate global state in diagnostic-color.cc David Malcolm 0 siblings, 2 replies; 8+ messages in thread From: David Malcolm @ 2024-05-28 20:07 UTC (permalink / raw) To: gcc-patches; +Cc: David Malcolm Avoid selftest.h requiring the "tree" type. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r15-873-gfb7a943ead689e. gcc/analyzer/ChangeLog: * region-model.cc: Include "selftest-tree.h". gcc/ChangeLog: * function-tests.cc: Include "selftest-tree.h". * selftest-tree.h: New file. * selftest.h (make_fndecl): Move to selftest-tree.h. Signed-off-by: David Malcolm <dmalcolm@redhat.com> --- gcc/analyzer/region-model.cc | 1 + gcc/function-tests.cc | 1 + gcc/selftest-tree.h | 41 ++++++++++++++++++++++++++++++++++++ gcc/selftest.h | 7 ------ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 gcc/selftest-tree.h diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index bebe2ed3cd69..0dd5671db1be 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -42,6 +42,7 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic-color.h" #include "bitmap.h" #include "selftest.h" +#include "selftest-tree.h" #include "analyzer/analyzer.h" #include "analyzer/analyzer-logging.h" #include "ordered-hash-map.h" diff --git a/gcc/function-tests.cc b/gcc/function-tests.cc index 827734422d88..ea3d722d4b69 100644 --- a/gcc/function-tests.cc +++ b/gcc/function-tests.cc @@ -76,6 +76,7 @@ along with GCC; see the file COPYING3. If not see #include "ipa-ref.h" #include "cgraph.h" #include "selftest.h" +#include "selftest-tree.h" #include "print-rtl.h" #if CHECKING_P diff --git a/gcc/selftest-tree.h b/gcc/selftest-tree.h new file mode 100644 index 000000000000..9922af3340f2 --- /dev/null +++ b/gcc/selftest-tree.h @@ -0,0 +1,41 @@ +/* A self-testing framework, for use by -fself-test. + Copyright (C) 2015-2024 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#ifndef GCC_SELFTEST_TREE_H +#define GCC_SELFTEST_TREE_H + +/* The selftest code should entirely disappear in a production + configuration, hence we guard all of it with #if CHECKING_P. */ + +#if CHECKING_P + +namespace selftest { + +/* Helper function for selftests that need a function decl. */ + +extern tree make_fndecl (tree return_type, + const char *name, + vec <tree> ¶m_types, + bool is_variadic = false); + +} /* end of namespace selftest. */ + +#endif /* #if CHECKING_P */ + +#endif /* GCC_SELFTEST_TREE_H */ diff --git a/gcc/selftest.h b/gcc/selftest.h index 3bddaf1c3228..808d432ec480 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -178,13 +178,6 @@ class line_table_test ~line_table_test (); }; -/* Helper function for selftests that need a function decl. */ - -extern tree make_fndecl (tree return_type, - const char *name, - vec <tree> ¶m_types, - bool is_variadic = false); - /* Run TESTCASE multiple times, once for each case in our test matrix. */ extern void -- 2.26.3 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [pushed 2/3] libcpp: move label_text to its own header 2024-05-28 20:07 [pushed 1/3] selftests: split out make_fndecl from selftest.h to its own header David Malcolm @ 2024-05-28 20:07 ` David Malcolm 2024-06-06 13:01 ` Bert Wesarg 2024-05-28 20:07 ` [pushed 3/3] diagnostics: consolidate global state in diagnostic-color.cc David Malcolm 1 sibling, 1 reply; 8+ messages in thread From: David Malcolm @ 2024-05-28 20:07 UTC (permalink / raw) To: gcc-patches; +Cc: David Malcolm No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r15-874-g9bda2c4c81b668. libcpp/ChangeLog: * Makefile.in (TAGS_SOURCES): Add include/label-text.h. * include/label-text.h: New file. * include/rich-location.h: Include "label-text.h". (class label_text): Move to label-text.h. Signed-off-by: David Malcolm <dmalcolm@redhat.com> --- libcpp/Makefile.in | 2 +- libcpp/include/label-text.h | 102 +++++++++++++++++++++++++++++++++ libcpp/include/rich-location.h | 79 +------------------------ 3 files changed, 105 insertions(+), 78 deletions(-) create mode 100644 libcpp/include/label-text.h diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in index ebbca37777fb..7e47153264c0 100644 --- a/libcpp/Makefile.in +++ b/libcpp/Makefile.in @@ -271,7 +271,7 @@ ETAGS = @ETAGS@ TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h system.h ucnid.h \ include/cpplib.h include/line-map.h include/mkdeps.h include/symtab.h \ - include/rich-location.h + include/rich-location.h include/label-text.h TAGS: $(TAGS_SOURCES) diff --git a/libcpp/include/label-text.h b/libcpp/include/label-text.h new file mode 100644 index 000000000000..13562cda41f9 --- /dev/null +++ b/libcpp/include/label-text.h @@ -0,0 +1,102 @@ +/* A very simple string class. + Copyright (C) 2015-2024 Free Software Foundation, Inc. + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! */ + +#ifndef LIBCPP_LABEL_TEXT_H +#define LIBCPP_LABEL_TEXT_H + +/* A struct for the result of range_label::get_text: a NUL-terminated buffer + of localized text, and a flag to determine if the caller should "free" the + buffer. */ + +class label_text +{ +public: + label_text () + : m_buffer (NULL), m_owned (false) + {} + + ~label_text () + { + if (m_owned) + free (m_buffer); + } + + /* Move ctor. */ + label_text (label_text &&other) + : m_buffer (other.m_buffer), m_owned (other.m_owned) + { + other.release (); + } + + /* Move assignment. */ + label_text & operator= (label_text &&other) + { + if (m_owned) + free (m_buffer); + m_buffer = other.m_buffer; + m_owned = other.m_owned; + other.release (); + return *this; + } + + /* Delete the copy ctor and copy-assignment operator. */ + label_text (const label_text &) = delete; + label_text & operator= (const label_text &) = delete; + + /* Create a label_text instance that borrows BUFFER from a + longer-lived owner. */ + static label_text borrow (const char *buffer) + { + return label_text (const_cast <char *> (buffer), false); + } + + /* Create a label_text instance that takes ownership of BUFFER. */ + static label_text take (char *buffer) + { + return label_text (buffer, true); + } + + void release () + { + m_buffer = NULL; + m_owned = false; + } + + const char *get () const + { + return m_buffer; + } + + bool is_owner () const + { + return m_owned; + } + +private: + char *m_buffer; + bool m_owned; + + label_text (char *buffer, bool owned) + : m_buffer (buffer), m_owned (owned) + {} +}; + +#endif /* !LIBCPP_LABEL_TEXT_H */ diff --git a/libcpp/include/rich-location.h b/libcpp/include/rich-location.h index a2ece8b033c0..be424cb4b65f 100644 --- a/libcpp/include/rich-location.h +++ b/libcpp/include/rich-location.h @@ -22,6 +22,8 @@ along with this program; see the file COPYING3. If not see #ifndef LIBCPP_RICH_LOCATION_H #define LIBCPP_RICH_LOCATION_H +#include "label-text.h" + class range_label; class label_effects; @@ -541,83 +543,6 @@ protected: const diagnostic_path *m_path; }; -/* A struct for the result of range_label::get_text: a NUL-terminated buffer - of localized text, and a flag to determine if the caller should "free" the - buffer. */ - -class label_text -{ -public: - label_text () - : m_buffer (NULL), m_owned (false) - {} - - ~label_text () - { - if (m_owned) - free (m_buffer); - } - - /* Move ctor. */ - label_text (label_text &&other) - : m_buffer (other.m_buffer), m_owned (other.m_owned) - { - other.release (); - } - - /* Move assignment. */ - label_text & operator= (label_text &&other) - { - if (m_owned) - free (m_buffer); - m_buffer = other.m_buffer; - m_owned = other.m_owned; - other.release (); - return *this; - } - - /* Delete the copy ctor and copy-assignment operator. */ - label_text (const label_text &) = delete; - label_text & operator= (const label_text &) = delete; - - /* Create a label_text instance that borrows BUFFER from a - longer-lived owner. */ - static label_text borrow (const char *buffer) - { - return label_text (const_cast <char *> (buffer), false); - } - - /* Create a label_text instance that takes ownership of BUFFER. */ - static label_text take (char *buffer) - { - return label_text (buffer, true); - } - - void release () - { - m_buffer = NULL; - m_owned = false; - } - - const char *get () const - { - return m_buffer; - } - - bool is_owner () const - { - return m_owned; - } - -private: - char *m_buffer; - bool m_owned; - - label_text (char *buffer, bool owned) - : m_buffer (buffer), m_owned (owned) - {} -}; - /* Abstract base class for labelling a range within a rich_location (e.g. for labelling expressions with their type). -- 2.26.3 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [pushed 2/3] libcpp: move label_text to its own header 2024-05-28 20:07 ` [pushed 2/3] libcpp: move label_text " David Malcolm @ 2024-06-06 13:01 ` Bert Wesarg 2024-06-06 15:40 ` Andrew Pinski 0 siblings, 1 reply; 8+ messages in thread From: Bert Wesarg @ 2024-06-06 13:01 UTC (permalink / raw) To: David Malcolm; +Cc: gcc-patches Dear David, On Tue, May 28, 2024 at 10:07 PM David Malcolm <dmalcolm@redhat.com> wrote: > > No functional change intended. > > Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. > Pushed to trunk as r15-874-g9bda2c4c81b668. > > libcpp/ChangeLog: > * Makefile.in (TAGS_SOURCES): Add include/label-text.h. > * include/label-text.h: New file. > * include/rich-location.h: Include "label-text.h". > (class label_text): Move to label-text.h. > > Signed-off-by: David Malcolm <dmalcolm@redhat.com> > --- > libcpp/Makefile.in | 2 +- > libcpp/include/label-text.h | 102 +++++++++++++++++++++++++++++++++ > libcpp/include/rich-location.h | 79 +------------------------ > 3 files changed, 105 insertions(+), 78 deletions(-) > create mode 100644 libcpp/include/label-text.h > > diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in > index ebbca37777fb..7e47153264c0 100644 > --- a/libcpp/Makefile.in > +++ b/libcpp/Makefile.in > @@ -271,7 +271,7 @@ ETAGS = @ETAGS@ > > TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h system.h ucnid.h \ > include/cpplib.h include/line-map.h include/mkdeps.h include/symtab.h \ > - include/rich-location.h > + include/rich-location.h include/label-text.h this does not seem to be enough that the new header will be installed. I get compile errors when compiling an plug-in with this patch: In file included from /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux-gnu/15.0.0/plugin/include/diagnostic.h:24, from /home/bitten/builds/oCyPvWN6/1/perftools/cicd/scorep/src/build-gcc-plugin/../src/adapters/compiler/gcc-plugin/scorep_plugin_inst_descriptor.cpp:43: /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux-gnu/15.0.0/plugin/include/rich-location.h:25:10: fatal error: label-text.h: No such file or directory 25 | #include "label-text.h" | ^~~~~~~~~~~~~~ compilation terminated. Best, Bert > > > TAGS: $(TAGS_SOURCES) > diff --git a/libcpp/include/label-text.h b/libcpp/include/label-text.h > new file mode 100644 > index 000000000000..13562cda41f9 > --- /dev/null > +++ b/libcpp/include/label-text.h > @@ -0,0 +1,102 @@ > +/* A very simple string class. > + Copyright (C) 2015-2024 Free Software Foundation, Inc. > + > +This program is free software; you can redistribute it and/or modify it > +under the terms of the GNU General Public License as published by the > +Free Software Foundation; either version 3, or (at your option) any > +later version. > + > +This program is distributed in the hope that it will be useful, > +but WITHOUT ANY WARRANTY; without even the implied warranty of > +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > +GNU General Public License for more details. > + > +You should have received a copy of the GNU General Public License > +along with this program; see the file COPYING3. If not see > +<http://www.gnu.org/licenses/>. > + > + In other words, you are welcome to use, share and improve this program. > + You are forbidden to forbid anyone else to use, share and improve > + what you give them. Help stamp out software-hoarding! */ > + > +#ifndef LIBCPP_LABEL_TEXT_H > +#define LIBCPP_LABEL_TEXT_H > + > +/* A struct for the result of range_label::get_text: a NUL-terminated buffer > + of localized text, and a flag to determine if the caller should "free" the > + buffer. */ > + > +class label_text > +{ > +public: > + label_text () > + : m_buffer (NULL), m_owned (false) > + {} > + > + ~label_text () > + { > + if (m_owned) > + free (m_buffer); > + } > + > + /* Move ctor. */ > + label_text (label_text &&other) > + : m_buffer (other.m_buffer), m_owned (other.m_owned) > + { > + other.release (); > + } > + > + /* Move assignment. */ > + label_text & operator= (label_text &&other) > + { > + if (m_owned) > + free (m_buffer); > + m_buffer = other.m_buffer; > + m_owned = other.m_owned; > + other.release (); > + return *this; > + } > + > + /* Delete the copy ctor and copy-assignment operator. */ > + label_text (const label_text &) = delete; > + label_text & operator= (const label_text &) = delete; > + > + /* Create a label_text instance that borrows BUFFER from a > + longer-lived owner. */ > + static label_text borrow (const char *buffer) > + { > + return label_text (const_cast <char *> (buffer), false); > + } > + > + /* Create a label_text instance that takes ownership of BUFFER. */ > + static label_text take (char *buffer) > + { > + return label_text (buffer, true); > + } > + > + void release () > + { > + m_buffer = NULL; > + m_owned = false; > + } > + > + const char *get () const > + { > + return m_buffer; > + } > + > + bool is_owner () const > + { > + return m_owned; > + } > + > +private: > + char *m_buffer; > + bool m_owned; > + > + label_text (char *buffer, bool owned) > + : m_buffer (buffer), m_owned (owned) > + {} > +}; > + > +#endif /* !LIBCPP_LABEL_TEXT_H */ > diff --git a/libcpp/include/rich-location.h b/libcpp/include/rich-location.h > index a2ece8b033c0..be424cb4b65f 100644 > --- a/libcpp/include/rich-location.h > +++ b/libcpp/include/rich-location.h > @@ -22,6 +22,8 @@ along with this program; see the file COPYING3. If not see > #ifndef LIBCPP_RICH_LOCATION_H > #define LIBCPP_RICH_LOCATION_H > > +#include "label-text.h" > + > class range_label; > class label_effects; > > @@ -541,83 +543,6 @@ protected: > const diagnostic_path *m_path; > }; > > -/* A struct for the result of range_label::get_text: a NUL-terminated buffer > - of localized text, and a flag to determine if the caller should "free" the > - buffer. */ > - > -class label_text > -{ > -public: > - label_text () > - : m_buffer (NULL), m_owned (false) > - {} > - > - ~label_text () > - { > - if (m_owned) > - free (m_buffer); > - } > - > - /* Move ctor. */ > - label_text (label_text &&other) > - : m_buffer (other.m_buffer), m_owned (other.m_owned) > - { > - other.release (); > - } > - > - /* Move assignment. */ > - label_text & operator= (label_text &&other) > - { > - if (m_owned) > - free (m_buffer); > - m_buffer = other.m_buffer; > - m_owned = other.m_owned; > - other.release (); > - return *this; > - } > - > - /* Delete the copy ctor and copy-assignment operator. */ > - label_text (const label_text &) = delete; > - label_text & operator= (const label_text &) = delete; > - > - /* Create a label_text instance that borrows BUFFER from a > - longer-lived owner. */ > - static label_text borrow (const char *buffer) > - { > - return label_text (const_cast <char *> (buffer), false); > - } > - > - /* Create a label_text instance that takes ownership of BUFFER. */ > - static label_text take (char *buffer) > - { > - return label_text (buffer, true); > - } > - > - void release () > - { > - m_buffer = NULL; > - m_owned = false; > - } > - > - const char *get () const > - { > - return m_buffer; > - } > - > - bool is_owner () const > - { > - return m_owned; > - } > - > -private: > - char *m_buffer; > - bool m_owned; > - > - label_text (char *buffer, bool owned) > - : m_buffer (buffer), m_owned (owned) > - {} > -}; > - > /* Abstract base class for labelling a range within a rich_location > (e.g. for labelling expressions with their type). > > -- > 2.26.3 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [pushed 2/3] libcpp: move label_text to its own header 2024-06-06 13:01 ` Bert Wesarg @ 2024-06-06 15:40 ` Andrew Pinski 2024-06-06 16:00 ` David Malcolm 0 siblings, 1 reply; 8+ messages in thread From: Andrew Pinski @ 2024-06-06 15:40 UTC (permalink / raw) To: Bert Wesarg; +Cc: David Malcolm, gcc-patches On Thu, Jun 6, 2024 at 6:02 AM Bert Wesarg <bert.wesarg@googlemail.com> wrote: > > Dear David, > > On Tue, May 28, 2024 at 10:07 PM David Malcolm <dmalcolm@redhat.com> wrote: > > > > No functional change intended. > > > > Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. > > Pushed to trunk as r15-874-g9bda2c4c81b668. > > > > libcpp/ChangeLog: > > * Makefile.in (TAGS_SOURCES): Add include/label-text.h. > > * include/label-text.h: New file. > > * include/rich-location.h: Include "label-text.h". > > (class label_text): Move to label-text.h. > > > > Signed-off-by: David Malcolm <dmalcolm@redhat.com> > > --- > > libcpp/Makefile.in | 2 +- > > libcpp/include/label-text.h | 102 +++++++++++++++++++++++++++++++++ > > libcpp/include/rich-location.h | 79 +------------------------ > > 3 files changed, 105 insertions(+), 78 deletions(-) > > create mode 100644 libcpp/include/label-text.h > > > > diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in > > index ebbca37777fb..7e47153264c0 100644 > > --- a/libcpp/Makefile.in > > +++ b/libcpp/Makefile.in > > @@ -271,7 +271,7 @@ ETAGS = @ETAGS@ > > > > TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h system.h ucnid.h \ > > include/cpplib.h include/line-map.h include/mkdeps.h include/symtab.h \ > > - include/rich-location.h > > + include/rich-location.h include/label-text.h > > this does not seem to be enough that the new header will be installed. > I get compile errors when compiling an plug-in with this patch: > > In file included from > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux-gnu/15.0.0/plugin/include/diagnostic.h:24, > from /home/bitten/builds/oCyPvWN6/1/perftools/cicd/scorep/src/build-gcc-plugin/../src/adapters/compiler/gcc-plugin/scorep_plugin_inst_descriptor.cpp:43: > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux-gnu/15.0.0/plugin/include/rich-location.h:25:10: > fatal error: label-text.h: No such file or directory > 25 | #include "label-text.h" > | ^~~~~~~~~~~~~~ > compilation terminated. I have a fix which I am testing. > > Best, > Bert > > > > > > > TAGS: $(TAGS_SOURCES) > > diff --git a/libcpp/include/label-text.h b/libcpp/include/label-text.h > > new file mode 100644 > > index 000000000000..13562cda41f9 > > --- /dev/null > > +++ b/libcpp/include/label-text.h > > @@ -0,0 +1,102 @@ > > +/* A very simple string class. > > + Copyright (C) 2015-2024 Free Software Foundation, Inc. > > + > > +This program is free software; you can redistribute it and/or modify it > > +under the terms of the GNU General Public License as published by the > > +Free Software Foundation; either version 3, or (at your option) any > > +later version. > > + > > +This program is distributed in the hope that it will be useful, > > +but WITHOUT ANY WARRANTY; without even the implied warranty of > > +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > > +GNU General Public License for more details. > > + > > +You should have received a copy of the GNU General Public License > > +along with this program; see the file COPYING3. If not see > > +<http://www.gnu.org/licenses/>. > > + > > + In other words, you are welcome to use, share and improve this program. > > + You are forbidden to forbid anyone else to use, share and improve > > + what you give them. Help stamp out software-hoarding! */ > > + > > +#ifndef LIBCPP_LABEL_TEXT_H > > +#define LIBCPP_LABEL_TEXT_H > > + > > +/* A struct for the result of range_label::get_text: a NUL-terminated buffer > > + of localized text, and a flag to determine if the caller should "free" the > > + buffer. */ > > + > > +class label_text > > +{ > > +public: > > + label_text () > > + : m_buffer (NULL), m_owned (false) > > + {} > > + > > + ~label_text () > > + { > > + if (m_owned) > > + free (m_buffer); > > + } > > + > > + /* Move ctor. */ > > + label_text (label_text &&other) > > + : m_buffer (other.m_buffer), m_owned (other.m_owned) > > + { > > + other.release (); > > + } > > + > > + /* Move assignment. */ > > + label_text & operator= (label_text &&other) > > + { > > + if (m_owned) > > + free (m_buffer); > > + m_buffer = other.m_buffer; > > + m_owned = other.m_owned; > > + other.release (); > > + return *this; > > + } > > + > > + /* Delete the copy ctor and copy-assignment operator. */ > > + label_text (const label_text &) = delete; > > + label_text & operator= (const label_text &) = delete; > > + > > + /* Create a label_text instance that borrows BUFFER from a > > + longer-lived owner. */ > > + static label_text borrow (const char *buffer) > > + { > > + return label_text (const_cast <char *> (buffer), false); > > + } > > + > > + /* Create a label_text instance that takes ownership of BUFFER. */ > > + static label_text take (char *buffer) > > + { > > + return label_text (buffer, true); > > + } > > + > > + void release () > > + { > > + m_buffer = NULL; > > + m_owned = false; > > + } > > + > > + const char *get () const > > + { > > + return m_buffer; > > + } > > + > > + bool is_owner () const > > + { > > + return m_owned; > > + } > > + > > +private: > > + char *m_buffer; > > + bool m_owned; > > + > > + label_text (char *buffer, bool owned) > > + : m_buffer (buffer), m_owned (owned) > > + {} > > +}; > > + > > +#endif /* !LIBCPP_LABEL_TEXT_H */ > > diff --git a/libcpp/include/rich-location.h b/libcpp/include/rich-location.h > > index a2ece8b033c0..be424cb4b65f 100644 > > --- a/libcpp/include/rich-location.h > > +++ b/libcpp/include/rich-location.h > > @@ -22,6 +22,8 @@ along with this program; see the file COPYING3. If not see > > #ifndef LIBCPP_RICH_LOCATION_H > > #define LIBCPP_RICH_LOCATION_H > > > > +#include "label-text.h" > > + > > class range_label; > > class label_effects; > > > > @@ -541,83 +543,6 @@ protected: > > const diagnostic_path *m_path; > > }; > > > > -/* A struct for the result of range_label::get_text: a NUL-terminated buffer > > - of localized text, and a flag to determine if the caller should "free" the > > - buffer. */ > > - > > -class label_text > > -{ > > -public: > > - label_text () > > - : m_buffer (NULL), m_owned (false) > > - {} > > - > > - ~label_text () > > - { > > - if (m_owned) > > - free (m_buffer); > > - } > > - > > - /* Move ctor. */ > > - label_text (label_text &&other) > > - : m_buffer (other.m_buffer), m_owned (other.m_owned) > > - { > > - other.release (); > > - } > > - > > - /* Move assignment. */ > > - label_text & operator= (label_text &&other) > > - { > > - if (m_owned) > > - free (m_buffer); > > - m_buffer = other.m_buffer; > > - m_owned = other.m_owned; > > - other.release (); > > - return *this; > > - } > > - > > - /* Delete the copy ctor and copy-assignment operator. */ > > - label_text (const label_text &) = delete; > > - label_text & operator= (const label_text &) = delete; > > - > > - /* Create a label_text instance that borrows BUFFER from a > > - longer-lived owner. */ > > - static label_text borrow (const char *buffer) > > - { > > - return label_text (const_cast <char *> (buffer), false); > > - } > > - > > - /* Create a label_text instance that takes ownership of BUFFER. */ > > - static label_text take (char *buffer) > > - { > > - return label_text (buffer, true); > > - } > > - > > - void release () > > - { > > - m_buffer = NULL; > > - m_owned = false; > > - } > > - > > - const char *get () const > > - { > > - return m_buffer; > > - } > > - > > - bool is_owner () const > > - { > > - return m_owned; > > - } > > - > > -private: > > - char *m_buffer; > > - bool m_owned; > > - > > - label_text (char *buffer, bool owned) > > - : m_buffer (buffer), m_owned (owned) > > - {} > > -}; > > - > > /* Abstract base class for labelling a range within a rich_location > > (e.g. for labelling expressions with their type). > > > > -- > > 2.26.3 > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [pushed 2/3] libcpp: move label_text to its own header 2024-06-06 15:40 ` Andrew Pinski @ 2024-06-06 16:00 ` David Malcolm 2024-06-06 17:05 ` Andrew Pinski 0 siblings, 1 reply; 8+ messages in thread From: David Malcolm @ 2024-06-06 16:00 UTC (permalink / raw) To: Andrew Pinski, Bert Wesarg; +Cc: gcc-patches On Thu, 2024-06-06 at 08:40 -0700, Andrew Pinski wrote: > On Thu, Jun 6, 2024 at 6:02 AM Bert Wesarg > <bert.wesarg@googlemail.com> wrote: > > > > Dear David, > > > > On Tue, May 28, 2024 at 10:07 PM David Malcolm > > <dmalcolm@redhat.com> wrote: > > > > > > No functional change intended. > > > > > > Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. > > > Pushed to trunk as r15-874-g9bda2c4c81b668. > > > > > > libcpp/ChangeLog: > > > * Makefile.in (TAGS_SOURCES): Add include/label-text.h. > > > * include/label-text.h: New file. > > > * include/rich-location.h: Include "label-text.h". > > > (class label_text): Move to label-text.h. > > > > > > Signed-off-by: David Malcolm <dmalcolm@redhat.com> > > > --- > > > libcpp/Makefile.in | 2 +- > > > libcpp/include/label-text.h | 102 > > > +++++++++++++++++++++++++++++++++ > > > libcpp/include/rich-location.h | 79 +------------------------ > > > 3 files changed, 105 insertions(+), 78 deletions(-) > > > create mode 100644 libcpp/include/label-text.h > > > > > > diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in > > > index ebbca37777fb..7e47153264c0 100644 > > > --- a/libcpp/Makefile.in > > > +++ b/libcpp/Makefile.in > > > @@ -271,7 +271,7 @@ ETAGS = @ETAGS@ > > > > > > TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h system.h ucnid.h \ > > > include/cpplib.h include/line-map.h include/mkdeps.h > > > include/symtab.h \ > > > - include/rich-location.h > > > + include/rich-location.h include/label-text.h > > > > this does not seem to be enough that the new header will be > > installed. > > I get compile errors when compiling an plug-in with this patch: > > > > In file included from > > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux- > > gnu/15.0.0/plugin/include/diagnostic.h:24, > > from > > /home/bitten/builds/oCyPvWN6/1/perftools/cicd/scorep/src/build-gcc- > > plugin/../src/adapters/compiler/gcc- > > plugin/scorep_plugin_inst_descriptor.cpp:43: > > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux- > > gnu/15.0.0/plugin/include/rich-location.h:25:10: > > fatal error: label-text.h: No such file or directory > > 25 | #include "label-text.h" > > > ^~~~~~~~~~~~~~ > > compilation terminated. > > I have a fix which I am testing. Likewise (and sorry about the breakage) Dave ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [pushed 2/3] libcpp: move label_text to its own header 2024-06-06 16:00 ` David Malcolm @ 2024-06-06 17:05 ` Andrew Pinski 2024-06-17 17:42 ` Bert Wesarg 0 siblings, 1 reply; 8+ messages in thread From: Andrew Pinski @ 2024-06-06 17:05 UTC (permalink / raw) To: David Malcolm; +Cc: Bert Wesarg, gcc-patches On Thu, Jun 6, 2024 at 9:00 AM David Malcolm <dmalcolm@redhat.com> wrote: > > On Thu, 2024-06-06 at 08:40 -0700, Andrew Pinski wrote: > > On Thu, Jun 6, 2024 at 6:02 AM Bert Wesarg > > <bert.wesarg@googlemail.com> wrote: > > > > > > Dear David, > > > > > > On Tue, May 28, 2024 at 10:07 PM David Malcolm > > > <dmalcolm@redhat.com> wrote: > > > > > > > > No functional change intended. > > > > > > > > Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. > > > > Pushed to trunk as r15-874-g9bda2c4c81b668. > > > > > > > > libcpp/ChangeLog: > > > > * Makefile.in (TAGS_SOURCES): Add include/label-text.h. > > > > * include/label-text.h: New file. > > > > * include/rich-location.h: Include "label-text.h". > > > > (class label_text): Move to label-text.h. > > > > > > > > Signed-off-by: David Malcolm <dmalcolm@redhat.com> > > > > --- > > > > libcpp/Makefile.in | 2 +- > > > > libcpp/include/label-text.h | 102 > > > > +++++++++++++++++++++++++++++++++ > > > > libcpp/include/rich-location.h | 79 +------------------------ > > > > 3 files changed, 105 insertions(+), 78 deletions(-) > > > > create mode 100644 libcpp/include/label-text.h > > > > > > > > diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in > > > > index ebbca37777fb..7e47153264c0 100644 > > > > --- a/libcpp/Makefile.in > > > > +++ b/libcpp/Makefile.in > > > > @@ -271,7 +271,7 @@ ETAGS = @ETAGS@ > > > > > > > > TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h system.h ucnid.h \ > > > > include/cpplib.h include/line-map.h include/mkdeps.h > > > > include/symtab.h \ > > > > - include/rich-location.h > > > > + include/rich-location.h include/label-text.h > > > > > > this does not seem to be enough that the new header will be > > > installed. > > > I get compile errors when compiling an plug-in with this patch: > > > > > > In file included from > > > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux- > > > gnu/15.0.0/plugin/include/diagnostic.h:24, > > > from > > > /home/bitten/builds/oCyPvWN6/1/perftools/cicd/scorep/src/build-gcc- > > > plugin/../src/adapters/compiler/gcc- > > > plugin/scorep_plugin_inst_descriptor.cpp:43: > > > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux- > > > gnu/15.0.0/plugin/include/rich-location.h:25:10: > > > fatal error: label-text.h: No such file or directory > > > 25 | #include "label-text.h" > > > > ^~~~~~~~~~~~~~ > > > compilation terminated. > > > > I have a fix which I am testing. > > Likewise (and sorry about the breakage) Committed as r15-1076-g6e6471806d886b . > > Dave > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [pushed 2/3] libcpp: move label_text to its own header 2024-06-06 17:05 ` Andrew Pinski @ 2024-06-17 17:42 ` Bert Wesarg 0 siblings, 0 replies; 8+ messages in thread From: Bert Wesarg @ 2024-06-17 17:42 UTC (permalink / raw) To: Andrew Pinski; +Cc: David Malcolm, gcc-patches Hi, On Thu, Jun 6, 2024 at 7:05 PM Andrew Pinski <pinskia@gmail.com> wrote: > > On Thu, Jun 6, 2024 at 9:00 AM David Malcolm <dmalcolm@redhat.com> wrote: > > > > On Thu, 2024-06-06 at 08:40 -0700, Andrew Pinski wrote: > > > On Thu, Jun 6, 2024 at 6:02 AM Bert Wesarg > > > <bert.wesarg@googlemail.com> wrote: > > > > > > > > Dear David, > > > > > > > > On Tue, May 28, 2024 at 10:07 PM David Malcolm > > > > <dmalcolm@redhat.com> wrote: > > > > > > > > > > No functional change intended. > > > > > > > > > > Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. > > > > > Pushed to trunk as r15-874-g9bda2c4c81b668. > > > > > > > > > > libcpp/ChangeLog: > > > > > * Makefile.in (TAGS_SOURCES): Add include/label-text.h. > > > > > * include/label-text.h: New file. > > > > > * include/rich-location.h: Include "label-text.h". > > > > > (class label_text): Move to label-text.h. > > > > > > > > > > Signed-off-by: David Malcolm <dmalcolm@redhat.com> > > > > > --- > > > > > libcpp/Makefile.in | 2 +- > > > > > libcpp/include/label-text.h | 102 > > > > > +++++++++++++++++++++++++++++++++ > > > > > libcpp/include/rich-location.h | 79 +------------------------ > > > > > 3 files changed, 105 insertions(+), 78 deletions(-) > > > > > create mode 100644 libcpp/include/label-text.h > > > > > > > > > > diff --git a/libcpp/Makefile.in b/libcpp/Makefile.in > > > > > index ebbca37777fb..7e47153264c0 100644 > > > > > --- a/libcpp/Makefile.in > > > > > +++ b/libcpp/Makefile.in > > > > > @@ -271,7 +271,7 @@ ETAGS = @ETAGS@ > > > > > > > > > > TAGS_SOURCES = $(libcpp_a_SOURCES) internal.h system.h ucnid.h \ > > > > > include/cpplib.h include/line-map.h include/mkdeps.h > > > > > include/symtab.h \ > > > > > - include/rich-location.h > > > > > + include/rich-location.h include/label-text.h > > > > > > > > this does not seem to be enough that the new header will be > > > > installed. > > > > I get compile errors when compiling an plug-in with this patch: > > > > > > > > In file included from > > > > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux- > > > > gnu/15.0.0/plugin/include/diagnostic.h:24, > > > > from > > > > /home/bitten/builds/oCyPvWN6/1/perftools/cicd/scorep/src/build-gcc- > > > > plugin/../src/adapters/compiler/gcc- > > > > plugin/scorep_plugin_inst_descriptor.cpp:43: > > > > /home/bitten/opt/gcc-15-20240602/lib/gcc/x86_64-pc-linux- > > > > gnu/15.0.0/plugin/include/rich-location.h:25:10: > > > > fatal error: label-text.h: No such file or directory > > > > 25 | #include "label-text.h" > > > > > ^~~~~~~~~~~~~~ > > > > compilation terminated. > > > > > > I have a fix which I am testing. > > > > Likewise (and sorry about the breakage) > > Committed as r15-1076-g6e6471806d886b . Thanks. I can confirm, that my external plugin builds again. Bert > > > > > Dave > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* [pushed 3/3] diagnostics: consolidate global state in diagnostic-color.cc 2024-05-28 20:07 [pushed 1/3] selftests: split out make_fndecl from selftest.h to its own header David Malcolm 2024-05-28 20:07 ` [pushed 2/3] libcpp: move label_text " David Malcolm @ 2024-05-28 20:07 ` David Malcolm 1 sibling, 0 replies; 8+ messages in thread From: David Malcolm @ 2024-05-28 20:07 UTC (permalink / raw) To: gcc-patches; +Cc: David Malcolm Simplify the table of default colors, avoiding the need to manually add the strlen of each entry. Consolidate the global state in diagnostic-color.cc into a g_color_dict, adding selftests for the new class diagnostic_color_dict. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Tested with "make selftest-valgrind" and manually with various values for GCC_COLORS. Pushed to trunk as r15-875-g21fc89bac61983. gcc/ChangeLog: * diagnostic-color.cc: Define INCLUDE_VECTOR. Include "label-text.h" and "selftest.h". (struct color_cap): Replace with... (struct color_default): ...this, adding "m_" prefixes to fields and dropping "name_len" and "free_val" field. (color_dict): Convert to... (gcc_color_defaults): ...this, making const, dropping the trailing strlen and "false" from each entry. (class diagnostic_color_dict): New. (g_color_dict): New. (colorize_start): Reimplement in terms of g_color_dict. (diagnostic_color_dict::get_entry_by_name): New, based on colorize_start. (diagnostic_color_dict::get_start_by_name): Likewise. (diagnostic_color_dict::diagnostic_color_dict): New. (parse_gcc_colors): Reimplement, moving body... (diagnostic_color_dict::parse_envvar_value): ...here. (colorize_init): Lazily create g_color_dict. (selftest::test_empty_color_dict): New. (selftest::test_default_color_dict): New. (selftest::test_color_dict_envvar_parsing): New. (selftest::diagnostic_color_cc_tests): New. * selftest-run-tests.cc (selftest::run_tests): Call selftest::diagnostic_color_cc_tests. * selftest.h (selftest::diagnostic_color_cc_tests): New decl. Signed-off-by: David Malcolm <dmalcolm@redhat.com> --- gcc/diagnostic-color.cc | 277 +++++++++++++++++++++++++++++--------- gcc/selftest-run-tests.cc | 1 + gcc/selftest.h | 1 + 3 files changed, 216 insertions(+), 63 deletions(-) diff --git a/gcc/diagnostic-color.cc b/gcc/diagnostic-color.cc index f01a0fc2e377..cbe57ce763f2 100644 --- a/gcc/diagnostic-color.cc +++ b/gcc/diagnostic-color.cc @@ -17,9 +17,11 @@ 02110-1301, USA. */ #include "config.h" +#define INCLUDE_VECTOR #include "system.h" #include "diagnostic-color.h" #include "diagnostic-url.h" +#include "label-text.h" #ifdef __MINGW32__ # define WIN32_LEAN_AND_MEAN @@ -27,6 +29,7 @@ #endif #include "color-macros.h" +#include "selftest.h" /* The context and logic for choosing default --color screen attributes (foreground and background colors, etc.) are the following. @@ -72,56 +75,124 @@ counterparts) and possibly bold blue. */ /* Default colors. The user can overwrite them using environment variable GCC_COLORS. */ -struct color_cap +struct color_default { - const char *name; - const char *val; - unsigned char name_len; - bool free_val; + const char *m_name; + const char *m_val; }; /* For GCC_COLORS. */ -static struct color_cap color_dict[] = +static const color_default gcc_color_defaults[] = { - { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 5, false }, - { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA), - 7, false }, - { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false }, - { "range1", SGR_SEQ (COLOR_FG_GREEN), 6, false }, - { "range2", SGR_SEQ (COLOR_FG_BLUE), 6, false }, - { "locus", SGR_SEQ (COLOR_BOLD), 5, false }, - { "quote", SGR_SEQ (COLOR_BOLD), 5, false }, - { "path", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false }, - { "fnname", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 6, false }, - { "targs", SGR_SEQ (COLOR_FG_MAGENTA), 5, false }, - { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false }, - { "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false }, - { "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false }, - { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false }, - { "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false }, - { "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false }, - { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false }, - { "valid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 5, false }, - { "invalid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 7, false }, - { NULL, NULL, 0, false } + { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED) }, + { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA) }, + { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN) }, + { "range1", SGR_SEQ (COLOR_FG_GREEN) }, + { "range2", SGR_SEQ (COLOR_FG_BLUE) }, + { "locus", SGR_SEQ (COLOR_BOLD) }, + { "quote", SGR_SEQ (COLOR_BOLD) }, + { "path", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN) }, + { "fnname", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN) }, + { "targs", SGR_SEQ (COLOR_FG_MAGENTA) }, + { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN) }, + { "fixit-delete", SGR_SEQ (COLOR_FG_RED) }, + { "diff-filename", SGR_SEQ (COLOR_BOLD) }, + { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN) }, + { "diff-delete", SGR_SEQ (COLOR_FG_RED) }, + { "diff-insert", SGR_SEQ (COLOR_FG_GREEN) }, + { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN) }, + { "valid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN) }, + { "invalid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED) } }; +class diagnostic_color_dict +{ +public: + diagnostic_color_dict (const color_default *default_values, + size_t num_default_values); + + bool parse_envvar_value (const char *const envvar_value); + + const char *get_start_by_name (const char *name, size_t name_len) const; + const char *get_start_by_name (const char *name) const + { + return get_start_by_name (name, strlen (name)); + } + +private: + struct entry + { + entry (const color_default &d) + : m_name (d.m_name), + m_name_len (strlen (d.m_name)), + m_val (label_text::borrow (d.m_val)) + { + } + + const char *m_name; + size_t m_name_len; + label_text m_val; + }; + + const entry *get_entry_by_name (const char *name, size_t name_len) const; + entry *get_entry_by_name (const char *name, size_t name_len); + + std::vector<entry> m_entries; +}; + +static diagnostic_color_dict *g_color_dict; + const char * colorize_start (bool show_color, const char *name, size_t name_len) { - struct color_cap const *cap; - if (!show_color) return ""; - for (cap = color_dict; cap->name; cap++) - if (cap->name_len == name_len - && memcmp (cap->name, name, name_len) == 0) - break; - if (cap->name == NULL) + if (!g_color_dict) return ""; - return cap->val; + return g_color_dict->get_start_by_name (name, name_len); +} + +/* Look for an entry named NAME of length NAME_LEN within this + diagnostic_color_dict, or nullptr if there isn't one. */ + +const diagnostic_color_dict::entry * +diagnostic_color_dict::get_entry_by_name (const char *name, + size_t name_len) const +{ + for (auto &iter : m_entries) + if (iter.m_name_len == name_len + && memcmp (iter.m_name, name, name_len) == 0) + return &iter; + return nullptr; +} + +/* Non-const version of the above. */ + +diagnostic_color_dict::entry * +diagnostic_color_dict::get_entry_by_name (const char *name, + size_t name_len) +{ + for (auto &iter : m_entries) + if (iter.m_name_len == name_len + && memcmp (iter.m_name, name, name_len) == 0) + return &iter; + return nullptr; +} + +/* Return the SGR codes to start a color entry named NAME of length + NAME_LEN within this diagnostic_color_dict, or the empty string if + there isn't one. */ + +const char * +diagnostic_color_dict::get_start_by_name (const char *name, + size_t name_len) const +{ + if (const entry *e = get_entry_by_name (name, name_len)) + return e->m_val.get (); + + return ""; } const char * @@ -130,56 +201,58 @@ colorize_stop (bool show_color) return show_color ? SGR_RESET : ""; } -/* Parse GCC_COLORS. The default would look like: - GCC_COLORS='error=01;31:warning=01;35:note=01;36:\ - range1=32:range2=34:locus=01:quote=01:path=01;36:\ - fixit-insert=32:fixit-delete=31:'\ - diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\ - type-diff=01;32' - No character escaping is needed or supported. */ -static bool -parse_gcc_colors (void) +/* diagnostic_color_dict's ctor. Initialize it from the given array + of color_default values. */ + +diagnostic_color_dict:: +diagnostic_color_dict (const color_default *default_values, + size_t num_default_values) { - const char *p, *q, *name, *val; - char *b; - size_t name_len = 0, val_len = 0; + m_entries.reserve (num_default_values); + for (size_t idx = 0; idx < num_default_values; idx++) + m_entries.push_back (entry (default_values[idx])); +} - p = getenv ("GCC_COLORS"); /* Plural! */ - if (p == NULL) +/* Parse a list of color definitions from an environment variable + value (such as that of GCC_COLORS). + No character escaping is needed or supported. */ + +bool +diagnostic_color_dict::parse_envvar_value (const char *const envvar_value) +{ + /* envvar not set: use the default colors. */ + if (envvar_value == nullptr) return true; - if (*p == '\0') + + /* envvar set to empty string: disable colorization. */ + if (*envvar_value == '\0') return false; - name = q = p; + const char *q, *name, *val; + size_t name_len = 0, val_len = 0; + + name = q = envvar_value; val = NULL; /* From now on, be well-formed or you're gone. */ for (;;) if (*q == ':' || *q == '\0') { - struct color_cap *cap; - if (val) val_len = q - val; else name_len = q - name; /* Empty name without val (empty cap) won't match and will be ignored. */ - for (cap = color_dict; cap->name; cap++) - if (cap->name_len == name_len - && memcmp (cap->name, name, name_len) == 0) - break; + entry *e = get_entry_by_name (name, name_len); /* If name unknown, go on for forward compatibility. */ - if (cap->val && val) + if (e && val) { - if (cap->free_val) - free (CONST_CAST (char *, cap->val)); - b = XNEWVEC (char, val_len + sizeof (SGR_SEQ (""))); + char *b = XNEWVEC (char, val_len + sizeof (SGR_SEQ (""))); memcpy (b, SGR_START, strlen (SGR_START)); memcpy (b + strlen (SGR_START), val, val_len); memcpy (b + strlen (SGR_START) + val_len, SGR_END, sizeof (SGR_END)); - cap->val = (const char *) b; - cap->free_val = true; + e->m_val = label_text::take (b); } if (*q == '\0') return true; @@ -203,6 +276,20 @@ parse_gcc_colors (void) return true; } +/* Parse GCC_COLORS. The default would look like: + GCC_COLORS='error=01;31:warning=01;35:note=01;36:\ + range1=32:range2=34:locus=01:quote=01:path=01;36:\ + fixit-insert=32:fixit-delete=31:'\ + diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\ + type-diff=01;32'. */ +static bool +parse_gcc_colors () +{ + if (!g_color_dict) + return false; + return g_color_dict->parse_envvar_value (getenv ("GCC_COLORS")); /* Plural! */ +} + /* Return true if we should use color when in auto mode, false otherwise. */ static bool should_colorize (void) @@ -229,6 +316,10 @@ should_colorize (void) bool colorize_init (diagnostic_color_rule_t rule) { + if (!g_color_dict) + g_color_dict = new diagnostic_color_dict (gcc_color_defaults, + ARRAY_SIZE (gcc_color_defaults)); + switch (rule) { case DIAGNOSTICS_COLOR_NO: @@ -351,3 +442,63 @@ determine_url_format (diagnostic_url_rule_t rule) gcc_unreachable (); } } + +#if CHECKING_P + +namespace selftest { + +/* Test of an empty diagnostic_color_dict. */ + +static void +test_empty_color_dict () +{ + diagnostic_color_dict d (nullptr, 0); + ASSERT_STREQ (d.get_start_by_name ("warning"), ""); + ASSERT_STREQ (d.get_start_by_name ("should-not-be-found"), ""); +} + +/* Test of a diagnostic_color_dict with GCC's defaults. */ + +static void +test_default_color_dict () +{ + diagnostic_color_dict d (gcc_color_defaults, + ARRAY_SIZE (gcc_color_defaults)); + ASSERT_STREQ (d.get_start_by_name ("warning"), + SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA)); + ASSERT_STREQ (d.get_start_by_name ("should-not-be-found"), ""); +} + +/* Test of a diagnostic_color_dict with GCC's defaults plus overrides from + an environment variable. */ + +static void +test_color_dict_envvar_parsing () +{ + diagnostic_color_dict d (gcc_color_defaults, + ARRAY_SIZE (gcc_color_defaults)); + + d.parse_envvar_value ("error=01;37:warning=01;42:unknown-value=01;36"); + + ASSERT_STREQ (d.get_start_by_name ("error"), + SGR_SEQ ("01;37")); + ASSERT_STREQ (d.get_start_by_name ("warning"), + SGR_SEQ ("01;42")); + ASSERT_STREQ (d.get_start_by_name ("unknown-value"), ""); + ASSERT_STREQ (d.get_start_by_name ("should-not-be-found"), ""); +} + + +/* Run all of the selftests within this file. */ + +void +diagnostic_color_cc_tests () +{ + test_empty_color_dict (); + test_default_color_dict (); + test_color_dict_envvar_parsing (); +} + +} // namespace selftest + +#endif /* #if CHECKING_P */ diff --git a/gcc/selftest-run-tests.cc b/gcc/selftest-run-tests.cc index 1c99de1e6c2a..d8f5e4b34c68 100644 --- a/gcc/selftest-run-tests.cc +++ b/gcc/selftest-run-tests.cc @@ -94,6 +94,7 @@ selftest::run_tests () /* Higher-level tests, or for components that other selftests don't rely on. */ + diagnostic_color_cc_tests (); diagnostic_show_locus_cc_tests (); diagnostic_format_json_cc_tests (); edit_context_cc_tests (); diff --git a/gcc/selftest.h b/gcc/selftest.h index 808d432ec480..9e294ad1e5f9 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -220,6 +220,7 @@ extern void attribs_cc_tests (); extern void bitmap_cc_tests (); extern void cgraph_cc_tests (); extern void convert_cc_tests (); +extern void diagnostic_color_cc_tests (); extern void diagnostic_format_json_cc_tests (); extern void diagnostic_show_locus_cc_tests (); extern void digraph_cc_tests (); -- 2.26.3 ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-06-17 17:42 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2024-05-28 20:07 [pushed 1/3] selftests: split out make_fndecl from selftest.h to its own header David Malcolm 2024-05-28 20:07 ` [pushed 2/3] libcpp: move label_text " David Malcolm 2024-06-06 13:01 ` Bert Wesarg 2024-06-06 15:40 ` Andrew Pinski 2024-06-06 16:00 ` David Malcolm 2024-06-06 17:05 ` Andrew Pinski 2024-06-17 17:42 ` Bert Wesarg 2024-05-28 20:07 ` [pushed 3/3] diagnostics: consolidate global state in diagnostic-color.cc David Malcolm
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 read-only IMAP folder(s) and NNTP newsgroup(s).