Skip to content

Commit 19265f9

Browse files
committed
inserted NOLINT with indent
Retrieved CurLine and PrevLine
1 parent 209e5ed commit 19265f9

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

clang-tools-extra/clangd/Diagnostics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
823823
if (!Info.getFixItHints().empty())
824824
AddFix(true /* try to invent a message instead of repeating the diag */);
825825
if (Fixer) {
826-
auto ExtraFixes = Fixer(LastDiag->Severity, Info);
826+
auto ExtraFixes = Fixer(*LastDiag, Info);
827827
LastDiag->Fixes.insert(LastDiag->Fixes.end(), ExtraFixes.begin(),
828828
ExtraFixes.end());
829829
}
@@ -842,7 +842,7 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
842842

843843
// Give include-fixer a chance to replace a note with a fix.
844844
if (Fixer) {
845-
auto ReplacementFixes = Fixer(LastDiag->Severity, Info);
845+
auto ReplacementFixes = Fixer(*LastDiag, Info);
846846
if (!ReplacementFixes.empty()) {
847847
assert(Info.getNumFixItHints() == 0 &&
848848
"Include-fixer replaced a note with clang fix-its attached!");

clang-tools-extra/clangd/Diagnostics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ class StoreDiags : public DiagnosticConsumer {
148148

149149
/// When passed a main diagnostic, returns fixes to add to it.
150150
/// When passed a note diagnostic, returns fixes to replace it with.
151-
using DiagFixer = std::function<std::vector<Fix>(DiagnosticsEngine::Level,
152-
const clang::Diagnostic &)>;
151+
using DiagFixer =
152+
std::function<std::vector<Fix>(const Diag &, const clang::Diagnostic &)>;
153153
using LevelAdjuster = std::function<DiagnosticsEngine::Level(
154154
DiagnosticsEngine::Level, const clang::Diagnostic &)>;
155155
using DiagCallback =

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "llvm/Support/Error.h"
6868
#include "llvm/Support/MemoryBuffer.h"
6969
#include <cassert>
70+
#include <cctype>
7071
#include <cstddef>
7172
#include <iterator>
7273
#include <memory>
@@ -401,6 +402,48 @@ filterFastTidyChecks(const tidy::ClangTidyCheckFactories &All,
401402

402403
} // namespace
403404

405+
std::vector<Fix>
406+
clangTidyNoLintFixes(const clang::tidy::ClangTidyContext &CTContext,
407+
const clang::Diagnostic &Info, const Diag &Diag) {
408+
auto RuleName = CTContext.getCheckName(Info.getID());
409+
if (RuleName.empty()) {
410+
return {};
411+
}
412+
if (!Diag.InsideMainFile) {
413+
return {};
414+
}
415+
auto &SrcMgr = Info.getSourceManager();
416+
auto &DiagLoc = Info.getLocation();
417+
418+
auto F = Fix{};
419+
F.Message = llvm::formatv("ignore [{0}] for this line", RuleName);
420+
auto &E = F.Edits.emplace_back();
421+
422+
auto File = SrcMgr.getFileID(DiagLoc);
423+
auto CodeTilDiag = toSourceCode(
424+
SrcMgr, SourceRange(SrcMgr.getLocForStartOfFile(File), DiagLoc));
425+
426+
auto StartCurLine = CodeTilDiag.find_last_of('\n') + 1;
427+
auto CurLine = CodeTilDiag.substr(StartCurLine);
428+
elog("CurLine: '{0}'", CurLine);
429+
auto Indent = CurLine.take_while([](char C) { return std::isspace(C); });
430+
431+
if (StartCurLine > 0) {
432+
auto StartPrevLine = CodeTilDiag.find_last_of('\n', StartCurLine - 1) + 1;
433+
auto PrevLine =
434+
CodeTilDiag.substr(StartPrevLine, StartCurLine - StartPrevLine - 1);
435+
elog("PrevLine: '{0}'", PrevLine);
436+
}
437+
438+
E.newText = llvm::formatv("{0}// NOLINTNEXTLINE({1})\n", Indent, RuleName);
439+
440+
auto InsertPos = sourceLocToPosition(SrcMgr, DiagLoc);
441+
InsertPos.character = 0;
442+
E.range = {InsertPos, InsertPos};
443+
444+
return {F};
445+
}
446+
404447
std::optional<ParsedAST>
405448
ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
406449
std::unique_ptr<clang::CompilerInvocation> CI,
@@ -655,10 +698,16 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
655698
: Symbol::Include;
656699
FixIncludes.emplace(Filename, Inserter, *Inputs.Index,
657700
/*IndexRequestLimit=*/5, Directive);
658-
ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
659-
const clang::Diagnostic &Info) {
660-
return FixIncludes->fix(DiagLevl, Info);
661-
});
701+
ASTDiags.contributeFixes(
702+
[&FixIncludes, &CTContext](const Diag &Diag,
703+
const clang::Diagnostic &Info) {
704+
auto Fixes = std::vector<Fix>();
705+
auto NoLintFixes = clangTidyNoLintFixes(*CTContext, Info, Diag);
706+
Fixes.insert(Fixes.end(), NoLintFixes.begin(), NoLintFixes.end());
707+
auto IncludeFixes = FixIncludes->fix(Diag.Severity, Info);
708+
Fixes.insert(Fixes.end(), IncludeFixes.begin(), IncludeFixes.end());
709+
return Fixes;
710+
});
662711
Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
663712
}
664713
}

0 commit comments

Comments
 (0)