Skip to content

Commit

Permalink
Merge pull request #2363 from ruricolist/sort-includes
Browse files Browse the repository at this point in the history
Sort includes when generating multiple files
  • Loading branch information
b-scholz committed Nov 29, 2022
2 parents 2b05b9c + 1dd3cc8 commit 5e232fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/synthesiser/GenDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,16 @@ std::string GenDb::emitMultipleFilesInDir(fs::path dir, std::vector<fs::path>& t
auto genHeader = [&](std::ofstream& hpp, std::ofstream& cpp, GenFile& gen) {
hpp << "#pragma once\n";
globalHeader(hpp);
for (const std::string& inc : gen.getDeclIncludes()) {
for (const std::string& inc : gen.getSortedDeclIncludes()) {
hpp << "#include " << inc << "\n";
}
for (GenFile* dep : gen.getDeclDependencies()) {
for (GenFile* dep : gen.getSortedDeclDependencies()) {
hpp << "#include " << dep->getHeader() << "\n";
}
for (const std::string& inc : gen.getIncludes()) {
for (const std::string& inc : gen.getSortedIncludes()) {
cpp << "#include " << inc << "\n";
}
for (GenFile* dep : gen.getDependencies()) {
for (GenFile* dep : gen.getSortedDependencies()) {
cpp << "#include " << dep->getHeader() << "\n";
}
cpp << "#include " << gen.getHeader() << "\n";
Expand Down
28 changes: 27 additions & 1 deletion src/synthesiser/GenDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include "souffle/utility/Types.h"
#include <algorithm>
#include <cassert>
#include <filesystem>
#include <iostream>
Expand Down Expand Up @@ -47,7 +48,7 @@ class GenFile {
return basename;
};

fs::path getHeader() {
fs::path getHeader() const {
fs::path header = basename;
return header.concat(".hpp");
}
Expand All @@ -70,15 +71,40 @@ class GenFile {
std::set<std::string>& getDeclIncludes() {
return decl_includes;
}
std::vector<std::string> getSortedDeclIncludes() {
std::vector<std::string> v{decl_includes.begin(), decl_includes.end()};
std::sort(v.begin(), v.end());
return v;
}

std::set<std::string>& getIncludes() {
return includes;
}
std::vector<std::string> getSortedIncludes() {
std::vector<std::string> v{includes.begin(), includes.end()};
std::sort(v.begin(), v.end());
return v;
}

std::set<GenFile*>& getDeclDependencies() {
return decl_dependencies;
}
std::vector<GenFile*> getSortedDeclDependencies() {
std::vector<GenFile*> v{decl_dependencies.begin(), decl_dependencies.end()};
std::sort(v.begin(), v.end(),
[](const GenFile* a, const GenFile* b) { return a->getHeader() < b->getHeader(); });
return v;
}

std::set<GenFile*>& getDependencies() {
return dependencies;
}
std::vector<GenFile*> getSortedDependencies() {
std::vector<GenFile*> v{dependencies.begin(), dependencies.end()};
std::sort(v.begin(), v.end(),
[](const GenFile* a, const GenFile* b) { return a->getHeader() < b->getHeader(); });
return v;
}

private:
fs::path basename;
Expand Down

0 comments on commit 5e232fd

Please sign in to comment.