common_fs

cpp_linter.common_fs.CACHE_PATH = PosixPath('.cpp-linter_cache')

A path to generated cache artifacts. (only used when verbosity is in debug mode)

class cpp_linter.common_fs.FileObj(name: str, additions: list[int] | None = None, diff_chunks: list[list[int]] | None = None)[source]

A class to represent a single file being analyzed.

Parameters:
name: str

The file name. This should use Unix style path delimiters (/), even on Windows.

additions: list[int] | None = None

A list of line numbers that have added changes in the diff. This value is used to populate the lines_added property.

diff_chunks: list[list[int]] | None = None

The ranges that define the beginning and ending line numbers for all hunks in the diff.

name : str

The file name

additions : List[int]

A list of line numbers that contain added changes. This will be empty if not focusing on lines changed only.

diff_chunks : List[List[int]]

A list of line numbers that define the beginning and ending of hunks in the diff. This will be empty if not focusing on lines changed only.

lines_added : List[List[int]]

A list of line numbers that define the beginning and ending of ranges that have added changes. This will be empty if not focusing on lines changed only.

tidy_advice : 'TidyAdvice' | None

The results from clang-tidy

format_advice : 'FormatAdvice' | None

The results from clang-format

range_of_changed_lines(lines_changed_only: int, get_ranges: bool = False) list[int] | list[list[int]][source]

Assemble a list of lines changed.

Parameters:
lines_changed_only: int

A flag to indicate the focus of certain lines.

  • 0: focuses on all lines in a file(s).

  • 1: focuses on any lines shown in the event’s diff (may include unchanged lines).

  • 2: focuses strictly on lines in the diff that contain additions.

get_ranges: bool = False

A flag to return a list of sequences representing range parameters. Defaults to False since this is only required when constructing clang-tidy or clang-format CLI arguments.

Returns:

A list of line numbers for which to give attention. If get_ranges is asserted, then the returned list will be a list of ranges. If lines_changed_only is 0, then an empty list is returned.

serialize() dict[str, Any][source]

For easy debugging, use this method to serialize the FileObj into a json compatible dict.

is_hunk_contained(hunk: DiffHunk) tuple[int, int] | None[source]

Does a given hunk start and end within a single diff hunk?

This also includes some compensations for hunk headers that are oddly formed.

Tip

This is mostly useful to create comments that can be posted within a git changes’ diff. Ideally, designed for PR reviews based on patches generated by clang tools’ output.

Returns:

The appropriate starting and ending line numbers of the given hunk. If hunk cannot fit in a single hunk, this returns None.

is_range_contained(start: int, end: int) tuple[int, int] | None[source]

Does the given start and end line numbers fit within a single diff hunk?

This is a helper function to is_hunk_contained().

Tip

This is mostly useful to create comments that can be posted within a git changes’ diff. Ideally, designed for PR reviews based on patches generated by clang tools’ output.

Returns:

The appropriate starting and ending line numbers of the given hunk. If hunk cannot fit in a single hunk, this returns None.

cpp_linter.common_fs.has_line_changes(lines_changed_only: int, diff_chunks: list[list[int]], additions: list[int]) bool[source]

Does this file actually apply to condition specified by lines_changed_only?

Parameters:
lines_changed_only: int

A value that means:

  • 0 = We don’t care. Analyze the whole file.

  • 1 = Only analyze lines in the diff chunks, which may include unchanged lines but not lines with subtractions.

  • 2 = Only analyze lines with additions.

diff_chunks: list[list[int]]

The ranges of lines in the diff for a single file.

additions: list[int]

The lines with additions in the diff for a single file.

cpp_linter.common_fs.get_line_cnt_from_cols(file_path: str, offset: int) tuple[int, int][source]

Gets a line count and columns offset from a file’s absolute offset.

Parameters:
file_path: str

Path to file.

offset: int

The byte offset to translate

Returns:

A tuple of 2 int numbers:

  • Index 0 is the line number for the given offset.

  • Index 1 is the column number for the given offset on the line.