Compiler Tweak HELP


For internal use, I need to make changes in the Swift compiler. I am not quite familiar with it so I need help.

I want the compiler to throw a warning if no parameters are passed to a function in Swift. I know it's not a syntax error or logical error so I didn't want to include it in the parser section. I believe it would be better to have it in the Swift Intermediate Language Generation (SILGen) phase.

I was able to locate where it checks for arguments through some readings. According to my understanding, it's on the line no 443 of lib/SILGen/SILGenProlog.cpp. Now, I need to perform a check if ArgNo == 0, I need to throw a warning in the terminal.

Now, I am not sure even if that's the correct thing. I would really appreciate it if someone could help me with understanding the warning/error throwing mechanism in the Swift and capturing the argNo in a function.


I am not sure if I understand this correctly - if you have a function that takes an argument (without a default value) and you call that function without passing a value for that argument, then you'll already get an error.

I don't think you need to touch SILGen in order to add warnings like that. Sema would probably be the right place for such checks.

1 Like

Hi Suyash,

Thank you for your response. I need to throw a warning when a function is declared not called — I should have been more clear. Also, in case if I touch Sema, can you please guide through the process of adding warnings?

Do you mean you want to find "dead" functions (i.e. functions that are declared, but never called)?

No I didn't mean that. Let's say I wrote Swift code where I declared a function:

class Home {
  func call() {
     // rest of the code

I should be able to show a warning in the terminal stating the line no and saying, hey fn call has no argument. Are you sure you want to declare a function like that?

There are a couple of places you can hook into I suppose. I think the easiest might be the parser, in Parser::parseDeclFunc:

if (BodyParams->size() == 0) {
    diagnose(NameLoc, diag::func_decl_no_args, FullName);

You'll have to define the func_decl_no_args diagnostic in the DiagnosticsParse.def file:

WARNING(func_decl_no_args, none, "function %0 has no arguments", (DeclName))

Thanks man!!! Really appreciate the help. I have been looking into it for more than a week now. I will give it a try and see if it works.

Also, if you could suggest any readings particular to understanding the Swift Compiler?

Does the change really need to be in the compiler? It would be trivial and very fast to process the source file using libSyntax instead.

1 Like
Terms of Service

Privacy Policy

Cookie Policy