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/SmallVector.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringSwitch.h"
45 auto Type = llvm::StringSwitch<std::optional<NoLintType>>(Str)
50 .Default(std::nullopt);
62 SmallVector<StringRef> Split;
64 for (StringRef &Check : Split)
66 return llvm::join(Split,
",");
81 const std::optional<StringRef> &
Checks)
82 : Type(Type), Pos(Pos), ChecksGlob(std::make_unique<CachedGlobList>(
96 std::unique_ptr<CachedGlobList> ChecksGlob;
99 const std::optional<std::string> &checks()
const {
return Checks; }
102 bool suppresses(StringRef Check)
const {
return ChecksGlob->contains(Check); }
105 std::optional<std::string> Checks;
111static SmallVector<NoLintToken>
getNoLints(StringRef Buffer) {
112 static constexpr llvm::StringLiteral NOLINT =
"NOLINT";
113 SmallVector<NoLintToken> NoLints;
116 while (Pos < Buffer.size()) {
118 const size_t NoLintPos = Buffer.find(NOLINT, Pos);
119 if (NoLintPos == StringRef::npos)
124 Pos = NoLintPos + NOLINT.size();
125 while (Pos < Buffer.size() && llvm::isAlpha(Buffer[Pos]))
135 std::optional<StringRef>
Checks;
136 if (Pos < Buffer.size() && Buffer[Pos] ==
'(') {
137 size_t ClosingBracket = Buffer.find_first_of(
"\n)", ++Pos);
138 if (ClosingBracket != StringRef::npos && Buffer[ClosingBracket] ==
')') {
139 Checks = Buffer.slice(Pos, ClosingBracket);
140 Pos = ClosingBracket + 1;
157class NoLintBlockToken {
159 NoLintBlockToken(
size_t BeginPos,
size_t EndPos,
160 std::unique_ptr<CachedGlobList> ChecksGlob)
161 : BeginPos(BeginPos), EndPos(EndPos), ChecksGlob(std::
move(ChecksGlob)) {}
165 bool suppresses(
size_t DiagPos, StringRef DiagName)
const {
166 return (BeginPos < DiagPos) && (DiagPos < EndPos) &&
167 ChecksGlob->contains(DiagName);
173 std::unique_ptr<CachedGlobList> ChecksGlob;
182 const NoLintToken &
NoLint) {
183 tooling::Diagnostic Error;
184 Error.DiagLevel = tooling::Diagnostic::Error;
185 Error.DiagnosticName =
"clang-tidy-nolint";
188 ? (
"unmatched 'NOLINTBEGIN' comment without a subsequent 'NOLINT"
190 : (
"unmatched 'NOLINTEND' comment without a previous 'NOLINT"
192 SourceLocation Loc = SrcMgr.getComposedLoc(File,
NoLint.Pos);
193 Error.Message = tooling::DiagnosticMessage(Message, SrcMgr, Loc);
200static SmallVector<NoLintBlockToken>
203 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors) {
204 SmallVector<NoLintBlockToken> CompletedBlocks;
205 SmallVector<NoLintToken> Stack;
212 for (NoLintToken &
NoLint : NoLints) {
215 Stack.emplace_back(std::move(
NoLint));
217 if (!Stack.empty() && Stack.back().checks() ==
NoLint.checks()) {
219 CompletedBlocks.emplace_back(Stack.back().Pos,
NoLint.Pos,
220 std::move(Stack.back().ChecksGlob));
228 for (
const NoLintToken &
NoLint : Stack)
231 return CompletedBlocks;
241 const Diagnostic &Diag, StringRef DiagName,
242 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
243 bool AllowIO,
bool EnableNoLintBlocks);
246 bool diagHasNoLintInMacro(
const Diagnostic &Diag, StringRef DiagName,
247 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
248 bool AllowIO,
bool EnableNoLintBlocks);
250 bool diagHasNoLint(StringRef DiagName, SourceLocation DiagLoc,
251 const SourceManager &SrcMgr,
252 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
253 bool AllowIO,
bool EnableNoLintBlocks);
255 void generateCache(
const SourceManager &SrcMgr, StringRef FileName,
256 FileID File, StringRef Buffer,
257 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors);
259 llvm::StringMap<SmallVector<NoLintBlockToken>> Cache;
263 DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Diag,
264 StringRef DiagName, SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
265 bool AllowIO,
bool EnableNoLintBlocks) {
266 if (DiagLevel >= DiagnosticsEngine::Error)
268 return diagHasNoLintInMacro(Diag, DiagName, NoLintErrors, AllowIO,
274bool NoLintDirectiveHandler::Impl::diagHasNoLintInMacro(
275 const Diagnostic &Diag, StringRef DiagName,
276 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
bool AllowIO,
277 bool EnableNoLintBlocks) {
278 SourceLocation DiagLoc = Diag.getLocation();
279 if (DiagLoc.isInvalid())
281 const SourceManager &SrcMgr = Diag.getSourceManager();
283 if (diagHasNoLint(DiagName, DiagLoc, SrcMgr, NoLintErrors, AllowIO,
286 if (!DiagLoc.isMacroID())
288 DiagLoc = SrcMgr.getImmediateExpansionRange(DiagLoc).getBegin();
297 size_t StartPos = Buffer.find_last_of(
'\n', From) + 1;
298 size_t EndPos = std::min(Buffer.find(
'\n', From), Buffer.size());
299 return {StartPos, EndPos};
305 std::pair<size_t, size_t> LineStartAndEnd,
308 Buffer = Buffer.slice(LineStartAndEnd.first, LineStartAndEnd.second);
309 SmallVector<NoLintToken> NoLints =
getNoLints(Buffer);
312 return llvm::any_of(NoLints, [&](
const NoLintToken &
NoLint) {
313 return NoLint.Type == Type &&
NoLint.suppresses(DiagName);
320 size_t DiagPos, StringRef DiagName) {
321 return llvm::any_of(NoLintBlocks, [&](
const NoLintBlockToken &NoLintBlock) {
322 return NoLintBlock.suppresses(DiagPos, DiagName);
327static std::optional<StringRef>
getBuffer(
const SourceManager &SrcMgr,
328 FileID File,
bool AllowIO) {
329 return AllowIO ? SrcMgr.getBufferDataOrNone(File)
330 : SrcMgr.getBufferDataIfLoaded(File);
337bool NoLintDirectiveHandler::Impl::diagHasNoLint(
338 StringRef DiagName, SourceLocation DiagLoc,
const SourceManager &SrcMgr,
339 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
bool AllowIO,
340 bool EnableNoLintBlocks) {
342 const auto [File, Pos] = SrcMgr.getDecomposedSpellingLoc(DiagLoc);
346 std::optional<StringRef> FileName = SrcMgr.getNonBuiltinFilenameForID(File);
351 std::optional<StringRef> Buffer =
getBuffer(SrcMgr, File, AllowIO);
361 if (ThisLine.first > 0) {
368 if (!EnableNoLintBlocks)
372 if (!Cache.contains(*FileName))
374 generateCache(SrcMgr, *FileName, File, *Buffer, NoLintErrors);
380void NoLintDirectiveHandler::Impl::generateCache(
381 const SourceManager &SrcMgr, StringRef FileName, FileID File,
382 StringRef Buffer, SmallVectorImpl<tooling::Diagnostic> &NoLintErrors) {
394 : PImpl(std::make_unique<
Impl>()) {}
399 DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Diag,
400 StringRef DiagName, SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
401 bool AllowIO,
bool EnableNoLintBlocks) {
402 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)