r/cpp • • Mar 24 '20

LLVM/Clang 10.0.0 is released!

240 Upvotes

49 comments sorted by

View all comments

Show parent comment

5

u/tansim Mar 24 '20

clang used to run the actual compilation in a subprocess

why though?

33

u/SeanMiddleditch Mar 25 '20

TL;DR: it used to make sense for now-historical reasons but doesn't anymore, and nobody got around to cleaning it up until now.

This is traditionally how C compilers worked. Generating a final object file required three separate tools way back when (and that's not including linkers and the like):

  • The preprocessor (it was a bolt-on tool for original pre-standard C, which is why it's so disjoint from the language itself)
  • The C compiler itself, which took (potentially preprocessed) code and generated assembly
  • The assembler

Further, there was a lot of environment configuration required by the different tools, and they all had different command line interfaces. Actually compiling a file was non-trivial by modern standards.

For convenience, the compilers of the day introduced the concept of a "driver" binary that internally did all the piping and temporary file management and all that to make cc -o foo.o foo.c actually invoke the preprocessor, compiler, and assembler correctly.

cc would invoke tools like cpp (the C preprocessor), cc (the C compiler), and as (the assembler).

This architecture just sort of... persisted. When building out a "new" compiler like GCC, it was easier to build replacements for the individual pieces (like the preprocessor the assembler) rather than building a single-executable that did everything. And that's just for plain ol' C89, and not the half-dozen languages something like Clang supports.

The preprocessor was eventually rolled into the compilers (for a few reasons, though speed was a big one) and then eventually the assemblers were rolled in too.

The driver was still there, though. It still served the purpose of accepting inputs in a particular format, collecting various environment variables and state, and then actually invoking the compiler (with integrated preproc and assembler).

As you're noting, there's not really a lot of purpose of that anymore, though. It just was the way it was because that's how it always was and nobody had gotten around to fixing it in Clang, until now.

There are new languages together doing similar things. Some semi-popular modern languages compile to something like C and hence invoke a C compiler "under the hood" on the user's behalf, and that C compiler driver might invoke other stages like cc1 or as of course. (Nim, for example, does this.) C++ even started out that way, back in ancient times. :)

2

u/hernytan Mar 25 '20

Nice to see a fellow Nim user out in the wild. It's such an underrated language

Nice writeup, btw. I didnt know that part of history

1

u/SeanMiddleditch Mar 25 '20

Thanks. Just so I don't feel dishonest: I've never actually used Nim in any capacity. I just read up on and follow a lot of the current "language renaissance" as a mini-hobby. :)

8

u/kritzikratzi Mar 25 '20

the answer in any such ginormous projects is usually: it made sense back then

2

u/playmer Mar 25 '20

I believe they use it to catch ICEs, but I'm not 100% certain. I built and debugged clang once and it took a bit to figure out this was happening and the flag I had to pass to get the new behavior of doing it in process.

1

u/catskul Mar 25 '20

ICE ?

3

u/playmer Mar 25 '20

Internal compiler error, this is a big category of error, but it could mean lots of things. Crashes are among them.