Skip to content

Commit

Permalink
Add total LOC to debug output (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Mar 17, 2024
1 parent 2ae1c28 commit c1aed78
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
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

0 comments on commit c1aed78

Please sign in to comment.