Using PCH from streamed header


I’m working on an application that requires the use of a pre compiled header (PCH) but we don’t want the header to be distributed as a separate file. I want to discuss my approach for suggestions and the solution I found as I believe it uncovered a corner case in the PCH implementation.

The chosen approach is to include the header as an app resource and load it when compiling a file. One problem is that the location of the header file used to generate the PCH is stored in the PCH and used to validate the PCH. The PCH file won’t be present on disk causing a compilation error if it’s used. I read the PHC documentation and neither PCH flavor (standard or relocatable) helps in this case. To go around the file check I generated the PCH by streaming the header file to clang. This adds the content in the PCH as a buffer blob instead of a file. The command line I used to build the PCH is something like this: clang -cc1 -x some_language -triple=some_target -emit-pch -o $myHeader.pch < myHeader.h. The x and triple switches need to match whatever is used by clang during actual source code compilation.

When using the PCH to compile a file I found a problem with ReadPredefinesBuffer() (PCHReader.cpp/ASTReader.cpp). This function performs some validation on the PCH and sets the preprocessor (PP) predefines to what’s in the PCH. The problem is that the function assumes that the PCH was created from a file. For PCHs from streamed sources the OriginalFileName string is empty causing problems in NormalizeDashIncludePath() and the assert/check following that call. I placed a check around the top block of code in ReadPredefinesBuffer() where if OriginalFileName is not empty the original code is executed but it is then the ‘Left’ string is set to the PP predefines from the PCH.

My questions are:

  1. Is there a better approach I can follow?

  2. Is generating a PCH from an streamed file a valid usage model?

  3. If so, is it correct for ReadPredefinesBuffer() to assume that OriginalFileName is never empty?

  4. If not then is anyone interested in checking-in the fix I made to allow compilation from PCH generated from a streamed file? I can also update the documentation to include this usage model.