From c1aed78260e69f3e77c9aff27d548c5e23c93322 Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Mon, 18 Mar 2024 00:54:32 +0100 Subject: [PATCH] Add total LOC to debug output (#499) --- src/SourceFile.cpp | 3 ++- src/global/GlobalResourceManager.cpp | 6 ++++++ src/global/GlobalResourceManager.h | 1 + src/util/FileUtil.cpp | 18 ++++++++++++++++++ src/util/FileUtil.h | 1 + 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/SourceFile.cpp b/src/SourceFile.cpp index f3aff6dd6..74c0e1195 100644 --- a/src/SourceFile.cpp +++ b/src/SourceFile.cpp @@ -542,7 +542,8 @@ void SourceFile::runBackEnd() { // NOLINT(misc-no-recursion) resourceManager.totalTimer.stop(); if (resourceManager.cliOptions.printDebugOutput) { CHECK_ABORT_FLAG_V() - std::cout << "\nSuccessfully compiled " << std::to_string(resourceManager.sourceFiles.size()) << " source file(s).\n"; + std::cout << "\nSuccessfully compiled " << std::to_string(resourceManager.sourceFiles.size()) << " source file(s)"; + std::cout << " or " << std::to_string(resourceManager.getTotalLineCount()) << " lines in total.\n"; std::cout << "Total compile time: " << std::to_string(resourceManager.totalTimer.getDurationMilliseconds()) << " ms\n"; } } diff --git a/src/global/GlobalResourceManager.cpp b/src/global/GlobalResourceManager.cpp index 42ab3e0c2..c6710fe1a 100644 --- a/src/global/GlobalResourceManager.cpp +++ b/src/global/GlobalResourceManager.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -78,4 +79,9 @@ SourceFile *GlobalResourceManager::createSourceFile(SourceFile *parent, const st uint64_t GlobalResourceManager::getNextCustomTypeId() { return nextCustomTypeId++; } +size_t GlobalResourceManager::getTotalLineCount() const { + const auto acc = [](size_t sum, const auto &sourceFile) { return sum + FileUtil::getLineCount(sourceFile.second->filePath); }; + return std::accumulate(sourceFiles.begin(), sourceFiles.end(), 0, acc); +} + } // namespace spice::compiler \ No newline at end of file diff --git a/src/global/GlobalResourceManager.h b/src/global/GlobalResourceManager.h index cc03acb2c..8852499a1 100644 --- a/src/global/GlobalResourceManager.h +++ b/src/global/GlobalResourceManager.h @@ -43,6 +43,7 @@ class GlobalResourceManager { // Public methods SourceFile *createSourceFile(SourceFile *parent, const std::string &depName, const std::filesystem::path &path, bool isStdFile); uint64_t getNextCustomTypeId(); + size_t getTotalLineCount() const; // Public members llvm::LLVMContext context; diff --git a/src/util/FileUtil.cpp b/src/util/FileUtil.cpp index ab98f75f9..abe81c78f 100644 --- a/src/util/FileUtil.cpp +++ b/src/util/FileUtil.cpp @@ -43,6 +43,24 @@ std::string FileUtil::getFileContent(const std::filesystem::path &filePath) { return stringStream.str(); } +/** + * Retrieve the number of lines of a file + * + * @param filePath File path + * @return Number of lines + */ +size_t FileUtil::getLineCount(const std::filesystem::path &filePath) { + std::ifstream file(filePath); + if (!file) + throw CompilerError(IO_ERROR, "Failed to open file: " + filePath.string()); + size_t lineCount = 0; + std::string line; + while (std::getline(file, line)) + lineCount++; + file.close(); + return lineCount; +} + /** * Execute external command. Used to execute compiled binaries * diff --git a/src/util/FileUtil.h b/src/util/FileUtil.h index 583bb1269..e3e09fcf9 100644 --- a/src/util/FileUtil.h +++ b/src/util/FileUtil.h @@ -19,6 +19,7 @@ class FileUtil { public: static void writeToFile(const std::filesystem::path &fileName, const std::string &fileContent); static std::string getFileContent(const std::filesystem::path &filePath); + static size_t getLineCount(const std::filesystem::path &filePath); static ExecResult exec(const std::string &command); static bool isCommandAvailable(const std::string &cmd); static bool isGraphvizInstalled();