[clang-tools-extra] [clangd] Clean formatting modernize-use-override (PR #81435)
https://github.com/kevinjoseph1995 created https://github.com/llvm/llvm-project/pull/81435 When applying the recommended fix for the "modernize-use-override" clang-tidy diagnostic there was a stray whitespace. This PR fixes that. Resolves https://github.com/clangd/clangd/issues/1704 >From db2c4ee74ffb0592ec7f3fd5557dbb5399ef998d Mon Sep 17 00:00:00 2001 From: Kevin Joseph Date: Sun, 11 Feb 2024 13:39:51 -0800 Subject: [PATCH] [clangd] Clean formatting modernize-use-override When applying the recommended fix for the "modernize-use-override" clang-tidy diagnostic there was a stray whitespace. This commit fixes: https://github.com/clangd/clangd/issues/1704 --- .../clang-tidy/modernize/UseOverrideCheck.cpp | 4 +-- .../clangd/unittests/DiagnosticsTests.cpp | 28 +++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp index e348968b325a5a..4db32b02ee5a0c 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp @@ -228,8 +228,8 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) { if (HasVirtual) { for (Token Tok : Tokens) { if (Tok.is(tok::kw_virtual)) { -Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange( -Tok.getLocation(), Tok.getLocation())); +Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( +Tok.getLocation(), Tok.getEndLoc().getLocWithOffset(1))); break; } } diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index f302dcf5f09db0..76874ac9a2a4e7 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -898,6 +898,34 @@ TEST(DiagnosticTest, ClangTidySelfContainedDiags) { withFix(equalToFix(ExpectedDFix)); } +TEST(DiagnosticTest, ClangTidySelfContainedDiagsFormatting) { + Annotations Main(R"cpp( +class Interface { +public: + virtual void Reset() = 0; +}; +class A : public Interface { + // This will be marked by clangd to use override instead of virtual + $virtual[[virtual ]] void $Reset[[Reset]]()$override[[]]; +}; + )cpp"); + TestTU TU = TestTU::withCode(Main.code()); + TU.ClangTidyProvider = + addTidyChecks("cppcoreguidelines-explicit-virtual-functions,"); + clangd::Fix const ExpectedFix{"prefer using 'override' or (rarely) 'final' " +"instead of 'virtual'", +{TextEdit{Main.range("override"), " override"}, + TextEdit{Main.range("virtual"), ""}}}; + // Note that in the Fix we expect the "virtual" keyword and the following + // whitespace to be deleted + EXPECT_THAT(TU.build().getDiagnostics(), + ifTidyChecks(UnorderedElementsAre( + AllOf(Diag(Main.range("Reset"), + "prefer using 'override' or (rarely) 'final' " + "instead of 'virtual'"), +withFix(equalToFix(ExpectedFix)); +} + TEST(DiagnosticsTest, Preprocessor) { // This looks like a preamble, but there's an #else in the middle! // Check that: ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Clean formatting modernize-use-override (PR #81435)
https://github.com/kevinjoseph1995 updated https://github.com/llvm/llvm-project/pull/81435 >From db2c4ee74ffb0592ec7f3fd5557dbb5399ef998d Mon Sep 17 00:00:00 2001 From: Kevin Joseph Date: Sun, 11 Feb 2024 13:39:51 -0800 Subject: [PATCH 1/2] [clangd] Clean formatting modernize-use-override When applying the recommended fix for the "modernize-use-override" clang-tidy diagnostic there was a stray whitespace. This commit fixes: https://github.com/clangd/clangd/issues/1704 --- .../clang-tidy/modernize/UseOverrideCheck.cpp | 4 +-- .../clangd/unittests/DiagnosticsTests.cpp | 28 +++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp index e348968b325a5a..4db32b02ee5a0c 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp @@ -228,8 +228,8 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) { if (HasVirtual) { for (Token Tok : Tokens) { if (Tok.is(tok::kw_virtual)) { -Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange( -Tok.getLocation(), Tok.getLocation())); +Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( +Tok.getLocation(), Tok.getEndLoc().getLocWithOffset(1))); break; } } diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index f302dcf5f09db0..76874ac9a2a4e7 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -898,6 +898,34 @@ TEST(DiagnosticTest, ClangTidySelfContainedDiags) { withFix(equalToFix(ExpectedDFix)); } +TEST(DiagnosticTest, ClangTidySelfContainedDiagsFormatting) { + Annotations Main(R"cpp( +class Interface { +public: + virtual void Reset() = 0; +}; +class A : public Interface { + // This will be marked by clangd to use override instead of virtual + $virtual[[virtual ]] void $Reset[[Reset]]()$override[[]]; +}; + )cpp"); + TestTU TU = TestTU::withCode(Main.code()); + TU.ClangTidyProvider = + addTidyChecks("cppcoreguidelines-explicit-virtual-functions,"); + clangd::Fix const ExpectedFix{"prefer using 'override' or (rarely) 'final' " +"instead of 'virtual'", +{TextEdit{Main.range("override"), " override"}, + TextEdit{Main.range("virtual"), ""}}}; + // Note that in the Fix we expect the "virtual" keyword and the following + // whitespace to be deleted + EXPECT_THAT(TU.build().getDiagnostics(), + ifTidyChecks(UnorderedElementsAre( + AllOf(Diag(Main.range("Reset"), + "prefer using 'override' or (rarely) 'final' " + "instead of 'virtual'"), +withFix(equalToFix(ExpectedFix)); +} + TEST(DiagnosticsTest, Preprocessor) { // This looks like a preamble, but there's an #else in the middle! // Check that: >From 7b868a4ede5f49f2c4471b85ac389b36aaf1c6e1 Mon Sep 17 00:00:00 2001 From: Kevin Joseph Date: Sun, 11 Feb 2024 19:35:29 -0800 Subject: [PATCH 2/2] fixup! [clangd] Clean formatting modernize-use-override --- .../clang-tidy/modernize/UseOverrideCheck.cpp | 12 ++-- .../clangd/unittests/DiagnosticsTests.cpp | 28 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../checkers/modernize/use-override.cpp | 5 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp index 4db32b02ee5a0c..fd5bd9f0b181b1 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp @@ -7,6 +7,7 @@ //===--===// #include "UseOverrideCheck.h" +#include "../utils/LexerUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" @@ -228,9 +229,14 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) { if (HasVirtual) { for (Token Tok : Tokens) { if (Tok.is(tok::kw_virtual)) { -Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( -Tok.getLocation(), Tok.getEndLoc().getLocWithOffset(1))); -break; +std::optional NextToken = +utils::lexer::findNextTokenIncludingComments( +Tok.getEndLoc(), Sources, getLangOpts()); +if (NextToken.has_value()) { + Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( + Tok.get
[clang-tools-extra] [clangd] Clean formatting modernize-use-override (PR #81435)
@@ -898,6 +898,34 @@ TEST(DiagnosticTest, ClangTidySelfContainedDiags) { withFix(equalToFix(ExpectedDFix)); } +TEST(DiagnosticTest, ClangTidySelfContainedDiagsFormatting) { kevinjoseph1995 wrote: Added a case in clang-tools-extra/test/clang-tidy/checkers/modernize/use-override.cpp. https://github.com/llvm/llvm-project/pull/81435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Clean formatting modernize-use-override (PR #81435)
kevinjoseph1995 wrote: > Release notes entry (for clang-tidy) is also missing. Added a small entry to the Release notes. I wasn't sure about which section to add it to. Please let me know me if that isn't the right one. https://github.com/llvm/llvm-project/pull/81435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Clean formatting modernize-use-override (PR #81435)
https://github.com/kevinjoseph1995 updated https://github.com/llvm/llvm-project/pull/81435 >From db2c4ee74ffb0592ec7f3fd5557dbb5399ef998d Mon Sep 17 00:00:00 2001 From: Kevin Joseph Date: Sun, 11 Feb 2024 13:39:51 -0800 Subject: [PATCH 1/3] [clangd] Clean formatting modernize-use-override When applying the recommended fix for the "modernize-use-override" clang-tidy diagnostic there was a stray whitespace. This commit fixes: https://github.com/clangd/clangd/issues/1704 --- .../clang-tidy/modernize/UseOverrideCheck.cpp | 4 +-- .../clangd/unittests/DiagnosticsTests.cpp | 28 +++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp index e348968b325a5a..4db32b02ee5a0c 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp @@ -228,8 +228,8 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) { if (HasVirtual) { for (Token Tok : Tokens) { if (Tok.is(tok::kw_virtual)) { -Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange( -Tok.getLocation(), Tok.getLocation())); +Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( +Tok.getLocation(), Tok.getEndLoc().getLocWithOffset(1))); break; } } diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index f302dcf5f09db0..76874ac9a2a4e7 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -898,6 +898,34 @@ TEST(DiagnosticTest, ClangTidySelfContainedDiags) { withFix(equalToFix(ExpectedDFix)); } +TEST(DiagnosticTest, ClangTidySelfContainedDiagsFormatting) { + Annotations Main(R"cpp( +class Interface { +public: + virtual void Reset() = 0; +}; +class A : public Interface { + // This will be marked by clangd to use override instead of virtual + $virtual[[virtual ]] void $Reset[[Reset]]()$override[[]]; +}; + )cpp"); + TestTU TU = TestTU::withCode(Main.code()); + TU.ClangTidyProvider = + addTidyChecks("cppcoreguidelines-explicit-virtual-functions,"); + clangd::Fix const ExpectedFix{"prefer using 'override' or (rarely) 'final' " +"instead of 'virtual'", +{TextEdit{Main.range("override"), " override"}, + TextEdit{Main.range("virtual"), ""}}}; + // Note that in the Fix we expect the "virtual" keyword and the following + // whitespace to be deleted + EXPECT_THAT(TU.build().getDiagnostics(), + ifTidyChecks(UnorderedElementsAre( + AllOf(Diag(Main.range("Reset"), + "prefer using 'override' or (rarely) 'final' " + "instead of 'virtual'"), +withFix(equalToFix(ExpectedFix)); +} + TEST(DiagnosticsTest, Preprocessor) { // This looks like a preamble, but there's an #else in the middle! // Check that: >From 7b868a4ede5f49f2c4471b85ac389b36aaf1c6e1 Mon Sep 17 00:00:00 2001 From: Kevin Joseph Date: Sun, 11 Feb 2024 19:35:29 -0800 Subject: [PATCH 2/3] fixup! [clangd] Clean formatting modernize-use-override --- .../clang-tidy/modernize/UseOverrideCheck.cpp | 12 ++-- .../clangd/unittests/DiagnosticsTests.cpp | 28 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../checkers/modernize/use-override.cpp | 5 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp index 4db32b02ee5a0c..fd5bd9f0b181b1 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp @@ -7,6 +7,7 @@ //===--===// #include "UseOverrideCheck.h" +#include "../utils/LexerUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" @@ -228,9 +229,14 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) { if (HasVirtual) { for (Token Tok : Tokens) { if (Tok.is(tok::kw_virtual)) { -Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( -Tok.getLocation(), Tok.getEndLoc().getLocWithOffset(1))); -break; +std::optional NextToken = +utils::lexer::findNextTokenIncludingComments( +Tok.getEndLoc(), Sources, getLangOpts()); +if (NextToken.has_value()) { + Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange( + Tok.get
[clang-tools-extra] [clangd] Clean formatting modernize-use-override (PR #81435)
@@ -160,6 +160,10 @@ Changes in existing checks `AllowStringArrays` option, enabling the exclusion of array types with deduced length initialized from string literals. +- Improved :doc:` kevinjoseph1995 wrote: Thanks, added the missing check name. https://github.com/llvm/llvm-project/pull/81435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Clean formatting modernize-use-override (PR #81435)
kevinjoseph1995 wrote: > Small fix in release notes needed. If with this change tests are passing, > then it's looking fine for me. ninja check-clang-tools reports the following: ``` Total Discovered Tests: 2813 Unsupported :7 (0.25%) Passed : 2804 (99.68%) Expectedly Failed:2 (0.07%) ``` And all of the ClangdTests pass as well: ``` [==] 1232 tests from 179 test suites ran. (86642 ms total) [ PASSED ] 1232 tests. ``` https://github.com/llvm/llvm-project/pull/81435 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits