Skip to content

Commit 46c6bd6

Browse files
carlosgalvezpCarlos Gálvez
authored andcommitted
[clang-tidy] Add option to disable bugprone-multi-level-pointer-conversion in C code (llvm#141209)
Sometimes a project may want to enable this check only in C++, and disable it in C, since the patterns the check warns about are quite common and idiomatic in C, and there are no better alternatives. Fixes llvm#140659 Co-authored-by: Carlos Gálvez <[email protected]>
1 parent 43975e5 commit 46c6bd6

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ AST_MATCHER(QualType, isPointerType) {
5757

5858
} // namespace
5959

60+
MultiLevelImplicitPointerConversionCheck::
61+
MultiLevelImplicitPointerConversionCheck(StringRef Name,
62+
ClangTidyContext *Context)
63+
: ClangTidyCheck(Name, Context), EnableInC(Options.get("EnableInC", true)) {
64+
}
65+
66+
void MultiLevelImplicitPointerConversionCheck::storeOptions(
67+
ClangTidyOptions::OptionMap &Opts) {
68+
Options.store(Opts, "EnableInC", EnableInC);
69+
}
70+
6071
void MultiLevelImplicitPointerConversionCheck::registerMatchers(
6172
MatchFinder *Finder) {
6273
Finder->addMatcher(

clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ namespace clang::tidy::bugprone {
2121
class MultiLevelImplicitPointerConversionCheck : public ClangTidyCheck {
2222
public:
2323
MultiLevelImplicitPointerConversionCheck(StringRef Name,
24-
ClangTidyContext *Context)
25-
: ClangTidyCheck(Name, Context) {}
24+
ClangTidyContext *Context);
25+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
2626
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2727
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2828
std::optional<TraversalKind> getCheckTraversalKind() const override;
29+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
30+
return EnableInC ? true : LangOpts.CPlusPlus;
31+
}
32+
33+
private:
34+
bool const EnableInC;
2935
};
3036

3137
} // namespace clang::tidy::bugprone

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ Changes in existing checks
159159
false positives on deleted constructors that cannot be used to construct
160160
objects, even if they have public or protected access.
161161

162+
- Added an option to :doc:`bugprone-multi-level-implicit-pointer-conversion
163+
<clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion>` to
164+
choose whether to enable the check in C code or not.
165+
162166
- Improved :doc:`bugprone-optional-value-conversion
163167
<clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
164168
conversion in argument of ``std::make_optional``.

clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ Additionally, it is recommended that developers thoroughly check and verify the
4242
safety of the conversion before using an explicit cast. This extra level of
4343
caution can help catch potential issues early on in the development process,
4444
improving the overall reliability and maintainability of the code.
45+
46+
Options
47+
-------
48+
49+
.. option:: EnableInC
50+
51+
If `true`, enables the check in C code (it is always enabled in C++ code).
52+
Default is `true`.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// 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}}"
2+
// 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}}"
3+
4+
void free(void*);
5+
6+
void test() {
7+
char **p;
8+
free(p);
9+
// 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]
10+
free((void *)p);
11+
}

0 commit comments

Comments
 (0)