Enable unused/unneeded member function warnings Enable -Wunused-member-function and -Wunneeded-member-function for non-GCC (Clang) builds in build/gen.py, and clean up the flagged member functions: - Remove unused Metrics struct, SetRightToListOfValue overload, OutputConversionTest::scope(), and redundant defaulted destructors in visual_studio_writer.cc. - Replace NonASCIIMask explicit specializations in string_util.cc with an if-constexpr function template so both 32-bit and 64-bit builds stay warning-free. - Guard Slab::destroy() and TargetWriteInfo::LeakOnPurpose() with ASAN_ENABLED to match their call sites. Bug: None Test: ninja -C out run_tests Change-Id: I29abe51019381a561f21861b229ee608a639604a Reviewed-on: https://gn-review.googlesource.com/c/gn/+/29642 Commit-Queue: Takuto Ikuta <tikuta@google.com> Reviewed-by: David Turner <digit@google.com>
diff --git a/build/gen.py b/build/gen.py index b2f1509..9bbf3d4 100755 --- a/build/gen.py +++ b/build/gen.py
@@ -572,7 +572,12 @@ cflags.append('-Wno-restrict') # flags not supported by gcc/g++. else: - cflags.extend(['-Wrange-loop-analysis', '-Wextra-semi-stmt']) + cflags.extend([ + '-Wextra-semi-stmt', + '-Wrange-loop-analysis', + '-Wunneeded-member-function', + '-Wunused-member-function', + ]) # We use "extern C" to communicate with rust, not C. So it doesn't matter # if the return type is an invalid C type. cflags.append('-Wno-return-type-c-linkage')
diff --git a/src/base/strings/string_util.cc b/src/base/strings/string_util.cc index b00d77f..0c5d137 100644 --- a/src/base/strings/string_util.cc +++ b/src/base/strings/string_util.cc
@@ -20,6 +20,7 @@ #include <iterator> #include <limits> #include <string> +#include <type_traits> #include <vector> #include "base/logging.h" @@ -65,24 +66,18 @@ ~kMachineWordAlignmentMask); } -template <size_t size, typename CharacterType> -struct NonASCIIMask; -template <> -struct NonASCIIMask<4, char16_t> { - static inline uint32_t value() { return 0xFF80FF80U; } -}; -template <> -struct NonASCIIMask<4, char> { - static inline uint32_t value() { return 0x80808080U; } -}; -template <> -struct NonASCIIMask<8, char16_t> { - static inline uint64_t value() { return 0xFF80FF80FF80FF80ULL; } -}; -template <> -struct NonASCIIMask<8, char> { - static inline uint64_t value() { return 0x8080808080808080ULL; } -}; +template <typename CharacterType> +constexpr MachineWord NonASCIIMask() { + static_assert(std::is_same_v<CharacterType, char> || + std::is_same_v<CharacterType, char16_t>); + if constexpr (sizeof(MachineWord) == 4) { + return std::is_same_v<CharacterType, char16_t> ? 0xFF80FF80U : 0x80808080U; + } else { + static_assert(sizeof(MachineWord) == 8); + return std::is_same_v<CharacterType, char16_t> ? 0xFF80FF80FF80FF80ULL + : 0x8080808080808080ULL; + } +} } // namespace @@ -433,8 +428,7 @@ ++characters; } - MachineWord non_ascii_bit_mask = - NonASCIIMask<sizeof(MachineWord), Char>::value(); + MachineWord non_ascii_bit_mask = NonASCIIMask<Char>(); return !(all_char_bits & non_ascii_bit_mask); }
diff --git a/src/gn/command_format.cc b/src/gn/command_format.cc index b015718..2ec0067 100644 --- a/src/gn/command_format.cc +++ b/src/gn/command_format.cc
@@ -151,13 +151,6 @@ kSequenceStyleBracedBlockAlreadyOpen, }; - struct Metrics { - Metrics() : first_length(-1), longest_length(-1), multiline(false) {} - int first_length; - int longest_length; - bool multiline; - }; - // Add to output. void Print(std::string_view str);
diff --git a/src/gn/command_gen.cc b/src/gn/command_gen.cc index 0b95483..8d94c0b 100644 --- a/src/gn/command_gen.cc +++ b/src/gn/command_gen.cc
@@ -105,7 +105,9 @@ std::unique_ptr<ResolvedTargetData> resolved = std::make_unique<ResolvedTargetData>(); +#ifndef ASAN_ENABLED void LeakOnPurpose() { (void)resolved.release(); } +#endif }; // Called on worker thread to write the ninja file.
diff --git a/src/gn/operators_unittest.cc b/src/gn/operators_unittest.cc index cdbbb7c..6590f44 100644 --- a/src/gn/operators_unittest.cc +++ b/src/gn/operators_unittest.cc
@@ -82,12 +82,6 @@ list.list_value().push_back(value); set_right(std::make_unique<TestParseNode>(list)); } - void SetRightToListOfValue(const Value& value1, const Value& value2) { - Value list(nullptr, Value::LIST); - list.list_value().push_back(value1); - list.list_value().push_back(value2); - set_right(std::make_unique<TestParseNode>(list)); - } private: // The base class takes the Token by reference, this manages the lifetime.
diff --git a/src/gn/output_conversion_unittest.cc b/src/gn/output_conversion_unittest.cc index 52f350c..4db3d42 100644 --- a/src/gn/output_conversion_unittest.cc +++ b/src/gn/output_conversion_unittest.cc
@@ -22,7 +22,6 @@ OutputConversionTest() = default; const Settings* settings() { return setup_.settings(); } - Scope* scope() { return setup_.scope(); } private: TestWithScope setup_;
diff --git a/src/gn/string_atom.cc b/src/gn/string_atom.cc index f9b3be7..00cb9b1 100644 --- a/src/gn/string_atom.cc +++ b/src/gn/string_atom.cc
@@ -160,11 +160,13 @@ return result; } +#ifdef ASAN_ENABLED void destroy(size_t max_index) { for (size_t i = 0; i < max_index; ++i) { items_[i].str.~basic_string(); } } +#endif private: StringStorage items_[kStringsPerSlab];
diff --git a/src/gn/visual_studio_writer.cc b/src/gn/visual_studio_writer.cc index 6d940eb..0798b23 100644 --- a/src/gn/visual_studio_writer.cc +++ b/src/gn/visual_studio_writer.cc
@@ -46,7 +46,6 @@ struct IncludeDirWriter { explicit IncludeDirWriter(PathOutput& path_output) : path_output_(path_output) {} - ~IncludeDirWriter() = default; void operator()(const SourceDir& dir, StringOutputBuffer& out) const { path_output_.WriteDir(out, dir, PathOutput::DIR_NO_LAST_SLASH); @@ -59,7 +58,6 @@ struct SourceFileWriter { SourceFileWriter(PathOutput& path_output, const SourceFile& source_file) : path_output_(path_output), source_file_(source_file) {} - ~SourceFileWriter() = default; void operator()(StringOutputBuffer& out) const { path_output_.WriteFile(out, source_file_);