Skip to content

fix(cpp): address clang-tidy findings and add static analysis config - #1171

Open
ColinLeeo wants to merge 4 commits into
developfrom
colin/fix-clang-tidy
Open

ColinLeeo wants to merge 4 commits into
developfrom
colin/fix-clang-tidy

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a clang-tidy configuration for the C++ implementation and fixes the
findings it reports on cpp/src (61 translation units; third_party and the
ANTLR-generated parser are excluded from the reported scope).

Real defects

  • restorable_tsfile_io_writer: an allocation failure during the recovery
    scan was swallowed and the function returned E_OK; E_OOM is now
    propagated to the caller (releasing the pending chunk group's device
    reference first).
  • tsfile_io_writer: the result of the bloom-filter add_path_entry call was
    overwritten by the following RET_FAIL(...), so a failed insert was
    ignored. The call is now checked, and the success-only assertion at the end
    of the index loop is gone so the failure propagates instead of aborting
    debug builds.
  • dataset_index: the name/series lookups used a section view without checking
    that the section was present, so a malformed index could be dereferenced
    through a null pointer; they now return NOT_FOUND.
  • local_random_access_read_file: the file generation probe could call
    fstat on a closed descriptor; it now rejects fd_ < 0 first.

Correctness / design cleanups

  • ResultSet now inherits enable_shared_from_this publicly.
  • Virtual calls made from constructors/destructors are qualified so they do
    not depend on virtual dispatch (PlainEncoder, TS2DIFFEncoder/Decoder,
    SingleDeviceTsBlockReader, TableResultSet, LZ4Compressor,
    ZstdCompressor, LocalRandomAccessReadFile, WriteFile).
  • TS2DIFFDecoder: error paths return a defined value instead of an
    indeterminate one; QDSWithoutTimeGenerator: removed dead assignments in
    the skip branches.
  • DeviceOrderedTsBlockReader::next guards against a null current reader.

Performance / bugprone

  • Internal helpers take read-only shared_ptr/vector/string parameters
    by const reference (or move them into members where they are stored);
    release() results that are intentionally discarded are cast to void.
  • Vectors are reserved before loop appends; std::endl is replaced with
    '\n' in logging.
  • Chimp leading-zero tables use uint8_t; unsigned char values are cast
    before streaming to ostream in debug output; argument comments in
    tsfile_io_reader were corrected.

ABI / interface compatibility

Only internal and private helpers take parameters by const reference. The
public entry-point classes (TsFileReader, TsFileTreeWriter) and the
virtual filter interface keep their original by-value parameters, so the
installed headers and the shared-library ABI (SONAME epoch 1) are unchanged.

Intentional patterns

ret = E_XXX; ASSERT(false); error paths, the cwrapper's bounded
timeseries[slot] access, and ALP's bitwise float/double equality are
annotated with targeted NOLINT comments instead of being rewritten.

Testing

  • libtsfile and TsFile_Test build in WSL (Ubuntu 26.04, GCC 15.2,
    CMake 3.31) against a checkout of develop plus this change.
  • Full C++ unit test suite: 1918 passed, 3 skipped, 2 failed. Both failures
    are CLI tests that create a FIFO with mkfifo(), which the NTFS-backed
    /mnt/d workspace does not support (mkfifo returns -1); they are
    environment-only and unrelated to this change.
  • clang-tidy reports no findings in cpp/src; remaining warnings are confined
    to the generated PathParser.cpp.

- restorable writer: propagate allocation failures during the recovery scan
  instead of returning E_OK
- io writer: check the bloom filter add_path_entry result before serializing
  the index
- dataset index: reject missing sections before dereferencing their records
- local random-access file: reject a closed descriptor before fstat()
- result_set: inherit enable_shared_from_this publicly; qualify virtual
  calls made from constructors/destructors
- ts2diff decoder: return a defined value on corrupt input; qds: drop dead
  assignments
- pass read-only shared_ptr/vector/string parameters by const reference,
  and move parameters that are stored into members
- reserve vectors before loop appends; replace std::endl with '\n' in logs
- use uint8_t for the chimp leading-zero tables; cast unsigned char values
  before streaming
- add cpp/.clang-tidy (clang-analyzer, bugprone, performance checks)
@codecov-commenter

codecov-commenter commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.26437% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.26%. Comparing base (5db3d0c) to head (67ede9e).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
cpp/src/file/tsfile_io_writer.cc 0.00% 8 Missing and 2 partials ⚠️
cpp/src/common/allocator/mem_alloc.cc 0.00% 4 Missing ⚠️
cpp/src/reader/expression.cc 20.00% 4 Missing ⚠️
cpp/src/common/row_record.h 0.00% 2 Missing ⚠️
cpp/src/file/restorable_tsfile_io_writer.cc 0.00% 1 Missing and 1 partial ⚠️
cpp/src/common/allocator/byte_stream.h 0.00% 1 Missing ⚠️
.../src/reader/block/device_ordered_tsblock_reader.cc 50.00% 0 Missing and 1 partial ⚠️
cpp/src/reader/filter/tag_filter.cc 94.11% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1171      +/-   ##
===========================================
- Coverage    69.27%   69.26%   -0.01%     
===========================================
  Files          791      791              
  Lines        58564    58578      +14     
  Branches      9516     9517       +1     
===========================================
+ Hits         40570    40574       +4     
- Misses       16247    16255       +8     
- Partials      1747     1749       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The changes break existing SDK symbols and virtual contracts, while failure paths can abort finalization or leak recovery resources.

4 open issues
What changed in this PR

Adds static-analysis configuration and addresses clang-tidy findings across TsFile’s C++ implementation, with corresponding CLI updates.

Changes:

  • Configures analyzer, bug-prone-code, and performance checks with targeted suppressions.
  • Improves recovery error handling, index-writing checks, lookup guards, and decoder fallback values.
  • Reduces unnecessary copies and allocations, and makes destructor cleanup calls explicit.
File Description
cpp/​tools/​commands/​row_query.cc Updates the CLI tag-filter override signature.
cpp/​src/​writer/​tsfile_writer.h Uses const references in schema-check declarations.
cpp/​src/​writer/​tsfile_writer.cc Updates schema checks and annotates ownership transfers.
cpp/​src/​writer/​tsfile_tree_writer.h Changes batch-registration parameters to const reference.
cpp/​src/​writer/​tsfile_tree_writer.cc Updates the batch-registration definition.
cpp/​src/​reader/​tsfile_tree_reader.cc Reserves device-ID output capacity and avoids copies.
cpp/​src/​reader/​tsfile_series_scan_iterator.h Moves the device ID into iterator state.
cpp/​src/​reader/​tsfile_series_scan_iterator.cc Documents and suppresses a defensive flag reset.
cpp/​src/​reader/​tsfile_reader.h Updates reader parameter declarations.
cpp/​src/​reader/​tsfile_reader.cc Reduces parameter copies and reserves metadata capacity.
cpp/​src/​reader/​task/​device_task_iterator.h Moves task-iterator constructor arguments.
cpp/​src/​reader/​task/​device_query_task.h Moves arguments into task members.
cpp/​src/​reader/​task/​device_query_task.cc Moves arguments when constructing query tasks.
cpp/​src/​reader/​table_result_set.h Moves column metadata into result-set members.
cpp/​src/​reader/​table_result_set.cc Qualifies destructor cleanup.
cpp/​src/​reader/​table_query_executor.cc Reserves tag-column schema capacity.
cpp/​src/​reader/​result_set.h Makes shared-ownership inheritance public and adjusts logging.
cpp/​src/​reader/​qds_without_timegenerator.cc Removes dead assignments from skip branches.
cpp/​src/​reader/​filter/​tag_filter.h Changes tag-filter virtual parameters to const references.
cpp/​src/​reader/​filter/​tag_filter.cc Updates tag-filter method definitions.
cpp/​src/​reader/​filter/​filter.h Updates the base row-filter virtual signature.
cpp/​src/​reader/​filter/​filter_type.h Avoids flushing diagnostic output on each newline.
cpp/​src/​reader/​filter/​aligned_field_filter.h Uses const-reference column-name parameters.
cpp/​src/​reader/​filter/​aligned_field_filter.cc Updates aligned-filter factory definitions.
cpp/​src/​reader/​expression.cc Replaces flushing newlines in diagnostics.
cpp/​src/​reader/​chunk_reader.cc Annotates intentional error assignments before assertions.
cpp/​src/​reader/​block/​single_device_tsblock_reader.h Qualifies destructor cleanup.
cpp/​src/​reader/​block/​device_ordered_tsblock_reader.cc Adds a null-reader guard in next().
cpp/​src/​reader/​aligned_chunk_reader.cc Annotates intentional error assignments before assertions.
cpp/​src/​file/​write_file.h Qualifies the destructor’s close call.
cpp/​src/​file/​tsfile_io_writer.h Updates shared-pointer parameter declarations.
cpp/​src/​file/​tsfile_io_writer.cc Checks bloom-filter insertion errors and reduces copies.
cpp/​src/​file/​tsfile_io_reader.h Changes reader shared-pointer parameters to const references.
cpp/​src/​file/​tsfile_io_reader.cc Updates definitions and corrects argument-name comments.
cpp/​src/​file/​restorable_tsfile_io_writer.cc Returns allocation failures before recovery finalization.
cpp/​src/​file/​local_random_access_read_file.h Qualifies destruction and close calls.
cpp/​src/​file/​local_random_access_read_file.cc Rejects closed descriptors before generation probing.
cpp/​src/​encoding/​zigzag_encoder.h Removes unused trailing shifts.
cpp/​src/​encoding/​ts2diff_encoder.h Qualifies destructor cleanup.
cpp/​src/​encoding/​ts2diff_decoder.h Initializes scalar fallback return values.
cpp/​src/​encoding/​plain_encoder.h Qualifies destructor cleanup.
cpp/​src/​encoding/​dictionary_encoder.h Accepts string input by const reference.
cpp/​src/​encoding/​chimp_encoder.h Uses unsigned leading-zero table values.
cpp/​src/​encoding/​chimp_decoder.h Uses unsigned decoding table values.
cpp/​src/​encoding/​alp/​alp_traits.h Documents and suppresses intentional bitwise comparisons.
cpp/​src/​dataset/​dataset_index.cc Guards unavailable sections before lookups.
cpp/​src/​cwrapper/​tsfile_cwrapper.cc Reserves column capacity and annotates bounded array access.
cpp/​src/​compress/​zstd_compressor.h Qualifies destructor cleanup.
cpp/​src/​compress/​lz4_compressor.h Qualifies destructor cleanup.
cpp/​src/​common/​tsfile_common.h Reduces copies and prints byte-valued fields numerically.
cpp/​src/​common/​tsfile_common.cc Updates lookup signatures and annotates an error assignment.
cpp/​src/​common/​tablet.h Moves schema ownership and checks the stored member.
cpp/​src/​common/​tablet.cc Documents and suppresses a required by-value specialization.
cpp/​src/​common/​schema.h Reserves data-type vector capacity.
cpp/​src/​common/​row_record.h Replaces flushing newlines in diagnostics.
cpp/​src/​common/​device_id.cc Casts the segment constant before addition.
cpp/​src/​common/​allocator/​page_arena.h Computes aligned addresses using pointer advancement.
cpp/​src/​common/​allocator/​mem_alloc.cc Avoids repeated flushing in memory statistics.
cpp/​src/​common/​allocator/​byte_stream.h Replaces a flushing diagnostic newline.
cpp/​.clang-tidy Defines static-analysis checks, exclusions, and header filtering.

Review effort: Balanced

Comment on lines +474 to +475
if (RET_FAIL(
filter.add_path_entry(tmp_device_name, measurement_name))) {
Comment thread cpp/src/reader/filter/filter.h Outdated
}
virtual bool satisfyRow(int time,
std::vector<std::string*> segments) const {
const std::vector<std::string*>& segments) const {
Comment thread cpp/src/reader/tsfile_reader.h Outdated
* @return Returns 0 on success, or a non-zero error code on failure.
*/
int get_timeseries_schema(std::shared_ptr<IDeviceID> device_id,
int get_timeseries_schema(const std::shared_ptr<IDeviceID>& device_id,
Comment on lines +831 to +834
if (ret == common::E_OOM) {
reader.close();
return ret;
}
- keep by-value parameters on exported entry points and virtual
  overrides so the installed headers and shared-library ABI stay
  unchanged; internal helpers still take const references
- tsfile_io_writer: remove the success-only assertion so a failed
  bloom-filter insert propagates instead of aborting debug builds
- restorable writer: release the pending chunk group's device
  reference before returning on OOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants