xref: /llvm-project/lldb/unittests/Core/SourceManagerTest.cpp (revision ab40ae8ff9f87b6e3d68cab2c47d692016ede958)
1 //===-- SourceManagerTest.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/SourceManager.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Utility/SupportFile.h"
12 #include "gtest/gtest.h"
13 
14 #include "TestingSupport/MockTildeExpressionResolver.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 class SourceFileCache : public ::testing::Test {
20 public:
21   void SetUp() override {
22     FileSystem::Initialize(std::unique_ptr<TildeExpressionResolver>(
23         new MockTildeExpressionResolver("Jonas", "/jonas")));
24   }
25   void TearDown() override { FileSystem::Terminate(); }
26 };
27 
28 TEST_F(SourceFileCache, FindSourceFileFound) {
29   SourceManager::SourceFileCache cache;
30 
31   // Insert: foo
32   FileSpec foo_file_spec("foo");
33   auto foo_file_sp = std::make_shared<SourceManager::File>(
34       std::make_shared<SupportFile>(foo_file_spec), lldb::DebuggerSP());
35   cache.AddSourceFile(foo_file_spec, foo_file_sp);
36 
37   // Query: foo, expect found.
38   FileSpec another_foo_file_spec("foo");
39   ASSERT_EQ(cache.FindSourceFile(another_foo_file_spec), foo_file_sp);
40 }
41 
42 TEST_F(SourceFileCache, FindSourceFileNotFound) {
43   SourceManager::SourceFileCache cache;
44 
45   // Insert: foo
46   FileSpec foo_file_spec("foo");
47   auto foo_file_sp = std::make_shared<SourceManager::File>(
48       std::make_shared<SupportFile>(foo_file_spec), lldb::DebuggerSP());
49   cache.AddSourceFile(foo_file_spec, foo_file_sp);
50 
51   // Query: bar, expect not found.
52   FileSpec bar_file_spec("bar");
53   ASSERT_EQ(cache.FindSourceFile(bar_file_spec), nullptr);
54 }
55 
56 TEST_F(SourceFileCache, FindSourceFileByUnresolvedPath) {
57   SourceManager::SourceFileCache cache;
58 
59   FileSpec foo_file_spec("~/foo");
60 
61   // Mimic the resolution in SourceManager::GetFile.
62   FileSpec resolved_foo_file_spec = foo_file_spec;
63   FileSystem::Instance().Resolve(resolved_foo_file_spec);
64 
65   // Create the file with the resolved file spec.
66   auto foo_file_sp = std::make_shared<SourceManager::File>(
67       std::make_shared<SupportFile>(resolved_foo_file_spec),
68       lldb::DebuggerSP());
69 
70   // Cache the result with the unresolved file spec.
71   cache.AddSourceFile(foo_file_spec, foo_file_sp);
72 
73   // Query the unresolved path.
74   EXPECT_EQ(cache.FindSourceFile(FileSpec("~/foo")), foo_file_sp);
75 
76   // Query the resolved path.
77   EXPECT_EQ(cache.FindSourceFile(FileSpec("/jonas/foo")), foo_file_sp);
78 }
79