Added option to include if and loop condition text in the diagram (fixes #162)

This commit is contained in:
Bartek Kryza
2023-07-04 23:58:42 +02:00
parent a514532e51
commit 3bd8f7f7a8
20 changed files with 365 additions and 13 deletions

View File

@@ -564,6 +564,75 @@ bool is_type_token(const std::string &t)
(is_identifier(t) && !is_qualifier(t) && !is_bracket(t));
}
std::string format_condition_text(const std::string &condition_text)
{
std::string result{condition_text};
if (result.size() < 2)
return {};
std::vector<std::string> text_lines = util::split(result, "\n", true);
// Trim each line
for (auto &line : text_lines) {
line = util::trim(line);
}
result = util::join(" ", text_lines);
if (result.at(0) == '(' && result.back() == ')')
return result.substr(1, result.size() - 2);
return result;
}
std::string get_condition_text(clang::SourceManager &sm, clang::IfStmt *stmt)
{
auto condition_range =
clang::SourceRange(stmt->getLParenLoc(), stmt->getRParenLoc());
return format_condition_text(get_source_text(condition_range, sm));
}
std::string get_condition_text(clang::SourceManager &sm, clang::WhileStmt *stmt)
{
auto condition_range =
clang::SourceRange(stmt->getLParenLoc(), stmt->getRParenLoc());
return format_condition_text(get_source_text(condition_range, sm));
}
std::string get_condition_text(
clang::SourceManager &sm, clang::CXXForRangeStmt *stmt)
{
auto condition_range = stmt->getRangeStmt()->getSourceRange();
return format_condition_text(get_source_text(condition_range, sm));
}
std::string get_condition_text(clang::SourceManager &sm, clang::ForStmt *stmt)
{
auto condition_range =
clang::SourceRange(stmt->getLParenLoc(), stmt->getRParenLoc());
return format_condition_text(get_source_text(condition_range, sm));
}
std::string get_condition_text(clang::SourceManager &sm, clang::DoStmt *stmt)
{
auto condition_range = stmt->getCond()->getSourceRange();
return format_condition_text(get_source_text(condition_range, sm));
}
std::string get_condition_text(
clang::SourceManager &sm, clang::ConditionalOperator *stmt)
{
auto condition_range = stmt->getCond()->getSourceRange();
return format_condition_text(get_source_text(condition_range, sm));
}
clang::QualType dereference(clang::QualType type)
{
auto res = type;

View File

@@ -23,6 +23,7 @@
#include "types.h"
#include "util/util.h"
#include <clang/AST/Expr.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <deque>
@@ -248,6 +249,23 @@ bool is_qualified_identifier(const std::string &t);
bool is_type_token(const std::string &t);
std::string format_condition_text(const std::string &condition_text);
std::string get_condition_text(clang::SourceManager &sm, clang::IfStmt *stmt);
std::string get_condition_text(
clang::SourceManager &sm, clang::WhileStmt *stmt);
std::string get_condition_text(
clang::SourceManager &sm, clang::CXXForRangeStmt *stmt);
std::string get_condition_text(clang::SourceManager &sm, clang::ForStmt *stmt);
std::string get_condition_text(clang::SourceManager &sm, clang::DoStmt *stmt);
std::string get_condition_text(
clang::SourceManager &sm, clang::ConditionalOperator *stmt);
clang::QualType dereference(clang::QualType type);
/**