[clang-tools-extra] c92cf31 - [clang-tidy][libc] Ignore implicit function inline (#71095)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 3 14:57:07 PDT 2023


Author: michaelrj-google
Date: 2023-11-03T14:57:03-07:00
New Revision: c92cf315c67edaffea1c4f3a914197dce2a194f3

URL: https://github.com/llvm/llvm-project/commit/c92cf315c67edaffea1c4f3a914197dce2a194f3
DIFF: https://github.com/llvm/llvm-project/commit/c92cf315c67edaffea1c4f3a914197dce2a194f3.diff

LOG: [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.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.cpp
    clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/llvmlibc/inline-function-decl.rst
    clang-tools-extra/test/clang-tidy/checkers/llvmlibc/inline-function-decl.hpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.cpp b/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.cpp
index c119e393d3ab692..0607fd9d5add869 100644
--- a/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.cpp
@@ -45,7 +45,9 @@ InlineFunctionDeclCheck::InlineFunctionDeclCheck(StringRef Name,
       HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
 
 void InlineFunctionDeclCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(decl(functionDecl()).bind("func_decl"), this);
+  // Ignore functions that have been deleted.
+  Finder->addMatcher(decl(functionDecl(unless(isDeleted()))).bind("func_decl"),
+                     this);
 }
 
 void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {

diff  --git a/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.h b/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.h
index 662a592abd9be12..52516f776ad49ce 100644
--- a/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.h
+++ b/clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.h
@@ -33,6 +33,11 @@ class InlineFunctionDeclCheck : public ClangTidyCheck {
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 
+  // Ignore implicit functions (e.g. implicit constructors or destructors)
+  std::optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
+
 private:
   FileExtensionsSet HeaderFileExtensions;
 };

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f9671a65a26fca3..fe8c7175d554c7b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -304,6 +304,10 @@ Changes in existing checks
   customizable namespace. This further allows for testing the libc when the
   system-libc is also LLVM's libc.
 
+- Improved :doc:`llvmlibc-inline-function-decl
+  <clang-tidy/checks/llvmlibc/inline-function-decl>` to properly ignore implicit
+  functions, such as struct constructors, and explicitly deleted functions.
+
 - Improved :doc:`misc-const-correctness
   <clang-tidy/checks/misc/const-correctness>` check to avoid false positive when
   using pointer to member function.

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/llvmlibc/inline-function-decl.rst b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc/inline-function-decl.rst
index 101217b64c82852..71d963969005bad 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/llvmlibc/inline-function-decl.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc/inline-function-decl.rst
@@ -3,6 +3,8 @@
 llvmlibc-inline-function-decl
 =============================
 
-Checks that all implicit and explicit inline functions in header files are
-tagged with the ``LIBC_INLINE`` macro. See the `libc style guide
-<https://libc.llvm.org/dev/code_style.html>`_ for more information about this macro.
+Checks that all implicitly and explicitly inline functions in header files are
+tagged with the ``LIBC_INLINE`` macro, except for functions implicit to classes
+or deleted functions.
+See the `libc style guide <https://libc.llvm.org/dev/code_style.html>`_ for more
+information about this macro.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc/inline-function-decl.hpp b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc/inline-function-decl.hpp
index 0028c575b1d6871..fd2a59289f7682e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc/inline-function-decl.hpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc/inline-function-decl.hpp
@@ -278,6 +278,28 @@ template <typename T>LIBC_INLINE void goodWeirdFormatting() {}
 template <typename T>void LIBC_INLINE badWeirdFormatting() {}
 // 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]
 
+
+template <unsigned int NumberOfBits> struct HasMemberAndTemplate {
+  char Data[NumberOfBits];
+
+  void explicitFunction() {}
+// 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]
+// CHECK-FIXES: LIBC_INLINE void explicitFunction() {}
+};
+
+static auto instanceOfStruct = HasMemberAndTemplate<16>(); 
+
+struct HasMemberAndExplicitDefault {
+  int TrivialMember;
+
+  HasMemberAndExplicitDefault() = default;
+// 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]
+// CHECK-FIXES: LIBC_INLINE HasMemberAndExplicitDefault() = default;
+
+  ~HasMemberAndExplicitDefault() = delete;
+};
+
+
 } // namespace issue_62746
 
 } // namespace __llvm_libc


        


More information about the cfe-commits mailing list