-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathcheckbase.cpp
201 lines (162 loc) · 5.56 KB
/
checkbase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
This file is part of the clang-lazy static checker.
Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
Author: Sérgio Martins <[email protected]>
Copyright (C) 2015 Sergio Martins <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include "checkbase.h"
#include "checkmanager.h"
#include "StringUtils.h"
#include <clang/AST/Decl.h>
#include <clang/AST/DeclCXX.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/ParentMap.h>
#include <clang/Rewrite/Frontend/FixItRewriter.h>
#include <vector>
using namespace clang;
using namespace std;
CheckBase::CheckBase(const string &name)
: m_ci(*CheckManager::instance()->m_ci)
, m_name(name)
, m_lastDecl(nullptr)
, m_enabledFixits(0)
{
ASTContext &context = m_ci.getASTContext();
m_tu = context.getTranslationUnitDecl();
m_lastMethodDecl = nullptr;
}
CheckBase::~CheckBase()
{
}
void CheckBase::VisitStatement(Stmt *stm)
{
if (!shouldIgnoreFile(stm->getLocStart())) {
VisitStmt(stm);
}
}
void CheckBase::VisitDeclaration(Decl *decl)
{
if (shouldIgnoreFile(decl->getLocStart()))
return;
m_lastDecl = decl;
auto mdecl = dyn_cast<CXXMethodDecl>(decl);
if (mdecl)
m_lastMethodDecl = mdecl;
VisitDecl(decl);
}
string CheckBase::name() const
{
return m_name;
}
void CheckBase::setParentMap(ParentMap *parentMap)
{
m_parentMap = parentMap;
}
void CheckBase::VisitStmt(Stmt *)
{
}
void CheckBase::VisitDecl(Decl *)
{
}
bool CheckBase::shouldIgnoreFile(SourceLocation loc) const
{
if (!loc.isValid() || m_ci.getSourceManager().isInSystemHeader(loc))
return true;
auto filename = m_ci.getSourceManager().getFilename(loc);
const std::vector<std::string> files = filesToIgnore();
for (auto &file : files) {
bool contains = filename.find(file) != std::string::npos;
if (contains)
return true;
}
return false;
}
std::vector<std::string> CheckBase::filesToIgnore() const
{
return {};
}
void CheckBase::emitWarning(clang::SourceLocation loc, std::string error, bool printWarningTag)
{
emitWarning(loc, error, {}, printWarningTag);
}
void CheckBase::emitWarning(clang::SourceLocation loc, std::string error, const vector<FixItHint> &fixits, bool printWarningTag)
{
if (loc.isMacroID()) {
if (warningAlreadyEmitted(loc))
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
m_emittedWarningsInMacro.push_back(loc.getRawEncoding());
}
const string tag = string(" [-Wclazy-") + name() + string("]");
if (printWarningTag)
error += tag;
reallyEmitWarning(loc, error, fixits);
for (auto l : m_queuedManualInterventionWarnings) {
reallyEmitWarning(l, string("FixIt failed, requires manual intervention") + tag, {});
}
m_queuedManualInterventionWarnings.clear();
}
void CheckBase::reallyEmitWarning(clang::SourceLocation loc, const std::string &error, const vector<FixItHint> &fixits)
{
FullSourceLoc full(loc, m_ci.getSourceManager());
unsigned id = m_ci.getDiagnostics().getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Warning, error.c_str());
DiagnosticBuilder B = m_ci.getDiagnostics().Report(full, id);
for (FixItHint fixit : fixits) {
if (!fixit.isNull())
B.AddFixItHint(fixit);
}
}
void CheckBase::queueManualFixitWarning(clang::SourceLocation loc, int fixitType)
{
if (isFixitEnabled(fixitType))
m_queuedManualInterventionWarnings.push_back(loc);
}
bool CheckBase::warningAlreadyEmitted(SourceLocation loc) const
{
PresumedLoc ploc = m_ci.getSourceManager().getPresumedLoc(loc);
for (auto rawLoc : m_emittedWarningsInMacro) {
SourceLocation l = SourceLocation::getFromRawEncoding(rawLoc);
PresumedLoc p = m_ci.getSourceManager().getPresumedLoc(l);
if (Utils::presumedLocationsEqual(p, ploc))
return true;
}
return false;
}
clang::FixItHint CheckBase::createReplacement(const SourceRange &range, const string &replacement)
{
if (range.getBegin().isInvalid()) {
return {};
} else {
return FixItHint::CreateReplacement(range, replacement);
}
}
clang::FixItHint CheckBase::createInsertion(const SourceLocation &start, const string &insertion)
{
if (start.isInvalid()) {
return {};
} else {
StringUtils::printLocation(start);
return FixItHint::CreateInsertion(start, insertion);
}
}
void CheckBase::setEnabledFixits(int fixits)
{
m_enabledFixits = fixits;
}
bool CheckBase::isFixitEnabled(int fixit) const
{
return (m_enabledFixits & fixit) || CheckManager::instance()->allFixitsEnabled();
}