# Question about finding nested for-loops

Hi All,

This is my example function to explaining my problem:

void BM_MatrixMultiplication_var(int NVar)
{
for(int i=0; i < NConst1; i++) //MatrixARow
{
for(int j=0; j < NVar; j++) //MatrixBColumn

{
fResultMatrix[i][j] = 0.0;
for(int k = 0; k < NVar; k++) //MatrixAColumn
{
fResultMatrix[i][j] += fMatrixA[i][k] * fMatrixB[k][j];
}
}

}

}

This is my function what i want to change with clang.

This is my goal:

void BM_MatrixMultiplication_var(int NVar)
{
LIKWID_MARKER_START(for-loop-1-BM_MatrixMultiplication_var);
for(int i=0; i < NConst1; i++) //MatrixARow
{
for(int j=0; j < NVar; j++) //MatrixBColumn

{
fResultMatrix[i][j] = 0.0;
for(int k = 0; k < NVar; k++) //MatrixAColumn

{
fResultMatrix[i][j] += fMatrixA[i][k] * fMatrixB[k][j];
}
}

}

}
LIKWID_MARKER_STOP(for-loop-1-BM_MatrixMultiplication_var);

I tried to work with AST Matcher.

void BM_MatrixMultiplication_var(int NVar)
{
for(int i=0; i < NConst1; i++) //MatrixARow
{
LIKWID_MARKER_START();
for(int j=0; j < NVar; j++) //MatrixBColumn
{
fResultMatrix[i][j] = 0.0;
LIKWID_MARKER_START();
for(int k = 0; k < NVar; k++) //MatrixAColumn
{
fResultMatrix[i][j] += fMatrixA[i][k] * fMatrixB[k][j];
}
LIKWID_MARKER_STOP();
}
LIKWID_MARKER_STOP();
}
}

With the following statement: StatementMatcher ForLoop = forStmt(hasAncestor(forStmt())).bind( “forStmt” );

// Finding nested Loop
//for (int i= 0; i < expr(); ++i) hasAncestor(forStmt())
//{
// for (int i= 0; i < expr(); ++i) forStmt()
// {
// Berechnung
// }
//}
//

It is not perfect. What i have to do?

Can you tell me?

I tried different statements and got different errors. what i am doing wrong?

//StatementMatcher ForLoop = compoundStmt( hasParent( forStmt( hasAncestor( forStmt( ))))).bind( “forStmt” );

//
//for (int i= 0; i < expr(); ++i) hasAncestor(forStmt())
//{
// for (int i= 0; i < expr(); ++i) hasParent(forStmt())
// { compoundStmt()
// Berechnung
// } compoundStmt()
//}
//
//{ hasParent(compoundStmt())
// for (int i= 0; i < expr(); ++i) hasAncestor(forStmt())
// {
// for (int i= 0; i < expr(); ++i) forStmt()
// {
// Berechnung
// }
// }
//} compoundStmt()
//
//StatementMatcher ForLoop = forStmt(hasAncestor(forStmt(hasParent(compoundStmt()).bind(“outerForStmt”));
//StatementMatcher ForLoop = forStmt().bind(“forStmt”);
//
// for (int i= 0; i < expr(); ++i) forStmt()
// {
// Berechnung
// }
//

//for (int i= 0; i < expr(); ++i) { … }
StatementMatcher LoopMatcher =
forStmt( // for ([init]; [condition]; [increment])
hasLoopInit(
declStmt(
hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0)))) // Die Initialisierung hat eine einzige Variable und ist mit dem Ganzzahlliteral 0 initialisiert
.bind(“initVarName”)))),
hasIncrement( // “increment” part of for-loop
unaryOperator( // any unary op, e.g. *, &, –
hasOperatorName(“++”), // exact unary op: ++
hasUnaryOperand(declRefExpr(to(varDecl(hasType(isInteger())).bind(“incVarName”))))
)
),
hasCondition(binaryOperator(
hasOperatorName(“<”),
hasLHS(ignoringParenImpCasts(declRefExpr(to(varDecl(hasType(isInteger())).bind(“condVarName”))))),
hasRHS(expr(hasType(isInteger()))))
)
).bind(“forLoop”);