slycelote / caide-cpp-inliner

Transform a C++ program consisting of multiple source files and headers into a single self-contained source file without any external dependencies (except for standard system headers). Unused code is not included in the resulting file.
Other
32 stars 12 forks source link

Function is removed if it is used from an object defined with auto #18

Closed mfornet closed 4 years ago

mfornet commented 4 years ago

Note function foo in the following code:

Input:

#include <vector>
#include <algorithm>

using namespace std;

class Value
{
public:
    int value;

    Value(int value) : value(value) {}

    int foo() { return value; }
};

int main()
{
    vector<Value> v;

    sort(v.begin(), v.end(), [&](auto &a, auto &b) {
        return a.foo() < b.foo();
    });

    return 0;
}

The expected output is the same code, however, the function foo is removed. Here is the result code:

#include <vector>
#include <algorithm>

using namespace std;

class Value
{
public:
    int value;

};

int main()
{
    vector<Value> v;

    sort(v.begin(), v.end(), [&](auto &a, auto &b) {
        return a.foo() < b.foo();
    });

    return 0;
}

It works ok, if in the comparer, instead of using auto I use [&](Value &a, Value &b) { ... }

slycelote commented 4 years ago

Thanks for the bug report! I could reproduce the issue when an older version of clang library (7.0) is used. With the recent version it works as expected: Value constructor is removed because it's never used; however the function foo is kept. If you build from source, can you try updating the llvm submodule (git submodule update --init) and then passing -DCAIDE_USE_SYSTEM_CLANG=OFF to cmake? If this doesn't help, what are the exact clang options that you provide?

slycelote commented 4 years ago

As for the failure with old clang, this seems to be caused by a clang bug that was fixed in version 8.0.

mfornet commented 4 years ago

Compiling from source is very hard/expensive so I'm using the released binary from caide to use the inliner. I see there is a new release using clang 10.0 so it should work. I'll reopen if that is not the case. Thanks.