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

Add backtrace to valueflow in debug mode #4195

Merged
merged 9 commits into from
Jun 12, 2022

Conversation

pfultz2
Copy link
Contributor

@pfultz2 pfultz2 commented Jun 10, 2022

When using debug_valueflow this will now print out the backtrace where value was set or forwarded from.

This relies on std::source_location which is a C++20 feature. However, if std::experimental::source_location(usually require c++14) is available, then it will use that. If not, it uses a fallback with an empty source location.

I also update the CMAKE_CXX_STANDARD variable to make it a cache variable so it can be switched to C++14 without changing the source, since at least C++14 is needed.

It is possible to enable support on newer clang or gcc with just C++11 using intrinsics, but I think this is simpler for now.

The backtrace is appended to the regular path and looks something like this:

test.cpp:17:33: debug: The value is always !-1 [valueFlow]
    if (debug_valueflow(std::min(apple, banana)) != -1) {
                                ^
test.cpp:14:15: note: Assuming that condition 'fruit==-1' is not redundant
    if (fruit == -1) {
              ^
test.cpp:11:16: note: banana is assigned 'bananas.empty()?-1:bananas.front()' here.
    int banana = bananas.empty() ? -1 : bananas.front();
               ^
test.cpp:13:15: note: fruit is assigned 'std::max(apple,banana)' here.
    int fruit = std::max(apple, banana);
              ^
test.cpp:14:15: note: Assuming condition is false
    if (fruit == -1) {
              ^
test.cpp:11:16: note: banana is assigned 'bananas.empty()?-1:bananas.front()' here.
    int banana = bananas.empty() ? -1 : bananas.front();
               ^
test.cpp:14:15: note: Assuming condition is false
    if (fruit == -1) {
              ^
test.cpp:14:19: note: valueflow.cpp:1154: valueFlowSetConstantValue => setTokenValue: always -1
    if (fruit == -1) {
                  ^
test.cpp:17:5: note: valueflow.cpp:6100: operator() => valueFlowForward: always !-1
    if (debug_valueflow(std::min(apple, banana)) != -1) {
    ^
test.cpp:17:33: note: valueflow.cpp:2809: update => setTokenValue: always !-1
    if (debug_valueflow(std::min(apple, banana)) != -1) {
                                ^
test.cpp:17:33: note: The value is always !-1
    if (debug_valueflow(std::min(apple, banana)) != -1) {
                                ^

@pfultz2
Copy link
Contributor Author

pfultz2 commented Jun 11, 2022

@danmar any feedback?

Copy link
Owner

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it will help you it's good.

@danmar danmar merged commit cee48e5 into danmar:main Jun 12, 2022
@pfultz2 pfultz2 deleted the valueflow-debug-backtrace branch June 12, 2022 11:39
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

Successfully merging this pull request may close these issues.

2 participants