Friday, March 25, 2016

Compiler Dysfunction

<< Prev: Linux Upgrade       Next: 67 Steps >>

Pop Quiz: what's the purpose of a compiler?

Three things come to mind:
  • Make it so you don't have to write in assembly
  • Ensure that you use the programming language correctly (e.g. no misspelled keywords or basic syntax errors)
  • Validate the API calls you make (e.g. not calling API functions that don't exist)

I mean, surely there are more, but that's my starter list.

Except the C compiler disagrees with me 33% of the time.

Which is to say, it apparently doesn't give a damn whether the API calls you make exist or not.

Uh, run that one by me again? So I can type code like this, and it totally compiles?

thisFunctionDoesNotExist(17);

Why yes, that's totally OK with this compiler.

Well that's easy -- the compiler is fired. Not doing it's job. I'll go find another one.

Except I don't get to do that, because you kind of have to use the Xcode compiler to build kexts.

This one is a complete mystery to me. You have to declare a struct before you use it, so why don't you have to declare a function before you call it? There exists such a thing called header files, for the express purpose of declaring things before you use them, right? We have this lovely tool called #include just to inform the compiler which header files we would like to reference, right?

OK, yes, I can compile the code and THEN run kextlibs and it can tell me if I'm calling functions that don't exist. But this is not what happens when I push the "build" button and it says "no errors".

Well, in fairness, there is a warning:
Warning:(736, 5) implicit declaration of function
                 'thisFunctionDoesNotExist' is invalid in C99

But there are (in this build) 175 warnings. Last time there were 473. (I don't know how it comes up with these numbers and why they're always different, but as my gripes with the compiler go, that's a pretty minor one.) The point is, there's no way I'm going to notice that one of the 175 or 473 or whatever warnings actually matters.

I'd written another paragraph or two of complaints, but I'll spare you the extra vitriol. There's good news! It turns out that there is a way to make this one warning into an error:
  1. In Xcode, select the Project and go to Build Settings and click All
  2. Look toward the bottom of the giant list under Apple LLVM 7.1 - Warnings - All Languages
  3. Set Treat Missing Function Prototypes as Errors to Yes
Whew!

<< Prev: Linux Upgrade       Next: 67 Steps >>

No comments:

Post a Comment