Skip to content

Big clean-up PR!#111

Merged
qdeslandes merged 13 commits into
facebook:mainfrom
qdeslandes:clang_tidy
Sep 9, 2024
Merged

Big clean-up PR!#111
qdeslandes merged 13 commits into
facebook:mainfrom
qdeslandes:clang_tidy

Conversation

@qdeslandes

Copy link
Copy Markdown
Contributor

Fix all existing clang-tidy warnings (or update the configuration file if needed), so make check can run clang-format and clang-tidy and be used from the CI. Main changes:

  • Use jq (added as a dependency) to copy compile_commands.json and remove files under tests/, otherwise clang-tidy would analyse the same file twice (once for src/ and once for tests/unit/).
  • Update logger implementation to distinguish between colour and style of the prefix: both would share the same enum and would be combined (e.g. BF_STYLE_GREEN | BF_STYLE_BOLD), creating invalid enum values.
  • Add destructors to custom types in parser.y.
  • Improve bfcli's parser error management logic: wrap calls to yyerror() and log through bpfilter's logger.
  • Move make test target into tests/unit/CMakeLists.txt, with the tests_unit target.
  • Create make check to run ClangFormat and ClangTidy. Add custom commands to run clang-tidy for each source file, allowing make check to benefit from parallel builds.
  • Explicitly disable DOT in Doxygen configuration file, otherwise Ubuntu would generate the DOT graphs.
  • Add bf_err_v() (for other log levels too) to log from a format and a va_list of arguments.
  • Fix all the clang-tidy warnings, update .clang-tidy accordingly.

clang-tidy uses compile_commands.json as a source of truth for compiler
and linker options. However, files under src/ might be compiled twice:
once for bpfilter and once for the unit tests. The unit tests build has
specific flags that might generate erroneous clang-tidy warning (e.g.
with mock_assert() and null pointer dereference).

To solve this issue, jq is used to filter out every file from tests/unit
and create a new compile_commands.json to use as a source for
clang-tidy.
bf_logger_get_color() requires a bf_style enum argument, but it was
passed a combination of bf_style arguments (e.g. BF_STYLE_BLUE |
BF_STYLE_BOLD) which passes an invalid bf_style value.

Change bf_logger_get_color() to receive a bf_color and a bf_style
argument.
Set destructors for custom parser types to avoid memory leaks on
YYABORT.
Create struct bf_ruleset to contain a parsed ruleset and use it as an
argument to yyparse(). Define a macro bf_parse_err() to call yyerror()
and YYABORT.
make lint was sitting unused as a clang-tidy is slow and an overwhelming
amount of warning would be spitted out by clang. Only make checkstyle
was used to validate the code's style.

Now that clang-tidy's warning are fixed, a new target has been added:
make check. This new target will run clang-tidy and clang-format (with
--dry-run) for every source file.

A custom CMake command has been defined for each source file to check,
so clang-tidy can be run in parallel (instead of calling it once with
all the source files as argument). On my host, with 9 threads, linting
all the files in src/ takes ~4 seconds instead of ~12 seconds.
make fixstyle makes it easier to make clang-format fix the formatting
warnings for files under src/. Use it with caution: ensure the fix
doesn't modify the code's behavior.
Ubuntu's Doxygen version has different option than Fedora's version and
the documentation generation will fail when it tries to generate the DOT
representation of the project. Explicitly disable DOT to avoid this
issue.
Simplify the user-facing logging macros and move the coloring and
alignment logic into the common implementation.
As per the main CMakeLists.txt, NDEBUG is never defined, so there is no
reason to gate bf_dbg() macros behind it. Also, the --verbose option
will already filter those logs out.
Rename bf_err_code() (and similar functions) as bf_err_r(), this way the
function name is shorter, and future addition (e.g. bf_err_v() for va_list)
can follow the same pattern.
Introduce bf_err_v() (and similar functions for other log levels) to
print a log message with a format and arguments from a va_list. This
function is required to print error from yyerror().
@qdeslandes qdeslandes merged commit a3883c8 into facebook:main Sep 9, 2024
@qdeslandes qdeslandes deleted the clang_tidy branch September 9, 2024 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants