[PATCH] D17695: clang-cl: Implement initial limited support for precompiled headers.

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 28 09:40:41 PST 2016

thakis created this revision.
thakis added a reviewer: hans.
thakis added a subscriber: cfe-commits.

In the gcc precompiled header model, one explicitly runs clang with `-x c++-header` on a .h file to produce a gch file, and then includes the header with `-include foo.h` and if a .gch file exists for that header it gets used. This is documented at http://clang.llvm.org/docs/UsersManual.html#precompiled-headers

cl.exe's model is fairly different, and controlled by the two flags /Yc and /Yu. A pch file is generated as a side effect of a regular compilation when /Ycheader.h is passed. While the compilation is running, the compiler keeps track of #include lines in the main translation unit and writes everything up to an `#include "header.h"` line into a pch file. Conversely, /Yuheader.h tells the compiler to skip all code in the main TU up to and including `#include "header.h"` and instead load header.pch. (It's also possible to use /Yc and /Yu without an argument, in that case a `#pragma hrdstop` takes the role of controlling the point where pch ends and real code begins.)

This file implements limited support for this in that it requires the pch header to be passed as a /FI force include flag – with this restriction, support for this can be implemented completely in the driver with fairly small amounts of code. For /Yu, this is trivial, and for /Yc a separate pch action is added that runs before the actual compilation. To make sure the compilation doesn't run if building the pch fails, Action gets support for "implicit inputs": Inputs that are needed from a dependency point of view so that a command won't run if an implicit input failed to build, but that aren't passed as inputs to the action. It looks like this concept might be useful for CUDA as well.

If /Yc /Yu are used in a setup that clang-cl doesn't implement yet, clang-cl will now emit a "not implemented yet, ignoring flag" warning that can be disabled by -Wno-clang-cl-pch.

(The default stdafx.h setup passes stdafx.h as explicit argument to /Yc but not as /FI – instead every single TU has to `#include <stdafx.h>` as first thing it does. Implementing support for this should be possible with the approach in this patch with minimal frontend changes by passing a --stop-at / --start-at flag from the driver to the frontend. This is left for a follow-up. I don't think we ever want to support `#pragma hdrstop`, and supporting it with this approach isn't easy: This approach relies on the driver knowing the pch filename in advance, and `#pragma hdrstop(out.pch)` can set the output filename, so the driver can't know about it in advance.)

clang-cl now also honors /Fp and puts pch files in the same spot that cl.exe would put them, but the pch file format is of course incompatible. This has ramifications on /fallback, so /Yc /Yu aren't passed through to cl.exe in /fallback builds.



-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17695.49321.patch
Type: text/x-patch
Size: 33921 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160228/6a12f3eb/attachment-0001.bin>

More information about the cfe-commits mailing list