Skip to content

Commit 14a0ee7

Browse files
njames93cachemeifyoucan
authored andcommitted
[clangd] Fix clang tidy provider when multiple config files exist in directory tree
Currently Clang tidy provider searches from the root directory up to the target directory, this is the opposite of how clang-tidy searches for config files. The result of this is .clang-tidy files are ignored in any subdirectory of a directory containing a .clang-tidy file. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D96204 (cherry picked from commit ba3ea9c)
1 parent 038d08f commit 14a0ee7

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

clang-tools-extra/clangd/TidyProvider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class DotClangTidyTree {
106106
llvm::SmallVector<DotClangTidyCache *> Caches;
107107
{
108108
std::lock_guard<std::mutex> Lock(Mu);
109-
for (auto I = path::begin(Parent), E = path::end(Parent); I != E; ++I) {
109+
for (auto I = path::rbegin(Parent), E = path::rend(Parent); I != E; ++I) {
110110
assert(I->end() >= Parent.begin() && I->end() <= Parent.end() &&
111111
"Canonical path components should be substrings");
112112
llvm::StringRef Ancestor(Parent.begin(), I->end() - Parent.begin());

clang-tools-extra/clangd/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ add_unittest(ClangdUnitTests ClangdTests
9292
TestIndex.cpp
9393
TestTU.cpp
9494
TestWorkspace.cpp
95+
TidyProviderTests.cpp
9596
TypeHierarchyTests.cpp
9697
URITests.cpp
9798
XRefsTests.cpp
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===-- TidyProviderTests.cpp - Clang tidy configuration provider tests ---===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "TestFS.h"
10+
#include "TidyProvider.h"
11+
#include "gtest/gtest.h"
12+
13+
namespace clang {
14+
namespace clangd {
15+
16+
namespace {
17+
18+
TEST(TidyProvider, NestedDirectories) {
19+
MockFS FS;
20+
FS.Files[testPath(".clang-tidy")] = R"yaml(
21+
Checks: 'llvm-*'
22+
CheckOptions:
23+
- key: TestKey
24+
value: 1
25+
)yaml";
26+
FS.Files[testPath("sub1/.clang-tidy")] = R"yaml(
27+
Checks: 'misc-*'
28+
CheckOptions:
29+
- key: TestKey
30+
value: 2
31+
)yaml";
32+
FS.Files[testPath("sub1/sub2/.clang-tidy")] = R"yaml(
33+
Checks: 'bugprone-*'
34+
CheckOptions:
35+
- key: TestKey
36+
value: 3
37+
InheritParentConfig: true
38+
)yaml";
39+
40+
TidyProvider Provider = provideClangTidyFiles(FS);
41+
42+
auto BaseOptions = getTidyOptionsForFile(Provider, testPath("File.cpp"));
43+
ASSERT_TRUE(BaseOptions.Checks.hasValue());
44+
EXPECT_EQ(*BaseOptions.Checks, "llvm-*");
45+
EXPECT_EQ(BaseOptions.CheckOptions.lookup("TestKey").Value, "1");
46+
47+
auto Sub1Options = getTidyOptionsForFile(Provider, testPath("sub1/File.cpp"));
48+
ASSERT_TRUE(Sub1Options.Checks.hasValue());
49+
EXPECT_EQ(*Sub1Options.Checks, "misc-*");
50+
EXPECT_EQ(Sub1Options.CheckOptions.lookup("TestKey").Value, "2");
51+
52+
auto Sub2Options =
53+
getTidyOptionsForFile(Provider, testPath("sub1/sub2/File.cpp"));
54+
ASSERT_TRUE(Sub2Options.Checks.hasValue());
55+
EXPECT_EQ(*Sub2Options.Checks, "misc-*,bugprone-*");
56+
EXPECT_EQ(Sub2Options.CheckOptions.lookup("TestKey").Value, "3");
57+
}
58+
} // namespace
59+
} // namespace clangd
60+
} // namespace clang

0 commit comments

Comments
 (0)