Skip to content

Commit c92cf31

Browse files
[clang-tidy][libc] Ignore implicit function inline (#71095)
This patch adjusts the inline function decl check for LLVM libc to ignore implicit functions. For the moment the plan is to ignore these and mark the class with a macro so that it can be given the appropriate properties without explicitly defining all its ctors/dtors.
1 parent bc61122 commit c92cf31

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ InlineFunctionDeclCheck::InlineFunctionDeclCheck(StringRef Name,
4545
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
4646

4747
void InlineFunctionDeclCheck::registerMatchers(MatchFinder *Finder) {
48-
Finder->addMatcher(decl(functionDecl()).bind("func_decl"), this);
48+
// Ignore functions that have been deleted.
49+
Finder->addMatcher(decl(functionDecl(unless(isDeleted()))).bind("func_decl"),
50+
this);
4951
}
5052

5153
void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {

clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class InlineFunctionDeclCheck : public ClangTidyCheck {
3333
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
3434
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3535

36+
// Ignore implicit functions (e.g. implicit constructors or destructors)
37+
std::optional<TraversalKind> getCheckTraversalKind() const override {
38+
return TK_IgnoreUnlessSpelledInSource;
39+
}
40+
3641
private:
3742
FileExtensionsSet HeaderFileExtensions;
3843
};

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ Changes in existing checks
304304
customizable namespace. This further allows for testing the libc when the
305305
system-libc is also LLVM's libc.
306306

307+
- Improved :doc:`llvmlibc-inline-function-decl
308+
<clang-tidy/checks/llvmlibc/inline-function-decl>` to properly ignore implicit
309+
functions, such as struct constructors, and explicitly deleted functions.
310+
307311
- Improved :doc:`misc-const-correctness
308312
<clang-tidy/checks/misc/const-correctness>` check to avoid false positive when
309313
using pointer to member function.

clang-tools-extra/docs/clang-tidy/checks/llvmlibc/inline-function-decl.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
llvmlibc-inline-function-decl
44
=============================
55

6-
Checks that all implicit and explicit inline functions in header files are
7-
tagged with the ``LIBC_INLINE`` macro. See the `libc style guide
8-
<https://libc.llvm.org/dev/code_style.html>`_ for more information about this macro.
6+
Checks that all implicitly and explicitly inline functions in header files are
7+
tagged with the ``LIBC_INLINE`` macro, except for functions implicit to classes
8+
or deleted functions.
9+
See the `libc style guide <https://libc.llvm.org/dev/code_style.html>`_ for more
10+
information about this macro.

clang-tools-extra/test/clang-tidy/checkers/llvmlibc/inline-function-decl.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,28 @@ template <typename T>LIBC_INLINE void goodWeirdFormatting() {}
278278
template <typename T>void LIBC_INLINE badWeirdFormatting() {}
279279
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 'badWeirdFormatting' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
280280

281+
282+
template <unsigned int NumberOfBits> struct HasMemberAndTemplate {
283+
char Data[NumberOfBits];
284+
285+
void explicitFunction() {}
286+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'explicitFunction' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
287+
// CHECK-FIXES: LIBC_INLINE void explicitFunction() {}
288+
};
289+
290+
static auto instanceOfStruct = HasMemberAndTemplate<16>();
291+
292+
struct HasMemberAndExplicitDefault {
293+
int TrivialMember;
294+
295+
HasMemberAndExplicitDefault() = default;
296+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'HasMemberAndExplicitDefault' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
297+
// CHECK-FIXES: LIBC_INLINE HasMemberAndExplicitDefault() = default;
298+
299+
~HasMemberAndExplicitDefault() = delete;
300+
};
301+
302+
281303
} // namespace issue_62746
282304

283305
} // namespace __llvm_libc

0 commit comments

Comments
 (0)