[clang] 970e1ea - [clang] Fix crash with -funique-internal-linkage-names

Ellis Hoag via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 17 08:57:28 PDT 2022


Author: Ellis Hoag
Date: 2022-10-17T08:57:23-07:00
New Revision: 970e1ea01aa0dce4f606eee1610c92b8b838f303

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

LOG: [clang] Fix crash with -funique-internal-linkage-names

Calling `getFunctionLinkage(CalleeInfo.getCalleeDecl())` will crash when the declaration does not have a body, e.g., `extern void foo();`. Instead, we can use `isExternallyVisible()` to see if the delcaration has internal linkage.

I believe using `!isExternallyVisible()` is correct because the clang linkage must be `InternalLinkage` or `UniqueExternalLinkage`, both of which are "internal linkage" in llvm.
https://github.com/llvm/llvm-project/blob/9c26f51f5e178ac0fda98419e3a61d205d3b58b1/clang/include/clang/Basic/Linkage.h#L28-L40

Fixes https://github.com/llvm/llvm-project/issues/54139

Reviewed By: tmsriram

Differential Revision: https://reviews.llvm.org/D135926

Added: 
    clang/test/CodeGen/unique-internal-linkage-names.c

Modified: 
    clang/lib/CodeGen/CGCall.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index a59d702faaa2..8cea9f3397b6 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2264,9 +2264,8 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
   // Add "sample-profile-suffix-elision-policy" attribute for internal linkage
   // functions with -funique-internal-linkage-names.
   if (TargetDecl && CodeGenOpts.UniqueInternalLinkageNames) {
-    if (isa<FunctionDecl>(TargetDecl)) {
-      if (this->getFunctionLinkage(CalleeInfo.getCalleeDecl()) ==
-          llvm::GlobalValue::InternalLinkage)
+    if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
+      if (!FD->isExternallyVisible())
         FuncAttrs.addAttribute("sample-profile-suffix-elision-policy",
                                "selected");
     }

diff  --git a/clang/test/CodeGen/unique-internal-linkage-names.c b/clang/test/CodeGen/unique-internal-linkage-names.c
new file mode 100644
index 000000000000..6f8f243ef14a
--- /dev/null
+++ b/clang/test/CodeGen/unique-internal-linkage-names.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 %s -S -emit-llvm -funique-internal-linkage-names -o - | FileCheck %s
+
+// Check that we do not crash when overloading extern functions.
+
+inline void overloaded_external() {}
+extern void overloaded_external();
+
+// CHECK: define internal void @overloaded_internal() [[ATTR:#[0-9]+]] {
+static void overloaded_internal() {}
+extern void overloaded_internal();
+
+void markUsed() {
+  overloaded_external();
+  overloaded_internal();
+}
+
+// CHECK: attributes [[ATTR]] =
+// CHECK-SAME: "sample-profile-suffix-elision-policy"="selected"


        


More information about the cfe-commits mailing list