Debian Codegraph @

src/PreProcessorVisitor.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "PreProcessorVisitor.h"
8 #include "MacroUtils.h"
9
10 #include <clang/Basic/IdentifierTable.h>
11 #include <clang/Basic/SourceLocation.h>
12 #include <clang/Basic/SourceManager.h>
13 #include <clang/Frontend/CompilerInstance.h>
14 #include <clang/Lex/MacroInfo.h>
15 #include <clang/Lex/PPCallbacks.h>
16 #include <clang/Lex/Preprocessor.h>
17 #include <clang/Lex/Token.h>
18 #include <llvm/ADT/ArrayRef.h>
19
20 #include <cstdlib>
21 #include <memory>
22
23 using namespace clang;
24
25 PreProcessorVisitor::PreProcessorVisitor(Preprocessor &pp)
26 : clang::PPCallbacks()
27 , m_pp(pp)
28
29 {
30 pp.addPPCallbacks(std::unique_ptr<PPCallbacks>(this));
31
32 // This catches -DQT_NO_KEYWORDS passed to compiler. In MacroExpands() we catch when defined via in code
33 m_isQtNoKeywords = clazy::isPredefined(pp.getPreprocessorOpts(), "QT_NO_KEYWORDS");
34 }
35
36 bool PreProcessorVisitor::isBetweenQtNamespaceMacros(SourceLocation loc)
37 {
38 if (loc.isInvalid()) {
39 return false;
40 }
41
42 const auto &sm = m_pp.getSourceManager();
43 if (loc.isMacroID()) {
44 loc = sm.getExpansionLoc(loc);
45 }
46
47 uint fileId = sm.getFileID(loc).getHashValue();
48
49 std::vector<SourceRange> &pairs = m_q_namespace_macro_locations[fileId];
50 for (SourceRange &pair : pairs) {
51 if (pair.getBegin().isInvalid() || pair.getEnd().isInvalid()) {
52 // llvm::errs() << "PreProcessorVisitor::isBetweenQtNamespaceMacros Found invalid location\n";
53 continue; // shouldn't happen
54 }
55
56 if (sm.isBeforeInSLocAddrSpace(pair.getBegin(), loc) && sm.isBeforeInSLocAddrSpace(loc, pair.getEnd())) {
57 return true;
58 }
59 }
60
61 return false;
62 }
63
64 bool PreProcessorVisitor::hasInclude(const std::string &fileName, bool IsAngled) const
65 {
66 return std::ranges::any_of(m_includeInfo, [&](const IncludeInfo &info) {
67 return info.fileName == fileName && info.IsAngled == IsAngled;
68 });
69 }
70
71 SourceLocation PreProcessorVisitor::endOfIncludeSection() const
72 {
73 if (m_includeInfo.empty()) {
74 return {};
75 }
76 return m_includeInfo.back().filenameRange.getEnd();
77 }
78
79 std::string PreProcessorVisitor::getTokenSpelling(const MacroDefinition &def) const
80 {
81 if (!def) {
82 return {};
83 }
84
85 MacroInfo *info = def.getMacroInfo();
86 if (!info) {
87 return {};
88 }
89
90 std::string result;
91 for (const auto &tok : info->tokens()) {
92 result += m_pp.getSpelling(tok);
93 }
94
95 return result;
96 }
97
98 void PreProcessorVisitor::updateQtVersion()
99 {
100 if (m_qtMajorVersion == -1 || m_qtPatchVersion == -1 || m_qtMinorVersion == -1) {
101 m_qtVersion = -1;
102 } else {
103 m_qtVersion = m_qtPatchVersion + m_qtMinorVersion * 100 + m_qtMajorVersion * 10000;
104 }
105 }
106
107 void PreProcessorVisitor::handleQtNamespaceMacro(SourceLocation loc, StringRef name)
108 {
109 const bool isBegin = name == "QT_BEGIN_NAMESPACE";
110 uint fileId = m_pp.getSourceManager().getFileID(loc).getHashValue();
111 std::vector<SourceRange> &pairs = m_q_namespace_macro_locations[fileId];
112
113 if (isBegin) {
114 pairs.push_back(SourceRange(loc, {}));
115 } else {
116 if (pairs.empty()) {
117 // llvm::errs() << "FOO Received end!!";
118 } else {
119 SourceRange &range = pairs[pairs.size() - 1];
120 if (range.getBegin().isInvalid()) {
121 // llvm::errs() << "FOO Error received end before a begin\n";
122 } else {
123 range.setEnd(loc);
124 }
125 }
126 }
127 }
128
129 static int stringToNumber(const std::string &str)
130 {
131 if (str.empty()) {
132 return -1;
133 }
134
135 return atoi(str.c_str());
136 }
137
138 void PreProcessorVisitor::MacroExpands(const Token &MacroNameTok, const MacroDefinition &def, SourceRange range, const MacroArgs *)
139 {
140 IdentifierInfo *ii = MacroNameTok.getIdentifierInfo();
141 if (!ii) {
142 return;
143 }
144
145 if (ii->getName() == "QT_BEGIN_NAMESPACE" || ii->getName() == "QT_END_NAMESPACE") {
146 handleQtNamespaceMacro(range.getBegin(), ii->getName());
147 return;
148 }
149
150 if (!m_isQtNoKeywords && ii->getName() == "QT_NO_KEYWORDS") {
151 m_isQtNoKeywords = true;
152 return;
153 }
154
155 if (m_qtVersion != -1) {
156 return;
157 }
158
159 auto name = ii->getName();
160 if (name == "QT_VERSION_MAJOR") {
161 m_qtMajorVersion = stringToNumber(getTokenSpelling(def));
162 updateQtVersion();
163 }
164
165 if (name == "QT_VERSION_MINOR") {
166 m_qtMinorVersion = stringToNumber(getTokenSpelling(def));
167 updateQtVersion();
168 }
169
170 if (name == "QT_VERSION_PATCH") {
171 m_qtPatchVersion = stringToNumber(getTokenSpelling(def));
172 updateQtVersion();
173 }
174 }
175
176 void PreProcessorVisitor::InclusionDirective(clang::SourceLocation,
177 const clang::Token &,
178 clang::StringRef FileName,
179 bool IsAngled,
180 clang::CharSourceRange FilenameRange,
181 clazy::OptionalFileEntryRef,
182 clang::StringRef,
183 clang::StringRef,
184 const clang::Module *,
185 bool,
186 clang::SrcMgr::CharacteristicKind)
187 {
188 if (m_pp.isInPrimaryFile() && !clazy::endsWith(FileName.str(), ".moc")) {
189 m_includeInfo.push_back(IncludeInfo{FileName, IsAngled, FilenameRange});
190 }
191 }