|
| 1 | +//===--- NoLintFixes.cpp -------------------------------------------*- |
| 2 | +// C++-*-===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "NoLintFixes.h" |
| 11 | +#include "../clang-tidy/ClangTidyCheck.h" |
| 12 | +#include "../clang-tidy/ClangTidyDiagnosticConsumer.h" |
| 13 | +#include "../clang-tidy/ClangTidyModule.h" |
| 14 | +#include "AST.h" |
| 15 | +#include "Diagnostics.h" |
| 16 | +#include "FeatureModule.h" |
| 17 | +#include "SourceCode.h" |
| 18 | +#include "clang/AST/ASTContext.h" |
| 19 | +#include "clang/Basic/Diagnostic.h" |
| 20 | +#include "clang/Basic/DiagnosticIDs.h" |
| 21 | +#include "clang/Basic/LLVM.h" |
| 22 | +#include "clang/Basic/SourceLocation.h" |
| 23 | +#include "clang/Basic/SourceManager.h" |
| 24 | +#include "clang/Frontend/CompilerInstance.h" |
| 25 | +#include "clang/Frontend/CompilerInvocation.h" |
| 26 | +#include "clang/Lex/Lexer.h" |
| 27 | +#include "clang/Serialization/ASTWriter.h" |
| 28 | +#include "llvm/ADT/STLFunctionalExtras.h" |
| 29 | +#include "llvm/ADT/SmallVector.h" |
| 30 | +#include "llvm/ADT/StringRef.h" |
| 31 | +#include <cassert> |
| 32 | +#include <cctype> |
| 33 | +#include <optional> |
| 34 | +#include <regex> |
| 35 | +#include <string> |
| 36 | +#include <vector> |
| 37 | + |
| 38 | +namespace clang { |
| 39 | +namespace clangd { |
| 40 | + |
| 41 | +std::vector<Fix> noLintFixes(const clang::tidy::ClangTidyContext &CTContext, |
| 42 | + const clang::Diagnostic &Info, const Diag &Diag) { |
| 43 | + auto RuleName = CTContext.getCheckName(Diag.ID); |
| 44 | + if ( |
| 45 | + // If this isn't a clang-tidy diag |
| 46 | + RuleName.empty() || |
| 47 | + // NOLINT does not work on Serverity Error or above |
| 48 | + Diag.Severity >= DiagnosticsEngine::Error || |
| 49 | + // No point adding extra fixes if the Diag is for a different file |
| 50 | + !Diag.InsideMainFile) { |
| 51 | + return {}; |
| 52 | + } |
| 53 | + |
| 54 | + auto &SrcMgr = Info.getSourceManager(); |
| 55 | + auto DiagLoc = SrcMgr.getSpellingLoc(Info.getLocation()); |
| 56 | + |
| 57 | + auto F = Fix{}; |
| 58 | + F.Message = llvm::formatv("ignore [{0}] for this line", RuleName); |
| 59 | + auto &E = F.Edits.emplace_back(); |
| 60 | + |
| 61 | + auto File = SrcMgr.getFileID(DiagLoc); |
| 62 | + auto FileLoc = SrcMgr.getLocForStartOfFile(File); |
| 63 | + auto CodeTilDiag = toSourceCode(SrcMgr, SourceRange(FileLoc, DiagLoc)); |
| 64 | + |
| 65 | + auto StartCurLine = CodeTilDiag.find_last_of('\n') + 1; |
| 66 | + auto CurLine = CodeTilDiag.substr(StartCurLine); |
| 67 | + auto Indent = CurLine.take_while([](char C) { return std::isspace(C); }); |
| 68 | + |
| 69 | + auto ExistingNoLintNextLineInsertPos = std::optional<int>(); |
| 70 | + if (StartCurLine > 0) { |
| 71 | + auto StartPrevLine = CodeTilDiag.find_last_of('\n', StartCurLine - 1) + 1; |
| 72 | + auto PrevLine = |
| 73 | + CodeTilDiag.substr(StartPrevLine, StartCurLine - StartPrevLine - 1); |
| 74 | + auto NLPos = PrevLine.find("NOLINTNEXTLINE("); |
| 75 | + if (NLPos != StringRef::npos) { |
| 76 | + ExistingNoLintNextLineInsertPos = |
| 77 | + std::make_optional(PrevLine.find(")", NLPos)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + auto InsertPos = sourceLocToPosition(SrcMgr, DiagLoc); |
| 82 | + if (ExistingNoLintNextLineInsertPos) { |
| 83 | + E.newText = llvm::formatv(", {0}", RuleName); |
| 84 | + InsertPos.line -= 1; |
| 85 | + InsertPos.character = *ExistingNoLintNextLineInsertPos; |
| 86 | + } else { |
| 87 | + E.newText = llvm::formatv("{0}// NOLINTNEXTLINE({1})\n", Indent, RuleName); |
| 88 | + InsertPos.character = 0; |
| 89 | + } |
| 90 | + E.range = {InsertPos, InsertPos}; |
| 91 | + |
| 92 | + return {F}; |
| 93 | +} |
| 94 | + |
| 95 | +const auto Regex = std::regex(NoLintFixMsgRegexStr); |
| 96 | +bool isNoLintFixes(const Fix &F) { return std::regex_match(F.Message, Regex); } |
| 97 | + |
| 98 | +} // namespace clangd |
| 99 | +} // namespace clang |
0 commit comments