Merge pull request #2997 from RandomShaper/remove_unused
[platform/upstream/glslang.git] / StandAlone / DirStackFileIncluder.h
1 //
2 // Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 // Copyright (C) 2017 Google, Inc.
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //    Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 //
14 //    Redistributions in binary form must reproduce the above
15 //    copyright notice, this list of conditions and the following
16 //    disclaimer in the documentation and/or other materials provided
17 //    with the distribution.
18 //
19 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 //    contributors may be used to endorse or promote products derived
21 //    from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
35 //
36
37 #pragma once
38
39 #include <vector>
40 #include <string>
41 #include <fstream>
42 #include <algorithm>
43 #include <set>
44
45 #include "./../glslang/Public/ShaderLang.h"
46
47 // Default include class for normal include convention of search backward
48 // through the stack of active include paths (for nested includes).
49 // Can be overridden to customize.
50 class DirStackFileIncluder : public glslang::TShader::Includer {
51 public:
52     DirStackFileIncluder() : externalLocalDirectoryCount(0) { }
53
54     virtual IncludeResult* includeLocal(const char* headerName,
55                                         const char* includerName,
56                                         size_t inclusionDepth) override
57     {
58         return readLocalPath(headerName, includerName, (int)inclusionDepth);
59     }
60
61     virtual IncludeResult* includeSystem(const char* headerName,
62                                          const char* /*includerName*/,
63                                          size_t /*inclusionDepth*/) override
64     {
65         return readSystemPath(headerName);
66     }
67
68     // Externally set directories. E.g., from a command-line -I<dir>.
69     //  - Most-recently pushed are checked first.
70     //  - All these are checked after the parse-time stack of local directories
71     //    is checked.
72     //  - This only applies to the "local" form of #include.
73     //  - Makes its own copy of the path.
74     virtual void pushExternalLocalDirectory(const std::string& dir)
75     {
76         directoryStack.push_back(dir);
77         externalLocalDirectoryCount = (int)directoryStack.size();
78     }
79
80     virtual void releaseInclude(IncludeResult* result) override
81     {
82         if (result != nullptr) {
83             delete [] static_cast<tUserDataElement*>(result->userData);
84             delete result;
85         }
86     }
87
88     virtual std::set<std::string> getIncludedFiles()
89     {
90         return includedFiles;
91     }
92
93     virtual ~DirStackFileIncluder() override { }
94
95 protected:
96     typedef char tUserDataElement;
97     std::vector<std::string> directoryStack;
98     int externalLocalDirectoryCount;
99     std::set<std::string> includedFiles;
100
101     // Search for a valid "local" path based on combining the stack of include
102     // directories and the nominal name of the header.
103     virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth)
104     {
105         // Discard popped include directories, and
106         // initialize when at parse-time first level.
107         directoryStack.resize(depth + externalLocalDirectoryCount);
108         if (depth == 1)
109             directoryStack.back() = getDirectory(includerName);
110
111         // Find a directory that works, using a reverse search of the include stack.
112         for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) {
113             std::string path = *it + '/' + headerName;
114             std::replace(path.begin(), path.end(), '\\', '/');
115             std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
116             if (file) {
117                 directoryStack.push_back(getDirectory(path));
118                 includedFiles.insert(path);
119                 return newIncludeResult(path, file, (int)file.tellg());
120             }
121         }
122
123         return nullptr;
124     }
125
126     // Search for a valid <system> path.
127     // Not implemented yet; returning nullptr signals failure to find.
128     virtual IncludeResult* readSystemPath(const char* /*headerName*/) const
129     {
130         return nullptr;
131     }
132
133     // Do actual reading of the file, filling in a new include result.
134     virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
135     {
136         char* content = new tUserDataElement [length];
137         file.seekg(0, file.beg);
138         file.read(content, length);
139         return new IncludeResult(path, content, length, content);
140     }
141
142     // If no path markers, return current working directory.
143     // Otherwise, strip file name and return path leading up to it.
144     virtual std::string getDirectory(const std::string path) const
145     {
146         size_t last = path.find_last_of("/\\");
147         return last == std::string::npos ? "." : path.substr(0, last);
148     }
149 };