1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef CPP_H |
5 | #define CPP_H |
6 | |
7 | #include "lupdate.h" |
8 | |
9 | #include <QtCore/QSet> |
10 | |
11 | #include <iostream> |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | struct HashString { |
16 | HashString() : m_hash(0x80000000) {} |
17 | explicit HashString(const QString &str) : m_str(str), m_hash(0x80000000) {} |
18 | void setValue(const QString &str) { m_str = str; m_hash = 0x80000000; } |
19 | const QString &value() const { return m_str; } |
20 | bool operator==(const HashString &other) const { return m_str == other.m_str; } |
21 | QString m_str; |
22 | |
23 | mutable uint m_hash; // We use the highest bit as a validity indicator (set => invalid) |
24 | }; |
25 | |
26 | struct HashStringList { |
27 | explicit HashStringList(const QList<HashString> &list) : m_list(list), m_hash(0x80000000) {} |
28 | const QList<HashString> &value() const { return m_list; } |
29 | bool operator==(const HashStringList &other) const { return m_list == other.m_list; } |
30 | |
31 | QList<HashString> m_list; |
32 | mutable uint m_hash; // We use the highest bit as a validity indicator (set => invalid) |
33 | }; |
34 | |
35 | typedef QList<HashString> NamespaceList; |
36 | |
37 | struct Namespace { |
38 | |
39 | Namespace() : |
40 | classDef(this), |
41 | hasTrFunctions(false), complained(false) |
42 | {} |
43 | ~Namespace() |
44 | { |
45 | qDeleteAll(c: children); |
46 | } |
47 | |
48 | QHash<HashString, Namespace *> children; |
49 | QHash<HashString, NamespaceList> aliases; |
50 | QList<HashStringList> usings; |
51 | |
52 | // Class declarations set no flags and create no namespaces, so they are ignored. |
53 | // Class definitions may appear multiple times - but only because we are trying to |
54 | // "compile" all sources irrespective of build configuration. |
55 | // Nested classes may be forward-declared inside a definition, and defined in another file. |
56 | // The latter will detach the class' child list, so clones need a backlink to the original |
57 | // definition (either one in case of multiple definitions). |
58 | // Namespaces can have tr() functions as well, so we need to track parent definitions for |
59 | // them as well. The complication is that we may have to deal with a forrest instead of |
60 | // a tree - in that case the parent will be arbitrary. However, it seem likely that |
61 | // Q_DECLARE_TR_FUNCTIONS would be used either in "class-like" namespaces with a central |
62 | // header or only locally in a file. |
63 | Namespace *classDef; |
64 | |
65 | QString trQualification; |
66 | |
67 | bool hasTrFunctions; |
68 | bool complained; // ... that tr functions are missing. |
69 | }; |
70 | |
71 | struct ParseResults { |
72 | int fileId; |
73 | Namespace rootNamespace; |
74 | QSet<const ParseResults *> includes; |
75 | }; |
76 | |
77 | struct IncludeCycle { |
78 | QSet<QString> fileNames; |
79 | QSet<const ParseResults *> results; |
80 | }; |
81 | |
82 | typedef QHash<QString, IncludeCycle *> IncludeCycleHash; |
83 | typedef QHash<QString, const Translator *> TranslatorHash; |
84 | |
85 | class CppFiles { |
86 | |
87 | public: |
88 | static QSet<const ParseResults *> getResults(const QString &cleanFile); |
89 | static void setResults(const QString &cleanFile, const ParseResults *results); |
90 | static const Translator *getTranslator(const QString &cleanFile); |
91 | static void setTranslator(const QString &cleanFile, const Translator *results); |
92 | static bool isBlacklisted(const QString &cleanFile); |
93 | static void setBlacklisted(const QString &cleanFile); |
94 | static void addIncludeCycle(const QSet<QString> &fileNames); |
95 | |
96 | private: |
97 | static IncludeCycleHash &includeCycles(); |
98 | static TranslatorHash &translatedFiles(); |
99 | static QSet<QString> &blacklistedFiles(); |
100 | }; |
101 | |
102 | QT_END_NAMESPACE |
103 | |
104 | #endif // CPP_H |
105 | |