How does flycheck or any other linter that uses compile_commands.json find the sources of the standard library? How do they determine the language standard? By the standard flag specified for clang or otherwise? Can I just change this flag in compile_commands.json, say c++11 change to c++17 and errors related to the absence of some std::optional will disappear, because it will pull them up? Provided, of course, that the code has been compiled and the compile_commands file has already been generated! Does the compiler affect these sources? I.e. the clang/clang++ compiler of some unreal engine that he has in his directory is a heavily modified version, what will happen if I change the compiler it was built with to a compiler from UE sources in compile_commands, for a project that is not related to UE at all and uses the 17th standard at all?
I think you misunderstand the role of the file. The file is not generated by the compiler, but by the build tools. It does not imply the compilation took place at all and it is not passed to the compiler.
compile_commands.json
stores the exact commands which the build tool will execute during build, no less and no more.
The linters should do whatever the called compilers would have done if passed the same flags. That is of course non-trivial. Some linters might only look for specific flags, some might do more work. The LLVM toolset is very well integrated with the file - clang-tidy and clangd both use it heavily and they have the internal knowledge of what clang would do. But for most linters I would assume the important flags are the include paths and the language standard.
how does ... find the sources of the standard library?
The same way the compiler does - there are system paths where the standard library is searched for.
How do they determine the language standard?
From the -std=XXX
flags.
Can I just change this flag in compile_commands.json, say c++11 change to c++17 and errors related to the absence of some std::optional will disappear, because it will pull them up?
Yes, because as far as the linter knows, you would use c++17 during your build.
Provided, of course, that the code has been compiled and the compile_commands file has already been generated! Does the compiler affect these sources?
No, compile_commands.json
is not generated by the compiler as explained earlier. The file could be used to invoke the build commands.
if I change the compiler it was built with to a compiler from UE sources in compile_commands, for a project that is not related to UE at all and uses the 17th standard at all?
Then again, the linter would believe that is the compiler you intent to compile your code with, whether that is something the linter cares about is another thing.