CppEditor: Let users check for unused functions in (sub-)projects

Note that especially in C++, there can be a lot of false positives,
especially in template-heavy code bases. We filter out the most notorious
offenders, namely:
    - templates themselves
    - constructors and destructors
    - *begin() and *end()
    - qHash()
    - main()
Since the code model does not know about symbol visibility, the
functionality is quite useless for libraries, unless you want to check
your test coverage.
The procedure is rather slow, but that shouldn't matter so much, as it's
something you'll only run "once in a while".

Fixes: QTCREATORBUG-6772
Change-Id: If00a537b760a9b0babdda6c848133715c3240155
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2022-10-27 14:49:15 +02:00
parent 43b21595e9
commit bfecefabc0
18 changed files with 559 additions and 45 deletions

View File

@@ -276,6 +276,27 @@ bool CppEditorPlugin::initialize(const QStringList & /*arguments*/, QString *err
connect(showPreprocessedInSplitAction, &QAction::triggered,
this, [] { CppModelManager::showPreprocessedFile(true); });
QAction * const findUnusedFunctionsAction = new QAction(tr("Find Unused Functions"), this);
command = ActionManager::registerAction(findUnusedFunctionsAction,
"CppTools.FindUnusedFunctions");
mcpptools->addAction(command);
connect(findUnusedFunctionsAction, &QAction::triggered,
this, [] { CppModelManager::findUnusedFunctions({}); });
QAction * const findUnusedFunctionsInSubProjectAction
= new QAction(tr("Find Unused C/C++ Functions"), this);
command = ActionManager::registerAction(findUnusedFunctionsInSubProjectAction,
"CppTools.FindUnusedFunctionsInSubProject");
for (ActionContainer * const projectContextMenu : {
ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT),
ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT)}) {
projectContextMenu->addSeparator(ProjectExplorer::Constants::G_PROJECT_TREE);
projectContextMenu->addAction(command, ProjectExplorer::Constants::G_PROJECT_TREE);
}
connect(findUnusedFunctionsInSubProjectAction, &QAction::triggered, this, [] {
if (const Node * const node = ProjectTree::currentNode(); node && node->asFolderNode())
CppModelManager::findUnusedFunctions(node->directory());
});
MacroExpander *expander = globalMacroExpander();
expander->registerVariable("Cpp:LicenseTemplate",
tr("The license template."),