citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 1866d00f4cc680cae9864fcbc1fcb0b738dca3f8
parent 31264853649abd253473010a5eb497de395da6b1
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Wed,  6 Apr 2016 20:55:00 -0600

src: wrap instrumentation preamble in extern "C"

Diffstat:
MSCV/Project.pm | 4++--
Msrc/instrument_action.cc | 11+++++++++++
Mt/inst_preamble.t | 6++++++
3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/SCV/Project.pm b/SCV/Project.pm @@ -65,7 +65,7 @@ sub instrumented_src { open( my $inst_fh, "<", "$self->{tmp_dir}/inst/source_0.c" ); # Knock off the instrumentation preamble - my $line = <$inst_fh> for (1..19); + my $line = <$inst_fh> for (1..25); my $inst_src; while (my $line = <$inst_fh>) { @@ -81,7 +81,7 @@ sub inst_src_preamble { open( my $inst_fh, "<", "$self->{tmp_dir}/inst/source_0.c" ); my $preamble; - for (1..19) { + for (1..25) { my $line = <$inst_fh>; $preamble .= $line; } diff --git a/src/instrument_action.cc b/src/instrument_action.cc @@ -95,6 +95,12 @@ InstrumentAction::EndSourceFileAction() unsigned int tu_number = read_src_number(); std::stringstream ss; + // Add preprocessor stuff so that the C runtime library links against + // C++ object code. + ss << "#ifdef __cplusplus" << std::endl; + ss << "extern \"C\" {" << std::endl; + ss << "#endif" << std::endl; + // Embed the header directly in the primary source file. ss << runtime_h << std::endl; @@ -117,6 +123,11 @@ InstrumentAction::EndSourceFileAction() << " .next = &_scv_node" << tu_number + 1 << "," << std::endl << "};" << std::endl; + // Close extern "C" { + ss << "#ifdef __cplusplus" << std::endl; + ss << "}" << std::endl; + ss << "#endif" << std::endl; + TheRewriter.InsertTextAfter(start, ss.str()); size_t last_slash = file_name.find_last_of('/'); diff --git a/t/inst_preamble.t b/t/inst_preamble.t @@ -22,6 +22,9 @@ my $tmp_dir = $project->get_tmpdir(); $tmp_dir = substr( $tmp_dir, 8 ) if ($^O eq "darwin"); my $preamble_good = <<EOF; +#ifdef __cplusplus +extern "C" { +#endif #include <stdint.h> struct _scv_node { uint64_t *lines_ptr; @@ -41,6 +44,9 @@ struct _scv_node _scv_node0 = { .file_name = "$tmp_dir/source_0.c", .next = &_scv_node1, }; +#ifdef __cplusplus +} +#endif EOF my $preamble = $project->inst_src_preamble();