Skip to content

Conversation

@ericonr
Copy link
Member

@ericonr ericonr commented Jan 8, 2025

Testing at least two options: clang-tidy+analyzer, and cppcheck

Could try and use https://github.com/SonarSource/sonarqube-scan-action , but requires creating an account and stuff, even if supposedly free for open source (does seem to be free: https://www.sonarsource.com/solutions/commitment-to-open-source/)

Would like to use GCC -fanalyzer, but so far it's not recommended for C++

https://github.com/cpplint/cpplint?tab=readme-ov-file is google-specific, but could try it at least locally to see where it complains and if it's compatible

There's also https://github.com/verateam/vera

Might be best to have a separate job for this, since I don't need to run it for every build. And separate jobs for each tool would also speed up things. Perhaps add flags to fail if errors are found?

Another nice addition would be running the testsuite under sanitizers

@ericonr ericonr force-pushed the static-analysis branch 23 times, most recently from 0deceb0 to e4dfb45 Compare January 16, 2025 16:12
@gustavosr8
Copy link

gustavosr8 commented Jan 16, 2025

Some checks that could be omitted (imo) in clang-tidy:

  • readability-isolate-declaration
  • modernize-type-traits
  • cppcoreguidelines-no-malloc
  • readability-function-cognitive-complexity (maybe just set IgnoreMacros to true could be enough )
  • readability-math-missing-parentheses
  • modernize-avoid-c-arrays
  • performance-avoid-endl
  • bugprone-suspicious-string-compare
  • bugprone-easily-swappable-parameters
  • modernize-use-auto

@gustavosr8
Copy link

With a regex-based python script, could find each occurrence and how many times it happens. Hope it's helpful.

cppcoreguidelines-avoid-magic-numbers: 158                                                                                                                                                                      
readability-magic-numbers: 158                                                                                                                                                                                  
cppcoreguidelines-non-private-member-variables-in-classes: 101                                                                                                                                                  
readability-named-parameter: 97                                                                                                                                                                                 
bugprone-narrowing-conversions: 97                                                                                                                                                                              
cppcoreguidelines-narrowing-conversions: 97                                                                                                                                                                     
cppcoreguidelines-special-member-functions: 74                                                                                                                                                                  
bugprone-chained-comparison: 56                                                                                                                                                                                 
cppcoreguidelines-pro-bounds-constant-array-index: 40                                                                                                                                                           
cppcoreguidelines-pro-bounds-array-to-pointer-decay: 35                                                                                                                                                         
cppcoreguidelines-avoid-c-arrays: 28                                                                                                                                                                            
modernize-avoid-c-arrays: 28                                                                                                                                                                                    
cppcoreguidelines-init-variables: 27
cppcoreguidelines-virtual-class-destructor: 21
cppcoreguidelines-avoid-non-const-global-variables: 19
readability-math-missing-parentheses: 18
cppcoreguidelines-macro-to-enum: 16
modernize-macro-to-enum: 16
cppcoreguidelines-pro-type-member-init: 13
readability-isolate-declaration: 10
cppcoreguidelines-pro-type-cstyle-cast: 10
readability-function-cognitive-complexity: 9
cppcoreguidelines-explicit-virtual-functions: 7
modernize-use-override: 7
cppcoreguidelines-macro-usage: 7
cppcoreguidelines-owning-memory: 6
modernize-use-auto: 6
bugprone-implicit-widening-of-multiplication-result: 6
readability-else-after-return: 6
bugprone-easily-swappable-parameters: 6
modernize-deprecated-headers: 5
performance-enum-size: 5
performance-unnecessary-value-param: 5
cppcoreguidelines-no-malloc: 4
readability-qualified-auto: 4
bugprone-switch-missing-default-case: 4
modernize-use-designated-initializers: 4
readability-redundant-inline-specifier: 4
bugprone-macro-parentheses: 4
modernize-use-using: 4
performance-avoid-endl: 3
concurrency-mt-unsafe: 3
bugprone-unchecked-optional-access: 3
readability-convert-member-functions-to-static: 3
bugprone-exception-escape: 2
modernize-use-ranges: 2
readability-redundant-casting: 2
readability-static-definition-in-anonymous-namespace: 2
modernize-pass-by-value: 2
nodiscard: 2
cppcoreguidelines-avoid-const-or-ref-data-members: 2 
readability-avoid-return-with-void-value: 2
modernize-type-traits: 2
bugprone-infinite-loop: 1
cppcoreguidelines-use-default-member-init: 1
modernize-use-default-member-init: 1
modernize-use-equals-default: 1
readability-simplify-boolean-expr: 1
readability-non-const-parameter: 1
bugprone-empty-catch: 1
modernize-use-nullptr: 1
bugprone-casting-through-void: 1
readability-make-member-function-const: 1
cppcoreguidelines-pro-type-reinterpret-cast: 1
cppcoreguidelines-pro-type-const-cast: 1
bugprone-suspicious-string-compare: 1

@gustavosr8
Copy link

The script, if want to reproduce the results:

import re
from collections import Counter

with open('static-analysis.txt', 'r') as file:
    log_data = file.read()

pattern = re.compile(r'warning:.*?\[([a-zA-Z\-]+(?:,[a-zA-Z\-]+)*)\]')
matches = pattern.findall(log_data)

all_warnings = []
for match in matches:
    all_warnings.extend(match.split(','))

counter = Counter([warning.strip() for warning in all_warnings])

for warning, count in counter.most_common():
    print(f"{warning}: {count}")

@ericonr
Copy link
Member Author

ericonr commented Jan 17, 2025

Thank you!

@sonarqubecloud
Copy link

@ericonr ericonr mentioned this pull request Apr 7, 2025
@ericonr ericonr changed the title Add some static analysis to the code base. Add clang-tidy to the code base Apr 7, 2025
@ericonr ericonr force-pushed the static-analysis branch 2 times, most recently from 589502e to e1df251 Compare April 9, 2025 14:54
ericonr added 5 commits April 9, 2025 12:27
The test groups that were added were the ones considered relevant for
this project. Test groups used to enforce code style for specific
projects and platforms were omitted, as was cppcoreguidelines, because
it's harder to apply to a codebase that wasn't using it from the start.

The specific tests from the groups in use that had to be removed are
explained below:

- bugprone-empty-catch: too eager, recommends longer code using find()
  member function;
- concurrency-mt-unsafe: flags usage of exit(3) and strerror(3), which
  is basically noise. exit(3) has to be called by someone, and
  strerror(3) is thread-safe on the common case with locale set up and
  valid error values;
- modernize-use-trailing-return-type: though more modern, this function
  declaration style isn't widespread, and would conflict with the style
  used in other projects this one interacts with. It would also increase
  the barrier to entry for this project;
- performance-avoid-endl: we want to be able to use endl;
- readability-braces-around-statements: braces are skipped for single
  statements, which should always be indented (this will be guaranteed
  by clang-format in the future);
- readability-identifier-length: short names for iterator variables and
  throwaway values, as well as known shorthand (f for FILEs, v for
  verbose) should be used;
- readability-implicit-bool-conversion: treating pointers as bools is a
  common idiom;
- readability-named-parameter: parameters are named in implementation
  files as a design choice.

We want clang-tidy to verify our own headers, which are under
include/modules/ and utils/.

We also want clang-tidy-diff, when used, to exit with an error.
Reported-by: clang-tidy (bugprone-casting-through-void)
set_defaults for isla216p should be a member function, when it's
implemented. Reference 'this' in the function body so clang-tidy doesn't
try to turn it into a static function.
These were applied using the command below.

  clang-tidy --fix --fix-notes  '--warnings-as-errors=-*' -p build/ \
  $(git ls-files | grep '\.c\+$')

Automatic fixes were reviewed and some changed manually, mainly to keep
formatting consistency.

Reported-by: clang-tidy
The check for GITHUB_HEAD_REF is used so the same workflow works for PRs
and for branches.

The issue causing git-config(1) to be necessary is tracked in [1].

Meson supports a clang-tidy target of its own, but it's not really
usable for our purposes [2].

[1] actions/checkout#1169
[2] mesonbuild/meson#2383
@sonarqubecloud
Copy link

sonarqubecloud bot commented Apr 9, 2025

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