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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clangd

Author: Haojian Wu (hokein)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/109421.diff


2 Files Affected:

- (modified) clang-tools-extra/clangd/ParsedAST.cpp (+2) 
- (modified) clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp (+4) 


``````````diff
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) {

``````````

</details>


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


More information about the cfe-commits mailing list