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

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