[PATCH] D34755: [clangd] Added a test, checking that gcc install is searched via VFS.

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 28 07:54:43 PDT 2017


ilya-biryukov created this revision.

https://reviews.llvm.org/D34755

Files:
  unittests/clangd/ClangdTests.cpp


Index: unittests/clangd/ClangdTests.cpp
===================================================================
--- unittests/clangd/ClangdTests.cpp
+++ unittests/clangd/ClangdTests.cpp
@@ -166,8 +166,22 @@
 public:
   std::vector<tooling::CompileCommand>
   getCompileCommands(PathRef File) override {
-    return {};
+    if (ExtraClangFlags.empty())
+      return {};
+
+    std::vector<std::string> CommandLine;
+    CommandLine.reserve(3 + ExtraClangFlags.size());
+    CommandLine.insert(CommandLine.end(), {"clang", "-fsyntax-only"});
+    CommandLine.insert(CommandLine.end(), ExtraClangFlags.begin(),
+                       ExtraClangFlags.end());
+    CommandLine.push_back(File.str());
+
+    return {tooling::CompileCommand(llvm::sys::path::parent_path(File),
+                                    llvm::sys::path::filename(File),
+                                    CommandLine, "")};
   }
+
+  std::vector<std::string> ExtraClangFlags;
 };
 
 class MockFSProvider : public FileSystemProvider {
@@ -394,6 +408,53 @@
   EXPECT_EQ(Server.codeComplete(FooCpp, Position{0, 0}).Tag, FS.Tag);
 }
 
+TEST_F(ClangdVFSTest, SearchLibDir) {
+  // Checks that searches for GCC installation is done through vfs.
+  MockFSProvider FS;
+  ErrorCheckingDiagConsumer DiagConsumer;
+  MockCompilationDatabase CDB;
+  CDB.ExtraClangFlags = {"-xc++", "-target", "x86_64-linux-unknown", "-m64"};
+  ClangdServer Server(CDB, DiagConsumer, FS,
+                      /*RunSynchronously=*/true);
+
+  // Just a random gcc version string
+  SmallString<8> Version("4.9.3");
+
+  // A lib dir for gcc installation
+  SmallString<64> LibDir("/usr/lib/gcc/x86_64-linux-gnu");
+  llvm::sys::path::append(LibDir, Version);
+
+  // Put crtbegin.o into LibDir/64 to trick clang into thinking there's a gcc
+  // installation there.
+  SmallString<64> DummyLibFile;
+  llvm::sys::path::append(DummyLibFile, LibDir, "64", "crtbegin.o");
+  FS.Files[DummyLibFile] = "";
+
+  SmallString<64> IncludeDir("/usr/include/c++");
+  llvm::sys::path::append(IncludeDir, Version);
+
+  SmallString<64> StringPath;
+  llvm::sys::path::append(StringPath, IncludeDir, "string");
+  FS.Files[StringPath] = "class mock_string {};";
+
+  auto FooCpp = getVirtualTestFilePath("foo.cpp");
+  const auto SourceContents = R"cpp(
+#include <string>
+mock_string x;
+)cpp";
+  FS.Files[FooCpp] = SourceContents;
+
+  Server.addDocument(FooCpp, SourceContents);
+  EXPECT_FALSE(DiagConsumer.hadErrorInLastDiags());
+
+  const auto SourceContentsWithError = R"cpp(
+#include <string>
+std::string x;
+)cpp";
+  Server.addDocument(FooCpp, SourceContentsWithError);
+  EXPECT_TRUE(DiagConsumer.hadErrorInLastDiags());
+}
+
 class ClangdCompletionTest : public ClangdVFSTest {
 protected:
   bool ContainsItem(std::vector<CompletionItem> const &Items, StringRef Name) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34755.104420.patch
Type: text/x-patch
Size: 2820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170628/1022a14d/attachment.bin>


More information about the cfe-commits mailing list