[clang-tools-extra] r352501 - [clangd] Unit test for sourceLocationInMainFile.

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 29 06:31:19 PST 2019


Author: ibiryukov
Date: Tue Jan 29 06:31:19 2019
New Revision: 352501

URL: http://llvm.org/viewvc/llvm-project?rev=352501&view=rev
Log:
[clangd] Unit test for sourceLocationInMainFile.

This should have been part of r352494, which added the corresponding
function. The unit test ended up as a separate commit accidentally.

Modified:
    clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp

Modified: clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp?rev=352501&r1=352500&r2=352501&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/SourceCodeTests.cpp Tue Jan 29 06:31:19 2019
@@ -5,6 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+#include "Annotations.h"
 #include "SourceCode.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/raw_os_ostream.h"
@@ -16,6 +17,9 @@ namespace clang {
 namespace clangd {
 namespace {
 
+using llvm::Failed;
+using llvm::HasValue;
+
 MATCHER_P2(Pos, Line, Col, "") {
   return arg.line == Line && arg.character == Col;
 }
@@ -140,6 +144,38 @@ TEST(SourceCodeTests, IsRangeConsecutive
       isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 4}, {2, 5})));
 }
 
+TEST(SourceCodeTests, SourceLocationInMainFile) {
+  Annotations Source(R"cpp(
+    ^in^t ^foo
+    ^bar
+    ^baz ^() {}  {} {} {} { }^
+)cpp");
+
+  SourceManagerForFile Owner("foo.cpp", Source.code());
+  SourceManager &SM = Owner.get();
+
+  SourceLocation StartOfFile = SM.getLocForStartOfFile(SM.getMainFileID());
+  EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 0)),
+                       HasValue(StartOfFile));
+  // End of file.
+  EXPECT_THAT_EXPECTED(
+      sourceLocationInMainFile(SM, position(4, 0)),
+      HasValue(StartOfFile.getLocWithOffset(Source.code().size())));
+  // Column number is too large.
+  EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 1)), Failed());
+  EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 100)),
+                       Failed());
+  EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(4, 1)), Failed());
+  // Line number is too large.
+  EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(5, 0)), Failed());
+  // Check all positions mentioned in the test return valid results.
+  for (auto P : Source.points()) {
+    size_t Offset = llvm::cantFail(positionToOffset(Source.code(), P));
+    EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, P),
+                         HasValue(StartOfFile.getLocWithOffset(Offset)));
+  }
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang




More information about the cfe-commits mailing list