Skip to content

Commit

Permalink
fix: avoid have extra empty node
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer committed Nov 12, 2022
1 parent 79a19b3 commit 7b79a5b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cxx2flow"
version = "0.5.10"
version = "0.5.11"
edition = "2018"
authors = ["mgt <[email protected]>"]
description = "Convert your C/C++ code to control flow chart"
Expand Down
40 changes: 19 additions & 21 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,26 @@ For those who are not familiar with command line, I recommend the GUI version of
To compile the generated dot file, you need graphviz. You can also copy the output to online graphviz services such as http://magjac.com/graphviz-visual-editor/ .

```
cxx2flow 0.5.9
mgt <[email protected]>
Convert your C/C++ code to control flow chart
USAGE:
cxx2flow [OPTIONS] [ARGS]
ARGS:
<INPUT> Sets the path of the input file. e.g. test.cpp
If not specified, cxx2flow will read from stdin.
<FUNCTION> The function you want to convert. e.g. main [default: main]
OPTIONS:
-c, --curly Sets the style of the flow chart.
If specified, output flow chart will have curly connection line.
--cpp Use C preprocessor.
-h, --help Print help information
-o, --output <OUTPUT> Sets the output file.
If not specified, result will be directed to stdout.
e.g. graph.dot
-t, --tikz Use tikz backend.
-V, --version Print version information
Usage: cxx2flow [OPTIONS] [INPUT] [FUNCTION]
Arguments:
[INPUT] Sets the path of the input file. e.g. test.cpp
If not specified, cxx2flow will read from stdin.
[FUNCTION] The function you want to convert. e.g. main [default: main]
Options:
-o, --output <OUTPUT> Sets the output file.
If not specified, result will be directed to stdout.
e.g. graph.dot
-c, --curly Sets the style of the flow chart.
If specified, output flow chart will have curly connection line.
--cpp Use C preprocessor.
-t, --tikz Use tikz backend.
-d, --dump-ast Dump AST(For debug purpose only).
-h, --help Print help information
-V, --version Print version information
Note that you need to manually compile the dot file using graphviz to get SVG or PNG files.
Expand All @@ -97,4 +95,4 @@ https://github.com/Enter-tainer/cxx2flow

- The support of preprocessor is based on `cpp`, and is disabled by default. `--cpp` flag is needed to enable it. It will fail if `cpp` does not exist in `PATH`.
- Supported control flow keyword: while,for,if,break,continue,break,return,switch, goto, do-while。
- Very basic support for range based loop in C++ 11.
- Very basic support for range based loop in C++ 11.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,26 @@ cargo install cxx2flow
为了编译生成的 dot 文件,你需要安装 graphviz,并将其添加到 PATH 中。也可以将生成的结果复制进在线的 graphviz 服务中,如 http://magjac.com/graphviz-visual-editor/

```
cxx2flow 0.5.9
mgt <[email protected]>
Convert your C/C++ code to control flow chart
USAGE:
cxx2flow [OPTIONS] [ARGS]
ARGS:
<INPUT> Sets the path of the input file. e.g. test.cpp
If not specified, cxx2flow will read from stdin.
<FUNCTION> The function you want to convert. e.g. main [default: main]
OPTIONS:
-c, --curly Sets the style of the flow chart.
If specified, output flow chart will have curly connection line.
--cpp Use C preprocessor.
-h, --help Print help information
-o, --output <OUTPUT> Sets the output file.
If not specified, result will be directed to stdout.
e.g. graph.dot
-t, --tikz Use tikz backend.
-V, --version Print version information
Usage: cxx2flow [OPTIONS] [INPUT] [FUNCTION]
Arguments:
[INPUT] Sets the path of the input file. e.g. test.cpp
If not specified, cxx2flow will read from stdin.
[FUNCTION] The function you want to convert. e.g. main [default: main]
Options:
-o, --output <OUTPUT> Sets the output file.
If not specified, result will be directed to stdout.
e.g. graph.dot
-c, --curly Sets the style of the flow chart.
If specified, output flow chart will have curly connection line.
--cpp Use C preprocessor.
-t, --tikz Use tikz backend.
-d, --dump-ast Dump AST(For debug purpose only).
-h, --help Print help information
-V, --version Print version information
Note that you need to manually compile the dot file using graphviz to get SVG or PNG files.
Expand Down
21 changes: 21 additions & 0 deletions assets/extra-semicolon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
int main() {
int t;
double ans, x;
printf("Please input your salary:");
scanf("%lf", &x);
printf("Please choose a method\n1:with if; 2:with swtch:");
scanf("%d", &t);
if (t == 1)
ans = useif(x);
else if (t == 2)
ans = usesw(x);
else {
printf("Invalid input!\n");
return 0;
};
if (t == 1)
printf("calculate by if, the tax is:%.2lf\n", ans);
else
printf("calculate by switch, the tax is:%.2lf\n", ans);
return 0;
}
2 changes: 1 addition & 1 deletion src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ pub fn from_ast(ast: Rc<RefCell<Ast>>, source: &str, file_name: &str) -> Result<
while remove_zero_in_degree_nodes(&mut ctx.graph, source) {}
while remove_single_node(&mut ctx.graph, source, |_, t| *t == GraphNodeType::Dummy)? {}
let remove_empty_nodes: fn(NodeIndex, &GraphNodeType) -> bool = |_, t| match t {
GraphNodeType::Node(t) => t.is_empty(),
GraphNodeType::Node(t) => t.is_empty() || t.trim() == ";",
_ => false,
};
while remove_single_node(&mut ctx.graph, source, remove_empty_nodes)? {}
Expand Down

0 comments on commit 7b79a5b

Please sign in to comment.