Skip to content

Conversation

PiJoules
Copy link
Contributor

@PiJoules PiJoules commented Sep 9, 2025

Clang would complain about conflicting types between a function declaration and definition if the declaraion was marked with the attribute but the definition wasn't. Do not treat this as an error. It should only be necessary to mark the declaration with the attribute.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 9, 2025

@llvm/pr-subscribers-clang

Author: None (PiJoules)

Changes

Clang would complain about conflicting types between a function declaration and definition if the declaraion was marked with the attribute but the definition wasn't. Do not treat this as an error. It should only be necessary to mark the declaration with the attribute.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+17)
  • (modified) clang/test/Frontend/cfi-unchecked-callee-attribute.cpp (+4)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 7c1459e320167..91c4589b7b971 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -3877,6 +3877,23 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S,
     RequiresAdjustment = true;
   }
 
+  // If the declaration is marked with cfi_unchecked_callee but the definition
+  // isn't, the definition is also cfi_unchecked_callee.
+  if (auto *FPT1 = OldType->getAs<FunctionProtoType>()) {
+    if (auto *FPT2 = NewType->getAs<FunctionProtoType>()) {
+      FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo();
+      FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo();
+
+      if (EPI1.CFIUncheckedCallee && !EPI2.CFIUncheckedCallee) {
+        EPI2.CFIUncheckedCallee = true;
+        NewQType = Context.getFunctionType(FPT2->getReturnType(),
+                                           FPT2->getParamTypes(), EPI2);
+        NewType = cast<FunctionType>(NewQType);
+        New->setType(NewQType);
+      }
+    }
+  }
+
   // Merge regparm attribute.
   if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() ||
       OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) {
diff --git a/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp b/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp
index f2c4e9e2f8890..072f217ff7b19 100644
--- a/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp
+++ b/clang/test/Frontend/cfi-unchecked-callee-attribute.cpp
@@ -233,3 +233,7 @@ void lambdas() {
     checked_func = checked_lambda;
   };
 }
+
+CFI_UNCHECKED_CALLEE
+void func(void);
+void func(void) {}  // No warning expected.

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

Does this need a release note?

@PiJoules PiJoules force-pushed the cfi_unchecked_callee-conflicting-types branch from 94b27b5 to fe62082 Compare September 17, 2025 05:00
Clang would complain about conflicting types between a function
declaration and definition if the declaraion was marked with the
attribute but the definition wasn't. Do not treat this as an error. It
should only be necessary to mark the declaration with the attribute.
@PiJoules PiJoules force-pushed the cfi_unchecked_callee-conflicting-types branch from fe62082 to b88fb87 Compare September 17, 2025 05:05
@PiJoules
Copy link
Contributor Author

Does this need a release note?

Added one

@PiJoules PiJoules requested a review from erichkeane September 17, 2025 05:05
@PiJoules PiJoules merged commit 97dfc09 into llvm:main Sep 17, 2025
10 checks passed
@PiJoules PiJoules deleted the cfi_unchecked_callee-conflicting-types branch September 17, 2025 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants