Skip to content

Commit

Permalink
Merge branch 'feat/50' into 'devel'
Browse files Browse the repository at this point in the history
File format information for MCGWriter / Reader

Closes #50

See merge request tuda-sc/projects/metacg!63
  • Loading branch information
jplehr committed Apr 1, 2022
2 parents d59cde8 + fc40fe5 commit de6f3ab
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 103 deletions.
15 changes: 13 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ mcg-container:
script:
- podman login ${CI_REGISTRY} -u ${CI_CONTAINER_REG_USER} -p ${CONTAINER_REGISTRY_READ_TOKEN}
- podman build -t registry.git.rwth-aachen.de/tuda-sc/projects/metacg/metacg:$CI_COMMIT_SHA -f container/metacg .
- podman run --rm -t metacg:$CI_COMMIT_SHA /opt/metacg/metacg/build/pgis/test/unit/basictests
- podman run --rm -t metacg:$CI_COMMIT_SHA /opt/metacg/metacg/build/pgis/test/unit/pgistests
- podman run --rm -t metacg:$CI_COMMIT_SHA /opt/metacg/metacg/build/graph/test/unit/libtests
- podman image rm metacg:$CI_COMMIT_SHA

build-mcg:
Expand Down Expand Up @@ -119,6 +120,16 @@ install-mcg:
- /tmp/metacg/bin/cgcollector --help
- rm -r /tmp/metacg

test-graphlib:
stage: test
tags:
- general
before_script: *lb-setup
variables:
GIT_STRATEGY: none
GIT_CLONE_PATH: $CI_BUILDS_DIR/$CI_COMMIT_SHA
script:
- cd build/graph/test/unit && ./libtests

test-pgis:
stage: test
Expand All @@ -129,7 +140,7 @@ test-pgis:
GIT_STRATEGY: none
GIT_CLONE_PATH: $CI_BUILDS_DIR/$CI_COMMIT_SHA
script:
- cd build/pgis/test/unit && ./basictests
- cd build/pgis/test/unit && ./pgistests

test-static-pgis:
stage: integration-test
Expand Down
2 changes: 1 addition & 1 deletion cgcollector/tools/CGMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ nlohmann::json mergeFileFormatTwo(std::string wholeCGFilename, std::vector<std::
}

// {
// "_Meta": {...}, <-- Gets attached by "attachFormatTwoHeader"
// "_Meta": {...}, <-- Gets attached by "attachMCGFormatHeader"
// "_CG": { /** Gets constructed in the for loop **/ }
// }
attachFormatTwoHeader(wholeCGFinal);
Expand Down
4 changes: 4 additions & 0 deletions formatSrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ find ./cgcollector/lib -iname ".cpp" -exec clang-format -i {} \;
find ./cgcollector/lib -iname ".h" -exec clang-format -i {} \;
find ./cgcollector/tools -iname ".cpp" -exec clang-format -i {} \;
find ./cgcollector/tools -iname ".h" -exec clang-format -i {} \;

# Apply CMake formatting
find . -name "CMakeLists.txt" -exec cmake-format -i {} \;
find ./cmake -name "*.cmake" -type f -exec cmake-format -i {} \;
4 changes: 4 additions & 0 deletions graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(MetacgGraphLibSources
include/MCGWriter.h
src/MCGWriter.cpp
include/Util.h
include/MCGBaseInfo.h
)

add_library(metacg SHARED ${MetacgGraphLibSources})
Expand Down Expand Up @@ -44,3 +45,6 @@ configure_package_config_file(
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake DESTINATION lib/cmake)

# Add the directory containing unit tests
add_subdirectory(test/unit)
95 changes: 95 additions & 0 deletions graph/include/MCGBaseInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* File: MCGBaseInfo.h
* License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at
* https://github.com/tudasc/metacg/LICENSE.txt
*/

#ifndef METACG_MCGBASEINFO_H
#define METACG_MCGBASEINFO_H

#include "config.h"
#include <set>
#include <string>

namespace metacg {

struct MCGFileFormatVersion {
MCGFileFormatVersion(int major, int minor) : major(major), minor(minor) {}
MCGFileFormatVersion(const MCGFileFormatVersion &other) = default;
MCGFileFormatVersion(MCGFileFormatVersion &&other) = default;
bool operator==(const MCGFileFormatVersion &rhs) const { return major == rhs.major && minor == rhs.minor; }
bool operator!=(const MCGFileFormatVersion &rhs) const { return !(rhs == *this); }
bool operator<(const MCGFileFormatVersion &rhs) const {
return major < rhs.major || (major == rhs.major && minor < rhs.minor);
}
bool operator>(const MCGFileFormatVersion &rhs) const { return !this->operator<(rhs); }

[[nodiscard]] std::string getVersionStr() const { return std::to_string(major) + '.' + std::to_string(minor); }

[[nodiscard]] std::string getJsonIdentifier() const { return {"version"}; }

int major;
int minor;
};

struct MCGGeneratorVersionInfo {
MCGGeneratorVersionInfo(std::string &name, int major, int minor, std::string gitSHA = {})
: name(name), major(major), minor(minor), sha(std::move(gitSHA)) {}
MCGGeneratorVersionInfo(const MCGGeneratorVersionInfo &other) = default;
MCGGeneratorVersionInfo(MCGGeneratorVersionInfo &&other) = default;

[[nodiscard]] std::string getVersionStr() const { return std::to_string(major) + '.' + std::to_string(minor); }

[[nodiscard]] std::string getJsonIdentifier() const { return {"generator"}; }

[[nodiscard]] std::string getJsonNameIdentifier() const { return {"name"}; }

[[nodiscard]] std::string getJsonVersionIdentifier() const { return {"version"}; }

[[nodiscard]] std::string getJsonShaIdentifier() const { return {"sha"}; }

std::string name;
int major;
int minor;
std::string sha;
};

struct MCGFileFormatInfo {
MCGFileFormatInfo(int major, int minor) : version(major, minor), cgFieldName("_CG"), metaInfoFieldName("_MetaCG") {}
MCGFileFormatInfo(const MCGFileFormatInfo &other) = default;
MCGFileFormatInfo(MCGFileFormatInfo &&other) = default;

MCGFileFormatVersion version;
std::string cgFieldName;
std::string metaInfoFieldName;
};

struct MCGFileNodeInfo {
const std::string calleesStr{"callees"};
const std::string isVirtualStr{"isVirtual"};
const std::string doesOverrideStr{"doesOverride"};
const std::string overridesStr{"overrides"};
const std::string overriddenByStr{"overriddenBy"};
const std::string callersStr{"callers"};
const std::string hasBodyStr{"hasBody"};
const std::string metaStr{"meta"};
};

struct MCGFileInfo {
MCGFileInfo(MCGFileFormatInfo ffInfo, MCGGeneratorVersionInfo genInfo)
: formatInfo(std::move(ffInfo)), generatorInfo(std::move(genInfo)) {}
MCGFileInfo(const MCGFileInfo &other) = default;
MCGFileInfo(MCGFileInfo &&other) = default;

MCGFileFormatInfo formatInfo;
MCGGeneratorVersionInfo generatorInfo;
MCGFileNodeInfo nodeInfo;
};

MCGFileInfo getVersionTwoFileInfo(MCGGeneratorVersionInfo mcgGenInfo);

MCGGeneratorVersionInfo getCGCollectorGeneratorInfo();

} // namespace metacg

#endif // METACG_MCGBASEINFO_H
3 changes: 0 additions & 3 deletions graph/include/MCGReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ class VersionTwoMetaCGReader : public MetaCGReader {
void read(metacg::graph::MCGManager &cgManager) override;
};




} // namespace metacg::io

#endif
46 changes: 23 additions & 23 deletions graph/include/MCGWriter.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* File: MCGWriter.h
* License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at
* https://github.com/tudasc/metacg/LICENSE.txt
*/
* File: MCGWriter.h
* License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at
* https://github.com/tudasc/metacg/LICENSE.txt
*/

#ifndef METACG_MCGWRITER_H
#define METACG_MCGWRITER_H

#include "config.h"
#include "Callgraph.h"
#include "MCGBaseInfo.h"
#include "config.h"

#include "nlohmann/json.hpp"

Expand All @@ -19,15 +20,9 @@ namespace metacg::io {
*/
class JsonSink {
public:
explicit JsonSink() {}
void setJson(nlohmann::json jsonIn) { j = jsonIn; }

void setJson(nlohmann::json jsonIn) {
j = jsonIn;
}

[[nodiscard]] const nlohmann::json & getJson() const {
return j;
}
[[nodiscard]] const nlohmann::json &getJson() const { return j; }

/**
* Outputs the Json stored in this sink into os and flushes.
Expand All @@ -39,15 +34,17 @@ class JsonSink {
}

private:
nlohmann::json j;
nlohmann::json j{};
};

/**
* Class to serialize the CG.
*/
class MCGWriter {
public:
explicit MCGWriter(graph::MCGManager &mcgm) : mcgManager(mcgm) {}
explicit MCGWriter(graph::MCGManager &mcgm,
MCGFileInfo fileInfo = getVersionTwoFileInfo(getCGCollectorGeneratorInfo()))
: mcgManager(mcgm), fileInfo(std::move(fileInfo)) {}

void write(JsonSink &js);

Expand All @@ -56,13 +53,15 @@ class MCGWriter {
* Adds the CG version data to the MetaCG in json Format.
* @param j
*/
inline void attachFormatTwoHeader(nlohmann::json &j) {
std::string cgcMajorVersion = std::to_string(CGCollector_VERSION_MAJOR);
std::string cgcMinorVersion = std::to_string(CGCollector_VERSION_MINOR);
std::string cgcVersion{cgcMajorVersion + '.' + cgcMinorVersion};
j = {{"_MetaCG", {}}, {"_CG", {}}};
j["_MetaCG"] = {{"version", "2.0"},
{"generator", {{"name", "CGCollector"}, {"version", cgcVersion}, {"sha", MetaCG_GIT_SHA}}}};
inline void attachMCGFormatHeader(nlohmann::json &j) {
const auto formatInfo = fileInfo.formatInfo;
const auto generatorInfo = fileInfo.generatorInfo;
j = {{formatInfo.metaInfoFieldName, {}}, {formatInfo.cgFieldName, {}}};
j[formatInfo.metaInfoFieldName] = {{formatInfo.version.getJsonIdentifier(), formatInfo.version.getVersionStr()},
{generatorInfo.getJsonIdentifier(),
{{generatorInfo.getJsonNameIdentifier(), generatorInfo.name},
{generatorInfo.getJsonVersionIdentifier(), generatorInfo.getVersionStr()},
{generatorInfo.getJsonShaIdentifier(), generatorInfo.sha}}}};
}

/**
Expand All @@ -79,6 +78,7 @@ class MCGWriter {
void createAndAddMetaData(CgNodePtr node, const graph::MCGManager &mcgm, nlohmann::json &j);

graph::MCGManager &mcgManager;
MCGFileInfo fileInfo;
};

/*
Expand Down Expand Up @@ -130,5 +130,5 @@ void annotateJSON(metacg::Callgraph &cg, const std::string &filename, PropRetrie
}
}

} // namespace metacg::io
} // namespace metacg::io
#endif // METACG_MCGWRITER_H
43 changes: 42 additions & 1 deletion graph/include/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,58 @@
#include "CgNode.h"
#include <set>
#include <string>
#include <cstdlib>
#include <algorithm>

namespace metacg::util {

std::set<std::string> to_string(const std::set<CgNodePtr> &nodes) {
inline std::set<std::string> to_string(const std::set<CgNodePtr> &nodes) {
std::set<std::string> names;
for (const auto n : nodes) {
names.emplace(n->getFunctionName());
}
return names;
}

inline std::vector<std::string> string_split(const std::string &in, const char c = '.') {
std::vector<std::string::size_type > positions;
positions.emplace_back(0);
while (auto pos = in.find(c, positions.back() + 1)) {
if (pos == std::string::npos) {
break;
}
positions.push_back(pos + 1);
if (positions.size() > 5000) {
exit(-1);
}
}
std::vector<std::string> vec;
for (auto bg = positions.begin(); bg != positions.end(); ++bg) {
auto end = bg + 1;
vec.emplace_back(in.substr(*bg, (end - bg)));
}
return vec;
}

inline int getVersionNoAtPosition(const std::string &versionStr, int index) {
auto numOccurrences = std::count(versionStr.begin(), versionStr.end(), '.');
if (numOccurrences < 1) {
spdlog::get("errconsole")->error("Could not interpret version string");
exit(-1);
}
auto versionParts = string_split(versionStr);
int version = std::atoi(versionParts.at(index).c_str());
return version;
}

inline int getMajorVersionFromString(const std::string &versionStr) {
return getVersionNoAtPosition(versionStr, 0);
}

inline int getMinorVersionFromString(const std::string &versionStr) {
return getVersionNoAtPosition(versionStr, 1);
}

}

#endif // METACG_UTIL_H
Loading

0 comments on commit de6f3ab

Please sign in to comment.