From e259a5fd0dbd4065c6f64393016340f052da3a9c Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 25 Sep 2019 10:30:22 +0000 Subject: [PATCH] [clangd] Move the existing heder-source-switch implemenation out of clangdServer. Summary: This is a NFC change. Reviewers: kadircet Reviewed By: kadircet Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D67960 llvm-svn: 372841 --- clang-tools-extra/clangd/CMakeLists.txt | 1 + clang-tools-extra/clangd/ClangdServer.cpp | 56 +--------------- clang-tools-extra/clangd/HeaderSourceSwitch.cpp | 68 +++++++++++++++++++ clang-tools-extra/clangd/HeaderSourceSwitch.h | 22 +++++++ clang-tools-extra/clangd/unittests/CMakeLists.txt | 1 + clang-tools-extra/clangd/unittests/ClangdTests.cpp | 76 ---------------------- .../clangd/unittests/HeaderSourceSwitchTests.cpp | 76 ++++++++++++++++++++++ 7 files changed, 170 insertions(+), 130 deletions(-) create mode 100644 clang-tools-extra/clangd/HeaderSourceSwitch.cpp create mode 100644 clang-tools-extra/clangd/HeaderSourceSwitch.h create mode 100644 clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt index 50de3a3..4a816e5 100644 --- a/clang-tools-extra/clangd/CMakeLists.txt +++ b/clang-tools-extra/clangd/CMakeLists.txt @@ -56,6 +56,7 @@ add_clang_library(clangDaemon FuzzyMatch.cpp GlobalCompilationDatabase.cpp Headers.cpp + HeaderSourceSwitch.cpp IncludeFixer.cpp JSONTransport.cpp Logger.cpp diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index 71a1a99..86d9439 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -11,6 +11,7 @@ #include "FindSymbols.h" #include "Format.h" #include "FormattedString.h" +#include "HeaderSourceSwitch.h" #include "Headers.h" #include "Logger.h" #include "ParsedAST.h" @@ -449,60 +450,7 @@ void ClangdServer::locateSymbolAt(PathRef File, Position Pos, } llvm::Optional ClangdServer::switchSourceHeader(PathRef Path) { - - llvm::StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx", - ".c++", ".m", ".mm"}; - llvm::StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"}; - - llvm::StringRef PathExt = llvm::sys::path::extension(Path); - - // Lookup in a list of known extensions. - auto SourceIter = - llvm::find_if(SourceExtensions, [&PathExt](PathRef SourceExt) { - return SourceExt.equals_lower(PathExt); - }); - bool IsSource = SourceIter != std::end(SourceExtensions); - - auto HeaderIter = - llvm::find_if(HeaderExtensions, [&PathExt](PathRef HeaderExt) { - return HeaderExt.equals_lower(PathExt); - }); - - bool IsHeader = HeaderIter != std::end(HeaderExtensions); - - // We can only switch between the known extensions. - if (!IsSource && !IsHeader) - return None; - - // Array to lookup extensions for the switch. An opposite of where original - // extension was found. - llvm::ArrayRef NewExts; - if (IsSource) - NewExts = HeaderExtensions; - else - NewExts = SourceExtensions; - - // Storage for the new path. - llvm::SmallString<128> NewPath = llvm::StringRef(Path); - - // Instance of vfs::FileSystem, used for file existence checks. - auto FS = FSProvider.getFileSystem(); - - // Loop through switched extension candidates. - for (llvm::StringRef NewExt : NewExts) { - llvm::sys::path::replace_extension(NewPath, NewExt); - if (FS->exists(NewPath)) - return NewPath.str().str(); // First str() to convert from SmallString to - // StringRef, second to convert from StringRef - // to std::string - - // Also check NewExt in upper-case, just in case. - llvm::sys::path::replace_extension(NewPath, NewExt.upper()); - if (FS->exists(NewPath)) - return NewPath.str().str(); - } - - return None; + return getCorrespondingHeaderOrSource(Path, FSProvider.getFileSystem()); } llvm::Expected diff --git a/clang-tools-extra/clangd/HeaderSourceSwitch.cpp b/clang-tools-extra/clangd/HeaderSourceSwitch.cpp new file mode 100644 index 0000000..70b28e7a --- /dev/null +++ b/clang-tools-extra/clangd/HeaderSourceSwitch.cpp @@ -0,0 +1,68 @@ +//===--- HeaderSourceSwitch.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 "HeaderSourceSwitch.h" + +namespace clang { +namespace clangd { + +llvm::Optional getCorrespondingHeaderOrSource( + const Path &OriginalFile, + llvm::IntrusiveRefCntPtr VFS) { + llvm::StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx", + ".c++", ".m", ".mm"}; + llvm::StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"}; + + llvm::StringRef PathExt = llvm::sys::path::extension(OriginalFile); + + // Lookup in a list of known extensions. + auto SourceIter = + llvm::find_if(SourceExtensions, [&PathExt](PathRef SourceExt) { + return SourceExt.equals_lower(PathExt); + }); + bool IsSource = SourceIter != std::end(SourceExtensions); + + auto HeaderIter = + llvm::find_if(HeaderExtensions, [&PathExt](PathRef HeaderExt) { + return HeaderExt.equals_lower(PathExt); + }); + bool IsHeader = HeaderIter != std::end(HeaderExtensions); + + // We can only switch between the known extensions. + if (!IsSource && !IsHeader) + return None; + + // Array to lookup extensions for the switch. An opposite of where original + // extension was found. + llvm::ArrayRef NewExts; + if (IsSource) + NewExts = HeaderExtensions; + else + NewExts = SourceExtensions; + + // Storage for the new path. + llvm::SmallString<128> NewPath = llvm::StringRef(OriginalFile); + + // Loop through switched extension candidates. + for (llvm::StringRef NewExt : NewExts) { + llvm::sys::path::replace_extension(NewPath, NewExt); + if (VFS->exists(NewPath)) + return NewPath.str().str(); // First str() to convert from SmallString to + // StringRef, second to convert from StringRef + // to std::string + + // Also check NewExt in upper-case, just in case. + llvm::sys::path::replace_extension(NewPath, NewExt.upper()); + if (VFS->exists(NewPath)) + return NewPath.str().str(); + } + return None; +} + +} // namespace clangd +} // namespace clang diff --git a/clang-tools-extra/clangd/HeaderSourceSwitch.h b/clang-tools-extra/clangd/HeaderSourceSwitch.h new file mode 100644 index 0000000..902f87e0 --- /dev/null +++ b/clang-tools-extra/clangd/HeaderSourceSwitch.h @@ -0,0 +1,22 @@ +//===--- HeaderSourceSwitch.h - ----------------------------------*- 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 "ParsedAST.h" +#include "llvm/ADT/Optional.h" + +namespace clang { +namespace clangd { + +/// Given a header file, returns the best matching source file, and vice visa. +/// It only uses the filename heuristics to do the inference. +llvm::Optional getCorrespondingHeaderOrSource( + const Path &OriginalFile, + llvm::IntrusiveRefCntPtr VFS); + +} // namespace clangd +} // namespace clang diff --git a/clang-tools-extra/clangd/unittests/CMakeLists.txt b/clang-tools-extra/clangd/unittests/CMakeLists.txt index ec835bc..d25745f 100644 --- a/clang-tools-extra/clangd/unittests/CMakeLists.txt +++ b/clang-tools-extra/clangd/unittests/CMakeLists.txt @@ -46,6 +46,7 @@ add_unittest(ClangdUnitTests ClangdTests FuzzyMatchTests.cpp GlobalCompilationDatabaseTests.cpp HeadersTests.cpp + HeaderSourceSwitchTests.cpp IndexActionTests.cpp IndexTests.cpp JSONTransportTests.cpp diff --git a/clang-tools-extra/clangd/unittests/ClangdTests.cpp b/clang-tools-extra/clangd/unittests/ClangdTests.cpp index 25c82e4..3cbd33a 100644 --- a/clang-tools-extra/clangd/unittests/ClangdTests.cpp +++ b/clang-tools-extra/clangd/unittests/ClangdTests.cpp @@ -765,82 +765,6 @@ int d; } } -TEST_F(ClangdVFSTest, CheckSourceHeaderSwitch) { - MockFSProvider FS; - ErrorCheckingDiagConsumer DiagConsumer; - MockCompilationDatabase CDB; - ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest()); - - auto SourceContents = R"cpp( - #include "foo.h" - int b = a; - )cpp"; - - auto FooCpp = testPath("foo.cpp"); - auto FooH = testPath("foo.h"); - auto Invalid = testPath("main.cpp"); - - FS.Files[FooCpp] = SourceContents; - FS.Files[FooH] = "int a;"; - FS.Files[Invalid] = "int main() { \n return 0; \n }"; - - Optional PathResult = Server.switchSourceHeader(FooCpp); - EXPECT_TRUE(PathResult.hasValue()); - ASSERT_EQ(PathResult.getValue(), FooH); - - PathResult = Server.switchSourceHeader(FooH); - EXPECT_TRUE(PathResult.hasValue()); - ASSERT_EQ(PathResult.getValue(), FooCpp); - - SourceContents = R"c( - #include "foo.HH" - int b = a; - )c"; - - // Test with header file in capital letters and different extension, source - // file with different extension - auto FooC = testPath("bar.c"); - auto FooHH = testPath("bar.HH"); - - FS.Files[FooC] = SourceContents; - FS.Files[FooHH] = "int a;"; - - PathResult = Server.switchSourceHeader(FooC); - EXPECT_TRUE(PathResult.hasValue()); - ASSERT_EQ(PathResult.getValue(), FooHH); - - // Test with both capital letters - auto Foo2C = testPath("foo2.C"); - auto Foo2HH = testPath("foo2.HH"); - FS.Files[Foo2C] = SourceContents; - FS.Files[Foo2HH] = "int a;"; - - PathResult = Server.switchSourceHeader(Foo2C); - EXPECT_TRUE(PathResult.hasValue()); - ASSERT_EQ(PathResult.getValue(), Foo2HH); - - // Test with source file as capital letter and .hxx header file - auto Foo3C = testPath("foo3.C"); - auto Foo3HXX = testPath("foo3.hxx"); - - SourceContents = R"c( - #include "foo3.hxx" - int b = a; - )c"; - - FS.Files[Foo3C] = SourceContents; - FS.Files[Foo3HXX] = "int a;"; - - PathResult = Server.switchSourceHeader(Foo3C); - EXPECT_TRUE(PathResult.hasValue()); - ASSERT_EQ(PathResult.getValue(), Foo3HXX); - - // Test if asking for a corresponding file that doesn't exist returns an empty - // string. - PathResult = Server.switchSourceHeader(Invalid); - EXPECT_FALSE(PathResult.hasValue()); -} - TEST_F(ClangdThreadingTest, NoConcurrentDiagnostics) { class NoConcurrentAccessDiagConsumer : public DiagnosticsConsumer { public: diff --git a/clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp b/clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp new file mode 100644 index 0000000..6835fdf --- /dev/null +++ b/clang-tools-extra/clangd/unittests/HeaderSourceSwitchTests.cpp @@ -0,0 +1,76 @@ +//===--- HeaderSourceSwitchTests.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 "HeaderSourceSwitch.h" + +#include "TestFS.h" +#include "TestTU.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace clang { +namespace clangd { +namespace { + +TEST(HeaderSourceSwitchTest, FileHeuristic) { + MockFSProvider FS; + auto FooCpp = testPath("foo.cpp"); + auto FooH = testPath("foo.h"); + auto Invalid = testPath("main.cpp"); + + FS.Files[FooCpp]; + FS.Files[FooH]; + FS.Files[Invalid]; + Optional PathResult = + getCorrespondingHeaderOrSource(FooCpp, FS.getFileSystem()); + EXPECT_TRUE(PathResult.hasValue()); + ASSERT_EQ(PathResult.getValue(), FooH); + + PathResult = getCorrespondingHeaderOrSource(FooH, FS.getFileSystem()); + EXPECT_TRUE(PathResult.hasValue()); + ASSERT_EQ(PathResult.getValue(), FooCpp); + + // Test with header file in capital letters and different extension, source + // file with different extension + auto FooC = testPath("bar.c"); + auto FooHH = testPath("bar.HH"); + + FS.Files[FooC]; + FS.Files[FooHH]; + PathResult = getCorrespondingHeaderOrSource(FooC, FS.getFileSystem()); + EXPECT_TRUE(PathResult.hasValue()); + ASSERT_EQ(PathResult.getValue(), FooHH); + + // Test with both capital letters + auto Foo2C = testPath("foo2.C"); + auto Foo2HH = testPath("foo2.HH"); + FS.Files[Foo2C]; + FS.Files[Foo2HH]; + PathResult = getCorrespondingHeaderOrSource(Foo2C, FS.getFileSystem()); + EXPECT_TRUE(PathResult.hasValue()); + ASSERT_EQ(PathResult.getValue(), Foo2HH); + + // Test with source file as capital letter and .hxx header file + auto Foo3C = testPath("foo3.C"); + auto Foo3HXX = testPath("foo3.hxx"); + + FS.Files[Foo3C]; + FS.Files[Foo3HXX]; + PathResult = getCorrespondingHeaderOrSource(Foo3C, FS.getFileSystem()); + EXPECT_TRUE(PathResult.hasValue()); + ASSERT_EQ(PathResult.getValue(), Foo3HXX); + + // Test if asking for a corresponding file that doesn't exist returns an empty + // string. + PathResult = getCorrespondingHeaderOrSource(Invalid, FS.getFileSystem()); + EXPECT_FALSE(PathResult.hasValue()); +} + +} // namespace +} // namespace clangd +} // namespace clang -- 2.7.4