//===--- IncludeCleanerTests.cpp --------------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "Annotations.h" #include "Config.h" #include "IncludeCleaner.h" #include "SourceCode.h" #include "TestFS.h" #include "TestTU.h" #include "support/Context.h" #include "llvm/ADT/ScopeExit.h" #include "llvm/Testing/Support/SupportHelpers.h" #include "gmock/gmock.h" #include "gtest/gtest.h" namespace clang { namespace clangd { namespace { using ::testing::ElementsAre; using ::testing::ElementsAreArray; using ::testing::IsEmpty; using ::testing::Pointee; using ::testing::UnorderedElementsAre; std::string guard(llvm::StringRef Code) { return "#pragma once\n" + Code.str(); } TEST(IncludeCleaner, ReferencedLocations) { struct TestCase { std::string HeaderCode; std::string MainCode; }; TestCase Cases[] = { // DeclRefExpr { "int ^x();", "int y = x();", }, // RecordDecl { "class ^X;", "X *y;", }, // When definition is available, we don't need to mark forward // declarations as used. { "class ^X {}; class X;", "X *y;", }, // We already have forward declaration in the main file, the other // non-definition declarations are not needed. { "class ^X {}; class X;", "class X; X *y;", }, // Nested class definition can occur outside of the parent class // definition. Bar declaration should be visible to its definition but // it will always be because we will mark Foo definition as used. { "class ^Foo { class Bar; };", "class Foo::Bar {};", }, // TypedefType and UsingDecls { "using ^Integer = int;", "Integer x;", }, { "namespace ns { void ^foo(); void ^foo() {} }", "using ns::foo;", }, { "namespace ns { void foo(); void foo() {}; }", "using namespace ns;", }, { // Refs from UsingTypeLoc and implicit constructor! "struct ^A {}; using B = A; using ^C = B;", "C a;", }, {"namespace ns { template class A {}; } using ns::^A;", "A* a;"}, {"namespace ns { template class A {}; } using ns::^A;", R"cpp( template