rizsotto / Bear

Bear is a tool that generates a compilation database for clang tooling.
GNU General Public License v3.0
4.64k stars 306 forks source link

Compile option "--include <path>" generates incorrect database entry #562

Closed HclX closed 2 months ago

HclX commented 4 months ago

Describe the bug A clear and concise description of what the bug is. Both clang and gcc support "--include " to include a header file when compiling a C files. It seems the right format should be either "--include=" or "--include" according to man page, but separated by space also works in practice. However, when separated by space, bear doesn't generate correct result database.

To Reproduce Steps to reproduce the behavior:

Here are the test files:

~/Source/test$ cat foo.c
#ifdef BAR_AVAILABLE
int bar(int x) { return x + 5; }
#endif

int foo(int x) {
    return bar(x) * 5;
}
:~/Source/test$ cat bar.h
#define BAR_AVAILABLE

Running command bear -- clang --includebar.h -c foo.c -o foo.o or bear -- clang --include=bar.h -c foo.c -o foo.o will generate expected database.

[
  {
    "arguments": [
      "/usr/bin/clang",
      "--include=bar.h",
      "-c",
      "-o",
      "foo.o",
      "foo.c"
    ],
    "directory": "/usr/home/test/Source/test",
    "file": "/usr/home/test/Source/test/foo.c",
    "output": "/usr/home/test/Source/test/foo.o"
  }
]

However, bear -- clang --include bar.h -c foo.c -o foo.o will not:

[
  {
    "arguments": [
      "/usr/bin/gcc",
      "--include",
      "-c",
      "-o",
      "foo.o",
      "bar.h"
    ],
    "directory": "/usr/home/test/Source/test",
    "file": "/usr/home/test/Source/test/bar.h",
    "output": "/usr/home/test/Source/test/foo.o"
  },
  {
    "arguments": [
      "/usr/bin/gcc",
      "--include",
      "-c",
      "-o",
      "foo.o",
      "foo.c"
    ],
    "directory": "/usr/home/test/Source/test",
    "file": "/usr/home/test/Source/test/foo.c",
    "output": "/usr/home/test/Source/test/foo.o"
  }
]

All three commands build fine when not using bear.

Expected behavior I'd expect bear generates same, correct json file when using "--include " just like the other two ways.

Environment:

Additional context

I'm using both clang and gcc and the behavior is same. Here are the clang/gcc versions I'm using:

~/Source/test$ clang --version Debian clang version 16.0.6 (19) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

~/Source/test$ gcc --version gcc (Debian 13.2.0-10) 13.2.0

rizsotto commented 3 months ago

Thanks @HclX for this report. Looks like the compiler flag parsing logic needs to be fixed.

rizsotto commented 2 months ago

572 PR is suppose to fix this issue. (Although it is -include and not --include what we handle. But double checked the GCC manual, and it mentions the include with a single dash prefix.)