blob: 2185a0bd14db46701b32faf606f234b9601c0810 [file] [log] [blame]
Ben Clayton05e84592019-12-02 20:08:31 +00001// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "File.hpp"
16
17#include <mutex>
18#include <unordered_set>
19
20namespace
21{
22
23////////////////////////////////////////////////////////////////////////////////
24// FileBase
25////////////////////////////////////////////////////////////////////////////////
26class FileBase : public vk::dbg::File
27{
28public:
29 void clearBreakpoints() override;
30 void addBreakpoint(int line) override;
31 bool hasBreakpoint(int line) const override;
32
33protected:
34 FileBase(ID id, std::string dir, std::string name, std::string source);
35
36private:
37 mutable std::mutex breakpointMutex;
38 std::unordered_set<int> breakpoints; // guarded by breakpointMutex
39};
40
41FileBase::FileBase(ID id, std::string dir, std::string name, std::string source) :
42 File(id, std::move(dir), std::move(name), std::move(source)) {}
43
44void FileBase::clearBreakpoints()
45{
46 std::unique_lock<std::mutex> lock(breakpointMutex);
47 breakpoints.clear();
48}
49
50void FileBase::addBreakpoint(int line)
51{
52 std::unique_lock<std::mutex> lock(breakpointMutex);
53 breakpoints.emplace(line);
54}
55
56bool FileBase::hasBreakpoint(int line) const
57{
58 std::unique_lock<std::mutex> lock(breakpointMutex);
59 return breakpoints.count(line) > 0;
60}
61
62////////////////////////////////////////////////////////////////////////////////
63// VirtualFile
64////////////////////////////////////////////////////////////////////////////////
65class VirtualFile : public FileBase
66{
67public:
68 VirtualFile(ID id, std::string name, std::string source);
69
70 bool isVirtual() const override;
71
72private:
73};
74
75VirtualFile::VirtualFile(ID id, std::string name, std::string source) :
76 FileBase(id, "", std::move(name), std::move(source)) {}
77
78bool VirtualFile::isVirtual() const
79{
80 return true;
81}
82
83////////////////////////////////////////////////////////////////////////////////
84// PhysicalFile
85////////////////////////////////////////////////////////////////////////////////
86struct PhysicalFile : public FileBase
87{
88 PhysicalFile(ID id,
89 std::string dir,
90 std::string name);
91
92 bool isVirtual() const override;
93};
94
95PhysicalFile::PhysicalFile(ID id,
96 std::string dir,
97 std::string name) :
98 FileBase(id, std::move(dir), std::move(name), "") {}
99
100bool PhysicalFile::isVirtual() const
101{
102 return false;
103}
104
105} // anonymous namespace
106
107namespace vk
108{
109namespace dbg
110{
111
112std::shared_ptr<File> File::createVirtual(ID id, std::string name, std::string source)
113{
114 return std::make_shared<VirtualFile>(id, std::move(name), std::move(source));
115}
116
117std::shared_ptr<File> File::createPhysical(ID id, std::string path)
118{
119 auto pathstr = path;
120 auto pos = pathstr.rfind("/");
121 if(pos != std::string::npos)
122 {
123 auto dir = pathstr.substr(0, pos);
124 auto name = pathstr.substr(pos + 1);
125 return std::make_shared<PhysicalFile>(id, dir.c_str(), name.c_str());
126 }
127 return std::make_shared<PhysicalFile>(id, "", std::move(path));
128}
129
130} // namespace dbg
131} // namespace vk