[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Mon May 19 13:24:54 PDT 2025
================
@@ -7074,6 +7117,86 @@ bool CXXNameMangler::shouldHaveAbiTags(ItaniumMangleContextImpl &C,
return TrackAbiTags.AbiTagsRoot.getUsedAbiTags().size();
}
+namespace {
+
+class RISCVZicfilpFuncSigLabelMangler : public CXXNameMangler {
+ bool IsTopLevelAndCXXVirtualMethod;
+
+public:
+ RISCVZicfilpFuncSigLabelMangler(ItaniumMangleContextImpl &C, raw_ostream &Out,
+ const bool IsCXXVirtualMethod)
+ : CXXNameMangler(C, Out),
+ IsTopLevelAndCXXVirtualMethod(/*IsTopLevel=*/true &&
+ IsCXXVirtualMethod) {}
+
+ void mangleTypeImpl(const BuiltinType *T) override {
+ if (T->getKind() == BuiltinType::WChar_S ||
+ T->getKind() == BuiltinType::WChar_U) {
+ const Type *const OverrideT =
+ getASTContext().getWCharTypeInC().getTypePtr();
+ assert(isa<BuiltinType>(OverrideT) &&
+ "`wchar_t' in C is expected to be defined to a built-in type");
+ T = static_cast<const BuiltinType *>(OverrideT);
+ }
+ return CXXNameMangler::mangleTypeImpl(T);
+ }
+
+ // This <function-type> is the RISC-V psABI modified version
+ // <function-type> ::= [<CV-qualifiers>] [Dx] F <bare-function-type>
+ // [<ref-qualifier>] E
+ void mangleTypeImpl(const FunctionProtoType *T) override {
+ const bool WasTopLevelAndCXXVirtualMethod = IsTopLevelAndCXXVirtualMethod;
+ IsTopLevelAndCXXVirtualMethod = false; // Not top-level anymore
+
+ // Mangle CV-qualifiers, if present. These are 'this' qualifiers,
+ // e.g. "const" in "int (A::*)() const".
+ mangleQualifiers(T->getMethodQuals());
+
+ getStream() << 'F';
+
+ bool MangleReturnType = true;
+ if (const Type &RetT = *T->getReturnType().getTypePtr();
+ WasTopLevelAndCXXVirtualMethod && mayBeCovariant(RetT)) {
+ // Possible covariant types mangle dummy cv-unqualified `class v` as its
+ // class type
+ if (RetT.isPointerType())
+ getStream() << "P1v";
+ else if (RetT.isLValueReferenceType())
+ getStream() << "R1v";
+ else {
+ assert(RetT.isRValueReferenceType() &&
+ "Expect an r-value ref for covariant return type that is not a "
+ "pointer or an l-value ref");
+ getStream() << "O1v";
+ }
+ MangleReturnType = false;
+ }
+ mangleBareFunctionType(T, MangleReturnType);
----------------
efriedma-quic wrote:
Even if you're restricting yourself to C23, C type compatibility rules are complicated... this probably rejects some valid code. Like, C allows the following:
```
void f(int(*x)[]);
void g() { f(0); }
void f(int(*x)[12]) {}
```
See also #96992.
https://github.com/llvm/llvm-project/pull/111661
More information about the cfe-commits
mailing list