| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
SPDX-FileCopyrightText: 2025 Alexander Lohnau <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "SuppressionManager.h" |
| 9 |
#include "clazy_stl.h" |
| 10 |
|
| 11 |
#include <clang/Basic/SourceLocation.h> |
| 12 |
#include <clang/Basic/SourceManager.h> |
| 13 |
#include <clang/Basic/TokenKinds.h> |
| 14 |
#include <clang/Lex/Lexer.h> |
| 15 |
#include <clang/Lex/Token.h> |
| 16 |
#include <llvm/Support/MemoryBuffer.h> |
| 17 |
#include <llvm/Support/raw_ostream.h> |
| 18 |
|
| 19 |
#include <regex> |
| 20 |
#include <stack> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
using namespace clang; |
| 24 |
|
| 25 |
static llvm::Regex createRegexFromGlob(StringRef &Glob) |
| 26 |
{ |
| 27 |
std::string RegexText("^"); |
| 28 |
StringRef MetaChars("()^$|*+?.[]\\{}"); // Copied from clang-tidy to keep parsing identical |
| 29 |
for (char C : Glob) { |
| 30 |
if (C == '*') { |
| 31 |
RegexText.push_back('.'); |
| 32 |
} else if (MetaChars.contains(C)) { |
| 33 |
RegexText.push_back('\\'); |
| 34 |
} |
| 35 |
RegexText.push_back(C); |
| 36 |
} |
| 37 |
RegexText.push_back('$'); |
| 38 |
return {RegexText}; |
| 39 |
} |
| 40 |
|
| 41 |
static std::string parseDisabledChecks(const std::string &comment, size_t parentPosition) |
| 42 |
{ |
| 43 |
if (parentPosition < comment.length() && comment.at(parentPosition) == '(') { |
| 44 |
const int parentEnd = comment.find(')', parentPosition); |
| 45 |
if (parentEnd == -1) { |
| 46 |
return ""; // Malformed |
| 47 |
} |
| 48 |
return comment.substr(parentPosition + 1, parentEnd - parentPosition - 1); |
| 49 |
} |
| 50 |
return ""; |
| 51 |
} |
| 52 |
|
| 53 |
static std::vector<llvm::Regex> splitDisabledChecksText(const std::string &disableText) |
| 54 |
{ |
| 55 |
std::vector<llvm::Regex> disabledChecksRegexes; |
| 56 |
for (const auto &split : clazy::splitString(disableText, ',')) { |
| 57 |
if (split.find("clazy-") == 0) { |
| 58 |
StringRef sanitizedCheckName = llvm::StringRef(split).substr(strlen("clazy-")).rtrim(); |
| 59 |
disabledChecksRegexes.push_back(createRegexFromGlob(sanitizedCheckName)); |
| 60 |
} |
| 61 |
} |
| 62 |
return disabledChecksRegexes; |
| 63 |
} |
| 64 |
|
| 65 |
SuppressionManager::SuppressionManager() = default; |
| 66 |
|
| 67 |
bool SuppressionManager::isSuppressed(const std::string &checkName, |
| 68 |
clang::SourceLocation loc, |
| 69 |
const clang::SourceManager &sm, |
| 70 |
const clang::LangOptions &lo) const |
| 71 |
{ |
| 72 |
if (loc.isMacroID()) { |
| 73 |
loc = sm.getExpansionLoc(loc); |
| 74 |
} |
| 75 |
|
| 76 |
FileID fileID = sm.getFileID(loc); |
| 77 |
if (fileID.isInvalid()) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
auto it = m_processedFileIDs.find(fileID.getHashValue()); |
| 82 |
if (it == m_processedFileIDs.cend()) { |
| 83 |
parseFile(fileID, sm, lo); |
| 84 |
it = m_processedFileIDs.find(fileID.getHashValue()); |
| 85 |
} |
| 86 |
|
| 87 |
Suppressions &suppressions = it->second; |
| 88 |
|
| 89 |
// Case 1: clazy:skip, the whole file is skipped, regardless of which check |
| 90 |
if (suppressions.skipEntireFile) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
// Case 2: clazy:excludeall=foo, the check foo will be ignored for this file |
| 95 |
const bool checkIsSuppressed = suppressions.checksToSkip.find(checkName) != suppressions.checksToSkip.cend(); |
| 96 |
if (checkIsSuppressed) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
// Case 3: clazy:exclude=foo, the check foo will be ignored for this file in this line number |
| 101 |
if (loc.isInvalid()) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
const LineNumber lineNumber = sm.getSpellingLineNumber(loc); |
| 106 |
if (suppressions.skipNextLine.count(lineNumber) > 0) { |
| 107 |
suppressions.skipNextLine.erase(lineNumber); |
| 108 |
return true; |
| 109 |
} |
| 110 |
if (suppressions.checksToSkipByLine.find(LineAndCheckName(lineNumber, checkName)) != suppressions.checksToSkipByLine.cend()) |
| 111 |
return true; |
| 112 |
|
| 113 |
if (auto it = suppressions.checkWildcardsToSkipByLine.find(lineNumber); it != suppressions.checkWildcardsToSkipByLine.end()) { |
| 114 |
return it->second.match(checkName); |
| 115 |
} |
| 116 |
|
| 117 |
for (const auto &rangeSuppression : suppressions.checkWildcardsRanges) { |
| 118 |
if (rangeSuppression.begin <= lineNumber && rangeSuppression.end >= lineNumber) { |
| 119 |
return rangeSuppression.checksToSkipWildcard.empty() |
| 120 |
|| std::ranges::any_of(rangeSuppression.checksToSkipWildcard, [&checkName](const llvm::Regex ®ex) { |
| 121 |
return regex.match(checkName); |
| 122 |
}); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
void SuppressionManager::parseFile(FileID id, const SourceManager &sm, const clang::LangOptions &lo) const |
| 130 |
{ |
| 131 |
const unsigned hash = id.getHashValue(); |
| 132 |
auto it = m_processedFileIDs.insert({hash, Suppressions()}).first; |
| 133 |
Suppressions &suppressions = (*it).second; |
| 134 |
|
| 135 |
auto buffer = sm.getBufferOrNone(id); |
| 136 |
if (!buffer.has_value()) { |
| 137 |
llvm::errs() << "SuppressionManager::parseFile: Invalid buffer "; |
| 138 |
if (buffer) { |
| 139 |
llvm::errs() << buffer->getBuffer() << "\n"; |
| 140 |
} |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
clang::Lexer lexer(id, buffer.value(), sm, lo); |
| 145 |
lexer.SetCommentRetentionState(true); |
| 146 |
std::stack<std::pair<LineNumber, std::string>> checkRangeStack; |
| 147 |
|
| 148 |
Token token; |
| 149 |
while (!lexer.LexFromRawLexer(token)) { |
| 150 |
if (token.getKind() != tok::comment) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
const LineNumber lineNumber = sm.getSpellingLineNumber(token.getLocation()); |
| 155 |
const std::string comment = Lexer::getSpelling(token, sm, lo); |
| 156 |
|
| 157 |
const int foundNolint = comment.find("NOLINT"); |
| 158 |
int foundNoLintNextLine = -1; |
| 159 |
if (foundNolint != -1) { |
| 160 |
// Reuse starting position of previous match |
| 161 |
if (comment.compare(foundNolint + strlen("NOLINT"), strlen("NEXTLINE"), "NEXTLINE") == 0) { |
| 162 |
foundNoLintNextLine = foundNolint + strlen("NOLINT"); |
| 163 |
} |
| 164 |
|
| 165 |
if (const int idx = comment.find("NOLINTBEGIN", foundNolint); idx != -1) { |
| 166 |
const auto &beginChecks = parseDisabledChecks(comment, idx + strlen("NOLINTBEGIN")); |
| 167 |
checkRangeStack.push({lineNumber, beginChecks}); |
| 168 |
} else if (const int idx = comment.find("NOLINTEND", foundNolint); idx != -1) { |
| 169 |
const std::string &endChecks = parseDisabledChecks(comment, idx + strlen("NOLINTEND")); |
| 170 |
if (checkRangeStack.empty() || checkRangeStack.top().second != endChecks) { |
| 171 |
llvm::errs() << "unmatched 'NOLINTEND' comment without a previous 'NOLINTBEGIN' comment\n"; |
| 172 |
} else { |
| 173 |
const auto [beginLineNumber, disabledChecks] = checkRangeStack.top(); |
| 174 |
checkRangeStack.pop(); |
| 175 |
suppressions.checkWildcardsRanges.push_back(CheckBeginAndEnd{splitDisabledChecksText(disabledChecks), beginLineNumber, lineNumber}); |
| 176 |
} |
| 177 |
|
| 178 |
} else { |
| 179 |
// NOLINT or NOLINTNEXTLINE |
| 180 |
const int lineNumberToSuppress = foundNoLintNextLine == -1 ? lineNumber : lineNumber + 1; |
| 181 |
auto disableText = parseDisabledChecks(comment, foundNolint + strlen("NOLINT") + (foundNoLintNextLine == -1 ? 0 : strlen("NEXTLINE"))); |
| 182 |
if (disableText.empty()) { |
| 183 |
suppressions.skipNextLine.insert(lineNumberToSuppress); |
| 184 |
} else { |
| 185 |
for (llvm::Regex &split : splitDisabledChecksText(disableText)) { |
| 186 |
suppressions.checkWildcardsToSkipByLine.insert({lineNumberToSuppress, std::move(split)}); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
const auto startIdx = comment.find("clazy:"); |
| 193 |
if (startIdx == std::string::npos) { |
| 194 |
continue; // Early return, no need to look at any regex |
| 195 |
} |
| 196 |
|
| 197 |
if (clazy::contains(comment, "clazy:skip")) { |
| 198 |
suppressions.skipEntireFile = true; |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
static const std::regex rx_all("clazy:excludeall=([^\\s]+)"); |
| 203 |
static const std::regex rx_current("clazy:exclude=([^\\s]+)"); |
| 204 |
static const std::regex rx_next("clazy:exclude-next-line=([^\\s]+)"); |
| 205 |
|
| 206 |
const auto startIt = comment.begin() + startIdx; |
| 207 |
const auto endIt = comment.end(); |
| 208 |
std::smatch match; |
| 209 |
if (std::regex_search(startIt, endIt, match, rx_all)) { |
| 210 |
std::vector<std::string> checks = clazy::splitString(match[1], ','); |
| 211 |
suppressions.checksToSkip.insert(checks.cbegin(), checks.cend()); |
| 212 |
} else if (std::regex_search(startIt, endIt, match, rx_current)) { |
| 213 |
std::vector<std::string> checks = clazy::splitString(match[1], ','); |
| 214 |
for (const std::string &checkName : checks) { |
| 215 |
suppressions.checksToSkipByLine.insert(LineAndCheckName(lineNumber, checkName)); |
| 216 |
} |
| 217 |
} else if (std::regex_search(startIt, endIt, match, rx_next)) { |
| 218 |
std::vector<std::string> checks = clazy::splitString(match[1], ','); |
| 219 |
for (const std::string &checkName : checks) { |
| 220 |
suppressions.checksToSkipByLine.insert(LineAndCheckName(lineNumber + 1, checkName)); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |