[cfe-dev] PCH: separation from source header

Ilya Murav'jov muravev at yandex.ru
Sun Sep 19 07:08:55 PDT 2010


Argyrios Kyrtzidis пишет:
> On Sep 18, 2010, at 6:20 PM, Ilya Murav'jov wrote:
> 
>> So, here is the questions:
>> 1) How can I make sure that PCH header is being used, not test.h? For
>> gcc, I use special "fake" build/test.h with contents like so:
>> #error PCH' using is failed, that shouldn't be done :(
>>
>> then, if PCH cannot be used than compilation will break. But this is not
>> working for clang.
> 
> It should, can you post a test case where this doesn't work ?
> 

Well, I mean that that technique always fails now (fake header always
takes priority). Ok, here http://clang.llvm.org/docs/PCHInternals.html I
 found out about -print-stats option for Clang compiler frontend, clang
-cc1 . But Clang doesn't produce any statistics about PCH use as the doc
states:

clang -cc1 -triple i386-pc-linux-gnu -S -disable-free
-disable-llvm-verifier -main-file-name test.cpp -mrelocation-model
static -mdisable-fp-elim -mconstructor-aliases -target-cpu pentium4
-target-linker-version 2.20 -v -resource-dir
/opt/llvm-28dbg/lib/clang/2.8 -include-pch build/test.h.pch -I src
-ferror-limit 19 -fmessage-length 165 -fexceptions -fgnu-runtime
-fdiagnostics-show-option -fcolor-diagnostics -o /tmp/cc-acnTys.s -x c++
-print-stats src/test.cpp 2&>1 | grep PCH

P.S. I just want to say that I don't like that "Clang falls back to
directly processing the content of test.h" *silently* if it cannot use
.h.pch file. Some way to turn that behaviour into an error/warning would
 be sufficient.

>> 2) Is it possible to use Clang' PCH in the test case above? If not then
>> what is a solution?
> 
> $ clang -c -Isrc -include build/test.h -o build/test.o src/test.c
> 
> -Argiris

Ok, now build/test.h.pch is used somehow. But there plenty of errors if
I use PCH for C++, e.g.:

$ cat src/test.h
#include <sstream>
$ cat src/test.cpp
#include <test.h>

int main()
{
    std::stringstream strm;
    return 0;
}

$ clang++ -x c++-header -Isrc -o build/test.h.pch src/test.h
$ clang++ -c -Isrc -o build/test.o src/test.cpp		<---- no errors
$ clang++ -c -Isrc -include build/test.h -o build/test.o src/test.cpp
In file included from
/home/ilya/opt/programming/atom-project/Temp/pch/src/test.h:1:

In file included from /usr/include/c++/4.4/sstream:38:

In file included from /usr/include/c++/4.4/istream:39:

In file included from /usr/include/c++/4.4/ios:42:

In file included from /usr/include/c++/4.4/bits/ios_base.h:42:

In file included from /usr/include/c++/4.4/bits/locale_classes.h:41:
In file included from /usr/include/c++/4.4/string:52:
/usr/include/c++/4.4/bits/basic_string.h:417:7: error: constructor for
'std::basic_string<char>' must explicitly initialize the member
'_M_dataplus' which does not
      have a default constructor
      basic_string();
      ^
In file included from
/home/ilya/opt/programming/atom-project/Temp/pch/src/test.h:1:
/usr/include/c++/4.4/sstream:91:7: note: in instantiation of member
function 'std::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string'
      requested here
      basic_stringbuf(ios_base::openmode __mode = ios_base::in |
ios_base::out)
      ^
/usr/include/c++/4.4/sstream:509:7: note: in instantiation of member
function 'std::basic_stringbuf<char, std::char_traits<char>,
std::allocator<char>
      >::basic_stringbuf' requested here
      basic_stringstream(ios_base::openmode __m = ios_base::out |
ios_base::in)
      ^
src/test.cpp:5:23: note: in instantiation of member function
'std::basic_stringstream<char, std::char_traits<char>,
std::allocator<char> >::basic_stringstream'
      requested here
    std::stringstream strm;
                      ^
In file included from
/home/ilya/opt/programming/atom-project/Temp/pch/src/test.h:1:
In file included from /usr/include/c++/4.4/sstream:38:
In file included from /usr/include/c++/4.4/istream:39:
In file included from /usr/include/c++/4.4/ios:42:
In file included from /usr/include/c++/4.4/bits/ios_base.h:42:
In file included from /usr/include/c++/4.4/bits/locale_classes.h:41:
In file included from /usr/include/c++/4.4/string:52:
/usr/include/c++/4.4/bits/basic_string.h:268:28: note: member is
declared here
      mutable _Alloc_hider      _M_dataplus;
                                ^
/usr/include/c++/4.4/bits/basic_string.h:251:14: note:
'std::basic_string<char, std::char_traits<char>, std::allocator<char>
>::_Alloc_hider' declared here
      struct _Alloc_hider : _Alloc
             ^
1 error generated.

############### end of console output

Has Clang PCH support only for C, not for C++? Should I file in a bug
about that?

Regards,
 Ilya




More information about the cfe-dev mailing list