Skip to content

[clang-tidy] Add option to disable bugprone-multi-level-pointer-conversion in C code #141209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ AST_MATCHER(QualType, isPointerType) {

} // namespace

MultiLevelImplicitPointerConversionCheck::
MultiLevelImplicitPointerConversionCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), EnableInC(Options.get("EnableInC", true)) {
}

void MultiLevelImplicitPointerConversionCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "EnableInC", EnableInC);
}

void MultiLevelImplicitPointerConversionCheck::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ namespace clang::tidy::bugprone {
class MultiLevelImplicitPointerConversionCheck : public ClangTidyCheck {
public:
MultiLevelImplicitPointerConversionCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
ClangTidyContext *Context);
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
std::optional<TraversalKind> getCheckTraversalKind() const override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return EnableInC ? true : LangOpts.CPlusPlus;
}

private:
bool const EnableInC;
};

} // namespace clang::tidy::bugprone
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ Changes in existing checks
false positives on deleted constructors that cannot be used to construct
objects, even if they have public or protected access.

- Added an option to :doc:`bugprone-multi-level-implicit-pointer-conversion
<clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion>` to
choose whether to enable the check in C code or not.

- Improved :doc:`bugprone-optional-value-conversion
<clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
conversion in argument of ``std::make_optional``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ Additionally, it is recommended that developers thoroughly check and verify the
safety of the conversion before using an explicit cast. This extra level of
caution can help catch potential issues early on in the development process,
improving the overall reliability and maintainability of the code.

Options
-------

.. option:: EnableInC

If `true`, enables the check in C code (it is always enabled in C++ code).
Default is `true`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %check_clang_tidy -check-suffixes=ENABLE-IN-C %s bugprone-multi-level-implicit-pointer-conversion %t -- -config="{CheckOptions: {bugprone-multi-level-implicit-pointer-conversion.EnableInC: true}}"
// RUN: %check_clang_tidy -check-suffixes=DISABLE-IN-C %s bugprone-multi-level-implicit-pointer-conversion %t -- -config="{CheckOptions: {bugprone-multi-level-implicit-pointer-conversion.EnableInC: false}}"

void free(void*);

void test() {
char **p;
free(p);
// CHECK-MESSAGES-ENABLE-IN-C: :[[@LINE-1]]:8: warning: multilevel pointer conversion from 'char **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]
free((void *)p);
}
Loading