From a8bf389f41465415fa9088609956dfd3e153e9cf Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 10 Dec 2021 02:14:15 +0100 Subject: [PATCH] [clangd] Clean up some include-fixer tests. NFC --- .../clangd/unittests/DiagnosticsTests.cpp | 118 +++++---------------- 1 file changed, 28 insertions(+), 90 deletions(-) diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index 2c4a9ae..c46f084 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -13,7 +13,6 @@ #include "FeatureModule.h" #include "ParsedAST.h" #include "Protocol.h" -#include "SourceCode.h" #include "TestFS.h" #include "TestIndex.h" #include "TestTU.h" @@ -70,6 +69,8 @@ WithTag(::testing::Matcher TagMatcher) { return Field(&Diag::Tags, Contains(TagMatcher)); } +MATCHER_P(HasRange, Range, "") { return arg.Range == Range; } + MATCHER_P2(Diag, Range, Message, "Diag at " + llvm::to_string(Range) + " = [" + Message + "]") { return arg.Range == Range && arg.Message == Message; @@ -831,95 +832,36 @@ buildIndexWithSymbol(llvm::ArrayRef Syms) { } TEST(IncludeFixerTest, IncompleteType) { - Annotations Test(R"cpp(// error-ok -$insert[[]]namespace ns { - class X; - $nested[[X::]]Nested n; -} -class Y : $base[[public ns::X]] {}; -void test(ns::X *x, ns::X& ref_x) { - x$access[[->]]f(); - auto& $type[[[]]a] = *x; - - ns::X $incomplete[[var]]; - $tag[[ref_x]]->f(); - $use[[ns::X()]]; - $sizeof[[sizeof]](ns::X); - for (auto it : $for[[ref_x]]); -} - -ns::X $return[[func]]() {} - -class T { - ns::X $field[[x]]; -}; - )cpp"); - auto TU = TestTU::withCode(Test.code()); - TU.ExtraArgs.push_back("-std=c++17"); + auto TU = TestTU::withHeaderCode("namespace ns { class X; } ns::X *x;"); + TU.ExtraArgs.push_back("-std=c++20"); auto Index = buildIndexWithSymbol( {SymbolWithHeader{"ns::X", "unittest:///x.h", "\"x.h\""}}); TU.ExternalIndex = Index.get(); - EXPECT_THAT( - *TU.build().getDiagnostics(), - UnorderedElementsAreArray( - {AllOf(Diag(Test.range("nested"), - "incomplete type 'ns::X' named in nested name specifier"), - DiagName("incomplete_nested_name_spec"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("base"), "base class has incomplete type"), - DiagName("incomplete_base_class"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("access"), - "member access into incomplete type 'ns::X'"), - DiagName("incomplete_member_access"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf( - Diag( - Test.range("type"), - "incomplete type 'ns::X' where a complete type is required"), - DiagName("incomplete_type"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("incomplete"), - "variable has incomplete type 'ns::X'"), - DiagName("typecheck_decl_incomplete_type"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf( - Diag(Test.range("tag"), "incomplete definition of type 'ns::X'"), - DiagName("typecheck_incomplete_tag"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("use"), - "invalid use of incomplete type 'ns::X'"), - DiagName("invalid_incomplete_type_use"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("sizeof"), - "invalid application of 'sizeof' to " - "an incomplete type 'ns::X'"), - DiagName("sizeof_alignof_incomplete_or_sizeless_type"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("for"), - "cannot use incomplete type 'ns::X' as a range"), - DiagName("for_range_incomplete_type"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("return"), - "incomplete result type 'ns::X' in function definition"), - DiagName("func_def_incomplete_result"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X"))), - AllOf(Diag(Test.range("field"), "field has incomplete type 'ns::X'"), - DiagName("field_incomplete_or_sizeless"), - WithFix(Fix(Test.range("insert"), "#include \"x.h\"\n", - "Include \"x.h\" for symbol ns::X")))})) - << Test.code(); + std::vector> Tests{ + {"incomplete_nested_name_spec", "[[ns::X::]]Nested n;"}, + {"incomplete_base_class", "class Y : [[ns::X]] {};"}, + {"incomplete_member_access", "auto i = x[[->]]f();"}, + {"incomplete_type", "auto& [[[]]m] = *x;"}, + {"typecheck_decl_incomplete_type", "ns::X [[var]];"}, + {"typecheck_incomplete_tag", "auto i = [[(*x)]]->f();"}, + {"invalid_incomplete_type_use", "auto var = [[ns::X()]];"}, + {"sizeof_alignof_incomplete_or_sizeless_type", + "auto s = [[sizeof]](ns::X);"}, + {"for_range_incomplete_type", "void foo() { for (auto i : [[*]]x ) {} }"}, + {"func_def_incomplete_result", "ns::X [[func]] () {}"}, + {"field_incomplete_or_sizeless", "class M { ns::X [[member]]; };"}, + }; + for (auto Case : Tests) { + Annotations Main(Case.second); + TU.Code = Main.code().str() + "\n // error-ok"; + EXPECT_THAT( + *TU.build().getDiagnostics(), + ElementsAre(AllOf(DiagName(Case.first), HasRange(Main.range()), + WithFix(Fix(Range{}, "#include \"x.h\"\n", + "Include \"x.h\" for symbol ns::X"))))) + << Case.second; + } } TEST(IncludeFixerTest, NoSuggestIncludeWhenNoDefinitionInHeader) { @@ -1097,8 +1039,6 @@ void g() { ns::$[[scope]]::X_Y(); } )cpp"); TestTU TU; TU.Code = std::string(Test.code()); - // FIXME: Figure out why this is needed and remove it, PR43662. - TU.ExtraArgs.push_back("-fno-ms-compatibility"); auto Index = buildIndexWithSymbol( SymbolWithHeader{"ns::scope::X_Y", "unittest:///x.h", "\"x.h\""}); TU.ExternalIndex = Index.get(); @@ -1124,8 +1064,6 @@ void f() { )cpp"); TestTU TU; TU.Code = std::string(Test.code()); - // FIXME: Figure out why this is needed and remove it, PR43662. - TU.ExtraArgs.push_back("-fno-ms-compatibility"); auto Index = buildIndexWithSymbol( {SymbolWithHeader{"clang::clangd::X", "unittest:///x.h", "\"x.h\""}, SymbolWithHeader{"clang::clangd::ns::Y", "unittest:///y.h", "\"y.h\""}}); -- 2.7.4