Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Handle '::' in name_specs, replacing with '#'. #606

Merged
merged 5 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bindings/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ LIBRARY_SOURCES := \

TEST_SOURCES := \
buffer_test.cc \
event_test.cc \
macros_test.cc \
runtime_test.cc \
threaded_torture_test.cc
Expand Down Expand Up @@ -113,9 +114,11 @@ clean:
$(wildcard tmp*.wtf-trace)

### TESTING.
test: buffer_test macros_test runtime_test threaded_torture_test
test: buffer_test event_test macros_test runtime_test threaded_torture_test
@echo "Running buffer_test"
./buffer_test
@echo "Running event_test"
./event_test
@echo "Running macros_test"
./macros_test
@echo "Running runtime_test"
Expand All @@ -132,6 +135,9 @@ gtest.o: $(GTEST_ALL_CC)
buffer_test: buffer_test.o gtest.o libwtf.a
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)

event_test: event_test.o gtest.o libwtf.a
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)

macros_test: macros_test.o gtest.o libwtf.a
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)

Expand Down
36 changes: 30 additions & 6 deletions bindings/cpp/event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,44 @@ bool PeelArgName(const char** arg_names, const char** out_arg_name,
} // namespace

void EventDefinition::AppendName(std::string* output) const {
const char* colon = strchr(name_spec_, ':');
if (colon) {
output->append(name_spec_, (colon - name_spec_));
} else {
output->append(name_spec_);
// Colons are used as separators in WTF's binary format so can't be part of
// identifiers, but '::' commonly appears in auto-generated C++ identifier
// names, as with the __PRETTY_FUNCTION__ built-in macro.
// Replace double colons with '#', which is WTF's class/namespace separator.
//
// A single : in a name_spec separates the name part from arguments.
const char *src = name_spec_;
const char* colon = strchr(src, ':');
while (colon) {
output->append(src, (colon - src));
src = colon + 1;
if (*src == ':') {
// Double colon, replace with # and continue.
output->append("#");
src += 1;
colon = strchr(src, ':');
} else {
// This was a single colon. Output no more.
return;
}
}
// Append anything remaining in src.
output->append(src);
}

void EventDefinition::AppendArguments(std::string* output) const {
if (argument_zipper_ && name_spec_) {
const char* arg_names = strchr(name_spec_, ':');
if (arg_names) {
while (arg_names) {
// Colon found - advance.
arg_names += 1;
if (*arg_names == ':') {
// Actually a '::' namespace separator, keep looking.
arg_names += 1;
arg_names = strchr(arg_names, ':');
} else {
break;
}
}
argument_zipper_(output, arg_names);
}
Expand Down
70 changes: 70 additions & 0 deletions bindings/cpp/event_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "wtf/event.h"

#include <fstream>

#include "gtest/gtest.h"

namespace wtf {
namespace {

class EventTest : public ::testing::Test {
protected:
void TearDown() override {}

EventDefinition CreateEventDefinition(const char *name_spec) {
return EventDefinition::Create<int, const char*>(
/*wire_id=*/0, EventClass::kScoped, /*flags=*/0, name_spec);
}
};


TEST_F(EventTest, CheckNameSpecParsing) {
std::string output;

auto event = CreateEventDefinition("MyFunc");
event.AppendName(&output);
EXPECT_EQ(output, "MyFunc");
output.clear();
event.AppendArguments(&output);
EXPECT_EQ(output, "int32 a0, ascii a1");

output.clear();
event = CreateEventDefinition("MyNamespace::MyClass::MyFunc");
event.AppendName(&output);
EXPECT_EQ(output, "MyNamespace#MyClass#MyFunc");
output.clear();
event.AppendArguments(&output);
EXPECT_EQ(output, "int32 a0, ascii a1");

output.clear();
event = CreateEventDefinition("MyClass::MyFunc2: arg1 , arg2");
event.AppendName(&output);
EXPECT_EQ(output, "MyClass#MyFunc2");
output.clear();
event.AppendArguments(&output);
EXPECT_EQ(output, "int32 arg1, ascii arg2");

output.clear();
event = CreateEventDefinition("MyFunc2: arg1 , arg2");
event.AppendName(&output);
EXPECT_EQ(output, "MyFunc2");
output.clear();
event.AppendArguments(&output);
EXPECT_EQ(output, "int32 arg1, ascii arg2");

output.clear();
event = CreateEventDefinition("MyFunc3: arg1");
event.AppendName(&output);
EXPECT_EQ(output, "MyFunc3");
output.clear();
event.AppendArguments(&output);
EXPECT_EQ(output, "int32 arg1, ascii a1");
}

} // namespace
} // namespace wtf

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}