|
10 | 10 |
|
11 | 11 | const SourceFileAndLine_t InvalidSourceAndLine = {false, 0, "", 0}; |
12 | 12 |
|
13 | | -unsigned getControlDepth(const clang::Stmt *const p_stmt, clang::ASTContext *p_context) { |
| 13 | +bool isStmtAnIfFromElseIf(const clang::Stmt *const p_parentStmt, const clang::Stmt * const p_stmt) { |
| 14 | + if (p_parentStmt->getStmtClass() == clang::Stmt::IfStmtClass) { |
| 15 | + const clang::Stmt *elseStmt = static_cast<const clang::IfStmt *>(p_parentStmt)->getElse(); |
| 16 | + if ((elseStmt == p_stmt) && (elseStmt->getStmtClass() == clang::Stmt::IfStmtClass)) { |
| 17 | + return true; |
| 18 | + } |
| 19 | + } |
| 20 | + return false; |
| 21 | +} |
| 22 | + |
| 23 | +unsigned getControlDepth(const clang::Stmt *const p_stmt, clang::ASTContext *p_context, |
| 24 | + const bool p_childIsElseIf) { |
14 | 25 | unsigned ret_val = 0; |
15 | 26 |
|
16 | 27 | clang::SourceLocation loc, sloc; |
17 | 28 |
|
18 | 29 | /* Examine all the parents */ |
19 | 30 | for (const auto &Parent : p_context->getParents(*p_stmt)) { |
20 | | - /* Is the parent a Stmt? */ |
21 | 31 | const clang::Stmt *stmt = Parent.get<clang::Stmt>(); |
22 | 32 | if (stmt != NULL) { |
23 | | - ret_val += getControlDepth(stmt, p_context); |
| 33 | + ret_val += getControlDepth(stmt, p_context, isStmtAnIfFromElseIf(stmt, p_stmt)); |
24 | 34 | } |
25 | 35 | } |
26 | 36 |
|
27 | 37 | switch (p_stmt->getStmtClass()) { |
28 | | - case clang::Stmt::IfStmtClass: |
| 38 | + case clang::Stmt::IfStmtClass: { |
| 39 | + /* We don't count "else if"s as increasing the depth, as the original "if" will already |
| 40 | + have done that */ |
| 41 | + if (!p_childIsElseIf) { |
| 42 | + ret_val++; |
| 43 | + } |
| 44 | + } |
| 45 | + break; |
29 | 46 | case clang::Stmt::SwitchStmtClass: |
30 | 47 | case clang::Stmt::DoStmtClass: |
31 | 48 | case clang::Stmt::WhileStmtClass: |
32 | 49 | case clang::Stmt::ForStmtClass: |
33 | 50 | ret_val += 1; |
34 | 51 | #if 0 |
35 | | - std::cout << p_stmt->getStmtClassName() << ": " << ret_val << "\n"; |
| 52 | + std::cout << "getControlDepth : " << p_stmt->getStmtClassName() << ": " << ret_val << "\n"; |
36 | 53 | #endif |
37 | 54 | break; |
38 | 55 | default: |
|
0 commit comments