Skip to content

Commit

Permalink
Add a user facing error message for export proc argument mismatch (ch…
Browse files Browse the repository at this point in the history
…apel-lang#24660)

This replaces a compiler assertion with a user-facing error message.
Noted by @twesterhout on
[gitter](https://matrix.to/#/!PBYDSerrfYujeStENM:gitter.im/$D4ys_1Xy9ZLD437uk64oeOPAXzVkPMdBMYYgGJtLA1I?via=gitter.im&via=matrix.org&via=envs.net).

The assertion was triggered when Chapel code exports a function with
more number of arguments then there is in the C header that declares the
function on C side.

[Reviewed by @mppf]

Test:
- [x] linux64
  • Loading branch information
e-kayrakli authored Mar 20, 2024
2 parents 8e240cb + ae43487 commit 8a69c4b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/codegen/cg-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2938,7 +2938,7 @@ static GenRet codegenCallExprInner(GenRet function,
for (size_t i = 0; i < args.size(); i++) {
const clang::CodeGen::ABIArgInfo* argInfo = NULL;
if (CGI) {
argInfo = getCGArgInfo(CGI, i);
argInfo = getCGArgInfo(CGI, i, fn);
} else if (args[i].isLVPtr == GEN_VAL && useDarwinArmFix(args[i].chplType)) {
argInfo = getSingleCGArgInfo(args[i].chplType);
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/codegen/cg-symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ codegenFunctionTypeLLVM(FnSymbol* fn, llvm::AttributeList& attrs,
for_formals(formal, fn) {
const clang::CodeGen::ABIArgInfo* argInfo = NULL;
if (CGI) {
argInfo = getCGArgInfo(CGI, clangArgNum);
argInfo = getCGArgInfo(CGI, clangArgNum, fn);
} else if (useDarwinArmFix(formal->type)) {
argInfo = getSingleCGArgInfo(formal->type);
}
Expand Down Expand Up @@ -2767,7 +2767,7 @@ void FnSymbol::codegenDef() {
for_formals(arg, this) {
const clang::CodeGen::ABIArgInfo* argInfo = NULL;
if (CGI) {
argInfo = getCGArgInfo(CGI, clangArgNum);
argInfo = getCGArgInfo(CGI, clangArgNum, this);
} else if (useDarwinArmFix(arg->type)) {
argInfo = getSingleCGArgInfo(arg->type);
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/include/clangUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ const clang::CodeGen::CGFunctionInfo& getClangABIInfoFD(clang::FunctionDecl* FD)
const clang::CodeGen::CGFunctionInfo& getClangABIInfo(FnSymbol* fn);

const clang::CodeGen::ABIArgInfo*
getCGArgInfo(const clang::CodeGen::CGFunctionInfo* CGI, int curCArg);
getCGArgInfo(const clang::CodeGen::CGFunctionInfo* CGI, int curCArg,
FnSymbol* fn=nullptr);

const clang::CodeGen::ABIArgInfo*
getSingleCGArgInfo(Type* type);
Expand Down
14 changes: 11 additions & 3 deletions compiler/llvm/clangUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4002,17 +4002,25 @@ const clang::CodeGen::CGFunctionInfo& getClangABIInfo(FnSymbol* fn) {
}

const clang::CodeGen::ABIArgInfo*
getCGArgInfo(const clang::CodeGen::CGFunctionInfo* CGI, int curCArg)
getCGArgInfo(const clang::CodeGen::CGFunctionInfo* CGI, int curCArg,
FnSymbol* fn)
{

// Don't try to use the calling convention code for variadic args.
if ((unsigned) curCArg >= CGI->arg_size() && CGI->isVariadic())
return NULL;
if ((unsigned) curCArg >= CGI->arg_size()) {
if (CGI->isVariadic()) {
return NULL;
}
else {
USR_FATAL(fn, "Argument number mismatch in export proc");
}
}

const clang::CodeGen::ABIArgInfo* argInfo = NULL;
#if HAVE_LLVM_VER >= 100
llvm::ArrayRef<clang::CodeGen::CGFunctionInfoArgInfo> a=CGI->arguments();
argInfo = &a[curCArg].info;

#else
int i = 0;
for (auto &ii : CGI->arguments()) {
Expand Down
4 changes: 4 additions & 0 deletions test/interop/C/errorMessage/errorInArgNMismatch.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "errorInArgNMismatch.h";

export proc foo(x : real) { }

1 change: 1 addition & 0 deletions test/interop/C/errorMessage/errorInArgNMismatch.good
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
errorInArgNMismatch.chpl:3: error: Argument number mismatch in export proc
4 changes: 4 additions & 0 deletions test/interop/C/errorMessage/errorInArgNMismatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void foo(void);

0 comments on commit 8a69c4b

Please sign in to comment.