1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "itestparser.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cppeditor/cppmodelmanager.h>
#include <utils/textfileformat.h>
#include <utils/algorithm.h>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
namespace Autotest {
using LookupInfo = QPair<QString, QString>;
static QHash<LookupInfo, bool> s_pchLookupCache;
Q_GLOBAL_STATIC(QMutex, s_cacheMutex);
CppParser::CppParser(ITestFramework *framework)
: ITestParser(framework)
{
}
void CppParser::init(const Utils::FilePaths &filesToParse, bool fullParse)
{
Q_UNUSED(filesToParse)
Q_UNUSED(fullParse)
m_cppSnapshot = CppEditor::CppModelManager::instance()->snapshot();
m_workingCopy = CppEditor::CppModelManager::instance()->workingCopy();
}
bool CppParser::selectedForBuilding(const Utils::FilePath &fileName)
{
QList<CppEditor::ProjectPart::ConstPtr> projParts =
CppEditor::CppModelManager::instance()->projectPart(fileName);
return !projParts.isEmpty() && projParts.at(0)->selectedForBuilding;
}
QByteArray CppParser::getFileContent(const Utils::FilePath &filePath) const
{
QByteArray fileContent;
if (m_workingCopy.contains(filePath)) {
fileContent = m_workingCopy.source(filePath);
} else {
QString error;
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
if (Utils::TextFileFormat::readFileUTF8(filePath, codec, &fileContent, &error)
!= Utils::TextFileFormat::ReadSuccess) {
qDebug() << "Failed to read file" << filePath << ":" << error;
}
}
fileContent.replace("\r\n", "\n");
return fileContent;
}
bool precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const Utils::FilePath &filePath,
const QString &cacheString,
const std::function<bool(const QString &)> &checker)
{
const CppEditor::CppModelManager *modelManager = CppEditor::CppModelManager::instance();
const QList<CppEditor::ProjectPart::ConstPtr> projectParts = modelManager->projectPart(filePath);
if (projectParts.isEmpty())
return false;
const QStringList precompiledHeaders = projectParts.first()->precompiledHeaders;
auto headerContains = [&](const QString &header){
LookupInfo info{header, cacheString};
auto it = s_pchLookupCache.find(info);
if (it == s_pchLookupCache.end()) {
it = s_pchLookupCache.insert(info,
Utils::anyOf(snapshot.allIncludesForDocument(header),
checker));
}
return it.value();
};
QMutexLocker l(s_cacheMutex());
return Utils::anyOf(precompiledHeaders, headerContains);
}
bool CppParser::precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const Utils::FilePath &filePath,
const QString &headerFilePath)
{
return Autotest::precompiledHeaderContains(snapshot,
filePath,
headerFilePath,
[&](const QString &include) {
return include.endsWith(headerFilePath);
});
}
bool CppParser::precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const Utils::FilePath &filePath,
const QRegularExpression &headerFileRegex)
{
return Autotest::precompiledHeaderContains(snapshot,
filePath,
headerFileRegex.pattern(),
[&](const QString &include) {
return headerFileRegex.match(include).hasMatch();
});
}
void CppParser::release()
{
m_cppSnapshot = CPlusPlus::Snapshot();
m_workingCopy = CppEditor::WorkingCopy();
QMutexLocker l(s_cacheMutex());
s_pchLookupCache.clear();
}
CPlusPlus::Document::Ptr CppParser::document(const Utils::FilePath &fileName)
{
return selectedForBuilding(fileName) ? m_cppSnapshot.document(fileName) : nullptr;
}
} // namespace Autotest
|