From 6e53c52d3bf42bac72d7437684657ac442499616 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 3 Sep 2021 17:31:29 -0400 Subject: [PATCH] common: compile libstring-utils as C++ MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The code of string-utils.cpp is compiled as C++, but the functions are still exported as C symbols for the moment (until all users are converted to C++). The only thing of interest here is this error: CXX string-utils.lo /home/simark/src/lttng-tools/src/common/string-utils/string-utils.cpp: In function ‘star_glob_pattern_type_flags strutils_test_glob_pattern(const char*)’: /home/simark/src/lttng-tools/src/common/string-utils/string-utils.cpp:89:37: error: invalid conversion from ‘int’ to ‘star_glob_pattern_type_flags’ [-fpermissive] 89 | ret |= STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY; | ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | int In C++, you can't freely use bitwise operator on enumerators. I added an operator|= free function to handle it, which converts to the underlying type and back. If we have many of these enums used as flags, we could think of adding a class for that, like enum_flags in GDB: https://gitlab.com/gnutools/binutils-gdb/-/blob/7a6cb96b710257a4f5bc7e85cc103b6bf8dfc25c/gdbsupport/enum-flags.h Change-Id: I64b458a6f6c1e5a131525826a116607eef824aaa Signed-off-by: Simon Marchi Signed-off-by: Jérémie Galarneau --- src/common/string-utils/Makefile.am | 5 ++++- .../{string-utils.c => string-utils.cpp} | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) rename src/common/string-utils/{string-utils.c => string-utils.cpp} (94%) diff --git a/src/common/string-utils/Makefile.am b/src/common/string-utils/Makefile.am index 973c1d1e7..212b3d567 100644 --- a/src/common/string-utils/Makefile.am +++ b/src/common/string-utils/Makefile.am @@ -2,4 +2,7 @@ noinst_LTLIBRARIES = libstring-utils.la -libstring_utils_la_SOURCES = string-utils.h string-utils.c format.h +libstring_utils_la_SOURCES = \ + format.h \ + string-utils.cpp \ + string-utils.h diff --git a/src/common/string-utils/string-utils.c b/src/common/string-utils/string-utils.cpp similarity index 94% rename from src/common/string-utils/string-utils.c rename to src/common/string-utils/string-utils.cpp index 1bf0cc060..3051644f3 100644 --- a/src/common/string-utils/string-utils.c +++ b/src/common/string-utils/string-utils.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "string-utils.h" #include "../macros.h" @@ -19,6 +20,16 @@ enum star_glob_pattern_type_flags { STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY = 2, }; +static +star_glob_pattern_type_flags &operator|=(star_glob_pattern_type_flags &l, + star_glob_pattern_type_flags r) +{ + using T = std::underlying_type::type; + l = static_cast ( + static_cast (l) | static_cast (r)); + return l; +} + /* * Normalizes the star-only globbing pattern `pattern`, that is, crushes * consecutive `*` characters into a single `*`, avoiding `\*`. @@ -133,7 +144,7 @@ char *strutils_unescape_string(const char *input, char only_char) const char *i; LTTNG_ASSERT(input); - output = zmalloc(strlen(input) + 1); + output = (char *) zmalloc(strlen(input) + 1); if (!output) { goto end; } @@ -284,7 +295,7 @@ int strutils_split(const char *input, for (at = 0, s = input; at < number_of_substrings; at++) { const char *ss; char *d; - char *substring = zmalloc(longest_substring_len + 1); + char *substring = (char *) zmalloc(longest_substring_len + 1); if (!substring) { goto error; -- 2.34.1