From 986edc54cd8fae8f9b05f5d3ad3b670f6547bbbc Mon Sep 17 00:00:00 2001 From: Henny022p Date: Tue, 23 Nov 2021 06:36:40 +0100 Subject: moved scaninc --- tools/src/scaninc/source_file.cpp | 125 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100755 tools/src/scaninc/source_file.cpp (limited to 'tools/src/scaninc/source_file.cpp') diff --git a/tools/src/scaninc/source_file.cpp b/tools/src/scaninc/source_file.cpp new file mode 100755 index 00000000..f23ff6db --- /dev/null +++ b/tools/src/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 +#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 incbins; + std::set 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& 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& 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; +} + -- cgit v1.2.3 From dee773da4a12af1c48bf58d6275fe4fed20871b6 Mon Sep 17 00:00:00 2001 From: Henny022p Date: Tue, 23 Nov 2021 09:31:09 +0100 Subject: apply clang-format to tool sources --- tools/src/scaninc/source_file.cpp | 47 +++++++++++++-------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) mode change 100755 => 100644 tools/src/scaninc/source_file.cpp (limited to 'tools/src/scaninc/source_file.cpp') diff --git a/tools/src/scaninc/source_file.cpp b/tools/src/scaninc/source_file.cpp old mode 100755 new mode 100644 index f23ff6db..12daa0c6 --- a/tools/src/scaninc/source_file.cpp +++ b/tools/src/scaninc/source_file.cpp @@ -21,9 +21,7 @@ #include #include "source_file.h" - -SourceFileType GetFileType(std::string& path) -{ +SourceFileType GetFileType(std::string& path) { std::size_t pos = path.find_last_of('.'); if (pos == std::string::npos) @@ -41,13 +39,12 @@ SourceFileType GetFileType(std::string& path) return SourceFileType::Inc; else FATAL_ERROR("Unrecognized extension \"%s\"\n", extension.c_str()); - + // Unreachable return SourceFileType::Cpp; } -std::string GetDir(std::string& path) -{ +std::string GetDir(std::string& path) { std::size_t slash = path.rfind('/'); if (slash != std::string::npos) @@ -56,20 +53,15 @@ std::string GetDir(std::string& path) return std::string(""); } -SourceFile::SourceFile(std::string path) -{ +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) - { + 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 - { + } else { AsmFile file(path); std::set incbins; std::set includes; @@ -77,49 +69,40 @@ SourceFile::SourceFile(std::string path) IncDirectiveType incDirectiveType; std::string outputPath; - while ((incDirectiveType = file.ReadUntilIncDirective(outputPath)) != IncDirectiveType::None) - { + 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}; + + new (&m_source_file.asm_wrapper) SourceFile::InnerUnion::AsmWrapper{ incbins, includes }; } } -SourceFile::~SourceFile() -{ - if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) - { +SourceFile::~SourceFile() { + if (m_file_type == SourceFileType::Cpp || m_file_type == SourceFileType::Header) { m_source_file.c_file.~CFile(); - } - else - { + } else { m_source_file.asm_wrapper.asm_incbins.~set(); m_source_file.asm_wrapper.asm_includes.~set(); } } -const std::set& SourceFile::GetIncbins() -{ +const std::set& 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& SourceFile::GetIncludes() -{ +const std::set& 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() -{ +std::string& SourceFile::GetSrcDir() { return m_src_dir; } - -- cgit v1.2.3 From 1c932ffea86160bd5f71f7e5523b6d215dad4543 Mon Sep 17 00:00:00 2001 From: Henny022p Date: Tue, 30 Nov 2021 06:44:30 +0100 Subject: improve scaninc error messages --- tools/src/scaninc/source_file.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools/src/scaninc/source_file.cpp') diff --git a/tools/src/scaninc/source_file.cpp b/tools/src/scaninc/source_file.cpp index 12daa0c6..0cefc9f1 100644 --- a/tools/src/scaninc/source_file.cpp +++ b/tools/src/scaninc/source_file.cpp @@ -25,7 +25,7 @@ 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()); + fatal_error("no file extension in path \"%s\"\n", path.c_str()); std::string extension = path.substr(pos + 1); @@ -38,7 +38,7 @@ SourceFileType GetFileType(std::string& path) { else if (extension == "inc") return SourceFileType::Inc; else - FATAL_ERROR("Unrecognized extension \"%s\"\n", extension.c_str()); + fatal_error("Unrecognized extension \"%s\"\n", extension.c_str()); // Unreachable return SourceFileType::Cpp; -- cgit v1.2.3