Debian Codegraph @

src/checkbase.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileContributor: Sérgio Martins <[email protected]>
4
5 SPDX-FileCopyrightText: 2015-2017 Sergio Martins <[email protected]>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10 #include "checkbase.h"
11 #include "ClazyContext.h"
12 #include "SuppressionManager.h"
13 #include "Utils.h"
14 #include "clazy_stl.h"
15
16 #include <clang/AST/DeclBase.h>
17 #include <clang/AST/Stmt.h>
18 #include <clang/Basic/Diagnostic.h>
19 #include <clang/Basic/DiagnosticIDs.h>
20 #include <clang/Basic/SourceManager.h>
21 #include <clang/Frontend/CompilerInstance.h>
22 #include <clang/Lex/MacroInfo.h>
23 #include <clang/Lex/Preprocessor.h>
24 #include <llvm/ADT/IntrusiveRefCntPtr.h>
25 #include <llvm/ADT/StringRef.h>
26 #include <llvm/Support/raw_ostream.h>
27
28 #include <memory>
29 #include <vector>
30
31 namespace clang
32 {
33 class MacroArgs;
34 class Token;
35 } // namespace clang
36
37 using namespace clang;
38 using namespace clang::ast_matchers;
39
40 ClazyPreprocessorCallbacks::ClazyPreprocessorCallbacks(CheckBase *check)
41 : check(check)
42 {
43 }
44
45 void ClazyPreprocessorCallbacks::MacroExpands(const Token &macroNameTok, const MacroDefinition &md, SourceRange range, const MacroArgs *)
46 {
47 check->VisitMacroExpands(macroNameTok, range, md.getMacroInfo());
48 }
49
50 void ClazyPreprocessorCallbacks::Defined(const Token &macroNameTok, const MacroDefinition &, SourceRange range)
51 {
52 check->VisitDefined(macroNameTok, range);
53 }
54
55 void ClazyPreprocessorCallbacks::Ifdef(SourceLocation loc, const Token &macroNameTok, const MacroDefinition &)
56 {
57 check->VisitIfdef(loc, macroNameTok);
58 }
59
60 void ClazyPreprocessorCallbacks::Ifndef(SourceLocation loc, const Token &macroNameTok, const MacroDefinition &)
61 {
62 check->VisitIfndef(loc, macroNameTok);
63 }
64
65 void ClazyPreprocessorCallbacks::If(SourceLocation loc, SourceRange conditionRange, PPCallbacks::ConditionValueKind conditionValue)
66 {
67 check->VisitIf(loc, conditionRange, conditionValue);
68 }
69
70 void ClazyPreprocessorCallbacks::Elif(SourceLocation loc, SourceRange conditionRange, PPCallbacks::ConditionValueKind conditionValue, SourceLocation ifLoc)
71 {
72 check->VisitElif(loc, conditionRange, conditionValue, ifLoc);
73 }
74
75 void ClazyPreprocessorCallbacks::Else(SourceLocation loc, SourceLocation ifLoc)
76 {
77 check->VisitElse(loc, ifLoc);
78 }
79
80 void ClazyPreprocessorCallbacks::Endif(SourceLocation loc, SourceLocation ifLoc)
81 {
82 check->VisitEndif(loc, ifLoc);
83 }
84
85 void ClazyPreprocessorCallbacks::MacroDefined(const Token &macroNameTok, const MacroDirective *)
86 {
87 check->VisitMacroDefined(macroNameTok);
88 }
89
90 void ClazyPreprocessorCallbacks::InclusionDirective(clang::SourceLocation HashLoc,
91 const clang::Token &IncludeTok,
92 llvm::StringRef FileName,
93 bool IsAngled,
94 clang::CharSourceRange FilenameRange,
95 clazy::OptionalFileEntryRef File,
96 llvm::StringRef SearchPath,
97 llvm::StringRef RelativePath,
98 const clang::Module *SuggestedModule,
99 bool ModuleImported,
100 clang::SrcMgr::CharacteristicKind FileType)
101 {
102 check->VisitInclusionDirective(HashLoc,
103 IncludeTok,
104 FileName,
105 IsAngled,
106 FilenameRange,
107 File,
108 SearchPath,
109 RelativePath,
110 SuggestedModule,
111 ModuleImported,
112 FileType);
113 }
114
115 CheckBase::CheckBase(const std::string &name, Options options)
116 : m_name(name)
117 , m_preprocessorCallbacks(new ClazyPreprocessorCallbacks(this))
118 , m_options(options)
119
120 {
121 }
122
123 CheckBase::~CheckBase() = default;
124
125 void CheckBase::VisitStmt(Stmt *)
126 {
127 }
128
129 void CheckBase::VisitDecl(Decl *)
130 {
131 }
132
133 void CheckBase::VisitMacroExpands(const Token &, const SourceRange &, const clang::MacroInfo *)
134 {
135 }
136
137 void CheckBase::VisitMacroDefined(const Token &)
138 {
139 }
140
141 void CheckBase::VisitDefined(const Token &, const SourceRange &)
142 {
143 }
144
145 void CheckBase::VisitIfdef(clang::SourceLocation, const clang::Token &)
146 {
147 }
148
149 void CheckBase::VisitIfndef(SourceLocation, const Token &)
150 {
151 }
152
153 void CheckBase::VisitIf(SourceLocation, SourceRange, clang::PPCallbacks::ConditionValueKind)
154 {
155 }
156
157 void CheckBase::VisitElif(SourceLocation, SourceRange, clang::PPCallbacks::ConditionValueKind, SourceLocation)
158 {
159 }
160
161 void CheckBase::VisitElse(SourceLocation, SourceLocation)
162 {
163 }
164
165 void CheckBase::VisitEndif(SourceLocation, SourceLocation)
166 {
167 }
168
169 void CheckBase::VisitInclusionDirective(clang::SourceLocation,
170 const clang::Token &,
171 llvm::StringRef,
172 bool,
173 clang::CharSourceRange,
174 clazy::OptionalFileEntryRef,
175 llvm::StringRef,
176 llvm::StringRef,
177 const clang::Module *,
178 bool ModuleImported,
179 clang::SrcMgr::CharacteristicKind)
180 {
181 }
182
183 void CheckBase::enablePreProcessorCallbacks(clang::Preprocessor &pp)
184 {
185 pp.addPPCallbacks(std::unique_ptr<PPCallbacks>(m_preprocessorCallbacks));
186 }
187
188 bool CheckBase::shouldIgnoreFile(SourceLocation loc) const
189 {
190 if (m_filesToIgnore.empty()) {
191 return false;
192 }
193
194 if (!loc.isValid()) {
195 return true;
196 }
197
198 std::string filename = sm().getFilename(loc).str();
199 return std::ranges::any_of(m_filesToIgnore, [filename](const std::string &ignored) {
200 return clazy::contains(filename, ignored);
201 });
202 }
203
204 void CheckBase::emitWarning(const clang::Decl *d, const std::string &error, bool printWarningTag)
205 {
206 emitWarning(d->getBeginLoc(), error, printWarningTag);
207 }
208
209 void CheckBase::emitWarning(const clang::Stmt *s, const std::string &error, bool printWarningTag)
210 {
211 emitWarning(s->getBeginLoc(), error, printWarningTag);
212 }
213
214 void CheckBase::emitWarning(clang::SourceLocation loc, const std::string &error, bool printWarningTag)
215 {
216 emitWarning(loc, error, {}, printWarningTag);
217 }
218
219 std::string CheckBase::tag() const
220 {
221 return !m_context || m_context->m_isClangTidy ? "" : " [-Wclazy-" + m_name + ']';
222 }
223
224 void CheckBase::emitWarning(clang::SourceLocation loc, std::string error, const std::vector<FixItHint> &fixits, bool printWarningTag)
225 {
226 if (m_context->suppressionManager.isSuppressed(m_name, loc, sm(), lo())) {
227 return;
228 }
229
230 if (m_context->shouldIgnoreFile(loc)) {
231 return;
232 }
233
234 if (loc.isMacroID()) {
235 if (warningAlreadyEmitted(loc)) {
236 return; // For warnings in macro arguments we get a warning in each place the argument is used within the expanded macro, so filter all the dups
237 }
238 m_emittedWarningsInMacro.push_back(loc.getRawEncoding());
239 }
240
241 if (printWarningTag) {
242 error += tag();
243 }
244
245 reallyEmitWarning(loc, error, fixits);
246
247 for (const auto &l : m_queuedManualInterventionWarnings) {
248 std::string msg("FixIt failed, requires manual intervention: ");
249 if (!l.second.empty()) {
250 msg += ' ' + l.second;
251 }
252
253 reallyEmitWarning(l.first, msg + tag(), {});
254 }
255
256 m_queuedManualInterventionWarnings.clear();
257 }
258
259 void CheckBase::emitInternalError(SourceLocation loc, std::string error)
260 {
261 llvm::errs() << tag() << ": internal error: " << error << " at " << loc.printToString(sm()) << "\n";
262 }
263
264 void CheckBase::reallyEmitWarning(clang::SourceLocation loc, const std::string &error, const std::vector<FixItHint> &fixits)
265 {
266 auto severity = (m_context->treatAsError(m_name)
267 || (m_context->astContext && m_context->astContext->getDiagnostics().getWarningsAsErrors() && !m_context->userDisabledWError()))
268 ? DiagnosticIDs::Error
269 : DiagnosticIDs::Warning;
270
271 m_context->p_warningReporter(name(), loc, severity, error, fixits);
272 }
273
274 void CheckBase::queueManualFixitWarning(clang::SourceLocation loc, const std::string &message)
275 {
276 if (!manualFixitAlreadyQueued(loc)) {
277 m_queuedManualInterventionWarnings.emplace_back(loc, message);
278 m_emittedManualFixItsWarningsInMacro.emplace_back(loc.getRawEncoding());
279 }
280 }
281
282 bool CheckBase::warningAlreadyEmitted(SourceLocation loc) const
283 {
284 PresumedLoc ploc = sm().getPresumedLoc(loc);
285 for (auto rawLoc : m_emittedWarningsInMacro) {
286 SourceLocation l = SourceLocation::getFromRawEncoding(rawLoc);
287 PresumedLoc p = sm().getPresumedLoc(l);
288 if (Utils::presumedLocationsEqual(p, ploc)) {
289 return true;
290 }
291 }
292
293 return false;
294 }
295
296 bool CheckBase::manualFixitAlreadyQueued(SourceLocation loc) const
297 {
298 PresumedLoc ploc = sm().getPresumedLoc(loc);
299 for (auto loc : m_emittedManualFixItsWarningsInMacro) {
300 SourceLocation l = SourceLocation::getFromRawEncoding(loc);
301 PresumedLoc p = sm().getPresumedLoc(l);
302 if (Utils::presumedLocationsEqual(p, ploc)) {
303 return true;
304 }
305 }
306
307 return false;
308 }
309
310 bool CheckBase::isOptionSet(const std::string &optionName) const
311 {
312 const std::string qualifiedName = name() + '-' + optionName;
313 return m_context->isOptionSet(qualifiedName);
314 }
315
316 ClazyAstMatcherCallback::ClazyAstMatcherCallback(CheckBase *check)
317 : MatchFinder::MatchCallback()
318 , m_check(check)
319 {
320 }