0

I'm using libclang to analyse code, because my project has too many files, so I need clang's precompiled headers to speed up.

First I save the pch file in disk, but when using it, libclang give errors.

 const char* custom_args[] = {
    "-fms-extensions",
    "-fms-compatibility",
    "-fms-compatibility-version=19.0.24215",
    "-std=c++17",
    "-fcxx-exceptions",
    "-fexceptions",

    "-fshort-wchar",


    "-v",
    "-w",
    "-xc++-header",
    "D:\\src\\stdafx.h",
};

// Create and save pch.
CXTranslationUnit tu = clang_parseTranslationUnit(idx, NULL,
        cl_argv, cl_argc,
        NULL, 0,
        CXTranslationUnit_ForSerialization | CXTranslationUnit_CreatePreambleOnFirstParse);


    clang_saveTranslationUnit(tu,
        "D:\\src\\stdafx.h.pch", clang_defaultSaveOptions(tu));

// using pch to speed the procedure of parsing another file.
const char* code_args[] = {
    "-fms-extensions",
    "-fms-compatibility",
    "-fms-compatibility-version=19.0.24215",
    "-std=c++17",
    "-fcxx-exceptions",
    "-fexceptions",

    "-fshort-wchar",

    "-v",
    "-w",
    "-xc++",
    "-include-pch",
    "D:\\src\\stdafx.h.pch",
    "D:\\src\\my_file.cpp"
};
CXTranslationUnit tu = clang_parseTranslationUnit(idx, NULL,
        code_cl_argv, code_cl_argc,
        NULL, 0,
        CXTranslationUnit_KeepGoing);

The libclang's error as following:

D:\src\my_file.cpp:66:5: error: no matching function for call to 'wprintf'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:605:37: note: candidate function not viable: no known conversion from 'asl::char16 *' (aka 'unsigned short *') to 'const wchar_t *const' for 1st argument

Could someone help me solve this problem?

Roland Yim
  • 36
  • 6
  • 1
    The error has nothing to do with precompiled headers. The error message is self-explanatory - a `asl::char16*` is being passed where a `wchar_t*` is expected. An explicit type-cast is needed when passing the `char16*` to `wprintf()`, so you need to fix that call. – Remy Lebeau Oct 09 '19 at 18:52
  • @RemyLebeau Yes, even without the precompiled header, I get the same error when compiling my_file.cpp. What puzzles me is that I can compile my_file.cpp by visual studio 2015 successfully, but fail to compile even with clang --driver-mode=cl. How can I compile it with clang? – Roland Yim Oct 10 '19 at 02:03
  • @RemyLebeau asl::char16 is unsigned short , wchar_t on windows is also usigned short, so it seems there is no need to convert. It is posible that wchar_t in clang is not unsigned short, but it is aslo fail to compile with "-fshort-wchar" – Roland Yim Oct 10 '19 at 02:17
  • @RemyLebeau It seems there is difference between clang and visual studio. By adding explicitly the forced convert of (wchar_t*), the problem is gone. Now both precompiled headers generating and code compiling are successful, thanks. – Roland Yim Oct 10 '19 at 07:12
  • "*wchar_t on windows is also usigned short*" - [no, it is not](https://stackoverflow.com/questions/2395514/). The C++ standard dictates `wchar_t` is a distinct type. That is why a typecast is required. However, to override that, Visual Studio does have [`/Zc:wchar_t-`](https://learn.microsoft.com/en-us/cpp/build/reference/zc-wchar-t-wchar-t-is-native-type?view=vs-2019), and clang has `-fshort-wchar`, but you should not use them. – Remy Lebeau Oct 10 '19 at 14:51
  • Our project had used /Zc:wchar_t- in Visual Studio, so it can be compiled successfully. But in clang, even I specify -fshort-char, it's fail to compile. Only when force convert can do. How strangely! – Roland Yim Oct 11 '19 at 07:47
  • the fact that you had to resort to using compiler options to make the code compile should tell you that something was wrong with the code to begin with. But at least you have your solution now – Remy Lebeau Oct 11 '19 at 15:57

0 Answers0