|
| 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> |
| 42 | +clangTidyNoLintFixes(const clang::tidy::ClangTidyContext &CTContext, |
| 43 | + const clang::Diagnostic &Info, const Diag &Diag) { |
| 44 | + auto RuleName = CTContext.getCheckName(Diag.ID); |
| 45 | + if ( |
| 46 | + // If this isn't a clang-tidy diag |
| 47 | + RuleName.empty() || |
| 48 | + // NOLINT does not work on Serverity Error or above |
| 49 | + Diag.Severity >= DiagnosticsEngine::Error || |
| 50 | + // No point adding extra fixes if the Diag is for a different file |
| 51 | + !Diag.InsideMainFile) { |
| 52 | + return {}; |
| 53 | + } |
| 54 | + |
| 55 | + auto &SrcMgr = Info.getSourceManager(); |
| 56 | + auto &DiagLoc = Info.getLocation(); |
| 57 | + |
| 58 | + auto F = Fix{}; |
| 59 | + F.Message = llvm::formatv("ignore [{0}] for this line", RuleName); |
| 60 | + auto &E = F.Edits.emplace_back(); |
| 61 | + |
| 62 | + auto File = SrcMgr.getFileID(DiagLoc); |
| 63 | + auto CodeTilDiag = toSourceCode( |
| 64 | + SrcMgr, SourceRange(SrcMgr.getLocForStartOfFile(File), DiagLoc)); |
| 65 | + |
| 66 | + auto StartCurLine = CodeTilDiag.find_last_of('\n') + 1; |
| 67 | + auto CurLine = CodeTilDiag.substr(StartCurLine); |
| 68 | + auto Indent = CurLine.take_while([](char C) { return std::isspace(C); }); |
| 69 | + |
| 70 | + auto ExistingNoLintNextLineInsertPos = std::optional<int>(); |
| 71 | + if (StartCurLine > 0) { |
| 72 | + auto StartPrevLine = CodeTilDiag.find_last_of('\n', StartCurLine - 1) + 1; |
| 73 | + auto PrevLine = |
| 74 | + CodeTilDiag.substr(StartPrevLine, StartCurLine - StartPrevLine - 1); |
| 75 | + auto NLPos = PrevLine.find("NOLINTNEXTLINE("); |
| 76 | + if (NLPos != StringRef::npos) { |
| 77 | + ExistingNoLintNextLineInsertPos = |
| 78 | + std::make_optional(PrevLine.find(")", NLPos)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + auto InsertPos = sourceLocToPosition(SrcMgr, DiagLoc); |
| 83 | + if (ExistingNoLintNextLineInsertPos) { |
| 84 | + E.newText = llvm::formatv(", {0}", RuleName); |
| 85 | + InsertPos.line -= 1; |
| 86 | + InsertPos.character = *ExistingNoLintNextLineInsertPos; |
| 87 | + } else { |
| 88 | + E.newText = llvm::formatv("{0}// NOLINTNEXTLINE({1})\n", Indent, RuleName); |
| 89 | + InsertPos.character = 0; |
| 90 | + } |
| 91 | + E.range = {InsertPos, InsertPos}; |
| 92 | + |
| 93 | + return {F}; |
| 94 | +} |
| 95 | + |
| 96 | +const auto MsgRegex = std::regex("ignore \\[.*\\] for this line"); |
| 97 | +bool isClangTidyNoLintFixes(const Fix &F) { |
| 98 | + return std::regex_match(F.Message, MsgRegex); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace clangd |
| 102 | +} // namespace clang |
0 commit comments