Support attributes for access control 04/164304/6
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 18 Dec 2017 10:58:11 +0000 (19:58 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Tue, 19 Dec 2017 11:37:31 +0000 (11:37 +0000)
Change-Id: I79552ae01fd7fe40df5421cdea92e6fe70c1e3fe
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
12 files changed:
idlc/attribute.cc [new file with mode: 0644]
idlc/attribute.h [new file with mode: 0644]
idlc/generator.cc
idlc/interface.cc
idlc/interface.h
idlc/tidlc.ll
idlc/tidlc.yy
unit_tests/CMakeLists.txt
unit_tests/attribute_unittest.cc [new file with mode: 0644]
unit_tests/generator_unittest.cc
unit_tests/interface_unittest.cc
unit_tests/test.tidl

diff --git a/idlc/attribute.cc b/idlc/attribute.cc
new file mode 100644 (file)
index 0000000..666e071
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <utility>
+
+#include "idlc/attribute.h"
+
+namespace tidl {
+
+Attribute::Attribute(std::string key, std::string value, unsigned line)
+    : key_(std::move(key)), value_(std::move(value)), line_(line) {
+}
+
+const std::string& Attribute::GetKey() const {
+  return key_;
+}
+
+const std::string& Attribute::GetValue() const {
+  return value_;
+}
+
+const unsigned Attribute::GetLine() const {
+  return line_;
+}
+
+void Attributes::Add(Attribute* attr) {
+  attrs_.emplace_back(attr);
+}
+
+const std::list<std::unique_ptr<Attribute>>& Attributes::GetAttrs() const {
+  return attrs_;
+}
+
+bool Attributes::Exist(Attribute* attr) const {
+  for (auto& a : attrs_) {
+    if (a->GetKey() == attr->GetKey() &&
+        a->GetValue() == attr->GetValue())
+      return true;
+  }
+
+  return false;
+}
+
+}  // namespace tidl
diff --git a/idlc/attribute.h b/idlc/attribute.h
new file mode 100644 (file)
index 0000000..360348e
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef IDLC_ATTRIBUTE_H_
+#define IDLC_ATTRIBUTE_H_
+
+#include <string>
+#include <list>
+#include <memory>
+
+namespace tidl {
+
+class Attribute {
+ public:
+  Attribute(std::string key, std::string value, unsigned line);
+
+  const std::string& GetKey() const;
+  const std::string& GetValue() const;
+  const unsigned GetLine() const;
+
+ private:
+  std::string key_;
+  std::string value_;
+  unsigned line_;
+};
+
+class Attributes {
+ public:
+  void Add(Attribute* attr);
+  const std::list<std::unique_ptr<Attribute>>& GetAttrs() const;
+  bool Exist(Attribute* attr) const;
+
+ private:
+  std::list<std::unique_ptr<Attribute>> attrs_;
+};
+
+}  // namespace tidl
+
+#endif  // IDLC_ATTRIBUTE_H_
index 705690f..7060941 100644 (file)
@@ -19,6 +19,7 @@
 #include "idlc/interface.h"
 #include "idlc/structure.h"
 #include "idlc/declaration.h"
+#include "idlc/attribute.h"
 
 namespace tidl {
 
index 04dd466..6a4b3a4 100644 (file)
 #include <utility>
 
 #include "idlc/declaration.h"
+#include "idlc/attribute.h"
 #include "idlc/interface.h"
 #include "idlc/block.h"
 
 namespace tidl {
 
 Interface::Interface(std::string id, Declarations* decls, std::string comments,
-                     unsigned line)
+                     Attributes* attrs, unsigned line)
     : Block::Block(std::move(id), Block::TYPE_INTERFACE,
-                   std::move(comments), line), decls_(decls) {}
+                   std::move(comments), line),
+      decls_(decls),
+      attrs_(attrs) {}
 
 const Declarations& Interface::GetDeclarations() const {
   return *decls_;
 }
 
+const Attributes& Interface::GetAttributes() const {
+  return *attrs_;
+}
+
 }  // namespace tidl
index b2da729..b110872 100644 (file)
 
 #include "idlc/block.h"
 #include "idlc/declaration.h"
+#include "idlc/attribute.h"
 
 namespace tidl {
 
 class Interface : public Block {
  public:
   Interface(std::string id, Declarations* decls, std::string comments,
-            unsigned line);
+            Attributes* attrs, unsigned line);
 
   const Declarations& GetDeclarations() const;
+  const Attributes& GetAttributes() const;
 
  private:
   std::unique_ptr<Declarations> decls_;
+  std::unique_ptr<Attributes> attrs_;
 };
 
 }  // namespace tidl
index 3fe80c5..3bd17f6 100644 (file)
 #include "idlc/element.h"
 #include "idlc/structure.h"
 #include "idlc/block.h"
+#include "idlc/attribute.h"
 #include "idlc/tidlc_y.hpp"
 
 #define YY_USER_ACTION yylloc->columns(yyleng);
 %}
 
-%x COMMENT
+%x COMMENT VALUE
 
 %option yylineno
 %option noyywrap
@@ -27,6 +28,7 @@
 %%
 %{
   std::string comments;
+  std::string values;
 %}
 
 "/*"                    { comments += yytext; BEGIN(COMMENT); }
 
 "//".*\n                { comments += yytext; yylloc->step(); }
 
+"\""                    { BEGIN(VALUE); }
+<VALUE>"\""             {
+                          BEGIN(INITIAL);
+                          yylval->token = new tidl::Token(values, comments);
+                          return yy::parser::token::T_VALUE;
+                        }
+<VALUE>([^*]\n)+|.      { values += yytext; yylloc->step(); }
+
 [\n]+                   { yylloc->lines(yyleng); yylloc->step(); }
 
 [ \t\r\n] ; // ignore all whitespace
                           yylval->token = new tidl::Token(yytext, comments);
                           return yy::parser::token::T_ID;
                         }
+"["                     { // Square Bracket
+                          yylval->token = new tidl::Token(yytext, comments);
+                          return yy::parser::token::T_SB_OPEN;
+                        }
+"]"                     { // Square Bracket
+                          yylval->token = new tidl::Token(yytext, comments);
+                          return yy::parser::token::T_SB_CLOSE;
+                        }
+"="                     { return yy::parser::token::T_EQUAL; }
 
 %%
 
index 8c46a4f..fe4c3ce 100644 (file)
@@ -12,6 +12,7 @@
 #include "idlc/element.h"
 #include "idlc/structure.h"
 #include "idlc/block.h"
+#include "idlc/attribute.h"
 #include "idlc/tidlc_y.hpp"
 
 int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
@@ -26,6 +27,7 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
 %token T_LEFT T_RIGHT T_COMMA T_SEMICOLON T_BRACE_OPEN T_BRACE_CLOSE
 %token T_IN T_OUT T_REF T_ASYNC
 %token T_META_OPEN T_META_CLOSE
+%token T_EQUAL
 
 
 %start start
@@ -47,6 +49,8 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
   tidl::Element* elm;
   tidl::Elements* elms;
   tidl::Block* blk;
+  tidl::Attribute* attr;
+  tidl::Attributes* attrs;
 }
 
 %token<token> T_ID
@@ -63,6 +67,9 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
 %token<token> T_STRING
 %token<token> T_BOOL
 %token<token> T_LIST
+%token<token> T_VALUE
+%token<token> T_SB_OPEN
+%token<token> T_SB_CLOSE
 %type<doc> blocks
 %type<blk> block
 %type<structure> structure_block
@@ -78,6 +85,8 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
 %type<token> container_type_name
 %type<elm> element
 %type<elms> elements
+%type<attr> attribute
+%type<attrs> attributes
 
 %%
 
@@ -175,12 +184,57 @@ element: base_type T_ID T_SEMICOLON {
   }
 ;
 
+attributes: attribute {
+    $$ = new tidl::Attributes();
+    $$->Add($1);
+  }
+  | attributes T_COMMA attribute {
+    $$ = $1;
+    if ($$->Exist($3)) {
+      ps->ReportError("syntax error. \"Already Exist\".", $3->GetLine());
+      delete $3;
+    } else {
+      $$->Add($3);
+    }
+  }
+  | error {
+    ps->ReportError("syntax error in attributes", @1.begin.line);
+    $$ = new tidl::Attributes();
+  }
+;
+
+attribute: T_ID T_EQUAL T_VALUE {
+    $$ = new tidl::Attribute($1->ToString(), $3->ToString(), @1.begin.line);
+    delete $1;
+    delete $3;
+  }
+  | T_ID error T_VALUE  {
+    ps->ReportError("syntax error in attribute declaration.", @2.begin.line);
+    $$ = NULL;
+    delete $1;
+    delete $3;
+  }
+  | T_ID T_EQUAL error {
+    ps->ReportError("syntax error in attribute declaration.", @3.begin.line);
+    $$ = NULL;
+    delete $1;
+  }
+;
+
 interface_block: T_INTERFACE T_ID T_BRACE_OPEN declarations T_BRACE_CLOSE {
     $$ = new tidl::Interface($2->ToString(), $4, $1->GetComments(),
-        @1.begin.line);
+        new tidl::Attributes(), @1.begin.line);
     delete $1;
     delete $2;
   }
+  | T_SB_OPEN attributes T_SB_CLOSE T_INTERFACE T_ID T_BRACE_OPEN declarations T_BRACE_CLOSE {
+    $$ = new tidl::Interface($5->ToString(), $7, $1->GetComments(), $2,
+        @1.begin.line);
+    delete $1;
+    delete $3;
+    delete $4;
+    delete $5;
+  }
   | T_INTERFACE T_BRACE_OPEN declarations T_BRACE_CLOSE {
     ps->ReportError("syntax error. \"No identifier\".", @1.begin.line);
     $$ = NULL;
index ced432c..050d12b 100644 (file)
@@ -27,6 +27,7 @@ SET(TIDLC_SOURCES
        ../idlc/block.cc
        ../idlc/generator.cc
        ../idlc/parser.cc
+       ../idlc/attribute.cc
        )
 
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../idlc/cs_gen CS_GEN_SOURCES)
diff --git a/unit_tests/attribute_unittest.cc b/unit_tests/attribute_unittest.cc
new file mode 100644 (file)
index 0000000..506a2b7
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <iostream>
+
+#include "idlc/attribute.h"
+
+class AttributeTest : public testing::Test {
+ public:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(AttributeTest, Attribute_Constructor) {
+  tidl::Attribute* attr = new tidl::Attribute("key", "value", __LINE__);
+  EXPECT_NE(attr, nullptr);
+  delete attr;
+}
+
+TEST_F(AttributeTest, Attribute_GetKey) {
+  tidl::Attribute* attr = new tidl::Attribute("key", "value", __LINE__);
+  EXPECT_NE(attr, nullptr);
+  EXPECT_EQ(attr->GetKey(), "key");
+  delete attr;
+}
+
+TEST_F(AttributeTest, Attribute_GetValue) {
+  tidl::Attribute* attr = new tidl::Attribute("key", "value", __LINE__);
+  EXPECT_NE(attr, nullptr);
+  EXPECT_EQ(attr->GetValue(), "value");
+  delete attr;
+}
+
+TEST_F(AttributeTest, Attribute_GetLine) {
+  unsigned line = __LINE__;
+  tidl::Attribute* attr = new tidl::Attribute("key", "value", line);
+  EXPECT_NE(attr, nullptr);
+  EXPECT_EQ(attr->GetLine(), line);
+  delete attr;
+}
+
+class AttributesTest : public testing::Test {
+ public:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(AttributesTest, Attributes_Constructor) {
+  tidl::Attributes* attrs = new tidl::Attributes();
+  EXPECT_NE(attrs, nullptr);
+  delete attrs;
+}
+
+TEST_F(AttributesTest, Attributes_Add) {
+  tidl::Attributes* attrs = new tidl::Attributes();
+  EXPECT_NE(attrs, nullptr);
+  attrs->Add(new tidl::Attribute("key", "value", __LINE__));
+
+  bool flag = false;
+  for (auto& attr : attrs->GetAttrs()) {
+    if (attr->GetKey() == "key" &&
+        attr->GetValue() == "value")
+      flag = true;
+  }
+  EXPECT_EQ(flag, true);
+  delete attrs;
+}
+
+TEST_F(AttributesTest, Attributes_GetAttrs) {
+  tidl::Attributes* attrs = new tidl::Attributes();
+  EXPECT_NE(attrs, nullptr);
+  attrs->Add(new tidl::Attribute("key1", "value1", __LINE__));
+  attrs->Add(new tidl::Attribute("key2", "value2", __LINE__));
+
+  int count = 0;
+  for (auto& attr : attrs->GetAttrs()) {
+    if (attr->GetKey() == "key1" &&
+        attr->GetValue() == "value1")
+      count++;
+    if (attr->GetKey() == "key2" &&
+        attr->GetValue() == "value2")
+      count++;
+  }
+  EXPECT_EQ(count, 2);
+  delete attrs;
+}
+
+TEST_F(AttributesTest, Attributes_Exist) {
+  tidl::Attributes* attrs = new tidl::Attributes();
+  EXPECT_NE(attrs, nullptr);
+  attrs->Add(new tidl::Attribute("key", "value", __LINE__));
+  tidl::Attribute* attr = new tidl::Attribute("key", "value", __LINE__);
+  EXPECT_NE(attr, nullptr);
+  EXPECT_EQ(attrs->Exist(attr), true);
+  delete attr;
+  delete attrs;
+}
index 8d961a9..18163f4 100644 (file)
@@ -22,6 +22,7 @@
 #include "idlc/generator.h"
 #include "idlc/interface.h"
 #include "idlc/structure.h"
+#include "idlc/attribute.h"
 
 class SampleGenerator : public tidl::Generator {
  public:
index d5fddd5..c31778f 100644 (file)
@@ -43,14 +43,14 @@ class InterfaceTest : public testing::Test {
 
 TEST_F(InterfaceTest, Interface_Constructor) {
   tidl::Interface* interface = new tidl::Interface("TestInterface", decls,
-      "", __LINE__);
+      "", new tidl::Attributes(), __LINE__);
   EXPECT_NE(interface, nullptr);
   delete interface;
 }
 
 TEST_F(InterfaceTest, Interface_GetDeclrations) {
   tidl::Interface* interface = new tidl::Interface("TestInterface", decls,
-      "", __LINE__);
+      "", new tidl::Attributes(), __LINE__);
   EXPECT_NE(interface, nullptr);
 
   bool flag = false;
@@ -64,7 +64,7 @@ TEST_F(InterfaceTest, Interface_GetDeclrations) {
 
 TEST_F(InterfaceTest, Interface_GetID) {
   tidl::Interface* interface = new tidl::Interface("TestInterface", decls,
-      "", __LINE__);
+      "", new tidl::Attributes(), __LINE__);
   EXPECT_NE(interface, nullptr);
   EXPECT_EQ(interface->GetID(), "TestInterface");
   delete interface;
@@ -72,7 +72,7 @@ TEST_F(InterfaceTest, Interface_GetID) {
 
 TEST_F(InterfaceTest, Interface_GetType) {
   tidl::Interface* interface = new tidl::Interface("TestInterface", decls,
-      "", __LINE__);
+      "", new tidl::Attributes(), __LINE__);
   EXPECT_NE(interface, nullptr);
   EXPECT_EQ(interface->GetType(), tidl::Interface::TYPE_INTERFACE);
   delete interface;
@@ -81,7 +81,7 @@ TEST_F(InterfaceTest, Interface_GetType) {
 TEST_F(InterfaceTest, Interface_GetLine) {
   unsigned line = __LINE__;
   tidl::Interface* interface = new tidl::Interface("TestInterface", decls,
-      "", line);
+      "", new tidl::Attributes(), line);
   EXPECT_NE(interface, nullptr);
   EXPECT_EQ(interface->GetLine(), line);
   delete interface;
@@ -90,7 +90,7 @@ TEST_F(InterfaceTest, Interface_GetLine) {
 TEST_F(InterfaceTest, Interface_GetComments) {
   std::string comments = "Test Interface";
   tidl::Interface* interface = new tidl::Interface("TestInterface", decls,
-      comments, __LINE__);
+      comments, new tidl::Attributes(), __LINE__);
   EXPECT_NE(interface, nullptr);
   EXPECT_EQ(interface->GetComments(), comments);
   delete interface;
index 75113b1..a0103d4 100644 (file)
@@ -29,6 +29,7 @@ struct Class {
 }
 
 /* Interface School! */
+[privilege = "http://tizen.org/privilege/appmanager.launch", privilege = "http://tizen.org/privilege/appmanager.kill"]
 interface School {
        /* Method section */
        /* Method AddClass */