[clang] [compiler-rt] Reland "[Clang][CodeGen] Report when an alias points to an incompatible target" (PR #195550)
Igor Kudrin via cfe-commits
cfe-commits at lists.llvm.org
Mon May 4 22:52:34 PDT 2026
================
@@ -804,6 +804,70 @@ void CodeGenModule::checkAliases() {
continue;
}
+ if (!IsIFunc) {
+ GlobalDecl AliaseeGD;
+ if (!lookupRepresentativeDecl(GV->getName(), AliaseeGD) ||
+ !isa<VarDecl, FunctionDecl>(AliaseeGD.getDecl())) {
+ Diags.Report(Location, diag::err_alias_to_undefined)
+ << IsIFunc << IsIFunc;
+ Error = true;
+ continue;
+ }
+
+ bool aliasIsFuncDecl = isa<FunctionDecl>(D);
+ bool aliaseeIsFunc = isa<llvm::Function, llvm::GlobalIFunc>(GV);
+ // Function declarations can only alias functions (including IFUNCs).
+ // Similarly, variable declarations can only alias variables.
+ if (aliasIsFuncDecl != aliaseeIsFunc) {
+ Diags.Report(Location, diag::err_alias_between_function_and_variable)
+ << aliasIsFuncDecl;
+ Diags.Report(AliaseeGD.getDecl()->getLocation(),
+ diag::note_aliasee_declaration);
+ Error = true;
+ continue;
+ }
+
+ // Only report functions.
+ // Type mismatches for variables can be intentional.
+ if (aliasIsFuncDecl && aliaseeIsFunc) {
+ QualType AliasTy = D->getType();
+ QualType AliaseeTy = cast<ValueDecl>(AliaseeGD.getDecl())->getType();
+ auto shouldReportTypeMismatch = [&]() {
+ const auto *AliasFTy =
+ AliasTy.getCanonicalType()->getAs<FunctionType>();
+ const auto *AliaseeFTy =
+ AliaseeTy.getCanonicalType()->getAs<FunctionType>();
+ assert(AliasFTy && AliaseeFTy);
+ if (!Context.typesAreCompatible(AliasFTy->getReturnType(),
+ AliaseeFTy->getReturnType()))
+ return true;
+ const auto *AliasFPTy = dyn_cast<FunctionProtoType>(AliasFTy);
+ // Do not report aliases with unspecified parameter lists.
+ if (!AliasFPTy)
+ return false;
----------------
igorkudrin wrote:
> We should report isVariadic mismatches even if one of the types is a FunctionNoProtoType; the types are incompatible.
But `isVariadic()` is only defined for `FunctionProtoType`. Do you suggest always treating them as variadic, or not? As far as I can tell, GCC ignores type mismatches if any side of an alias link is a no-prototype function: https://godbolt.org/z/4z5M1x8ja.
> Also, the asymmetry here is a little weird; why "return false" if AliasFPTy is null, but "return true" if AliaseeFPTy is null?
Just trying to find the right balance between being permissive and restrictive. But you are right, they should be symmetrical. Thankfully, no-prototype functions have been deprecated in the new standard.
https://github.com/llvm/llvm-project/pull/195550
More information about the cfe-commits
mailing list