[clang-tools-extra] [clangd] Improve robustness when clang-tidy check names contain leading spaces. (PR #109421)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 20 06:34:12 PDT 2024


https://github.com/hokein created https://github.com/llvm/llvm-project/pull/109421

The current logic assumes that check names do not have leading spaces.

In cases like "-*, clang-diagnostic*", when processing the second check " clang-diagnostics-*" (with a leading space), the check fails on `CDPrefix.starts_with(Check)`, resulting in all diagnostics remaining disabled.

>From 8542638d3460aa659b34107260788e2126371c44 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Fri, 20 Sep 2024 15:19:12 +0200
Subject: [PATCH] [clangd] Improve robustness when clang-tidy check names
 contain leading spaces.

The current logic assumes that check names do not have leading spaces.

In cases like "-*, clang-diagnostic*", when processing the second check
" clang-diagnostics-*" (with a leading space), the check fails on
`CDPrefix.starts_with(Check)`, resulting in all diagnostics remaining
disabled.
---
 clang-tools-extra/clangd/ParsedAST.cpp                  | 2 ++
 clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/clang-tools-extra/clangd/ParsedAST.cpp b/clang-tools-extra/clangd/ParsedAST.cpp
index 4491be9aa0362b..5cf1691ce39617 100644
--- a/clang-tools-extra/clangd/ParsedAST.cpp
+++ b/clang-tools-extra/clangd/ParsedAST.cpp
@@ -280,6 +280,8 @@ class TidyDiagnosticGroups {
     llvm::StringRef Check;
     while (!Checks.empty()) {
       std::tie(Check, Checks) = Checks.split(',');
+      Check = Check.trim();
+
       if (Check.empty())
         continue;
 
diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 096f77e414f5a5..efb2e5ed2fbe1d 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -748,6 +748,10 @@ TEST(DiagnosticTest, ClangTidyEnablesClangWarning) {
   TU.ExtraArgs = {"-Wunused"};
   TU.ClangTidyProvider = addClangArgs({"-Wno-unused"}, {});
   EXPECT_THAT(TU.build().getDiagnostics(), IsEmpty());
+
+  TU.ExtraArgs = {"-Wno-unused"};
+  TU.ClangTidyProvider = addClangArgs({"-Wunused"}, {"-*, clang-diagnostic-*"});
+  EXPECT_THAT(TU.build().getDiagnostics(), SizeIs(1));
 }
 
 TEST(DiagnosticTest, LongFixMessages) {



More information about the cfe-commits mailing list