Skip to content

[clang-tidy] Fix assert in performance-unnecessary-copy-init. #96506

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 1 commit into from
Jun 25, 2024

Conversation

legrosbuffle
Copy link
Contributor

GetDirectCallee can be null.

Fixes #96498.

@llvmbot
Copy link
Member

llvmbot commented Jun 24, 2024

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang-tools-extra

Author: Clement Courbet (legrosbuffle)

Changes

GetDirectCallee can be null.

Fixes #96498.


Full diff: https://github.com/llvm/llvm-project/pull/96506.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp (+1-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp (+9)
diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
index a6062ccf42230..106feb7fb4172 100644
--- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
+++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
@@ -296,7 +296,7 @@ AST_MATCHER_P(DeclRefExpr, doesNotMutateObject, int, Indirections) {
           return false;
         }
         const auto *const Method =
-            dyn_cast<CXXMethodDecl>(OpCall->getDirectCallee());
+            dyn_cast_or_null<CXXMethodDecl>(OpCall->getDirectCallee());
 
         if (Method == nullptr) {
           // This is not a member operator. Typically, a friend operator. These
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
index f259552dc8f1d..d02bb98cf583c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
@@ -906,3 +906,12 @@ void negativeNonConstMemberExpr() {
   }
 }
 
+
+bool operator==(ExpensiveToCopyType, ExpensiveToCopyType);
+
+template<typename T> bool OperatorWithNoDirectCallee(T t) {
+  ExpensiveToCopyType a1;
+  ExpensiveToCopyType a2 = a1;
+  return a1 == t;
+}
+

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, please update release note.

@legrosbuffle legrosbuffle force-pushed the fix-crash-unnecessary-init branch from 5034dc9 to 834b63a Compare June 25, 2024 05:53
@legrosbuffle legrosbuffle merged commit 8348d72 into llvm:main Jun 25, 2024
5 of 7 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 25, 2024

LLVM Buildbot has detected a new failure on builder clang-arm64-windows-msvc running on linaro-armv8-windows-msvc-04 while building clang-tools-extra at step 4 "build stage 1".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/161/builds/174

Here is the relevant piece of the build log for the reference:

Step 4 (build stage 1) failure: 'ninja' (failure)
...
[6095/7474] Linking CXX static library lib\clangTidyMain.lib
[6096/7474] Linking CXX executable bin\clang-include-cleaner.exe
[6097/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/ClangdServer.cpp.obj
[6098/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/FileDistance.cpp.obj
[6099/7474] Linking CXX executable bin\clang-tidy.exe
[6100/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/FS.cpp.obj
[6101/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/FuzzyMatch.cpp.obj
[6102/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/CodeComplete.cpp.obj
[6103/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/CompileCommands.cpp.obj
[6104/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/Format.cpp.obj
FAILED: tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/Format.cpp.obj 
ccache C:\PROGRA~1\LLVM\bin\clang-cl.exe  /nologo -TP -DGTEST_HAS_RTTI=0 -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/stage1/tools/clang/tools/extra/clangd -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm/clang-tools-extra/clangd -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm/clang-tools-extra/clangd/../include-cleaner/include -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/stage1/tools/clang/tools/extra/clangd/../clang-tidy -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm/clang/include -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/stage1/tools/clang/include -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/stage1/include -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm/llvm/include -IC:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm/clang-tools-extra/pseudo/lib/../include /DWIN32 /D_WINDOWS   /Zc:inline /Zc:__cplusplus /Oi /Brepro /bigobj /permissive- -Werror=unguarded-availability-new /W4  -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported /Gw /O2 /Ob2  -MD  /EHs-c- /GR- -UNDEBUG -std:c++17 /showIncludes /Fotools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/Format.cpp.obj /Fdtools\clang\tools\extra\clangd\CMakeFiles\obj.clangDaemon.dir\ -c -- C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm/clang-tools-extra/clangd/Format.cpp
C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm/clang-tools-extra/clangd/Format.cpp(284,11): error: no member named 'KeepEmptyLinesAtTheStartOfBlocks' in 'clang::format::FormatStyle'
  284 |     Style.KeepEmptyLinesAtTheStartOfBlocks = true;
      |     ~~~~~ ^
1 error generated.
[6105/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/FindSymbols.cpp.obj
[6106/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/Diagnostics.cpp.obj
[6107/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/ClangdLSPServer.cpp.obj
[6108/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/AST.cpp.obj
[6109/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/GlobalCompilationDatabase.cpp.obj
[6110/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/Headers.cpp.obj
[6111/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/DumpAST.cpp.obj
[6112/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/HeaderSourceSwitch.cpp.obj
[6113/7474] Building CXX object tools/clang/tools/extra/clangd/CMakeFiles/obj.clangDaemon.dir/FindTarget.cpp.obj
ninja: build stopped: subcommand failed.

AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] Crash with performance-unnecessary-copy-initialization
5 participants