| 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 | #include <QtCore/QStack> |
| 11 | |
| 12 | #include <iostream> |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | struct HashString { |
| 17 | HashString() : m_hash(0x80000000) {} |
| 18 | explicit HashString(const QString &str) : m_str(str), m_hash(0x80000000) {} |
| 19 | void setValue(const QString &str) { m_str = str; m_hash = 0x80000000; } |
| 20 | const QString &value() const { return m_str; } |
| 21 | bool operator==(const HashString &other) const { return m_str == other.m_str; } |
| 22 | QString m_str; |
| 23 | |
| 24 | mutable uint m_hash; // We use the highest bit as a validity indicator (set => invalid) |
| 25 | }; |
| 26 | |
| 27 | struct HashStringList { |
| 28 | explicit HashStringList(const QList<HashString> &list) : m_list(list), m_hash(0x80000000) {} |
| 29 | const QList<HashString> &value() const { return m_list; } |
| 30 | bool operator==(const HashStringList &other) const { return m_list == other.m_list; } |
| 31 | |
| 32 | QList<HashString> m_list; |
| 33 | mutable uint m_hash; // We use the highest bit as a validity indicator (set => invalid) |
| 34 | }; |
| 35 | |
| 36 | typedef QList<HashString> NamespaceList; |
| 37 | |
| 38 | struct Namespace { |
| 39 | |
| 40 | Namespace() : |
| 41 | classDef(this), |
| 42 | hasTrFunctions(false), complained(false) |
| 43 | {} |
| 44 | ~Namespace() |
| 45 | { |
| 46 | qDeleteAll(c: children); |
| 47 | } |
| 48 | |
| 49 | QHash<HashString, Namespace *> children; |
| 50 | QHash<HashString, NamespaceList> aliases; |
| 51 | QList<HashStringList> usings; |
| 52 | |
| 53 | // Class declarations set no flags and create no namespaces, so they are ignored. |
| 54 | // Class definitions may appear multiple times - but only because we are trying to |
| 55 | // "compile" all sources irrespective of build configuration. |
| 56 | // Nested classes may be forward-declared inside a definition, and defined in another file. |
| 57 | // The latter will detach the class' child list, so clones need a backlink to the original |
| 58 | // definition (either one in case of multiple definitions). |
| 59 | // Namespaces can have tr() functions as well, so we need to track parent definitions for |
| 60 | // them as well. The complication is that we may have to deal with a forrest instead of |
| 61 | // a tree - in that case the parent will be arbitrary. However, it seem likely that |
| 62 | // Q_DECLARE_TR_FUNCTIONS would be used either in "class-like" namespaces with a central |
| 63 | // header or only locally in a file. |
| 64 | Namespace *classDef; |
| 65 | |
| 66 | QString trQualification; |
| 67 | |
| 68 | bool hasTrFunctions; |
| 69 | bool complained; // ... that tr functions are missing. |
| 70 | }; |
| 71 | |
| 72 | struct ParseResults { |
| 73 | int fileId; |
| 74 | Namespace rootNamespace; |
| 75 | QSet<const ParseResults *> includes; |
| 76 | }; |
| 77 | |
| 78 | struct IncludeCycle { |
| 79 | QSet<QString> fileNames; |
| 80 | QSet<const ParseResults *> results; |
| 81 | }; |
| 82 | |
| 83 | struct CppParserState |
| 84 | { |
| 85 | NamespaceList namespaces; |
| 86 | QStack<qsizetype> namespaceDepths; |
| 87 | NamespaceList functionContext; |
| 88 | QString functionContextUnresolved; |
| 89 | QString pendingContext; |
| 90 | |
| 91 | bool operator==(const CppParserState &other) const |
| 92 | { |
| 93 | return namespaces == other.namespaces |
| 94 | && namespaceDepths == other.namespaceDepths |
| 95 | && functionContext == other.functionContext |
| 96 | && functionContextUnresolved == other.functionContextUnresolved |
| 97 | && pendingContext == other.pendingContext; |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | size_t qHash(const CppParserState &s, size_t seed); |
| 102 | |
| 103 | struct ResultsCacheKey |
| 104 | { |
| 105 | const QString cleanFile; |
| 106 | const CppParserState parserState; |
| 107 | |
| 108 | ResultsCacheKey(const QString &filePath) |
| 109 | : cleanFile(filePath) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | ResultsCacheKey(const QString &filePath, const CppParserState &state) |
| 114 | : cleanFile(filePath), |
| 115 | parserState(state) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | bool operator==(const ResultsCacheKey &other) const |
| 120 | { |
| 121 | return cleanFile == other.cleanFile |
| 122 | && parserState == other.parserState; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | size_t qHash(const ResultsCacheKey &key, size_t seed); |
| 127 | |
| 128 | typedef QHash<ResultsCacheKey, IncludeCycle *> IncludeCycleHash; |
| 129 | typedef QHash<QString, const Translator *> TranslatorHash; |
| 130 | |
| 131 | class CppFiles { |
| 132 | public: |
| 133 | static QSet<const ParseResults *> getResults(const ResultsCacheKey &key); |
| 134 | static void setResults(const ResultsCacheKey &key, const ParseResults *results); |
| 135 | static const Translator *getTranslator(const QString &cleanFile); |
| 136 | static void setTranslator(const QString &cleanFile, const Translator *results); |
| 137 | static bool isBlacklisted(const QString &cleanFile); |
| 138 | static void setBlacklisted(const QString &cleanFile); |
| 139 | static void addIncludeCycle(const QSet<QString> &fileNames, const CppParserState &parserState); |
| 140 | |
| 141 | private: |
| 142 | static IncludeCycleHash &includeCycles(); |
| 143 | static TranslatorHash &translatedFiles(); |
| 144 | static QSet<QString> &blacklistedFiles(); |
| 145 | }; |
| 146 | |
| 147 | QT_END_NAMESPACE |
| 148 | |
| 149 | #endif // CPP_H |
| 150 | |