Smattr / clink

a modern re-implementation of Cscope
The Unlicense
43 stars 2 forks source link

fix recognition of compile commands with rearranged arguments #248

Closed Smattr closed 3 months ago

Smattr commented 3 months ago

The compilation database parsing code was written assuming the format:

  <compiler> … /path/to/source.c
    ▲            ▲
    │            └─ absolute path to source file
    └─ absolute or relative compiler path (“cc”, “/usr/bin/gcc”, …)

There was a minor quirk for “--” arguments, but this was sufficient to handle everything I have seen CMake produce. However, it appears differing argument lists can be written by other tools. Specifically the one that led to this issue looks like:

  <compiler> … ../foo/bar.c -o bar.o
                   ▲          ▲
                   │          └─ arguments following source path
                   └─ path not absolute

This is perfectly reasonable and well within the compilation database specification, but just not something I believed tools produced.

This fix attempts to be more agnostic to the arguments, both their form and the order in which they appear. We now drop “--” and everything following it as well as dropping anything that looks like a source file. This is subject to both false positives:

  cc -o bar.c …
         ▲
         └─ this will be incorrectly dropped, as it looks like a source

and false negatives:

  cc -x c my_source …
           ▲
           └─ this will be incorrectly kept, as it does not look like a source

but it seems enough to cope with reasonable imagined real world use cases.

Github: fixes https://github.com/Smattr/clink/issues/243 Reported-by: Timothy Madden