Skip to content

Commit 6a043ec

Browse files
committed
[clang-tidy] Fix ODR violation in unittests.
Both tests define clang::tidy::test::TestCheck::registerMatchers(). This is UB and causes linker to sometimes choose the wrong overload. Put classes into anonymous namespaces to avoid the problem. Differential Revision: https://reviews.llvm.org/D84902
1 parent b6635b5 commit 6a043ec

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

clang-tools-extra/unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace clang {
66
namespace tidy {
77
namespace test {
88

9+
namespace {
910
class TestCheck : public ClangTidyCheck {
1011
public:
1112
TestCheck(StringRef Name, ClangTidyContext *Context)
@@ -20,25 +21,15 @@ class TestCheck : public ClangTidyCheck {
2021
diag(Var->getTypeSpecStartLoc(), "type specifier");
2122
}
2223
};
24+
} // namespace
2325

24-
// FIXME: This test seems to cause a strange linking interference
25-
// with the ValidConfiguration.ValidEnumOptions test on macOS.
26-
// If both tests are enabled, this test will fail as if
27-
// runCheckOnCode() is not invoked at all. Looks like a linker bug.
28-
// For now both tests are disabled on macOS. It is not sufficient
29-
// to only disable the other test because this test keeps failing
30-
// under Address Sanitizer, which may be an indication of more
31-
// such linking interference with other tests and this test
32-
// seems to be in the center of it.
33-
#ifndef __APPLE__
3426
TEST(ClangTidyDiagnosticConsumer, SortsErrors) {
3527
std::vector<ClangTidyError> Errors;
3628
runCheckOnCode<TestCheck>("int a;", &Errors);
3729
EXPECT_EQ(2ul, Errors.size());
3830
EXPECT_EQ("type specifier", Errors[0].Message.Message);
3931
EXPECT_EQ("variable", Errors[1].Message.Message);
4032
}
41-
#endif
4233

4334
} // namespace test
4435
} // namespace tidy

clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ TEST(ParseConfiguration, MergeConfigurations) {
118118
EXPECT_TRUE(*Options.UseColor);
119119
}
120120

121+
namespace {
121122
class TestCheck : public ClangTidyCheck {
122123
public:
123124
TestCheck(ClangTidyContext *Context) : ClangTidyCheck("test", Context) {}
@@ -140,6 +141,7 @@ class TestCheck : public ClangTidyCheck {
140141
return Options.getLocalOrGlobal<IntType>(std::forward<Args>(Arguments)...);
141142
}
142143
};
144+
} // namespace
143145

144146
#define CHECK_VAL(Value, Expected) \
145147
do { \
@@ -222,9 +224,6 @@ TEST(CheckOptionsValidation, ValidIntOptions) {
222224
#undef CHECK_ERROR_INT
223225
}
224226

225-
// FIXME: Figure out why this test causes crashes on mac os.
226-
// See also comments around the ClangTidyDiagnosticConsumer.SortsErrors test.
227-
#ifndef __APPLE__
228227
TEST(ValidConfiguration, ValidEnumOptions) {
229228

230229
ClangTidyOptions Options;
@@ -276,7 +275,6 @@ TEST(ValidConfiguration, ValidEnumOptions) {
276275

277276
#undef CHECK_ERROR_ENUM
278277
}
279-
#endif
280278

281279
#undef CHECK_VAL
282280
#undef CHECK_ERROR

0 commit comments

Comments
 (0)