diff options
author | nullableVoidPtr <30564701+nullableVoidPtr@users.noreply.github.com> | 2019-08-04 10:18:13 +0000 |
---|---|---|
committer | nullableVoidPtr <30564701+nullableVoidPtr@users.noreply.github.com> | 2019-08-04 10:18:13 +0000 |
commit | ba6f243c728de5d5c024aeb177026bcc59909e2e (patch) | |
tree | af8fdeb1c6cdf9cd8584f0d693a4049bfc408b9d /tools/scaninc | |
parent | 6211b0c5ecbe4a43aa3f6e8fd96e99af29caa77a (diff) | |
parent | 250a331df9dbd312d572aaf0d629503417cfc9d4 (diff) |
Forgot upstream tools tracking
Diffstat (limited to 'tools/scaninc')
-rw-r--r-- | tools/scaninc/Makefile | 11 | ||||
-rw-r--r-- | tools/scaninc/asm_file.cpp | 3 | ||||
-rw-r--r-- | tools/scaninc/scaninc.cpp | 88 | ||||
-rw-r--r-- | tools/scaninc/source_file.cpp | 125 | ||||
-rw-r--r-- | tools/scaninc/source_file.h | 71 |
5 files changed, 224 insertions, 74 deletions
diff --git a/tools/scaninc/Makefile b/tools/scaninc/Makefile index 32c16c3..53c9d00 100644 --- a/tools/scaninc/Makefile +++ b/tools/scaninc/Makefile @@ -1,15 +1,12 @@ CXX = g++ -CXXFLAGS = -Wall -Werror -std=c++11 -O2 -s +CXXFLAGS = -Wall -Werror -std=c++11 -O2 -SRCS = scaninc.cpp c_file.cpp asm_file.cpp +SRCS = scaninc.cpp c_file.cpp asm_file.cpp source_file.cpp -HEADERS := scaninc.h asm_file.h c_file.h +HEADERS := scaninc.h asm_file.h c_file.h source_file.h -.PHONY: all clean - -all: scaninc - @: +.PHONY: clean scaninc: $(SRCS) $(HEADERS) $(CXX) $(CXXFLAGS) $(SRCS) -o $@ $(LDFLAGS) diff --git a/tools/scaninc/asm_file.cpp b/tools/scaninc/asm_file.cpp index 6322749..109e604 100644 --- a/tools/scaninc/asm_file.cpp +++ b/tools/scaninc/asm_file.cpp @@ -64,7 +64,8 @@ IncDirectiveType AsmFile::ReadUntilIncDirective(std::string &path) IncDirectiveType incDirectiveType = IncDirectiveType::None; - if (PeekChar() == '.') + char c = PeekChar(); + if (c == '.' || c == '#') { m_pos++; diff --git a/tools/scaninc/scaninc.cpp b/tools/scaninc/scaninc.cpp index 3dc2214..b95cbd0 100644 --- a/tools/scaninc/scaninc.cpp +++ b/tools/scaninc/scaninc.cpp @@ -25,8 +25,7 @@ #include <set> #include <string> #include "scaninc.h" -#include "asm_file.h" -#include "c_file.h" +#include "source_file.h" bool CanOpenFile(std::string path) { @@ -46,7 +45,7 @@ int main(int argc, char **argv) std::queue<std::string> filesToProcess; std::set<std::string> dependencies; - std::list<std::string> includeDirs; + std::vector<std::string> includeDirs; argc--; argv++; @@ -63,7 +62,7 @@ int main(int argc, char **argv) argv++; includeDir = std::string(argv[0]); } - if (includeDir.back() != '/') + if (!includeDir.empty() && includeDir.back() != '/') { includeDir += '/'; } @@ -83,79 +82,36 @@ int main(int argc, char **argv) std::string initialPath(argv[0]); - std::size_t pos = initialPath.find_last_of('.'); + filesToProcess.push(initialPath); - if (pos == std::string::npos) - FATAL_ERROR("no file extension in path \"%s\"\n", initialPath.c_str()); - - std::string extension = initialPath.substr(pos + 1); - - std::string srcDir(""); - std::size_t slash = initialPath.rfind('/'); - if (slash != std::string::npos) - { - srcDir = initialPath.substr(0, slash + 1); - } - includeDirs.push_back(srcDir); - - if (extension == "c" || extension == "h") + while (!filesToProcess.empty()) { - filesToProcess.push(initialPath); + std::string filePath = filesToProcess.front(); + SourceFile file(filePath); + filesToProcess.pop(); - while (!filesToProcess.empty()) + includeDirs.push_back(file.GetSrcDir()); + for (auto incbin : file.GetIncbins()) { - CFile file(filesToProcess.front()); - filesToProcess.pop(); - - file.FindIncbins(); - for (auto incbin : file.GetIncbins()) - { - dependencies.insert(incbin); - } - for (auto include : file.GetIncludes()) + dependencies.insert(incbin); + } + for (auto include : file.GetIncludes()) + { + for (auto includeDir : includeDirs) { - for (auto includeDir : includeDirs) + std::string path(includeDir + include); + if (CanOpenFile(path)) { - std::string path(includeDir + include); - if (CanOpenFile(path)) + bool inserted = dependencies.insert(path).second; + if (inserted) { - bool inserted = dependencies.insert(path).second; - if (inserted) - { - filesToProcess.push(path); - } - break; + filesToProcess.push(path); } + break; } } } - } - else if (extension == "s" || extension == "inc") - { - filesToProcess.push(initialPath); - - while (!filesToProcess.empty()) - { - AsmFile file(filesToProcess.front()); - - filesToProcess.pop(); - - IncDirectiveType incDirectiveType; - std::string path; - - while ((incDirectiveType = file.ReadUntilIncDirective(path)) != IncDirectiveType::None) - { - bool inserted = dependencies.insert(path).second; - if (inserted - && incDirectiveType == IncDirectiveType::Include - && CanOpenFile(path)) - filesToProcess.push(path); - } - } - } - else - { - FATAL_ERROR("unknown extension \"%s\"\n", extension.c_str()); + includeDirs.pop_back(); } for (const std::string &path : dependencies) diff --git a/tools/scaninc/source_file.cpp b/tools/scaninc/source_file.cpp new file mode 100644 index 0000000..f23ff6d --- /dev/null +++ b/tools/scaninc/source_file.cpp @@ -0,0 +1,125 @@ +// Copyright(c) 2019 Phlosioneer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include <new> +#include "source_file.h" + + +SourceFileType GetFileType(std::string& path) +{ + std::size_t pos = path.find_last_of('.'); + + if (pos == std::string::npos) + FATAL_ERROR("no file extension in path \"%s\"\n", path.c_str()); + + std::string extension = path.substr(pos + 1); + + if (extension == "c") + return SourceFileType::Cpp; + else if (extension == "s") + return SourceFileType::Asm; + else if (extension == "h") + return SourceFileType::Header; + else if (extension == "inc") + return SourceFileType::Inc; + else + FATAL_ERROR("Unrecognized extension \"%s\"\n", extension.c_str()); + + // Unreachable + return SourceFileType::Cpp; +} + +std::string GetDir(std::string& path) +{ + std::size_t slash = path.rfind('/'); + + if (slash != std::string::npos) + return path.substr(0, slash + 1); + else + return std::string(""); +} + +SourceFile::SourceFile(std::string path) +{ + m_file_type = GetFileType(path); + + m_src_dir = GetDir(path); + + if (m_file_type == SourceFileType::Cpp + || m_file_type == SourceFileType::Header) + { + new (&m_source_file.c_file) CFile(path); + m_source_file.c_file.FindIncbins(); + } + else + { + AsmFile file(path); + std::set<std::string> incbins; + std::set<std::string> includes; + + IncDirectiveType incDirectiveType; + std::string outputPath; + + while ((incDirectiveType = file.ReadUntilIncDirective(outputPath)) != IncDirectiveType::None) + { + if (incDirectiveType == IncDirectiveType::Include) + includes.insert(outputPath); + else + incbins.insert(outputPath); + } + + new (&m_source_file.asm_wrapper) SourceFile::InnerUnion::AsmWrapper{incbins, includes}; + } +} + +SourceFile::~SourceFile() +{ + if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) + { + m_source_file.c_file.~CFile(); + } + else + { + m_source_file.asm_wrapper.asm_incbins.~set(); + m_source_file.asm_wrapper.asm_includes.~set(); + } +} + +const std::set<std::string>& SourceFile::GetIncbins() +{ + if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) + return m_source_file.c_file.GetIncbins(); + else + return m_source_file.asm_wrapper.asm_incbins; +} + +const std::set<std::string>& SourceFile::GetIncludes() +{ + if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) + return m_source_file.c_file.GetIncludes(); + else + return m_source_file.asm_wrapper.asm_includes; +} + +std::string& SourceFile::GetSrcDir() +{ + return m_src_dir; +} + diff --git a/tools/scaninc/source_file.h b/tools/scaninc/source_file.h new file mode 100644 index 0000000..f7b6412 --- /dev/null +++ b/tools/scaninc/source_file.h @@ -0,0 +1,71 @@ +// Copyright(c) 2019 Phlosioneer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef SOURCE_FILE_H +#define SOURCE_FILE_H + +#include <string> +#include "scaninc.h" +#include "asm_file.h" +#include "c_file.h" + +enum class SourceFileType +{ + Cpp, + Header, + Asm, + Inc +}; + +SourceFileType GetFileType(std::string& path); + +class SourceFile +{ +public: + + SourceFile(std::string path); + ~SourceFile(); + SourceFile(SourceFile const&) = delete; + SourceFile(SourceFile&&) = delete; + SourceFile& operator =(SourceFile const&) = delete; + SourceFile& operator =(SourceFile&&) = delete; + bool HasIncbins(); + const std::set<std::string>& GetIncbins(); + const std::set<std::string>& GetIncludes(); + std::string& GetSrcDir(); + +private: + union InnerUnion { + CFile c_file; + struct AsmWrapper { + std::set<std::string> asm_incbins; + std::set<std::string> asm_includes; + } asm_wrapper; + + // Construction and destruction handled by SourceFile. + InnerUnion() {}; + ~InnerUnion() {}; + } m_source_file; + SourceFileType m_file_type; + std::string m_src_dir; +}; + +#endif // SOURCE_FILE_H + |