Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile_commands.json sets wrong compilation.standard #209

Open
Tradias opened this issue Jul 26, 2021 · 3 comments
Open

compile_commands.json sets wrong compilation.standard #209

Tradias opened this issue Jul 26, 2021 · 3 comments

Comments

@Tradias
Copy link

Tradias commented Jul 26, 2021

Issue: C++17 is not enabled when compilation.commands_dir is set, even if compile_commands.json contain the corresponding compile flag.

Consider the following CMake project, configured with -DCMAKE_EXPORT_COMPILE_COMMANDS=on

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(test)
add_executable(test main.cpp)
target_compile_features(test PRIVATE cxx_std_17)
find_program(STANDARDESE_TOOL standardese)
add_custom_target(
  generate-documentation
  COMMAND
    ${STANDARDESE_TOOL}
    "--output.prefix=${CMAKE_CURRENT_BINARY_DIR}/documentation/"
    "--compilation.commands_dir=${CMAKE_BINARY_DIR}"
    "--compilation.standard=c++17" # doesn't help
    "--compilation.gnu_extensions=1" # doesn't help either
   "${CMAKE_SOURCE_DIR}/inc.h")

where main.cpp is

#include "inc.h"

int main(int argc, char** argv) { return (int)My{}; }

and inc.h is

#pragma once

#include <cstddef>

/// Let's use c++17
using My = std::byte;

Building the target generate-documentation

[build] [1/1 100% :: 5.127] cmd.exe /C "cd /D C:\test\build--debug && C:\Users\dennis\bin\standardese.exe --output.prefix=C:/test/build--debug/documentation/ --compilation.commands_dir=C:/test/build--debug --compilation.gnu_extensions=1 --compilation.standard=c++17 --verbose=1 C:/test/inc.h"
[build] parsing C++ files...
[build] [libclang] [error] C:/test/inc.h:6: no type named 'byte' in namespace 'std'
[build] parsing documentation comments...
[build] [standardese comment] [warning] C:/test/inc.h:5: comment does not have a remote entity specified
[build] generating documentation...
[build] writing files in format 'md'...
[build] Build finished with exit code 0

no documentation for using My is being generated.

I tried with clang-cl version 11.1 and 12, where compile_commands.json is:

[
{
  "directory": "C:/test/build--debug",
  "command": "C:\\PROGRA~1\\LLVM\\bin\\CLANG_~1.EXE -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -std=gnu++17 -o CMakeFiles\\test.dir\\main.cpp.obj -c C:\\test\\main.cpp",
  "file": "C:/test/main.cpp"
}
]

and with cl from MSVC 19.29.30040.0 where compile_commands.json is:

[
{
  "directory": "C:/test/build--debug",
  "command": "C:\\PROGRA~2\\MICROS~2\\2019\\COMMUN~1\\VC\\Tools\\MSVC\\1429~1.300\\bin\\Hostx64\\x64\\cl.exe  /nologo /TP /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd -std:c++17 /FoCMakeFiles\\test.dir\\main.cpp.obj /FdTARGET_COMPILE_PDB /FS -c C:\\test\\main.cpp",
  "file": "C:/test/main.cpp"
}
]

Note that invoking:

standardese --compilation.standard=c++17 inc.h

works as expected.

I am using standardese v0.5.2 installed with miniconda as explained here.

@saraedum
Copy link
Member

So apparently an entry is found in the compilation database for your file, otherwise, this code would not ignore the explicit --compilation.standard flag.

Parsing the entries in the database is handled by cppast here and here. Unless libclang does some mangling to the arguments, I would not be suprised if parsing your second compile_commands.json fails since there is no -std. However, the first one should work from a look at the code.

Does libclang understand gnu++17? I guess it does but could you try to change that to c++17 in your compile commands?

@Tradias
Copy link
Author

Tradias commented Jul 26, 2021

Changing it to -std=c++17 in compile_commands.json for the first one (clang-cl) yields the same output.

I thought that --compilation args provided through the command line would take precedence over the compile commands. After all standardese --help says:

--compilation.commands_dir arg the directory where a
compile_commands.json is located, its
options have lower priority than the
other ones

@saraedum
Copy link
Member

Thanks for pointing this out. This does not seem to be the case, see https://github.com/standardese/standardese/blob/master/tool/generator.cpp#L36. I'll create a separate issue to change the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants