ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer(
ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine,
- bool RemoveIncompatibleErrors, bool GetFixesFromNotes)
+ bool RemoveIncompatibleErrors, bool GetFixesFromNotes,
+ bool EnableNolintBlocks)
: Context(Ctx), ExternalDiagEngine(ExternalDiagEngine),
RemoveIncompatibleErrors(RemoveIncompatibleErrors),
- GetFixesFromNotes(GetFixesFromNotes), LastErrorRelatesToUserCode(false),
+ GetFixesFromNotes(GetFixesFromNotes),
+ EnableNolintBlocks(EnableNolintBlocks), LastErrorRelatesToUserCode(false),
LastErrorPassesLineFilter(false), LastErrorWasIgnored(false) {}
void ClangTidyDiagnosticConsumer::finalizeLastError() {
lineIsMarkedWithNOLINT(const ClangTidyContext &Context,
SmallVectorImpl<ClangTidyError> &SuppressionErrors,
bool AllowIO, const SourceManager &SM,
- SourceLocation Loc, StringRef CheckName) {
+ SourceLocation Loc, StringRef CheckName,
+ bool EnableNolintBlocks) {
// Get source code for this location.
FileID File;
unsigned Offset;
return true;
// Check if this line is within a NOLINT(BEGIN...END) block.
- return lineIsWithinNolintBegin(Context, SuppressionErrors, SM, Loc, CheckName,
+ return EnableNolintBlocks &&
+ lineIsWithinNolintBegin(Context, SuppressionErrors, SM, Loc, CheckName,
TextBeforeDiag, TextAfterDiag);
}
static bool lineIsMarkedWithNOLINTinMacro(
const Diagnostic &Info, const ClangTidyContext &Context,
- SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO) {
+ SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO,
+ bool EnableNolintBlocks) {
const SourceManager &SM = Info.getSourceManager();
SourceLocation Loc = Info.getLocation();
std::string CheckName = Context.getCheckName(Info.getID());
while (true) {
if (lineIsMarkedWithNOLINT(Context, SuppressionErrors, AllowIO, SM, Loc,
- CheckName))
+ CheckName, EnableNolintBlocks))
return true;
if (!Loc.isMacroID())
return false;
bool shouldSuppressDiagnostic(
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info,
ClangTidyContext &Context,
- SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO) {
+ SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO,
+ bool EnableNolintBlocks) {
return Info.getLocation().isValid() &&
DiagLevel != DiagnosticsEngine::Error &&
DiagLevel != DiagnosticsEngine::Fatal &&
lineIsMarkedWithNOLINTinMacro(Info, Context, SuppressionErrors,
- AllowIO);
+ AllowIO, EnableNolintBlocks);
}
const llvm::StringMap<tooling::Replacements> *
return;
SmallVector<ClangTidyError, 1> SuppressionErrors;
- if (shouldSuppressDiagnostic(DiagLevel, Info, Context, SuppressionErrors)) {
+ if (shouldSuppressDiagnostic(DiagLevel, Info, Context, SuppressionErrors,
+ EnableNolintBlocks)) {
++Context.Stats.ErrorsIgnoredNOLINT;
// Ignored a warning, should ignore related notes as well
LastErrorWasIgnored = true;
/// If `AllowIO` is false, the function does not attempt to read source files
/// from disk which are not already mapped into memory; such files are treated
/// as not containing a suppression comment.
+/// \param EnableNolintBlocks controls whether to honor NOLINTBEGIN/NOLINTEND
+/// blocks; if false, only considers line-level disabling.
/// If suppression is not possible due to improper use of "NOLINT" comments -
/// for example, the use of a "NOLINTBEGIN" comment that is not followed by a
/// "NOLINTEND" comment - a diagnostic regarding the improper use is returned
bool shouldSuppressDiagnostic(
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info,
ClangTidyContext &Context,
- SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO = true);
+ SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO = true,
+ bool EnableNolintBlocks = true);
/// Gets the Fix attached to \p Diagnostic.
/// If there isn't a Fix attached to the diagnostic and \p AnyFix is true, Check
/// A diagnostic consumer that turns each \c Diagnostic into a
/// \c SourceManager-independent \c ClangTidyError.
+///
+/// \param EnableNolintBlocks Enables diagnostic-disabling inside blocks of
+/// code, delimited by NOLINTBEGIN and NOLINTEND.
//
// FIXME: If we move away from unit-tests, this can be moved to a private
// implementation file.
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx,
DiagnosticsEngine *ExternalDiagEngine = nullptr,
bool RemoveIncompatibleErrors = true,
- bool GetFixesFromNotes = false);
+ bool GetFixesFromNotes = false,
+ bool EnableNolintBlocks = true);
// FIXME: The concept of converting between FixItHints and Replacements is
// more generic and should be pulled out into a more useful Diagnostics
DiagnosticsEngine *ExternalDiagEngine;
bool RemoveIncompatibleErrors;
bool GetFixesFromNotes;
+ bool EnableNolintBlocks;
std::vector<ClangTidyError> Errors;
std::unique_ptr<llvm::Regex> HeaderFilter;
bool LastErrorRelatesToUserCode;