[PATCH] D50627: [clangd] Add a testcase for empty preamble.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 13 03:21:07 PDT 2018


hokein created this revision.
hokein added a reviewer: ilya-biryukov.
Herald added subscribers: arphaman, jkorous, MaskRay, ioeric, javed.absar.

clangd maintains the last good preamble for each TU and clang treats an empty
preamble as an error,  therefore, clangd will use the stale preamble for
a latest AST, which causes some wired issues for the features using AST
(e.g. GoToDefintion).

A testcase:

1. Open a file contains some includes (like "#include <vector>") in clangd
2. Change the whole content of the file with the source without includes (like "double funcc()")
3. Call findDefinition in "double fun^cc()", it will goes to #include file vector -- because we discard the empty preamble built in step2, and still hold the stale preamble in step1


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D50627

Files:
  unittests/clangd/TUSchedulerTests.cpp


Index: unittests/clangd/TUSchedulerTests.cpp
===================================================================
--- unittests/clangd/TUSchedulerTests.cpp
+++ unittests/clangd/TUSchedulerTests.cpp
@@ -308,6 +308,49 @@
               UnorderedElementsAre(Foo, AnyOf(Bar, Baz)));
 }
 
+TEST_F(TUSchedulerTests, EmptyPreamble) {
+  TUScheduler S(
+      /*AsyncThreadsCount=*/4, /*StorePreambleInMemory=*/true,
+      PreambleParsedCallback(),
+      /*UpdateDebounce=*/std::chrono::steady_clock::duration::zero(),
+      ASTRetentionPolicy());
+
+  auto Foo = testPath("foo.cpp");
+  auto Header = testPath("foo.h");
+
+  Files[Header] = "void foo()";
+  Timestamps[Header] = time_t(0);
+  auto WithPreamble = R"cpp(
+    #include "foo.h"
+    int main() {}
+  )cpp";
+  auto WithEmptyPreamble = R"cpp(int main() {})cpp";
+  S.update(Foo, getInputs(Foo, WithPreamble), WantDiagnostics::Auto,
+           [](std::vector<Diag>) {});
+  S.runWithPreamble("getNonEmptyPreamble", Foo,
+                    [&](llvm::Expected<InputsAndPreamble> Preamble) {
+                      // We expect to get a non-empty preamble.
+                      ASSERT_TRUE(bool(Preamble));
+                      EXPECT_GT(Preamble->Preamble->Preamble.getBounds().Size,
+                                0u);
+                    });
+  // Wait for the preamble is being built.
+  ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(1)));
+
+  // Update the file which results in an empty preamble.
+  S.update(Foo, getInputs(Foo, WithEmptyPreamble), WantDiagnostics::Auto,
+           [](std::vector<Diag>) {});
+  // Wait for the preamble is being built.
+  ASSERT_TRUE(S.blockUntilIdle(timeoutSeconds(1)));
+  S.runWithPreamble("getEmptyPreamble", Foo,
+                    [&](llvm::Expected<InputsAndPreamble> Preamble) {
+                      // We expect to get an empty preamble.
+                      ASSERT_TRUE(bool(Preamble));
+                      EXPECT_EQ(Preamble->Preamble->Preamble.getBounds().Size,
+                                0u);
+                    });
+}
+
 TEST_F(TUSchedulerTests, RunWaitsForPreamble) {
   // Testing strategy: we update the file and schedule a few preamble reads at
   // the same time. All reads should get the same non-null preamble.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50627.160318.patch
Type: text/x-patch
Size: 2255 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180813/a1e7595e/attachment.bin>


More information about the cfe-commits mailing list