commit ddaed162df00f2cff5af8d64aad98ff7776c5842
parent d6ba4813896a314472ed17a309b34f16f9845816
Author: Kyle Milz <kyle@0x30.net>
Date: Tue, 26 Jul 2016 21:13:56 -0600
src: light up function declarations
Diffstat:
9 files changed, 27 insertions(+), 19 deletions(-)
diff --git a/src/inst_ast_visitor.cc b/src/inst_ast_visitor.cc
@@ -81,19 +81,24 @@ RewriteASTVisitor::VisitFunctionDecl(clang::FunctionDecl *f)
if (f->hasBody() == 0)
return true;
- clang::Stmt *FuncBody = f->getBody();
+ std::stringstream rewrite_text;
+ // main() is a special case because it must start the runtime thread.
clang::DeclarationName DeclName = f->getNameInfo().getName();
- std::string FuncName = DeclName.getAsString();
+ if (DeclName.getAsString() == "main")
+ rewrite_text << "citrun_start();";
- if (FuncName.compare("main") != 0)
- // Function is not main
- return true;
+ clang::Stmt *FuncBody = f->getBody();
+ clang::SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1));
- std::string start_function("citrun_start();");
+ // Animate function calls by firing the entire declaration.
+ int decl_start = SM.getPresumedLineNumber(f->getLocStart());
+ int decl_end = SM.getPresumedLineNumber(curly_brace);
+ for (int i = decl_start; i <= decl_end; i++)
+ rewrite_text << "++_citrun_lines[" << i << "];";
- clang::SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1));
- TheRewriter.InsertTextBefore(curly_brace, start_function);
+ // Rewrite the function source right after the beginning curly brace.
+ TheRewriter.InsertTextBefore(curly_brace, rewrite_text.str());
return true;
}
diff --git a/t/fibonacci.t b/t/fibonacci.t
@@ -50,7 +50,7 @@ my $inst_src_good = <<EOF;
long long
fibonacci(long long n)
-{
+{++_citrun_lines[4];++_citrun_lines[5];++_citrun_lines[6];
if ((++_citrun_lines[7], n == 0))
return (++_citrun_lines[8], 0);
else if ((++_citrun_lines[9], n == 1))
@@ -61,7 +61,7 @@ fibonacci(long long n)
int
main(int argc, char *argv[])
-{citrun_start();
+{citrun_start();++_citrun_lines[15];++_citrun_lines[16];++_citrun_lines[17];
long long n;
if ((++_citrun_lines[20], argc != 2)) {
diff --git a/t/hello_world.t b/t/hello_world.t
@@ -27,7 +27,7 @@ my $inst_src_good = <<EOF;
int
main(void)
-{citrun_start();
+{citrun_start();++_citrun_lines[3];++_citrun_lines[4];++_citrun_lines[5];
(++_citrun_lines[6], printf("hello, world!"));
return (++_citrun_lines[7], 0);
}
diff --git a/t/inst_for.t b/t/inst_for.t
@@ -28,7 +28,7 @@ $project->compile();
my $inst_src_good = <<EOF;
int
main(void)
-{citrun_start();
+{citrun_start();++_citrun_lines[1];++_citrun_lines[2];++_citrun_lines[3];
int i;
for (i = 0; (++_citrun_lines[6], i < 19); i++) {
diff --git a/t/inst_if.t b/t/inst_if.t
@@ -38,7 +38,7 @@ my $inst_src_good = <<EOF;
int
main(int argc, char *argv[])
-{citrun_start();
+{citrun_start();++_citrun_lines[3];++_citrun_lines[4];++_citrun_lines[5];
if ((++_citrun_lines[6], argc == 1))
return (++_citrun_lines[7], 1);
else
diff --git a/t/inst_return.t b/t/inst_return.t
@@ -27,11 +27,11 @@ EOF
$project->compile();
my $inst_src_good = <<EOF;
-int foo() {
+int foo() {++_citrun_lines[1];
return (++_citrun_lines[2], 0);
}
-int main(void) {citrun_start();
+int main(void) {citrun_start();++_citrun_lines[5];
return (++_citrun_lines[6], 10);
return (++_citrun_lines[8], 10 + 10);
diff --git a/t/inst_switch.t b/t/inst_switch.t
@@ -32,7 +32,7 @@ $project->compile();
my $inst_src_good = <<EOF;
int
main(void)
-{citrun_start();
+{citrun_start();++_citrun_lines[1];++_citrun_lines[2];++_citrun_lines[3];
int i;
switch ((++_citrun_lines[6], i)) {
diff --git a/t/inst_while.t b/t/inst_while.t
@@ -30,7 +30,7 @@ $project->compile();
my $inst_src_good = <<EOF;
int
main(void)
-{citrun_start();
+{citrun_start();++_citrun_lines[1];++_citrun_lines[2];++_citrun_lines[3];
int i;
i = 0;
diff --git a/t/rt_sanity.t b/t/rt_sanity.t
@@ -79,7 +79,9 @@ my $data = $viewer->get_dynamic_data();
my ($s0, $s1, $s2) = sort keys %$data;
my @lines = @{ $data->{$s0} };
-is( $lines[$_], 0, "src 0 line $_ check" ) for (1..11);
+is( $lines[$_], 0, "src 0 line $_ check" ) for (1..6);
+is( $lines[$_], 1, "src 0 line $_ check" ) for (7..9);
+is( $lines[$_], 0, "src 0 line $_ check" ) for (10..11);
is( $lines[12], 1, "src 0 line 14 check" );
is( $lines[$_], 0, "src 0 line $_ check" ) for (13..14);
is( $lines[15], 1, "src 0 line 15 check" );
@@ -88,7 +90,8 @@ is( $lines[17], 2, "src 0 line 17 check" );
is( $lines[$_], 0, "src 0 line $_ check" ) for (18..19);
my @lines = @{ $data->{$s1} };
-is( $lines[$_], 0, "src 1 line $_ check" ) for (0..3);
+is( $lines[0], 0, "src 1 line 0 check" );
+cmp_ok ( $lines[$_], ">", 1, "src 1 line $_ check" ) for (1..3);
cmp_ok ( $lines[$_], ">", 10, "src 1 line $_ check" ) for (4..7);
is( $lines[8], 0, "src 1 line 8 check" );