[clang-tools-extra] f483481 - [clang-tidy] Fix crash on calls to overloaded operators in `llvmlibc-callee-namespace`

via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 20 01:15:51 PDT 2022


Author: Whisperity
Date: 2022-04-20T10:15:03+02:00
New Revision: f4834815f439d4b874e4b501f27a909f59f6a426

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

LOG: [clang-tidy] Fix crash on calls to overloaded operators in `llvmlibc-callee-namespace`

The routine that facilitated symbols to be explicitly allowed asked
the name of the called function, which resulted in a crash when the
check was accidentally run on non-trivial C++ code.

Differential Revision: http://reviews.llvm.org/D123992

Reviewed By: aaron.ballman

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp b/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp
index 91e9e026d0ad1..8f4f5452e9890 100644
--- a/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvmlibc/CalleeNamespaceCheck.cpp
@@ -52,7 +52,9 @@ void CalleeNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
   if (NS && NS->getName() == "__llvm_libc")
     return;
 
-  if (IgnoredFunctions.contains(FuncDecl->getName()))
+  const DeclarationName &Name = FuncDecl->getDeclName();
+  if (Name.isIdentifier() &&
+      IgnoredFunctions.contains(Name.getAsIdentifierInfo()->getName()))
     return;
 
   diag(UsageSiteExpr->getBeginLoc(), "%0 must resolve to a function declared "

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 9472797eca86e..50d5b8ecc72c1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -136,31 +136,39 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-- Improved :doc:`performance-inefficient-vector-operation 
-  <clang-tidy/checks/performance-inefficient-vector-operation>` to work when
-  the vector is a member of a structure.
-
-- Fixed a false positive in :doc:`readability-non-const-parameter
-  <clang-tidy/checks/readability-non-const-parameter>` when the parameter is referenced by an lvalue.
-
-- Fixed a crash in :doc:`readability-const-return-type
-  <clang-tidy/checks/readability-const-return-type>` when a pure virtual function
-  overrided has a const return type. Removed the fix for a virtual function.
+- Fixed a crash in :doc:`bugprone-sizeof-expression
+  <clang-tidy/checks/bugprone-sizeof-expression>` when `sizeof(...)` is
+  compared against a `__int128_t`.
 
-- Fixed a false positive in :doc:`misc-redundant-expression <clang-tidy/checks/misc-redundant-expression>`
-  involving overloaded comparison operators.
-
-- Fixed a crash in :doc:`bugprone-sizeof-expression <clang-tidy/checks/bugprone-sizeof-expression>` when
-  `sizeof(...)` is compared agains a `__int128_t`.
-  
 - Improved :doc:`cppcoreguidelines-prefer-member-initializer
   <clang-tidy/checks/cppcoreguidelines-prefer-member-initializer>` check.
 
   Fixed an issue when there was already an initializer in the constructor and
   the check would try to create another initializer for the same member.
 
-- Fixed a false positive in :doc:`misc-redundant-expression <clang-tidy/checks/misc-redundant-expression>`
-  involving assignments in conditions. This fixes `Issue 35853 <https://github.com/llvm/llvm-project/issues/35853>`_.
+- Fixed a crash in :doc:`llvmlibc-callee-namespace
+  <clang-tidy/checks/llvmlibc-callee-namespace>` when executing for C++ code
+  that contain calls to advanced constructs, e.g. overloaded operators.
+
+- Fixed a false positive in :doc:`misc-redundant-expression
+  <clang-tidy/checks/misc-redundant-expression>` involving overloaded
+  comparison operators.
+
+- Fixed a false positive in :doc:`misc-redundant-expression
+  <clang-tidy/checks/misc-redundant-expression>` involving assignments in
+  conditions. This fixes `Issue 35853 <https://github.com/llvm/llvm-project/issues/35853>`_.
+
+- Fixed a crash in :doc:`readability-const-return-type
+  <clang-tidy/checks/readability-const-return-type>` when a pure virtual function
+  overrided has a const return type. Removed the fix for a virtual function.
+
+- Fixed a false positive in :doc:`readability-non-const-parameter
+  <clang-tidy/checks/readability-non-const-parameter>` when the parameter is
+  referenced by an lvalue.
+
+- Improved :doc:`performance-inefficient-vector-operation
+  <clang-tidy/checks/performance-inefficient-vector-operation>` to work when
+  the vector is a member of a structure.
 
 Removed checks
 ^^^^^^^^^^^^^^

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp
index 8ee968433f61c..f18ab2b89e786 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-callee-namespace.cpp
@@ -5,6 +5,10 @@ namespace nested {
 void nested_func() {}
 } // namespace nested
 void libc_api_func() {}
+
+struct libc_api_struct {
+  int operator()() const { return 0; }
+};
 } // namespace __llvm_libc
 
 // Emulate a function from the public headers like string.h
@@ -13,6 +17,11 @@ void libc_api_func() {}
 // Emulate a function specifically allowed by the exception list.
 void malloc() {}
 
+// Emulate a non-trivially named symbol.
+struct global_struct {
+  int operator()() const { return 0; }
+};
+
 namespace __llvm_libc {
 void Test() {
   // Allow calls with the fully qualified name.
@@ -30,19 +39,28 @@ void Test() {
   void (*barePtr)(void) = __llvm_libc::libc_api_func;
   barePtr();
 
+  // Allow calling entities defined in the namespace.
+  __llvm_libc::libc_api_struct{}();
+
   // Disallow calling into global namespace for implemented entrypoints.
   ::libc_api_func();
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'libc_api_func' must resolve to a function declared within the '__llvm_libc' namespace
-  // CHECK-MESSAGES: :11:6: note: resolves to this declaration
+  // CHECK-MESSAGES: :15:6: note: resolves to this declaration
 
   // Disallow indirect references to functions in global namespace.
   void (*badPtr)(void) = ::libc_api_func;
   badPtr();
   // CHECK-MESSAGES: :[[@LINE-2]]:26: warning: 'libc_api_func' must resolve to a function declared within the '__llvm_libc' namespace
-  // CHECK-MESSAGES: :11:6: note: resolves to this declaration
+  // CHECK-MESSAGES: :15:6: note: resolves to this declaration
 
   // Allow calling into global namespace for specific functions.
   ::malloc();
+
+  // Disallow calling on entities that are not in the namespace, but make sure
+  // no crashes happen.
+  global_struct{}();
+  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'operator()' must resolve to a function declared within the '__llvm_libc' namespace
+  // CHECK-MESSAGES: :22:7: note: resolves to this declaration
 }
 
 } // namespace __llvm_libc


        


More information about the cfe-commits mailing list