Skip to content

Commit b64a4ef

Browse files
authored
Feat/plugin interface touchup (tudasc#138)
Promotes fileInfoMetadata to first party MD, remove demo plugin Moves cgcollector includes to own cgcollector namespace to avoid name congestions Moves Plugin header to interface folder Updates plugin interface to allow computeForGraph to add edges, edgemetadata and global metadata Adds CGC2 demo plugin Adds CaGe demo plugin Adds Tests
1 parent f556b70 commit b64a4ef

47 files changed

Lines changed: 774 additions & 325 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.ci/gitlab/.gitlab-ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,25 @@ install-test:
309309
- cd graph/test/install
310310
- CMAKE_PREFIX_PATH=$INSTALL/lib64/cmake/metacg cmake -S . -B $BUILD-install-test
311311
- cmake --build $BUILD-install-test --parallel
312+
313+
CGC2Plugin-test:
314+
<<: *job-setup
315+
stage: install
316+
needs: ["install"]
317+
script:
318+
- export BUILD=$(realpath $BUILD)
319+
- export INSTALL=$(realpath $INSTALL)
320+
- cd tools/cgcollector2/CGC2DemoPlugin
321+
- CMAKE_PREFIX_PATH=$INSTALL/lib64/cmake/metacg cmake -S . -B $BUILD-install-test
322+
- cmake --build $BUILD-install-test --parallel
323+
324+
CaGePlugin-test:
325+
<<: *job-setup
326+
stage: install
327+
needs: ["install"]
328+
script:
329+
- export BUILD=$(realpath $BUILD)
330+
- export INSTALL=$(realpath $INSTALL)
331+
- cd tools/cage/CaGeDemoPlugin
332+
- CMAKE_PREFIX_PATH=$INSTALL/lib64/cmake/metacg cmake -S . -B $BUILD-install-test
333+
- cmake --build $BUILD-install-test --parallel

.github/workflows/mcg-ci.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,25 @@ jobs:
100100
with:
101101
image: metacg-llvm-20-devel:latest
102102
run: /opt/metacg/checkTests.sh /opt/metacg/build/tools/cgformat/cgformat
103-
103+
- name: Run cgc2 Demo Plugin tests
104+
uses: maus007/docker-run-action-fork@207a4e2a8ebf7e4b985656ba990b1e53715dce2a
105+
with:
106+
image: metacg-llvm-20-devel:latest
107+
run: |
108+
export PATH=/tmp/metacg/bin/:$PATH
109+
export LD_LIBRARY_PATH=/tmp/metacg/lib:$LD_LIBRARY_PATH
110+
export LD_LIBRARY_PATH=/opt/metacg/tools/cgcollector2/CGC2DemoPlugin/build:$LD_LIBRARY_PATH
111+
cgcollector2 --pluginPaths=libCGC2DemoPlugin.so --metacg-format-version=4 /dev/null 2>&1 | grep -q "Successfully loaded Plugin: CGC2 Demo Plugin"
112+
- name: Run CaGe Demo Plugin tests
113+
uses: maus007/docker-run-action-fork@207a4e2a8ebf7e4b985656ba990b1e53715dce2a
114+
with:
115+
image: metacg-llvm-20-devel:latest
116+
run: |
117+
export PATH=/tmp/metacg/bin/:$PATH
118+
export LD_LIBRARY_PATH=/tmp/metacg/lib:$LD_LIBRARY_PATH
119+
export LD_LIBRARY_PATH=/opt/metacg/tools/cage/CaGeDemoPlugin/build:$LD_LIBRARY_PATH
120+
opt --load-pass-plugin=cage-plugin.so --plugin-paths=libCaGeDemoPlugin.so -S --cage-verbose --passes="CaGe" /dev/null | grep -q "Successfully loaded Plugin: Cage Demo Plugin"
121+
104122
build-container-llvm-18:
105123
runs-on: ubuntu-latest
106124
steps:
@@ -240,3 +258,21 @@ jobs:
240258
run: |
241259
cd /opt/metacg/build/tools/cgtodot/
242260
ctest . --verbose
261+
- name: Run cgc2 Demo Plugin tests
262+
uses: maus007/docker-run-action-fork@207a4e2a8ebf7e4b985656ba990b1e53715dce2a
263+
with:
264+
image: metacg-llvm-18-devel:latest
265+
run: |
266+
export PATH=/tmp/metacg/bin/:$PATH
267+
export LD_LIBRARY_PATH=/tmp/metacg/lib:$LD_LIBRARY_PATH
268+
export LD_LIBRARY_PATH=/opt/metacg/tools/cgcollector2/CGC2DemoPlugin/build:$LD_LIBRARY_PATH
269+
cgcollector2 --pluginPaths=libCGC2DemoPlugin.so --metacg-format-version=4 /dev/null 2>&1 | grep -q "Successfully loaded Plugin: CGC2 Demo Plugin"
270+
- name: Run CaGe Demo Plugin tests
271+
uses: maus007/docker-run-action-fork@207a4e2a8ebf7e4b985656ba990b1e53715dce2a
272+
with:
273+
image: metacg-llvm-18-devel:latest
274+
run: |
275+
export PATH=/tmp/metacg/bin/:$PATH
276+
export LD_LIBRARY_PATH=/tmp/metacg/lib:$LD_LIBRARY_PATH
277+
export LD_LIBRARY_PATH=/opt/metacg/tools/cage/CaGeDemoPlugin/build:$LD_LIBRARY_PATH
278+
opt --load-pass-plugin=cage-plugin.so --plugin-paths=libCaGeDemoPlugin.so -S --cage-verbose --passes="CaGe" /dev/null | grep -q "Successfully loaded Plugin: Cage Demo Plugin"

cmake/ClangLLVM.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,39 @@ function(add_clang target)
7171
)
7272
endif()
7373

74+
# clang internally relies on llvm to be linked so we also link some llvm libs when we need clang
7475
if(LLVM_LINK_LLVM_DYLIB)
7576
target_link_libraries(${target} PUBLIC LLVM)
7677
else()
7778
llvm_map_components_to_libnames(llvm_libs support)
7879
target_link_libraries(${target} PUBLIC ${llvm_libs})
7980
endif()
81+
endfunction()
82+
83+
function(
84+
add_llvm
85+
target
86+
scope
87+
)
88+
target_include_directories(
89+
${target}
90+
SYSTEM
91+
${scope}
92+
${LLVM_INCLUDE_DIRS}
93+
)
8094

95+
if(LLVM_LINK_LLVM_DYLIB)
96+
target_link_libraries(
97+
${target}
98+
${scope}
99+
LLVM
100+
)
101+
else()
102+
llvm_map_components_to_libnames(llvm_libs support)
103+
target_link_libraries(
104+
${target}
105+
${scope}
106+
${llvm_libs}
107+
)
108+
endif()
81109
endfunction()

container/full-build-llvm-18

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ RUN wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.
2626
lld-18
2727
RUN bash -c "ln -s $(which clang-18) /usr/bin/clang" && \
2828
bash -c "ln -s $(which clang++-18) /usr/bin/clang++" && \
29+
bash -c "ln -s $(which opt-18) /usr/bin/opt" && \
2930
bash -c "ln -s $(which llvm-config-18) /usr/bin/llvm-config"
3031

3132
ARG extinstalldir=/opt/metacg/extern/install
@@ -59,3 +60,12 @@ RUN cmake -S . -B build -GNinja -DCMAKE_BUILD_TYPE=Debug \
5960
RUN cmake --build build --parallel
6061

6162
RUN cmake --install build
63+
64+
RUN cmake -Dmetacg_ROOT=/tmp/metacg -S graph/test/install -B graph/test/build
65+
RUN cmake --build graph/test/build --parallel
66+
67+
RUN cmake -Dmetacg_ROOT=/tmp/metacg -S tools/cgcollector2/CGC2DemoPlugin -B tools/cgcollector2/CGC2DemoPlugin/build
68+
RUN cmake --build tools/cgcollector2/CGC2DemoPlugin/build --parallel
69+
70+
RUN cmake -Dmetacg_ROOT=/tmp/metacg -S tools/cage/CaGeDemoPlugin -B tools/cage/CaGeDemoPlugin/build
71+
RUN cmake --build tools/cage/CaGeDemoPlugin/build --parallel

container/full-build-llvm-20

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ RUN wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.
2626
lld-20 libmlir-20-dev mlir-20-tools flang-20
2727
RUN bash -c "ln -s $(which clang-20) /usr/bin/clang" && \
2828
bash -c "ln -s $(which clang++-20) /usr/bin/clang++" && \
29+
bash -c "ln -s $(which opt-20) /usr/bin/opt" && \
2930
bash -c "ln -s $(which llvm-config-20) /usr/bin/llvm-config" && \
3031
bash -c "ln -s $(flang-new-20) /usr/bin/flang-new"
3132

@@ -60,3 +61,12 @@ RUN cmake -S . -B build -GNinja -DCMAKE_BUILD_TYPE=Debug \
6061
RUN cmake --build build --parallel
6162

6263
RUN cmake --install build
64+
65+
RUN cmake -Dmetacg_ROOT=/tmp/metacg -S graph/test/install -B graph/test/build
66+
RUN cmake --build graph/test/build --parallel
67+
68+
RUN cmake -Dmetacg_ROOT=/tmp/metacg -S tools/cgcollector2/CGC2DemoPlugin -B tools/cgcollector2/CGC2DemoPlugin/build
69+
RUN cmake --build tools/cgcollector2/CGC2DemoPlugin/build --parallel
70+
71+
RUN cmake -Dmetacg_ROOT=/tmp/metacg -S tools/cage/CaGeDemoPlugin -B tools/cage/CaGeDemoPlugin/build
72+
RUN cmake --build tools/cage/CaGeDemoPlugin/build --parallel

tools/cgcollector2/fileInfoDemoPlugin/FileInfoMetadata.h renamed to graph/include/metacg/metadata/FileInfoMD.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* File: FileInfoMetadata.h
3-
* License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at
4-
* https://github.com/tudasc/metacg/LICENSE.txt
5-
*/
2+
* File: FileInfoMD.h
3+
* License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at
4+
* https://github.com/tudasc/metacg/LICENSE.txt
5+
*/
66

77
#ifndef CGCOLLECTOR2_FILEINFOMETADATA_H
88
#define CGCOLLECTOR2_FILEINFOMETADATA_H
@@ -14,21 +14,34 @@
1414
* The same restrictions apply:
1515
* Implement a static key, and the three virtual functions, and register your metadata via the Registrar
1616
*/
17+
1718
class FileInfoMetadata : public metacg::MetaData::Registrar<FileInfoMetadata> {
1819
public:
1920
static constexpr const char* key = "FilePropertiesMetaData";
2021
FileInfoMetadata() : origin("INVALID"), fromSystemInclude(false), lineNumber(0) {}
21-
explicit FileInfoMetadata(const nlohmann::json& j, metacg::StrToNodeMapping& strToNode);
22+
explicit FileInfoMetadata(const nlohmann::json& j, metacg::StrToNodeMapping& strToNode) {
23+
if (j.is_null()) {
24+
metacg::MCGLogger::instance().getConsole()->trace("Could not retrieve meta data for fileProperties");
25+
return;
26+
}
27+
origin = j["origin"].get<std::string>();
28+
fromSystemInclude = j["systemInclude"].get<bool>();
29+
}
2230

2331
FileInfoMetadata(const FileInfoMetadata& other)
2432
: origin(other.origin), fromSystemInclude(other.fromSystemInclude), lineNumber(other.lineNumber) {}
2533

26-
nlohmann::json toJson(metacg::NodeToStrMapping&) const final;
34+
nlohmann::json toJson(metacg::NodeToStrMapping&) const final {
35+
nlohmann::json j;
36+
j["origin"] = origin;
37+
j["systemInclude"] = fromSystemInclude;
38+
return j;
39+
}
2740

28-
virtual void applyMapping(const metacg::GraphMapping&){}
41+
void applyMapping(const metacg::GraphMapping&) final {}
2942

30-
virtual void merge(const MetaData&, std::optional<metacg::MergeAction>, const metacg::GraphMapping&) ;
31-
virtual const char* getKey() const final { return key; }
43+
void merge(const MetaData&, std::optional<metacg::MergeAction>, const metacg::GraphMapping&) final {}
44+
const char* getKey() const final { return key; }
3245

3346
std::unique_ptr<MetaData> clone() const final { return std::make_unique<FileInfoMetadata>(*this); }
3447

pgis/test/unit/CallgraphTest.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
#include "gtest/gtest.h"
88

9-
// #include "LoggerUtil.h"
10-
11-
#include "../../../graph/include/metacg/Callgraph.h"
12-
#include "../../../graph/include/metacg/LoggerUtil.h"
9+
#include "metacg/Callgraph.h"
10+
#include "metacg/LoggerUtil.h"
1311

1412
using namespace metacg;
1513

tools/cage/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
add_subdirectory(src)
22
add_subdirectory(test)
33

4+
add_library(cage-plugin-interface INTERFACE)
5+
6+
target_sources(
7+
cage-plugin-interface
8+
PUBLIC FILE_SET
9+
HEADERS
10+
BASE_DIRS
11+
include
12+
FILES
13+
include/cage/interface/CaGePlugin.h
14+
)
15+
16+
target_link_libraries(cage-plugin-interface INTERFACE metacg::metacg)
17+
add_llvm(cage-plugin-interface INTERFACE)
18+
19+
install(
20+
TARGETS cage-plugin-interface
21+
EXPORT cage-plugin-interfaceExport
22+
FILE_SET HEADERS
23+
DESTINATION include/
24+
)
25+
426
install(TARGETS cage cage-plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(CaGeDemoPlugin)
3+
4+
find_package(metacg REQUIRED)
5+
6+
if(metacg_FOUND)
7+
message(STATUS "metacg found")
8+
endif()
9+
10+
add_library(CaGeDemoPlugin SHARED CaGeDemoPlugin.cpp)
11+
12+
# Build metadata as separate shared object This .so can be preloaded to inform many graphlib tools about custom metadata
13+
add_library(CaGeDemoMD SHARED CaGeDemoMD.h)
14+
set_target_properties(CaGeDemoMD PROPERTIES LINKER_LANGUAGE CXX)
15+
target_link_libraries(CaGeDemoPlugin PRIVATE CaGeDemoMD)
16+
target_link_libraries(CaGeDemoPlugin PUBLIC metacg::metacg)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* File: CGC2DemoMD.h
3+
* License: Part of the MetaCG project. Licensed under BSD 3 clause license. See LICENSE.txt file at
4+
* https://github.com/tudasc/metacg/LICENSE.txt
5+
*/
6+
#ifndef METACG_CGCOLLECTOR2_CAGEDEMOMD_h
7+
#define METACG_CGCOLLECTOR2_CAGEDEMOMD_h
8+
9+
#include "metacg/metadata/MetaData.h"
10+
11+
namespace clang {
12+
class FunctionDecl;
13+
}
14+
15+
class CaGeDemoMD : public metacg::MetaData::Registrar<CaGeDemoMD> {
16+
public:
17+
static constexpr const char* key = "CaCeDemoMetadata";
18+
19+
CaGeDemoMD() = default;
20+
/**
21+
* Specify how to create your Metadata from a json input
22+
*/
23+
explicit CaGeDemoMD(const nlohmann::json&, metacg::StrToNodeMapping&) {
24+
assert(false && "This metadata should not be created via json");
25+
}
26+
27+
explicit CaGeDemoMD(const void* const f) : address(reinterpret_cast<uintptr_t>(f)) {}
28+
explicit CaGeDemoMD(const uintptr_t p) : address(p) {}
29+
30+
/**
31+
* Specify how to serialize your metadata into a json output
32+
* Return an empty json object to signal that this metadata will not be serialized
33+
* @return empty json object
34+
*/
35+
nlohmann::json toJson(metacg::NodeToStrMapping&) const final {
36+
// We just store the address
37+
return {{"Address", reinterpret_cast<uintptr_t>(address)}};
38+
}
39+
40+
/**
41+
* Allows to query the metadata-key from a baseclass-pointer via virtual dispatch
42+
* @return your metadata key
43+
*/
44+
[[nodiscard]] const char* getKey() const final { return key; }
45+
46+
/**
47+
* If your metadata stores ids of other callgraph-nodes, they might be invalidated
48+
* This can happen if the graph containing your metadata is merged with another graph.
49+
* Your metadata might be copied into the new graph
50+
* In this case you need to remap the stored node-ids to be valid in the new merged graph context
51+
*
52+
* @param g A map how to rename the old node-id to a new node-id, which is valid in the merged graph
53+
*/
54+
void applyMapping([[maybe_unused]] const metacg::GraphMapping& g) final {}
55+
56+
/**
57+
* How to merge your metadata with itself
58+
* This can happen if the graph containing your metadata is merged with another graph.
59+
* Update the state of your this metadata object accordingly
60+
* @param toMerge the other Metadata to merge with
61+
* @param mergeAction contains whether the merge will replace the other metadata
62+
* @param g A map how to rename the old node-id to a new node-id, which is valid in the merged graph
63+
*/
64+
void merge(const MetaData& toMerge, std::optional<metacg::MergeAction> mergeAction,
65+
const metacg::GraphMapping& g) final {
66+
if (std::strcmp(toMerge.getKey(), getKey()) != 0) {
67+
metacg::MCGLogger::instance().getErrConsole()->error(
68+
"The MetaData which was tried to merge with ASTNodeMetadata was of a different MetaData type");
69+
abort();
70+
}
71+
assert(false && "This metadata can not be exported and therefore is not mergeable");
72+
}
73+
74+
/**
75+
* Specify how to clone your metadata
76+
* @return a cloned version of your metadata
77+
*/
78+
[[nodiscard]] std::unique_ptr<MetaData> clone() const final { return std::make_unique<CaGeDemoMD>(); }
79+
80+
uintptr_t getAdressValue() { return reinterpret_cast<uintptr_t>(address); }
81+
82+
private:
83+
uintptr_t address = 0;
84+
};
85+
#endif

0 commit comments

Comments
 (0)