17#include "clang/Basic/LLVM.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Tooling/Core/Diagnostic.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringSwitch.h"
44 auto Type = llvm::StringSwitch<std::optional<NoLintType>>(Str)
49 .Default(std::nullopt);
61 SmallVector<StringRef> Split;
63 for (StringRef &Check : Split)
65 return llvm::join(Split,
",");
80 const std::optional<StringRef> &
Checks)
81 : Type(Type), Pos(Pos), ChecksGlob(std::make_unique<CachedGlobList>(
95 std::unique_ptr<CachedGlobList> ChecksGlob;
98 const std::optional<std::string> &checks()
const {
return Checks; }
101 bool suppresses(StringRef Check)
const {
return ChecksGlob->contains(Check); }
104 std::optional<std::string> Checks;
110static SmallVector<NoLintToken>
getNoLints(StringRef Buffer) {
111 static constexpr llvm::StringLiteral NOLINT =
"NOLINT";
112 SmallVector<NoLintToken> NoLints;
115 while (Pos < Buffer.size()) {
117 const size_t NoLintPos = Buffer.find(NOLINT, Pos);
118 if (NoLintPos == StringRef::npos)
123 Pos = NoLintPos + NOLINT.size();
124 while (Pos < Buffer.size() && llvm::isAlpha(Buffer[Pos]))
134 std::optional<StringRef>
Checks;
135 if (Pos < Buffer.size() && Buffer[Pos] ==
'(') {
136 const size_t ClosingBracket = Buffer.find_first_of(
"\n)", ++Pos);
137 if (ClosingBracket != StringRef::npos && Buffer[ClosingBracket] ==
')') {
138 Checks = Buffer.slice(Pos, ClosingBracket);
139 Pos = ClosingBracket + 1;
156class NoLintBlockToken {
158 NoLintBlockToken(
size_t BeginPos,
size_t EndPos,
159 std::unique_ptr<CachedGlobList> ChecksGlob)
160 : BeginPos(BeginPos), EndPos(EndPos), ChecksGlob(std::
move(ChecksGlob)) {}
164 bool suppresses(
size_t DiagPos, StringRef DiagName)
const {
165 return (BeginPos < DiagPos) && (DiagPos < EndPos) &&
166 ChecksGlob->contains(DiagName);
172 std::unique_ptr<CachedGlobList> ChecksGlob;
181 const NoLintToken &
NoLint) {
182 tooling::Diagnostic Error;
183 Error.DiagLevel = tooling::Diagnostic::Error;
184 Error.DiagnosticName =
"clang-tidy-nolint";
185 const StringRef Message =
187 ? (
"unmatched 'NOLINTBEGIN' comment without a subsequent 'NOLINT"
189 : (
"unmatched 'NOLINTEND' comment without a previous 'NOLINT"
191 const SourceLocation Loc = SrcMgr.getComposedLoc(File,
NoLint.Pos);
192 Error.Message = tooling::DiagnosticMessage(Message, SrcMgr, Loc);
199static SmallVector<NoLintBlockToken>
202 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors) {
203 SmallVector<NoLintBlockToken> CompletedBlocks;
204 SmallVector<NoLintToken> Stack;
211 for (NoLintToken &
NoLint : NoLints) {
214 Stack.emplace_back(std::move(
NoLint));
216 if (!Stack.empty() && Stack.back().checks() ==
NoLint.checks()) {
218 CompletedBlocks.emplace_back(Stack.back().Pos,
NoLint.Pos,
219 std::move(Stack.back().ChecksGlob));
227 for (
const NoLintToken &
NoLint : Stack)
230 return CompletedBlocks;
240 const Diagnostic &Diag, StringRef DiagName,
241 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
242 bool AllowIO,
bool EnableNoLintBlocks);
245 bool diagHasNoLintInMacro(
const Diagnostic &Diag, StringRef DiagName,
246 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
247 bool AllowIO,
bool EnableNoLintBlocks);
249 bool diagHasNoLint(StringRef DiagName, SourceLocation DiagLoc,
250 const SourceManager &SrcMgr,
251 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
252 bool AllowIO,
bool EnableNoLintBlocks);
254 void generateCache(
const SourceManager &SrcMgr, StringRef FileName,
255 FileID File, StringRef Buffer,
256 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors);
258 llvm::StringMap<SmallVector<NoLintBlockToken>> Cache;
262 DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Diag,
263 StringRef DiagName, SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
264 bool AllowIO,
bool EnableNoLintBlocks) {
265 if (DiagLevel >= DiagnosticsEngine::Error)
267 return diagHasNoLintInMacro(Diag, DiagName, NoLintErrors, AllowIO,
273bool NoLintDirectiveHandler::Impl::diagHasNoLintInMacro(
274 const Diagnostic &Diag, StringRef DiagName,
275 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
bool AllowIO,
276 bool EnableNoLintBlocks) {
277 SourceLocation DiagLoc = Diag.getLocation();
278 if (DiagLoc.isInvalid())
280 const SourceManager &SrcMgr = Diag.getSourceManager();
282 if (diagHasNoLint(DiagName, DiagLoc, SrcMgr, NoLintErrors, AllowIO,
285 if (!DiagLoc.isMacroID())
287 DiagLoc = SrcMgr.getImmediateExpansionRange(DiagLoc).getBegin();
296 const size_t StartPos = Buffer.find_last_of(
'\n', From) + 1;
297 const size_t EndPos = std::min(Buffer.find(
'\n', From), Buffer.size());
298 return {StartPos, EndPos};
304 std::pair<size_t, size_t> LineStartAndEnd,
307 Buffer = Buffer.slice(LineStartAndEnd.first, LineStartAndEnd.second);
308 SmallVector<NoLintToken> NoLints =
getNoLints(Buffer);
311 return llvm::any_of(NoLints, [&](
const NoLintToken &
NoLint) {
312 return NoLint.Type == Type &&
NoLint.suppresses(DiagName);
319 size_t DiagPos, StringRef DiagName) {
320 return llvm::any_of(NoLintBlocks, [&](
const NoLintBlockToken &NoLintBlock) {
321 return NoLintBlock.suppresses(DiagPos, DiagName);
326static std::optional<StringRef>
getBuffer(
const SourceManager &SrcMgr,
327 FileID File,
bool AllowIO) {
328 return AllowIO ? SrcMgr.getBufferDataOrNone(File)
329 : SrcMgr.getBufferDataIfLoaded(File);
336bool NoLintDirectiveHandler::Impl::diagHasNoLint(
337 StringRef DiagName, SourceLocation DiagLoc,
const SourceManager &SrcMgr,
338 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
bool AllowIO,
339 bool EnableNoLintBlocks) {
341 const auto [File, Pos] = SrcMgr.getDecomposedSpellingLoc(DiagLoc);
345 std::optional<StringRef> FileName = SrcMgr.getNonBuiltinFilenameForID(File);
350 std::optional<StringRef> Buffer =
getBuffer(SrcMgr, File, AllowIO);
360 if (ThisLine.first > 0) {
367 if (!EnableNoLintBlocks)
371 if (!Cache.contains(*FileName))
373 generateCache(SrcMgr, *FileName, File, *Buffer, NoLintErrors);
379void NoLintDirectiveHandler::Impl::generateCache(
380 const SourceManager &SrcMgr, StringRef FileName, FileID File,
381 StringRef Buffer, SmallVectorImpl<tooling::Diagnostic> &NoLintErrors) {
393 : PImpl(std::make_unique<
Impl>()) {}
398 DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Diag,
399 StringRef DiagName, SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
400 bool AllowIO,
bool EnableNoLintBlocks) {
401 return PImpl->shouldSuppress(DiagLevel, Diag, DiagName, NoLintErrors, AllowIO,
static cl::opt< std::string > Checks("checks", desc(R"(
Comma-separated list of globs with optional '-'
prefix. Globs are processed in order of
appearance in the list. Globs without '-'
prefix add checks with matching names to the
set, globs with the '-' prefix remove checks
with matching names from the set of enabled
checks. This option's value is appended to the
value of the 'Checks' option in .clang-tidy
file, if any.
)"), cl::init(""), cl::cat(ClangTidyCategory))
bool shouldSuppress(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Diag, StringRef DiagName, SmallVectorImpl< tooling::Diagnostic > &NoLintErrors, bool AllowIO, bool EnableNoLintBlocks)
~NoLintDirectiveHandler()
bool shouldSuppress(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Diag, llvm::StringRef DiagName, llvm::SmallVectorImpl< tooling::Diagnostic > &NoLintErrors, bool AllowIO, bool EnableNoLintBlocks)
static SmallVector< NoLintBlockToken > formNoLintBlocks(SmallVector< NoLintToken > NoLints, const SourceManager &SrcMgr, FileID File, SmallVectorImpl< tooling::Diagnostic > &NoLintErrors)
static std::optional< StringRef > getBuffer(const SourceManager &SrcMgr, FileID File, bool AllowIO)
static std::pair< size_t, size_t > getLineStartAndEnd(StringRef Buffer, size_t From)
static tooling::Diagnostic makeNoLintError(const SourceManager &SrcMgr, FileID File, const NoLintToken &NoLint)
static SmallVector< NoLintToken > getNoLints(StringRef Buffer)
static std::optional< NoLintType > strToNoLintType(StringRef Str)
static std::string trimWhitespace(StringRef Checks)
static bool withinNoLintBlock(ArrayRef< NoLintBlockToken > NoLintBlocks, size_t DiagPos, StringRef DiagName)
static bool lineHasNoLint(StringRef Buffer, std::pair< size_t, size_t > LineStartAndEnd, NoLintType Type, StringRef DiagName)