[clang] [clang] solve crash due to function overloading. (PR #90255)

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 26 12:40:39 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: None (lolloz98)

<details>
<summary>Changes</summary>

Closes #<!-- -->88917 

I am a newby in llvm development. 

I followed the suggestion for solving the issue. I added just a simple test to verify that the compilation does not crash.

Should the test not be good enough or any problems are found, please let me know, eager to learn :)

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


2 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+12-4) 
- (added) clang/test/CodeGen/function_overload_crash.c (+7) 


``````````diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 0c447b20cef40d..bed5669861392d 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5702,13 +5702,16 @@ CodeGenModule::getLLVMLinkageVarDefinition(const VarDecl *VD) {
 static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
                                           llvm::Function *newFn) {
   // Fast path.
-  if (old->use_empty()) return;
+  if (old->use_empty())
+    return;
 
   llvm::Type *newRetTy = newFn->getReturnType();
-  SmallVector<llvm::Value*, 4> newArgs;
+  SmallVector<llvm::Value *, 4> newArgs;
+
+  SmallVector<llvm::CallBase *> callSitesToBeRemovedFromParent;
 
   for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
-         ui != ue; ) {
+       ui != ue;) {
     llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
     llvm::User *user = use->getUser();
 
@@ -5722,7 +5725,8 @@ static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
 
     // Recognize calls to the function.
     llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
-    if (!callSite) continue;
+    if (!callSite)
+      continue;
     if (!callSite->isCallee(&*use))
       continue;
 
@@ -5792,6 +5796,10 @@ static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
     if (callSite->getDebugLoc())
       newCall->setDebugLoc(callSite->getDebugLoc());
 
+    callSitesToBeRemovedFromParent.push_back(callSite);
+  }
+
+  for (auto *callSite : callSitesToBeRemovedFromParent) {
     callSite->eraseFromParent();
   }
 }
diff --git a/clang/test/CodeGen/function_overload_crash.c b/clang/test/CodeGen/function_overload_crash.c
new file mode 100644
index 00000000000000..094b56030d7ae0
--- /dev/null
+++ b/clang/test/CodeGen/function_overload_crash.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -emit-llvm < %s
+
+int b();
+int main() { return b(b); }
+int b(int (*f)()){
+  return 0;
+}
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/90255


More information about the cfe-commits mailing list