Ben Clayton | 05e8459 | 2019-12-02 20:08:31 +0000 | [diff] [blame] | 1 | // 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 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | //////////////////////////////////////////////////////////////////////////////// |
| 24 | // FileBase |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
| 26 | class FileBase : public vk::dbg::File |
| 27 | { |
| 28 | public: |
| 29 | void clearBreakpoints() override; |
| 30 | void addBreakpoint(int line) override; |
| 31 | bool hasBreakpoint(int line) const override; |
| 32 | |
| 33 | protected: |
| 34 | FileBase(ID id, std::string dir, std::string name, std::string source); |
| 35 | |
| 36 | private: |
| 37 | mutable std::mutex breakpointMutex; |
| 38 | std::unordered_set<int> breakpoints; // guarded by breakpointMutex |
| 39 | }; |
| 40 | |
| 41 | FileBase::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 | |
| 44 | void FileBase::clearBreakpoints() |
| 45 | { |
| 46 | std::unique_lock<std::mutex> lock(breakpointMutex); |
| 47 | breakpoints.clear(); |
| 48 | } |
| 49 | |
| 50 | void FileBase::addBreakpoint(int line) |
| 51 | { |
| 52 | std::unique_lock<std::mutex> lock(breakpointMutex); |
| 53 | breakpoints.emplace(line); |
| 54 | } |
| 55 | |
| 56 | bool 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 | //////////////////////////////////////////////////////////////////////////////// |
| 65 | class VirtualFile : public FileBase |
| 66 | { |
| 67 | public: |
| 68 | VirtualFile(ID id, std::string name, std::string source); |
| 69 | |
| 70 | bool isVirtual() const override; |
| 71 | |
| 72 | private: |
| 73 | }; |
| 74 | |
| 75 | VirtualFile::VirtualFile(ID id, std::string name, std::string source) : |
| 76 | FileBase(id, "", std::move(name), std::move(source)) {} |
| 77 | |
| 78 | bool VirtualFile::isVirtual() const |
| 79 | { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | //////////////////////////////////////////////////////////////////////////////// |
| 84 | // PhysicalFile |
| 85 | //////////////////////////////////////////////////////////////////////////////// |
| 86 | struct PhysicalFile : public FileBase |
| 87 | { |
| 88 | PhysicalFile(ID id, |
| 89 | std::string dir, |
| 90 | std::string name); |
| 91 | |
| 92 | bool isVirtual() const override; |
| 93 | }; |
| 94 | |
| 95 | PhysicalFile::PhysicalFile(ID id, |
| 96 | std::string dir, |
| 97 | std::string name) : |
| 98 | FileBase(id, std::move(dir), std::move(name), "") {} |
| 99 | |
| 100 | bool PhysicalFile::isVirtual() const |
| 101 | { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | } // anonymous namespace |
| 106 | |
| 107 | namespace vk |
| 108 | { |
| 109 | namespace dbg |
| 110 | { |
| 111 | |
| 112 | std::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 | |
| 117 | std::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 |