File indexing completed on 2024-05-12 04:39:12

0001 /*
0002     SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #ifndef CLANGPROBLEM_H
0008 #define CLANGPROBLEM_H
0009 
0010 #include "clangprivateexport.h"
0011 
0012 #include <language/duchain/problem.h>
0013 #include <language/editor/documentrange.h>
0014 #include <interfaces/iassistant.h>
0015 
0016 #include <clang-c/Index.h>
0017 
0018 struct KDEVCLANGPRIVATE_EXPORT ClangFixit
0019 {
0020     QString replacementText;
0021     KDevelop::DocumentRange range;
0022     QString description;
0023     QString currentText;
0024 
0025     bool operator==(const ClangFixit& other) const
0026     {
0027         return replacementText == other.replacementText
0028             && range == other.range
0029             && description == other.description
0030             && currentText == other.currentText;
0031     }
0032 };
0033 Q_DECLARE_TYPEINFO(ClangFixit, Q_MOVABLE_TYPE);
0034 
0035 namespace QTest {
0036 template<>
0037 inline char *toString(const ClangFixit& fixit)
0038 {
0039     QByteArray ba = "ClangFixit[replacementText=" + fixit.replacementText.toUtf8()
0040         + ", range=" + QByteArray(QTest::toString(fixit.range))
0041         + ", description=" + fixit.description.toUtf8()
0042         + ", currentText=" + fixit.currentText.toUtf8()
0043         + "]";
0044     return qstrdup(ba.data());
0045 }
0046 }
0047 
0048 QDebug KDEVCLANGPRIVATE_EXPORT operator<<(QDebug debug, const ClangFixit& fixit);
0049 
0050 using ClangFixits = QVector<ClangFixit>;
0051 
0052 class KDEVCLANGPRIVATE_EXPORT ClangProblem : public KDevelop::Problem
0053 {
0054 public:
0055     using Ptr = QExplicitlySharedDataPointer<ClangProblem>;
0056     using ConstPtr = QExplicitlySharedDataPointer<const ClangProblem>;
0057 
0058     /**
0059      * Creates an empty ClangProblem.
0060      */
0061     ClangProblem();
0062 
0063     /**
0064      * Creates a deep copy of a ClangProblem.
0065      */
0066     ClangProblem(const ClangProblem& other);
0067 
0068     /**
0069      * Import @p diagnostic into a ClangProblem object
0070      *
0071      * @param[in] diagnostic To-be-imported clang diagnostic
0072      */
0073     ClangProblem(CXDiagnostic diagnostic, CXTranslationUnit unit);
0074 
0075     KDevelop::IAssistant::Ptr solutionAssistant() const override;
0076 
0077     ClangFixits fixits() const;
0078     void setFixits(const ClangFixits& fixits);
0079 
0080     /**
0081      * Retrieve all fixits of this problem and its child diagnostics
0082      *
0083      * @return A mapping of problem pointers to the list of associated fixits
0084      */
0085     ClangFixits allFixits() const;
0086 
0087 private:
0088     ClangFixits m_fixits;
0089 };
0090 
0091 
0092 class KDEVCLANGPRIVATE_EXPORT ClangFixitAssistant : public KDevelop::IAssistant
0093 {
0094     Q_OBJECT
0095 
0096 public:
0097     explicit ClangFixitAssistant(const ClangFixits& fixits);
0098     ClangFixitAssistant(const QString& title, const ClangFixits& fixits);
0099 
0100     QString title() const override;
0101 
0102     void createActions() override;
0103 
0104     ClangFixits fixits() const;
0105 
0106 private:
0107     QString m_title;
0108     ClangFixits m_fixits;
0109 };
0110 
0111 class KDEVCLANGPRIVATE_EXPORT ClangFixitAction : public KDevelop::IAssistantAction
0112 {
0113     Q_OBJECT
0114 
0115 public:
0116     explicit ClangFixitAction(const ClangFixit& fixit);
0117 
0118     QString description() const override;
0119 
0120 public Q_SLOTS:
0121     void execute() override;
0122 
0123 private:
0124     ClangFixit m_fixit;
0125 };
0126 
0127 #endif // CLANGPROBLEM_H