Skip to content

Don't set pure for const functions [refs #12696]#6383

Draft
chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_const
Draft

Don't set pure for const functions [refs #12696]#6383
chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_const

Conversation

@chrchr-github
Copy link
Copy Markdown
Collaborator

No description provided.

@chrchr-github chrchr-github changed the title Don't set pure for const functions Don't set pure for const functions [refs #12696] May 4, 2024
@chrchr-github chrchr-github marked this pull request as ready for review May 4, 2024 18:26
@chrchr-github
Copy link
Copy Markdown
Collaborator Author

FWIW here's a reduced example:

class QModelIndex;

struct ResultsTree {   
    void copy();
    QStandardItemModel mModel;
    QItemSelectionModel mSelectionModel{};
};

void ResultsTree::copy()
{
    QString text;
    for (QModelIndex index : mSelectionModel.selectedRows()) {
        QStandardItem* item = mModel.itemFromIndex(index);
        if (!item->parent()) {
            text += item->text() + '\n';
            continue;
        }
    }
}

Before this change, the valueflow was

Line 14
  ! always {!<=-1,!>=2}
  item always symbolic=(mModel.itemFromIndex(index))
Line 15
  item always symbolic=(mModel.itemFromIndex(index))
  '\n' always 10

Those symbolic values disappear without pure, affecting isAliasOf() and ultimaltely isVariableChanged().

Comment thread lib/library.cpp
func.ispure = true;
else if (functionnodename == "const") {
func.ispure = true;
func.isconst = true; // a constant function is pure
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

as I understand a const function is always pure. does valueflow assume that the function reads from global memory if it's pure?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure how ValueFlow uses these attributes.
We also need to figure out what const is supposed to mean in the library, see #6385

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