Skip to content

"rust-analyzer.cargo.features": "all" equivalent #13600

@loynoir

Description

@loynoir

Feature Request

actual

Given compile_commands.json use same lib.c with ALL below three status in order

  • without define, is lib mode, output library

  • define test, is test mode, output binary

  • define bench, is bench mode, output binary

Actually, vscode show lib.c

  • lib region code is not gray

  • test region code is gray!

  • bench region code is gray!

compile_commands.json
[
 {
   "directory": "/tmp/repo/build/example/example_add_u64",
   "command": "/usr/lib/ccache/bin/cc  -I/tmp/repo/src/example/example_add_u64/include  -o CMakeFiles/example_add_u64__lib.dir/src/lib.c.o -c /tmp/repo/src/example/example_add_u64/src/lib.c",
   "file": "/tmp/repo/src/example/example_add_u64/src/lib.c",
   "output": "example/example_add_u64/CMakeFiles/example_add_u64__lib.dir/src/lib.c.o"
 },
 {
   "directory": "/tmp/repo/build/example/example_add_u64",
   "command": "/usr/lib/ccache/bin/cc -Dtest -I/tmp/repo/src/example/example_add_u64/include  -o CMakeFiles/example_add_u64__lib_test.dir/src/lib.c.o -c /tmp/repo/src/example/example_add_u64/src/lib.c",
   "file": "/tmp/repo/src/example/example_add_u64/src/lib.c",
   "output": "example/example_add_u64/CMakeFiles/example_add_u64__lib_test.dir/src/lib.c.o"
 },
 {
   "directory": "/tmp/repo/build/example/example_add_u64",
   "command": "/usr/lib/ccache/bin/cc -Dbench -I/tmp/repo/src/example/example_add_u64/include  -o CMakeFiles/example_add_u64__lib_bench.dir/src/lib.c.o -c /tmp/repo/src/example/example_add_u64/src/lib.c",
   "file": "/tmp/repo/src/example/example_add_u64/src/lib.c",
   "output": "example/example_add_u64/CMakeFiles/example_add_u64__lib_bench.dir/src/lib.c.o"
 }
]
lib.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t add_u64(uint64_t left, uint64_t right) {
  uint64_t ret = left + right;

  return ret;
}

// TODO: gray
#ifdef test
#define TEST_MAIN(F)                                                           \
  int main(int argc, char *argv[]) {                                           \
    (void)argc;                                                                \
    (void)argv;                                                                \
    F();                                                                       \
    return EXIT_SUCCESS;                                                       \
  }

void it_works() {
  uint64_t result = add_u64(2, 2);
  if (result != 4) {
    exit(EXIT_FAILURE);
  }
}

TEST_MAIN(it_works)
#endif

// TODO: gray
#ifdef bench
#define BENCH_MAIN(F)                                                          \
  int main(int argc, char *argv[]) {                                           \
    (void)argc;                                                                \
    (void)argv;                                                                \
    F();                                                                       \
    return EXIT_SUCCESS;                                                       \
  }

void it_works() {
  uint64_t result = add_u64(2, 2);
  if (result != 4) {
    exit(EXIT_FAILURE);
  }
}

BENCH_MAIN(it_works)
#endif

workaround

Workaround

  • compile_commands.json remove lib mode element

vscode show lib.c

  • lib region code is not gray

  • test region code is not gray

  • bench region code is gray

compile_commands.json
[
  {
    "directory": "/tmp/repo/build/example/example_add_u64",
    "command": "/usr/lib/ccache/bin/cc -Dtest -I/tmp/repo/src/example/example_add_u64/include  -o CMakeFiles/example_add_u64__lib_test.dir/src/lib.c.o -c /tmp/repo/src/example/example_add_u64/src/lib.c",
    "file": "/tmp/repo/src/example/example_add_u64/src/lib.c",
    "output": "example/example_add_u64/CMakeFiles/example_add_u64__lib_test.dir/src/lib.c.o"
  },
  {
    "directory": "/tmp/repo/build/example/example_add_u64",
    "command": "/usr/lib/ccache/bin/cc -Dbench -I/tmp/repo/src/example/example_add_u64/include  -o CMakeFiles/example_add_u64__lib_bench.dir/src/lib.c.o -c /tmp/repo/src/example/example_add_u64/src/lib.c",
    "file": "/tmp/repo/src/example/example_add_u64/src/lib.c",
    "output": "example/example_add_u64/CMakeFiles/example_add_u64__lib_bench.dir/src/lib.c.o"
  }
]
lib.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t add_u64(uint64_t left, uint64_t right) {
  uint64_t ret = left + right;

  return ret;
}

#ifdef test
#define TEST_MAIN(F)                                                           \
  int main(int argc, char *argv[]) {                                           \
    (void)argc;                                                                \
    (void)argv;                                                                \
    F();                                                                       \
    return EXIT_SUCCESS;                                                       \
  }

void it_works() {
  uint64_t result = add_u64(2, 2);
  if (result != 4) {
    exit(EXIT_FAILURE);
  }
}

TEST_MAIN(it_works)
#endif

// TODO: gray
#ifdef bench
#define BENCH_MAIN(F)                                                          \
  int main(int argc, char *argv[]) {                                           \
    (void)argc;                                                                \
    (void)argv;                                                                \
    F();                                                                       \
    return EXIT_SUCCESS;                                                       \
  }

void it_works() {
  uint64_t result = add_u64(2, 2);
  if (result != 4) {
    exit(EXIT_FAILURE);
  }
}

BENCH_MAIN(it_works)
#endif

related

rust cargo features should be C define equivalent.

cargo have an auto enabled feature named default.

Inspired by

  "rust-analyzer.cargo.features": "all",

feat

I suggest

When

  • "C_Cpp.default.compileCommandsDefinesAll": false

UI behavior as same as current

When

  • "C_Cpp.default.compileCommandsDefinesAll": true

Given compile_commands.json use same lib.c with ALL below three status in order

  • without define, is lib mode, output library

  • define test, is test mode, output binary

  • define bench, is bench mode, output binary

Expect vscode show lib.c

  • lib region code is not gray

  • test region code is not gray

  • bench region code is not gray

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions