There is my C++ program to build a CFG presentation as a png
file:
#include <iostream>
#include <array>
#include <string>
#include <cstring>
int partitionFunc(std::string &str, int startInd, int endInd)
{
int primIndex = startInd;
int pivotVal = str[endInd];
for(int i = startInd; i < endInd; i++) {
if(str[i] <= pivotVal) {
char tempIn = str[i];
str[i] = str[primIndex];
str[primIndex] = tempIn;
primIndex++;
}
}
char tempOut = str[primIndex];
str[primIndex] = str[endInd];
str[endInd] = tempOut;
return primIndex;
}
void runQsort(std::string &str, int startInd, int endIndex)
{
if(startInd < endIndex) {
int primIndex = partitionFunc(str, startInd, endIndex);
runQsort(str, startInd, primIndex - 1);
runQsort(str, primIndex, endIndex);
}
}
int main(int argc, char * argv[])
{
std::array<std::string, 6> verbs = { "alternative", "destination", "airlines", "turbulence", "contribution", "ultimate" };
std::string selected;
int option = 14;
int endIndex;
switch (option)
{
case 1:
selected = verbs[0];
break;
case 2:
selected = verbs[1];
break;
case 3:
selected = verbs[2];
break;
case 4:
selected = verbs[3];
break;
case 5:
selected = verbs[4];
break;
default:
selected = verbs[5];
break;
}
runQsort(selected, 0, selected.length() - 1);
int x = 0;
while(x < selected.length()) {
std::cout << selected[x];
x++;
}
std::cout << std::endl;
return 0;
}
To enable a visualization of that, I process the whole code using clang++
, llvm
to parse source to dot
format:
clang -emit-llvm main.cpp -c -o main.bc
opt --dot-cfg-only main.bc
Lastly, this command to be executed to see graph as a flowchart:
dot -Tpng .main.dot -o main.png
The problem I struggle is appearing percent characters in the output file while function and operator identifiers are missing:
It's hardly enough to distinguish specific blocks as they are present in the source block. I would eliminate those embarrassing characters for seeing identifiers as they are put in the cpp
file. What instructions must be added here to do that (or what resources can be recommended to have some familiarity with this subject)? I would prefer to keep on parsing with clang++/llvm
if it's an available option after editing source appropriately.
LLVM uses Static Single Assignment (SSA) to represent the source in its intermediate representation(IR). This requires LLVM to add more SSA variables to the IR. These will be numbered. But you preserve your source variable names by including the flag
-fno-discard-value-names
.