File indexing completed on 2024-05-12 04:39:22
0001 /* 0002 SPDX-FileCopyrightText: 2016 Carlos Nihelton <carlosnsoliveira@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef CLANGTIDY_REPLACEMENTPARSER_H 0008 #define CLANGTIDY_REPLACEMENTPARSER_H 0009 0010 // KDevPlatform 0011 #include <language/editor/documentrange.h> 0012 // Qt 0013 #include <QFile> 0014 #include <QRegularExpression> 0015 #include <QVector> 0016 // Boost 0017 #include <boost/utility/string_ref.hpp> 0018 0019 using KDevelop::DocumentRange; 0020 using KDevelop::IndexedString; 0021 0022 namespace ClangTidy 0023 { 0024 0025 /** 0026 * \struct 0027 * \brief contains basic elements for one replacement in source code. 0028 * 0029 */ 0030 struct Replacement { 0031 size_t offset, length; ///< read from YAML. 0032 QString replacementText; ///< read from YAML. 0033 DocumentRange range; ///< created from line and column. 0034 }; 0035 0036 using Replacements = QVector<Replacement>; 0037 0038 /** 0039 * Implements the parser for the YAML file generated by clang-tidy with the recommended corrections. 0040 */ 0041 class ReplacementParser 0042 { 0043 public: 0044 ReplacementParser() = default; 0045 explicit ReplacementParser(const QString& yaml_file, const QString& source_file); 0046 0047 public: 0048 void parse(); 0049 size_t count() const { return cReplacements; } 0050 Replacements allReplacements() { return all_replacements; } 0051 0052 protected: 0053 /** 0054 * \function 0055 * \brief generates the next replacement from the regex capture list. 0056 * \param smatch the captured match. 0057 * \return Replacement 0058 */ 0059 Replacement nextNode(const QRegularExpressionMatch& smatch); 0060 0061 /** 0062 * \function 0063 * \brief compose a range in KTextEditor from the offset and length components of the Replacement being processed. 0064 * \return KDevelop::DocumentRange 0065 * \warning the range can be invalid in case offset and length overcome the substring length. 0066 */ 0067 KDevelop::DocumentRange composeNextNodeRange(size_t offset, size_t length); 0068 0069 private: 0070 size_t currentLine; ///< current line on source code while parsing. 0071 size_t currentColumn; ///< current column on source code while parsing. 0072 size_t currentOffset; ///< current offset in bytes since the beginning of the source code while parsing. 0073 size_t cReplacements; ///< current count of replacements parsed. 0074 0075 QString m_yamlname; 0076 QString m_sourceFile; 0077 IndexedString i_source; 0078 QString m_yamlContent; 0079 std::string m_sourceCode; 0080 boost::string_ref m_sourceView; 0081 static const QRegularExpression regex, check; 0082 Replacements all_replacements; 0083 }; 0084 0085 } 0086 0087 #endif // CLANGTIDY_REPLACEMENT_H