File indexing completed on 2024-05-19 15:45:09

0001 /*
0002     SPDX-FileCopyrightText: 2014 Aleix Pol <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "usebuilder.h"
0008 #include <cmakeutils.h>
0009 #include <QGlobalStatic>
0010 #include <language/duchain/declaration.h>
0011 
0012 static QSet<QString> initCommands()
0013 {
0014     QStringList ids = CMake::executeProcess(QStringLiteral("cmake"), QStringList(QStringLiteral("--help-command-list"))).split(QLatin1Char('\n'));
0015     if (ids.isEmpty()) {
0016         return {};
0017     }
0018     const auto secondIt = ids.constBegin() + 1;
0019     return QSet<QString>(secondIt, ids.constEnd());
0020 }
0021 
0022 // TODO: maybe share this again with codecompletionmodel and documentation
0023 Q_GLOBAL_STATIC_WITH_ARGS(QSet<QString>, s_commands, (initCommands()))
0024 
0025 UseBuilder::UseBuilder(const KDevelop::ReferencedTopDUContext& ctx)
0026     : m_ctx(ctx)
0027 {
0028 }
0029 
0030 void UseBuilder::startVisiting(CMakeContentIterator* node)
0031 {
0032     for(; node->hasNext(); ) {
0033         const CMakeFunctionDesc& func = node->next();
0034 
0035         QString fname = func.name.toLower();
0036         if (!s_commands->contains(fname)) {
0037             KDevelop::DUChainWriteLocker lock;
0038 
0039             KDevelop::Identifier nameid(fname);
0040             QList<KDevelop::Declaration*> declarations = m_ctx->findDeclarations(nameid, func.range().start);
0041             if (!declarations.isEmpty()) {
0042                 newUse(
0043                     func.nameRange(),
0044                     KDevelop::DeclarationPointer(declarations.first())
0045                 );
0046             }
0047         }
0048     }
0049 }
0050 
0051 void UseBuilder::newUse(const KDevelop::RangeInRevision& sr, const KDevelop::DeclarationPointer& d)
0052 {
0053     m_ctx->createUse(m_ctx->indexForUsedDeclaration(d.data()), sr, 0);
0054 }