Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add total LOC to debug output #499

Merged
merged 1 commit into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/SourceFile.cpp
Expand Up @@ -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";
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/global/GlobalResourceManager.cpp
Expand Up @@ -4,6 +4,7 @@

#include <SourceFile.h>
#include <ast/ASTNodes.h>
#include <util/FileUtil.h>

#include <llvm/MC/TargetRegistry.h>
#include <llvm/Support/TargetSelect.h>
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions src/global/GlobalResourceManager.h
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/util/FileUtil.cpp
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions src/util/FileUtil.h
Expand Up @@ -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();
Expand Down