1 | // Copyright (C) 2022 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "filesignificancecheck.h" |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | FileSignificanceCheck *FileSignificanceCheck::m_instance = nullptr; |
9 | |
10 | void FileSignificanceCheck::setRootDirectories(const QStringList &paths) |
11 | { |
12 | const size_t pathsSize = static_cast<size_t>(paths.size()); |
13 | m_rootDirs.resize(new_size: pathsSize); |
14 | for (size_t i = 0; i < pathsSize; ++i) |
15 | m_rootDirs[i].setPath(paths.at(i)); |
16 | } |
17 | |
18 | void FileSignificanceCheck::setExclusionRegExes(const QVector<QRegularExpression> &expressions) |
19 | { |
20 | m_exclusionRegExes = expressions; |
21 | } |
22 | |
23 | /* |
24 | * Return true if the given source file is significant for lupdate. |
25 | * A file is considered insignificant if |
26 | * - it's not within any project root |
27 | * - it's excluded |
28 | * |
29 | * This method is called from multiple threads. |
30 | * Results are cached. |
31 | */ |
32 | bool FileSignificanceCheck::isFileSignificant(const std::string &filePath) const |
33 | { |
34 | // cache lookup |
35 | { |
36 | QReadLocker locker(&m_cacheLock); |
37 | auto it = m_cache.find(x: filePath); |
38 | if (it != m_cache.end()) |
39 | return it->second; |
40 | } |
41 | |
42 | // cache miss |
43 | QWriteLocker locker(&m_cacheLock); |
44 | QString file = QString::fromUtf8(utf8: filePath); |
45 | QString cleanFile = QDir::cleanPath(path: file); |
46 | for (const QRegularExpression &rx : m_exclusionRegExes) { |
47 | if (rx.match(subject: cleanFile).hasMatch()) { |
48 | m_cache.insert(x: {filePath, false}); |
49 | return false; |
50 | } |
51 | } |
52 | |
53 | for (const QDir &rootDir : m_rootDirs) { |
54 | QString relativeFilePath = rootDir.relativeFilePath(fileName: file); |
55 | if (!relativeFilePath.startsWith(s: QLatin1String("../")) |
56 | && QFileInfo(relativeFilePath).isRelative()) { |
57 | m_cache.insert(x: {filePath, true}); |
58 | return true; |
59 | } |
60 | } |
61 | |
62 | m_cache.insert(x: {filePath, false}); |
63 | return false; |
64 | } |
65 | |
66 | QT_END_NAMESPACE |
67 |