Blame SOURCES/bpftrace-0.9-clang_parser-pass-BPFtrace-as-arg-instead-of-StructM.patch

05649e
From 5143209e8744d242431229972d9affa32ba3cc1a Mon Sep 17 00:00:00 2001
05649e
From: Matheus Marchini <mat@mmarchini.me>
05649e
Date: Fri, 12 Apr 2019 16:27:51 -0700
05649e
Subject: [PATCH 1/3] [clang_parser] pass BPFtrace as arg instead of StructMap
05649e
05649e
---
05649e
 src/clang_parser.cpp          |  6 +--
05649e
 src/clang_parser.h            |  3 +-
05649e
 src/main.cpp                  |  2 +-
05649e
 tests/clang_parser.cpp        | 71 +++++++++++++++++++++++------------
05649e
 tests/codegen/call_kstack.cpp |  4 +-
05649e
 tests/codegen/call_ustack.cpp |  4 +-
05649e
 tests/codegen/common.h        |  2 +-
05649e
 tests/codegen/general.cpp     |  2 +-
05649e
 tests/probe.cpp               |  2 +-
05649e
 tests/semantic_analyser.cpp   |  2 +-
05649e
 10 files changed, 61 insertions(+), 37 deletions(-)
05649e
05649e
diff --git a/src/clang_parser.cpp b/src/clang_parser.cpp
05649e
index b1db8ff..4bb8f87 100644
05649e
--- a/src/clang_parser.cpp
05649e
+++ b/src/clang_parser.cpp
05649e
@@ -172,7 +172,7 @@ static std::tuple<std::string, std::string> get_kernel_dirs(const struct utsname
05649e
   return std::make_tuple(ksrc, kobj);
05649e
 }
05649e
 
05649e
-void ClangParser::parse(ast::Program *program, StructMap &structs)
05649e
+void ClangParser::parse(ast::Program *program, BPFtrace &bpftrace)
05649e
 {
05649e
   auto input = program->c_definitions;
05649e
   if (input.size() == 0)
05649e
@@ -259,7 +259,6 @@ void ClangParser::parse(ast::Program *program, StructMap &structs)
05649e
       cursor,
05649e
       [](CXCursor c, CXCursor parent, CXClientData client_data)
05649e
       {
05649e
-        auto &structs = *static_cast<StructMap*>(client_data);
05649e
 
05649e
         if (clang_getCursorKind(parent) != CXCursor_StructDecl &&
05649e
             clang_getCursorKind(parent) != CXCursor_UnionDecl)
05649e
@@ -267,6 +266,7 @@ void ClangParser::parse(ast::Program *program, StructMap &structs)
05649e
 
05649e
         if (clang_getCursorKind(c) == CXCursor_FieldDecl)
05649e
         {
05649e
+          auto &structs = static_cast<BPFtrace*>(client_data)->structs_;
05649e
           auto struct_name = get_parent_struct_name(c);
05649e
           auto ident = get_clang_string(clang_getCursorSpelling(c));
05649e
           auto offset = clang_Cursor_getOffsetOfField(c) / 8;
05649e
@@ -290,7 +290,7 @@ void ClangParser::parse(ast::Program *program, StructMap &structs)
05649e
 
05649e
         return CXChildVisit_Recurse;
05649e
       },
05649e
-      &structs);
05649e
+      &bpftrace);
05649e
 
05649e
   clang_disposeTranslationUnit(translation_unit);
05649e
   clang_disposeIndex(index);
05649e
diff --git a/src/clang_parser.h b/src/clang_parser.h
05649e
index d2ada5d..4289796 100644
05649e
--- a/src/clang_parser.h
05649e
+++ b/src/clang_parser.h
05649e
@@ -1,6 +1,7 @@
05649e
 #pragma once
05649e
 
05649e
 #include "struct.h"
05649e
+#include "bpftrace.h"
05649e
 
05649e
 namespace bpftrace {
05649e
 
05649e
@@ -11,7 +12,7 @@ using StructMap = std::map<std::string, Struct>;
05649e
 class ClangParser
05649e
 {
05649e
 public:
05649e
-  void parse(ast::Program *program, StructMap &structs);
05649e
+  void parse(ast::Program *program, BPFtrace &bpftrace);
05649e
 };
05649e
 
05649e
 } // namespace bpftrace
05649e
diff --git a/src/main.cpp b/src/main.cpp
05649e
index ec3882d..f6659bf 100644
05649e
--- a/src/main.cpp
05649e
+++ b/src/main.cpp
05649e
@@ -272,7 +272,7 @@ int main(int argc, char *argv[])
05649e
   }
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
   err = semantics.analyse();
05649e
diff --git a/tests/clang_parser.cpp b/tests/clang_parser.cpp
05649e
index f12a5e4..0c1ca31 100644
05649e
--- a/tests/clang_parser.cpp
05649e
+++ b/tests/clang_parser.cpp
05649e
@@ -1,25 +1,28 @@
05649e
 #include "gtest/gtest.h"
05649e
 #include "clang_parser.h"
05649e
 #include "driver.h"
05649e
+#include "bpftrace.h"
05649e
 
05649e
 namespace bpftrace {
05649e
 namespace test {
05649e
 namespace clang_parser {
05649e
 
05649e
-void parse(const std::string &input, StructMap &structs)
05649e
+void parse(const std::string &input, BPFtrace &bpftrace)
05649e
 {
05649e
   auto extended_input = input + "kprobe:sys_read { 1 }";
05649e
   Driver driver;
05649e
   ASSERT_EQ(driver.parse_str(extended_input), 0);
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, structs);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 }
05649e
 
05649e
 TEST(clang_parser, integers)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Foo { int x; int y, z; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Foo { int x; int y, z; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 1U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -45,8 +48,10 @@ TEST(clang_parser, integers)
05649e
 
05649e
 TEST(clang_parser, c_union)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("union Foo { char c; short s; int i; long l; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("union Foo { char c; short s; int i; long l; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 1U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -77,8 +82,10 @@ TEST(clang_parser, c_union)
05649e
 
05649e
 TEST(clang_parser, integer_ptr)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Foo { int *x; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Foo { int *x; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 1U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -96,8 +103,10 @@ TEST(clang_parser, integer_ptr)
05649e
 
05649e
 TEST(clang_parser, string_ptr)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Foo { char *str; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Foo { char *str; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 1U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -115,8 +124,10 @@ TEST(clang_parser, string_ptr)
05649e
 
05649e
 TEST(clang_parser, string_array)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Foo { char str[32]; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Foo { char str[32]; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 1U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -132,8 +143,10 @@ TEST(clang_parser, string_array)
05649e
 
05649e
 TEST(clang_parser, nested_struct_named)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Bar { int x; } struct Foo { struct Bar bar; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Bar { int x; } struct Foo { struct Bar bar; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 2U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -151,8 +164,10 @@ TEST(clang_parser, nested_struct_named)
05649e
 
05649e
 TEST(clang_parser, nested_struct_ptr_named)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Bar { int x; } struct Foo { struct Bar *bar; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Bar { int x; } struct Foo { struct Bar *bar; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 2U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -172,8 +187,10 @@ TEST(clang_parser, nested_struct_ptr_named)
05649e
 
05649e
 TEST(clang_parser, nested_struct_anon)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Foo { struct { int x; } bar; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Foo { struct { int x; } bar; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.size(), 2U);
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
@@ -190,8 +207,10 @@ TEST(clang_parser, nested_struct_anon)
05649e
 
05649e
 TEST(clang_parser, nested_struct_indirect_fields)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Foo { struct { int x; int y;}; int a; struct { int z; }; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Foo { struct { int x; int y;}; int a; struct { int z; }; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs["Foo"].fields.size(), 4U);
05649e
   EXPECT_EQ(structs["Foo"].fields["x"].offset, 0);
05649e
@@ -206,8 +225,10 @@ TEST(clang_parser, nested_struct_indirect_fields)
05649e
 
05649e
 TEST(clang_parser, nested_struct_anon_union_struct)
05649e
 {
05649e
-  StructMap structs;
05649e
-  parse("struct Foo { union { long long _xy; struct { int x; int y;}; }; int a; struct { int z; }; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("struct Foo { union { long long _xy; struct { int x; int y;}; }; int a; struct { int z; }; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs["Foo"].fields.size(), 5U);
05649e
   EXPECT_EQ(structs["Foo"].fields["_xy"].offset, 0);
05649e
@@ -225,8 +246,10 @@ TEST(clang_parser, nested_struct_anon_union_struct)
05649e
 TEST(clang_parser, builtin_headers)
05649e
 {
05649e
   // size_t is definied in stddef.h
05649e
-  StructMap structs;
05649e
-  parse("#include <stddef.h>\nstruct Foo { size_t x, y, z; }", structs);
05649e
+  BPFtrace bpftrace;
05649e
+  parse("#include <stddef.h>\nstruct Foo { size_t x, y, z; }", bpftrace);
05649e
+
05649e
+  StructMap &structs = bpftrace.structs_;
05649e
 
05649e
   ASSERT_EQ(structs.count("Foo"), 1U);
05649e
 
05649e
diff --git a/tests/codegen/call_kstack.cpp b/tests/codegen/call_kstack.cpp
05649e
index a184af2..e64d498 100644
05649e
--- a/tests/codegen/call_kstack.cpp
05649e
+++ b/tests/codegen/call_kstack.cpp
05649e
@@ -68,7 +68,7 @@ TEST(codegen, call_kstack_mapids)
05649e
   ASSERT_EQ(driver.parse_str("kprobe:f { @x = kstack(5); @y = kstack(6); @z = kstack(6) }"), 0);
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
   ASSERT_EQ(semantics.analyse(), 0);
05649e
@@ -96,7 +96,7 @@ TEST(codegen, call_kstack_modes_mapids)
05649e
   ASSERT_EQ(driver.parse_str("kprobe:f { @x = kstack(perf); @y = kstack(bpftrace); @z = kstack() }"), 0);
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
   ASSERT_EQ(semantics.analyse(), 0);
05649e
diff --git a/tests/codegen/call_ustack.cpp b/tests/codegen/call_ustack.cpp
05649e
index 8e80558..1941d36 100644
05649e
--- a/tests/codegen/call_ustack.cpp
05649e
+++ b/tests/codegen/call_ustack.cpp
05649e
@@ -74,7 +74,7 @@ TEST(codegen, call_ustack_mapids)
05649e
   ASSERT_EQ(driver.parse_str("kprobe:f { @x = ustack(5); @y = ustack(6); @z = ustack(6) }"), 0);
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
   ASSERT_EQ(semantics.analyse(), 0);
05649e
@@ -102,7 +102,7 @@ TEST(codegen, call_ustack_modes_mapids)
05649e
   ASSERT_EQ(driver.parse_str("kprobe:f { @x = ustack(perf); @y = ustack(bpftrace); @z = ustack() }"), 0);
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
   ASSERT_EQ(semantics.analyse(), 0);
05649e
diff --git a/tests/codegen/common.h b/tests/codegen/common.h
05649e
index 32f8bc8..bdf733a 100644
05649e
--- a/tests/codegen/common.h
05649e
+++ b/tests/codegen/common.h
05649e
@@ -30,7 +30,7 @@ static void test(const std::string &input, const std::string expected_output)
05649e
   ASSERT_EQ(driver.parse_str(input), 0);
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
   ASSERT_EQ(semantics.analyse(), 0);
05649e
diff --git a/tests/codegen/general.cpp b/tests/codegen/general.cpp
05649e
index e7e7439..e67ae10 100644
05649e
--- a/tests/codegen/general.cpp
05649e
+++ b/tests/codegen/general.cpp
05649e
@@ -45,7 +45,7 @@ TEST(codegen, printf_offsets)
05649e
   // TODO (mmarchini): also test printf with a string argument
05649e
   ASSERT_EQ(driver.parse_str("struct Foo { char c; int i; } kprobe:f { $foo = (Foo*)0; printf(\"%c %u\\n\", $foo->c, $foo->i) }"), 0);
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
   ASSERT_EQ(semantics.analyse(), 0);
05649e
   ASSERT_EQ(semantics.create_maps(true), 0);
05649e
diff --git a/tests/probe.cpp b/tests/probe.cpp
05649e
index e030830..cb9b765 100644
05649e
--- a/tests/probe.cpp
05649e
+++ b/tests/probe.cpp
05649e
@@ -28,7 +28,7 @@ void gen_bytecode(const std::string &input, std::stringstream &out)
05649e
 	ASSERT_EQ(driver.parse_str(input), 0);
05649e
 
05649e
 	ClangParser clang;
05649e
-	clang.parse(driver.root_, bpftrace.structs_);
05649e
+	clang.parse(driver.root_, bpftrace);
05649e
 
05649e
 	ast::SemanticAnalyser semantics(driver.root_, bpftrace);
05649e
 	ASSERT_EQ(semantics.analyse(), 0);
05649e
diff --git a/tests/semantic_analyser.cpp b/tests/semantic_analyser.cpp
05649e
index 2067ed9..4e2485b 100644
05649e
--- a/tests/semantic_analyser.cpp
05649e
+++ b/tests/semantic_analyser.cpp
05649e
@@ -16,7 +16,7 @@ void test(BPFtrace &bpftrace, Driver &driver, const std::string &input, int expe
05649e
   ASSERT_EQ(driver.parse_str(input), 0);
05649e
 
05649e
   ClangParser clang;
05649e
-  clang.parse(driver.root_, bpftrace.structs_);
05649e
+  clang.parse(driver.root_, bpftrace);
05649e
 
05649e
   std::stringstream out;
05649e
   ast::SemanticAnalyser semantics(driver.root_, bpftrace, out);
05649e
-- 
05649e
2.20.1
05649e