1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include <cplusplus/Icons.h>
29 
30 #include <cpptools/projectpart.h>
31 #include <cpptools/compileroptionsbuilder.h>
32 
33 #include <QPair>
34 #include <QTextCursor>
35 
36 QT_BEGIN_NAMESPACE
37 class QTextBlock;
38 QT_END_NAMESPACE
39 
40 namespace CppTools {
41 class CppEditorDocumentHandle;
42 class ProjectInfo;
43 }
44 
45 namespace ClangBackEnd { class TokenInfoContainer; }
46 
47 namespace ClangCodeModel {
48 namespace Internal {
49 
50 CppTools::CppEditorDocumentHandle *cppDocument(const QString &filePath);
51 void setLastSentDocumentRevision(const QString &filePath, uint revision);
52 
53 QPair<Utils::Id, QStringList> createClangOptions(const CppTools::ProjectPart &projectPart,
54                                                  const QString &filePath);
55 
56 CppTools::ProjectPart::Ptr projectPartForFile(const QString &filePath);
57 CppTools::ProjectPart::Ptr projectPartForFileBasedOnProcessor(const QString &filePath);
58 bool isProjectPartLoaded(const CppTools::ProjectPart::Ptr projectPart);
59 QString projectPartIdForFile(const QString &filePath);
60 int clangColumn(const QTextBlock &line, int cppEditorColumn);
61 int cppEditorColumn(const QTextBlock &line, int clangColumn);
62 
63 QString currentCppEditorDocumentFilePath();
64 
65 QString diagnosticCategoryPrefixRemoved(const QString &text);
66 
67 Utils::CodeModelIcon::Type iconTypeForToken(const ClangBackEnd::TokenInfoContainer &token);
68 
69 class GenerateCompilationDbResult
70 {
71 public:
72     GenerateCompilationDbResult() = default;
GenerateCompilationDbResult(const QString & filePath,const QString & error)73     GenerateCompilationDbResult(const QString &filePath, const QString &error)
74         : filePath(filePath), error(error)
75     {}
76 
77     QString filePath;
78     QString error;
79 };
80 
81 enum class CompilationDbPurpose { Project, CodeModel };
82 GenerateCompilationDbResult generateCompilationDB(CppTools::ProjectInfo projectInfo,
83                                                   CompilationDbPurpose purpose);
84 
85 class DiagnosticTextInfo
86 {
87 public:
88     DiagnosticTextInfo(const QString &text);
89 
90     QString textWithoutOption() const;
91     QString option() const;
92     QString category() const;
93 
94     static bool isClazyOption(const QString &option);
95     static QString clazyCheckName(const QString &option);
96 
97 private:
98     const QString m_text;
99     const int m_squareBracketStartIndex;
100 };
101 
102 template <class CharacterProvider>
moveToPreviousChar(CharacterProvider & provider,QTextCursor & cursor)103 void moveToPreviousChar(CharacterProvider &provider, QTextCursor &cursor)
104 {
105     cursor.movePosition(QTextCursor::PreviousCharacter);
106     while (provider.characterAt(cursor.position()).isSpace())
107         cursor.movePosition(QTextCursor::PreviousCharacter);
108 }
109 
110 template <class CharacterProvider>
moveToPreviousWord(CharacterProvider & provider,QTextCursor & cursor)111 void moveToPreviousWord(CharacterProvider &provider, QTextCursor &cursor)
112 {
113     cursor.movePosition(QTextCursor::PreviousWord);
114     while (provider.characterAt(cursor.position()) == ':')
115         cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 2);
116 }
117 
118 template <class CharacterProvider>
matchPreviousWord(CharacterProvider & provider,QTextCursor cursor,QString pattern)119 bool matchPreviousWord(CharacterProvider &provider, QTextCursor cursor, QString pattern)
120 {
121     cursor.movePosition(QTextCursor::PreviousWord);
122     while (provider.characterAt(cursor.position()) == ':')
123         cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 2);
124 
125     int previousWordStart = cursor.position();
126     cursor.movePosition(QTextCursor::NextWord);
127     moveToPreviousChar(provider, cursor);
128     QString toMatch = provider.textAt(previousWordStart, cursor.position() - previousWordStart + 1);
129 
130     pattern = pattern.simplified();
131     while (!pattern.isEmpty() && pattern.endsWith(toMatch)) {
132         pattern.chop(toMatch.length());
133         if (pattern.endsWith(' '))
134             pattern.chop(1);
135         if (!pattern.isEmpty()) {
136             cursor.movePosition(QTextCursor::StartOfWord);
137             cursor.movePosition(QTextCursor::PreviousWord);
138             previousWordStart = cursor.position();
139             cursor.movePosition(QTextCursor::NextWord);
140             moveToPreviousChar(provider, cursor);
141             toMatch = provider.textAt(previousWordStart, cursor.position() - previousWordStart + 1);
142         }
143     }
144     return pattern.isEmpty();
145 }
146 
147 } // namespace Internal
148 } // namespace Clang
149