| 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 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "checkmanager.h" |
| 11 |
#include "Checks.h" |
| 12 |
#include "ClazyContext.h" |
| 13 |
#include "clazy_stl.h" |
| 14 |
|
| 15 |
#include <llvm/Support/raw_ostream.h> |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <cassert> |
| 19 |
#include <cstdlib> |
| 20 |
#include <cstring> |
| 21 |
#include <iterator> |
| 22 |
|
| 23 |
using namespace clang; |
| 24 |
|
| 25 |
static const char *s_fixitNamePrefix = "fix-"; |
| 26 |
static const char *s_levelPrefix = "level"; |
| 27 |
|
| 28 |
std::mutex CheckManager::m_lock; |
| 29 |
|
| 30 |
CheckManager::CheckManager() |
| 31 |
{ |
| 32 |
m_registeredChecks.reserve(100); |
| 33 |
registerChecks(); |
| 34 |
} |
| 35 |
|
| 36 |
bool CheckManager::checkExists(const std::string &name) const |
| 37 |
{ |
| 38 |
return checkForName(m_registeredChecks, name) != m_registeredChecks.cend(); |
| 39 |
} |
| 40 |
|
| 41 |
CheckManager *CheckManager::instance() |
| 42 |
{ |
| 43 |
static CheckManager s_instance; |
| 44 |
return &s_instance; |
| 45 |
} |
| 46 |
|
| 47 |
void CheckManager::registerCheck(const RegisteredCheck &check) |
| 48 |
{ |
| 49 |
m_registeredChecks.push_back(check); |
| 50 |
} |
| 51 |
|
| 52 |
void CheckManager::registerFixIt(int id, const std::string &fixitName, const std::string &checkName) |
| 53 |
{ |
| 54 |
if (!clazy::startsWith(fixitName, s_fixitNamePrefix)) { |
| 55 |
assert(false); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
auto &fixits = m_fixitsByCheckName[checkName]; |
| 60 |
for (const auto &fixit : fixits) { |
| 61 |
if (fixit.name == fixitName) { |
| 62 |
// It can't exist |
| 63 |
assert(false); |
| 64 |
return; |
| 65 |
} |
| 66 |
} |
| 67 |
RegisteredFixIt fixit = {id, fixitName}; |
| 68 |
fixits.push_back(fixit); |
| 69 |
m_fixitByName.insert({fixitName, fixit}); |
| 70 |
} |
| 71 |
|
| 72 |
CheckBase *CheckManager::createCheck(const std::string &name) |
| 73 |
{ |
| 74 |
for (const auto &rc : m_registeredChecks) { |
| 75 |
if (rc.name == name) { |
| 76 |
return rc.factory(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
llvm::errs() << "Invalid check name " << name << "\n"; |
| 81 |
return nullptr; |
| 82 |
} |
| 83 |
|
| 84 |
std::string CheckManager::checkNameForFixIt(const std::string &fixitName) const |
| 85 |
{ |
| 86 |
if (fixitName.empty()) { |
| 87 |
return {}; |
| 88 |
} |
| 89 |
|
| 90 |
for (const auto ®isteredCheck : m_registeredChecks) { |
| 91 |
auto it = m_fixitsByCheckName.find(registeredCheck.name); |
| 92 |
if (it != m_fixitsByCheckName.end()) { |
| 93 |
const auto &fixits = (*it).second; |
| 94 |
for (const RegisteredFixIt &fixit : fixits) { |
| 95 |
if (fixit.name == fixitName) { |
| 96 |
return (*it).first; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return {}; |
| 103 |
} |
| 104 |
|
| 105 |
RegisteredCheck::List CheckManager::availableChecks(CheckLevel maxLevel) const |
| 106 |
{ |
| 107 |
RegisteredCheck::List checks = m_registeredChecks; |
| 108 |
|
| 109 |
checks.erase(remove_if(checks.begin(), |
| 110 |
checks.end(), |
| 111 |
[maxLevel](const RegisteredCheck &r) { |
| 112 |
return r.level > maxLevel; |
| 113 |
}), |
| 114 |
checks.end()); |
| 115 |
|
| 116 |
return checks; |
| 117 |
} |
| 118 |
|
| 119 |
RegisteredCheck::List CheckManager::requestedChecksThroughEnv(std::vector<std::string> &userDisabledChecks) const |
| 120 |
{ |
| 121 |
static RegisteredCheck::List requestedChecksThroughEnv; |
| 122 |
static std::vector<std::string> disabledChecksThroughEnv; |
| 123 |
if (requestedChecksThroughEnv.empty()) { |
| 124 |
const char *checksEnv = getenv("CLAZY_CHECKS"); |
| 125 |
if (checksEnv) { |
| 126 |
const std::string checksEnvStr = clazy::unquoteString(checksEnv); |
| 127 |
requestedChecksThroughEnv = |
| 128 |
checksEnvStr == "all_checks" ? availableChecks(CheckLevel2) : checksForCommaSeparatedString(checksEnvStr, /*by-ref=*/disabledChecksThroughEnv); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
std::copy(disabledChecksThroughEnv.begin(), disabledChecksThroughEnv.end(), std::back_inserter(userDisabledChecks)); |
| 133 |
return requestedChecksThroughEnv; |
| 134 |
} |
| 135 |
|
| 136 |
RegisteredCheck::List::const_iterator CheckManager::checkForName(const RegisteredCheck::List &checks, const std::string &name) const |
| 137 |
{ |
| 138 |
return std::ranges::find_if(checks, [name](const RegisteredCheck &r) { |
| 139 |
return r.name == name; |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
RegisteredFixIt::List CheckManager::availableFixIts(const std::string &checkName) const |
| 144 |
{ |
| 145 |
auto it = m_fixitsByCheckName.find(checkName); |
| 146 |
return it == m_fixitsByCheckName.end() ? RegisteredFixIt::List() : (*it).second; |
| 147 |
} |
| 148 |
|
| 149 |
static bool takeArgument(const std::string &arg, std::vector<std::string> &args) |
| 150 |
{ |
| 151 |
auto it = std::ranges::find(args, arg); |
| 152 |
if (it != args.end()) { |
| 153 |
args.erase(it, it + 1); |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
RegisteredCheck::List CheckManager::requestedChecks(std::vector<std::string> &args) |
| 161 |
{ |
| 162 |
RegisteredCheck::List result; |
| 163 |
|
| 164 |
// #1 Check if a level was specified |
| 165 |
static const std::vector<std::string> levels = {"level0", "level1", "level2"}; |
| 166 |
const int numLevels = levels.size(); |
| 167 |
CheckLevel requestedLevel = CheckLevelUndefined; |
| 168 |
for (int i = 0; i < numLevels; ++i) { |
| 169 |
if (takeArgument(levels.at(i), args)) { |
| 170 |
requestedLevel = static_cast<CheckLevel>(i); |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (args.size() > 1) { // we only expect a level and a comma separated list of arguments |
| 176 |
return {}; |
| 177 |
} |
| 178 |
|
| 179 |
std::vector<std::string> userDisabledChecks; |
| 180 |
if (args.size() == 1) { |
| 181 |
// #2 Process list of comma separated checks that were passed to compiler |
| 182 |
result = checksForCommaSeparatedString(args[0], /*by-ref*/ userDisabledChecks); |
| 183 |
if (result.empty() && userDisabledChecks.empty()) { // User passed inexisting checks. |
| 184 |
return {}; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// #3 Append checks specified from env variable |
| 189 |
RegisteredCheck::List checksFromEnv = requestedChecksThroughEnv(/*by-ref*/ userDisabledChecks); |
| 190 |
copy(checksFromEnv.cbegin(), checksFromEnv.cend(), back_inserter(result)); |
| 191 |
|
| 192 |
if (result.empty() && requestedLevel == CheckLevelUndefined) { |
| 193 |
// No checks or level specified, lets use the default level |
| 194 |
requestedLevel = DefaultCheckLevel; |
| 195 |
} |
| 196 |
|
| 197 |
// #4 Add checks from requested level |
| 198 |
RegisteredCheck::List checksFromRequestedLevel = checksForLevel(requestedLevel); |
| 199 |
std::ranges::copy(checksFromRequestedLevel, std::back_inserter(result)); |
| 200 |
clazy::sort_and_remove_dups(result, checkLessThan); |
| 201 |
CheckManager::removeChecksFromList(result, userDisabledChecks); |
| 202 |
|
| 203 |
return result; |
| 204 |
} |
| 205 |
|
| 206 |
RegisteredCheck::List CheckManager::checksForLevel(int level) const |
| 207 |
{ |
| 208 |
RegisteredCheck::List result; |
| 209 |
if (level > CheckLevelUndefined && level <= MaxCheckLevel) { |
| 210 |
std::ranges::copy_if(m_registeredChecks, std::back_inserter(result), [&](const RegisteredCheck &r) { |
| 211 |
return r.level <= level; |
| 212 |
}); |
| 213 |
} |
| 214 |
|
| 215 |
return result; |
| 216 |
} |
| 217 |
|
| 218 |
std::vector<std::pair<CheckBase *, RegisteredCheck>> CheckManager::createChecks(const RegisteredCheck::List &requestedChecks, ClazyContext *context) |
| 219 |
{ |
| 220 |
assert(context); |
| 221 |
|
| 222 |
std::vector<std::pair<CheckBase *, RegisteredCheck>> checks; |
| 223 |
checks.reserve(requestedChecks.size() + 1); |
| 224 |
for (const auto &check : requestedChecks) { |
| 225 |
checks.emplace_back(createCheck(check.name), check); |
| 226 |
} |
| 227 |
|
| 228 |
return checks; |
| 229 |
} |
| 230 |
|
| 231 |
/*static */ |
| 232 |
void CheckManager::removeChecksFromList(RegisteredCheck::List &list, std::vector<std::string> &checkNames) |
| 233 |
{ |
| 234 |
for (auto &name : checkNames) { |
| 235 |
list.erase(remove_if(list.begin(), |
| 236 |
list.end(), |
| 237 |
[name](const RegisteredCheck &c) { |
| 238 |
return c.name == name; |
| 239 |
}), |
| 240 |
list.end()); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
RegisteredCheck::List CheckManager::checksForCommaSeparatedString(const std::string &str) const |
| 245 |
{ |
| 246 |
std::vector<std::string> byRefDummy; |
| 247 |
return checksForCommaSeparatedString(str, byRefDummy); |
| 248 |
} |
| 249 |
|
| 250 |
RegisteredCheck::List CheckManager::checksForCommaSeparatedString(const std::string &str, std::vector<std::string> &userDisabledChecks) const |
| 251 |
{ |
| 252 |
std::vector<std::string> checkNames = clazy::splitString(str, ','); |
| 253 |
RegisteredCheck::List result; |
| 254 |
|
| 255 |
for (const std::string &name : checkNames) { |
| 256 |
if (checkForName(result, name) != result.cend()) { |
| 257 |
continue; // Already added. Duplicate check specified. continue. |
| 258 |
} |
| 259 |
|
| 260 |
auto it = checkForName(m_registeredChecks, name); |
| 261 |
if (it == m_registeredChecks.cend()) { |
| 262 |
// Unknown, but might be a fixit name |
| 263 |
const std::string checkName = checkNameForFixIt(name); |
| 264 |
auto it = checkForName(m_registeredChecks, checkName); |
| 265 |
const bool checkDoesntExist = it == m_registeredChecks.cend(); |
| 266 |
if (checkDoesntExist) { |
| 267 |
if (clazy::startsWith(name, s_levelPrefix) && name.size() == strlen(s_levelPrefix) + 1) { |
| 268 |
auto lastChar = name.back(); |
| 269 |
const int digit = lastChar - '0'; |
| 270 |
if (digit > CheckLevelUndefined && digit <= MaxCheckLevel) { |
| 271 |
RegisteredCheck::List levelChecks = checksForLevel(digit); |
| 272 |
std::ranges::copy(levelChecks, std::back_inserter(result)); |
| 273 |
} else { |
| 274 |
llvm::errs() << "Invalid level: " << name << "\n"; |
| 275 |
} |
| 276 |
} else { |
| 277 |
if (clazy::startsWith(name, "no-")) { |
| 278 |
std::string checkName = name; |
| 279 |
checkName.erase(0, 3); |
| 280 |
if (checkExists(checkName)) { |
| 281 |
userDisabledChecks.push_back(checkName); |
| 282 |
} else { |
| 283 |
llvm::errs() << "Invalid check to disable: " << name << "\n"; |
| 284 |
} |
| 285 |
} else { |
| 286 |
llvm::errs() << "Invalid check: " << name << "\n"; |
| 287 |
} |
| 288 |
} |
| 289 |
} else { |
| 290 |
result.push_back(*it); |
| 291 |
} |
| 292 |
continue; |
| 293 |
} |
| 294 |
result.push_back(*it); |
| 295 |
} |
| 296 |
|
| 297 |
removeChecksFromList(result, userDisabledChecks); |
| 298 |
|
| 299 |
return result; |
| 300 |
} |
| 301 |
|
| 302 |
std::vector<std::string> CheckManager::checksAsErrors() const |
| 303 |
{ |
| 304 |
const auto *checksAsErrosEnv = getenv("CLAZY_CHECKS_AS_ERRORS"); |
| 305 |
|
| 306 |
if (checksAsErrosEnv) { |
| 307 |
auto checkNames = clazy::splitString(checksAsErrosEnv, ','); |
| 308 |
std::vector<std::string> result; |
| 309 |
|
| 310 |
// Check whether all supplied check names are valid |
| 311 |
for (const std::string &name : checkNames) { |
| 312 |
auto it = std::ranges::find_if(m_registeredChecks, [&name](const RegisteredCheck &check) { |
| 313 |
return check.name == name; |
| 314 |
}); |
| 315 |
if (it == m_registeredChecks.end()) { |
| 316 |
llvm::errs() << "Invalid check: " << name << '\n'; |
| 317 |
} else { |
| 318 |
result.emplace_back(name); |
| 319 |
} |
| 320 |
} |
| 321 |
return result; |
| 322 |
} |
| 323 |
return {}; |
| 324 |
} |