Skip to content

Commit b88fb87

Browse files
committed
[clang] Do not diagnose conflicting types for cfi_unchecked_callee
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.
1 parent ab0bb6d commit b88fb87

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ Removed Compiler Flags
233233

234234
Attribute Changes in Clang
235235
--------------------------
236+
- The definition of a function declaration with ``[[clang::cfi_unchecked_callee]]`` inherits this
237+
attribute, allowing the attribute to only be attached to the declaration. Prior, this would be
238+
treated as an error where the definition and declaration would have differing types.
236239

237240
Improvements to Clang's diagnostics
238241
-----------------------------------

clang/lib/Sema/SemaDecl.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3877,6 +3877,23 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S,
38773877
RequiresAdjustment = true;
38783878
}
38793879

3880+
// If the declaration is marked with cfi_unchecked_callee but the definition
3881+
// isn't, the definition is also cfi_unchecked_callee.
3882+
if (auto *FPT1 = OldType->getAs<FunctionProtoType>()) {
3883+
if (auto *FPT2 = NewType->getAs<FunctionProtoType>()) {
3884+
FunctionProtoType::ExtProtoInfo EPI1 = FPT1->getExtProtoInfo();
3885+
FunctionProtoType::ExtProtoInfo EPI2 = FPT2->getExtProtoInfo();
3886+
3887+
if (EPI1.CFIUncheckedCallee && !EPI2.CFIUncheckedCallee) {
3888+
EPI2.CFIUncheckedCallee = true;
3889+
NewQType = Context.getFunctionType(FPT2->getReturnType(),
3890+
FPT2->getParamTypes(), EPI2);
3891+
NewType = cast<FunctionType>(NewQType);
3892+
New->setType(NewQType);
3893+
}
3894+
}
3895+
}
3896+
38803897
// Merge regparm attribute.
38813898
if (OldTypeInfo.getHasRegParm() != NewTypeInfo.getHasRegParm() ||
38823899
OldTypeInfo.getRegParm() != NewTypeInfo.getRegParm()) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -ast-dump %s | FileCheck %s
2+
3+
4+
// CHECK: FunctionDecl [[PTR:0x[a-z0-9]*]] {{.*}}func 'void () __attribute__((cfi_unchecked_callee))'
5+
__attribute__((cfi_unchecked_callee))
6+
void func(void);
7+
8+
// CHECK-NEXT: FunctionDecl {{0x[a-z0-9]*}} prev [[PTR]] {{.*}}func 'void () __attribute__((cfi_unchecked_callee))'
9+
void func(void) {}

clang/test/Frontend/cfi-unchecked-callee-attribute.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,7 @@ void lambdas() {
233233
checked_func = checked_lambda;
234234
};
235235
}
236+
237+
CFI_UNCHECKED_CALLEE
238+
void func(void);
239+
void func(void) {} // No warning expected.

0 commit comments

Comments
 (0)