CoatiSoftware / Sourcetrail

Sourcetrail - free and open-source interactive source explorer
https://www.sourcetrail.com/
GNU General Public License v3.0
14.97k stars 1.42k forks source link

Sourcetrail doesn't recognize `-std:c++latest` MSVC argument #1029

Open nyanpasu64 opened 4 years ago

nyanpasu64 commented 4 years ago

I have a project built in Qt Creator with CMake. I decided to feed Sourcetrail a MSVC compilation database since Sourcetrail only knows how to import global include paths from VS2017.

Apparently Sourcetrail feeds MSVC compiler flags into its Clang-based analyzer. This result in Clang mistaking /-prefixed compiler flags for files. Apparently it also results in Clang thinking that std::optional doesn't exist...

In the compilation database, I have the hyphen-prefixed (not slash-prefixed) -std:c++latest flag. However it seems Clang doesn't enable C++20 mode when it sees this.

Are there any plans to fix command line argument parsing of MSVC flags? Or to recognize global include paths from MinGW-based compilers instead (in this case, MinGW installed by Qt Installer)?

nyanpasu64 commented 4 years ago

Chat logs:

if it doesn't understand auto [var, ...] or template argument inference, is that because of outdated clang or wrong flags?

it could technically be either, but i would place my money on flags. sourcetrail apparently is currently on clang 9, that supports up to some C++20 features...

I suspect that missing std::optional, missing structured bindings, and not understanding template argument inference are all because of the missing C++17/20 flag.

Workaround: If you pass in -std=c++2a as an extra compiler option, Clang parses my codebase without errors (aside from #1008 "SourceTrail mistakes compiler flag for file name").

mlangkabel commented 4 years ago

I just tested this and can reproduce it as you described on Windows. So as I understand, in your case -std=c++latest is coming from the compilation database generated by Qt Creator. But is there any version of Clang that would accept this argument for the std parameter?

nyanpasu64 commented 4 years ago

I tried building a small file containing std::optional:

#include <optional>

int main() {
    std::optional<int> x{1};
}

clang-cl version 8.0.1 supports -std:c++latest (with a colon, not -std=c++latest with an equals sign). However, I get error:

C:\Users\nyanpasu\tmp>clang-cl -std:c++latest optional.cpp
In file included from optional.cpp:1:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\optional:9:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\yvals.h:9:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\yvals_core.h(462,2): error:
      STL1000: Unexpected compiler version, expected Clang 9.0.0 or newer.
#error STL1000: Unexpected compiler version, expected Clang 9.0.0 or newer.

clang-cl version 10.0.0 builds this file fine using this command. I think clang-cl is compatible with cl.exe switches (though I don't know to what extent). I'm not an expert on llvm and clang and clang-cl, but https://clang.llvm.org/docs/UsersManual.html#clang-cl may be useful.

To enable clang-cl to find system headers, libraries, and the linker when run from the command-line, it should be executed inside a Visual Studio Native Tools Command Prompt or a regular Command Prompt where the environment has been set up using e.g. vcvarsall.bat.

To be compatible with cl.exe, clang-cl supports most of the same command-line options. Those options can start with either / or -. It also supports some of Clang’s core options, such as the -W options.

...