From 27a14ee0da46cb682da5e78affacd06d11bc973f Mon Sep 17 00:00:00 2001 From: Joseph Snyder Date: Fri, 8 Jan 2021 17:01:47 -0500 Subject: [PATCH 1/2] Add declaration object for deprecation message. Pass the newly captured deprecation information from CastXML to the declarations of those objects. --- pygccxml/declarations/declaration.py | 9 +++ pygccxml/parser/scanner.py | 8 +++ unittests/data/test_deprecation.hpp | 21 +++++++ unittests/test_all.py | 2 + unittests/test_deprecation.py | 87 ++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 unittests/data/test_deprecation.hpp create mode 100644 unittests/test_deprecation.py diff --git a/pygccxml/declarations/declaration.py b/pygccxml/declarations/declaration.py index e9367f956..0698d7429 100644 --- a/pygccxml/declarations/declaration.py +++ b/pygccxml/declarations/declaration.py @@ -39,6 +39,7 @@ def __init__( self._partial_name = None self._decorated_name = None self._comment = comment.comment_t() + self._deprecation = None def __str__(self): """ @@ -348,3 +349,11 @@ def comment(self): @comment.setter def comment(self, comment): self._comment = comment + + @property + def deprecation(self): + return self._deprecation + + @deprecation.setter + def deprecation(self, deprecation): + self._deprecation = deprecation diff --git a/pygccxml/parser/scanner.py b/pygccxml/parser/scanner.py index 3a887e63c..e739fb42f 100644 --- a/pygccxml/parser/scanner.py +++ b/pygccxml/parser/scanner.py @@ -30,6 +30,7 @@ XML_AN_CVS_REVISION = "cvs_revision" XML_AN_CASTXML_FORMAT = "format" XML_AN_DEFAULT = "default" +XML_AN_DEPRECATION = "deprecation" XML_AN_END_COLUMN = "end_column" XML_AN_END_LINE = "end_line" XML_AN_END_OFFSET = "end_offset" @@ -309,6 +310,7 @@ def startElement(self, name, attrs): if isinstance(obj, declarations.class_t): self.__read_bases(obj, attrs) self.__read_artificial(obj, attrs) + self.__read_deprecation(obj, attrs) self.__read_mangled(obj, attrs) self.__read_attributes(obj, attrs) @@ -384,6 +386,12 @@ def __read_mangled(self, decl, attrs): mangled = mangled[:self.__mangled_suffix_len] decl.mangled = mangled + def __read_deprecation(self, decl, attrs): + deprecation = attrs.get(XML_AN_DEPRECATION) + # the following patch is defined here for performance reasons + if isinstance(deprecation, str): + decl.deprecation = deprecation + def __read_attributes(self, decl, attrs): attribute = attrs.get(XML_AN_ATTRIBUTES) if attribute is not None and \ diff --git a/unittests/data/test_deprecation.hpp b/unittests/data/test_deprecation.hpp new file mode 100644 index 000000000..3f80aae10 --- /dev/null +++ b/unittests/data/test_deprecation.hpp @@ -0,0 +1,21 @@ +// Copyright 2021 Insight Software Consortium. +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +namespace deprecation { + + + class __attribute__((deprecated("Test class Deprecated"))) test + { + public: + test(); + __attribute__((deprecated("One arg constructor is Deprecated"))) test(int a); + + int hello(); + void __attribute__((deprecated("Function is deprecated"))) goodbye(); + }; + enum __attribute__((deprecated("Enumeration is Deprecated"))) com_enum { + One = 1, + Two = 2 + }; +} \ No newline at end of file diff --git a/unittests/test_all.py b/unittests/test_all.py index 263667ca4..9a31f3155 100644 --- a/unittests/test_all.py +++ b/unittests/test_all.py @@ -84,6 +84,7 @@ from . import test_hash from . import test_null_comparison from . import test_comments +from . import test_deprecation from . import test_warn_missing_include_dirs from . import test_overrides @@ -161,6 +162,7 @@ test_hash, test_null_comparison, test_comments, + test_deprecation, test_warn_missing_include_dirs, test_overrides, ] diff --git a/unittests/test_deprecation.py b/unittests/test_deprecation.py new file mode 100644 index 000000000..d69f2d4e2 --- /dev/null +++ b/unittests/test_deprecation.py @@ -0,0 +1,87 @@ +# Copyright 2014-2020 Insight Software Consortium. +# Copyright 2004-2009 Roman Yakovenko. +# Distributed under the Boost Software License, Version 1.0. +# See http://www.boost.org/LICENSE_1_0.txt + +import unittest + +from . import parser_test_case + +from pygccxml import parser +from pygccxml import declarations + + +class Test(parser_test_case.parser_test_case_t): + global_ns = None + + def __init__(self, *args): + parser_test_case.parser_test_case_t.__init__(self, *args) + self.header = "test_deprecation.hpp" + self.global_ns = None + self.config.castxml_epic_version = 1 + + def _check_text_content(self, list, comment_decl): + if comment_decl: + self.assertEqual(list, comment_decl) + else: + print("No text in comment to check") + + def setUp(self): + + if not self.global_ns: + decls = parser.parse([self.header], self.config) + Test.global_ns = declarations.get_global_namespace(decls) + Test.xml_generator_from_xml_file = \ + self.config.xml_generator_from_xml_file + self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file + + self.global_ns = Test.global_ns + + def test(self): + """ + Check the comment parsing + """ + + if self.config.castxml_epic_version != 1: + # Run this test only with castxml epic version == 1 + return + tnamespace = self.global_ns.namespace("deprecation") + + tenumeration = tnamespace.enumeration("com_enum") + self.assertIn("deprecation", dir(tenumeration)) + self._check_text_content('Enumeration is Deprecated', + tenumeration.deprecation) + + tclass = tnamespace.class_("test") + self.assertIn("deprecation", dir(tclass)) + self._check_text_content("Test class Deprecated", tclass.deprecation) + + tmethod = tclass.member_functions()[0] + tmethod_dep = tclass.member_functions()[1] + + self.assertIn("deprecation", dir(tmethod)) + self.assertIsNone(tmethod.deprecation) + self._check_text_content("Function is deprecated", + tmethod_dep.deprecation) + + tconstructor = tclass.constructors()[0] + tconstructor_dep = tclass.constructors()[1] + + self.assertIsNone(tconstructor.deprecation) + self.assertIn("deprecation", dir(tconstructor_dep)) + self._check_text_content("One arg constructor is Deprecated", + tconstructor_dep.deprecation) + + +def create_suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(Test)) + return suite + + +def run_suite(): + unittest.TextTestRunner(verbosity=2).run(create_suite()) + + +if __name__ == "__main__": + run_suite() From 135f8c81c16069bf0354ab6dd75f64db2aa5d123 Mon Sep 17 00:00:00 2001 From: Joseph Snyder Date: Wed, 27 Jan 2021 15:56:48 -0500 Subject: [PATCH 2/2] Make requested updates Fix copy/paste copyright header to only be for the year 2021 Fix variable names in the text checking function. Use the latest version of CastXML for testing too. --- .github/workflows/tests.yml | 4 ++-- unittests/test_deprecation.py | 12 +++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 666280b27..fc68b4088 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -75,11 +75,11 @@ jobs: - name: Setup castxml for Linux if: matrix.os == 'ubuntu-18.04' && matrix.castxml == 'castxml' run: | - wget -q -O - https://data.kitware.com/api/v1/file/hashsum/sha512/b580374e48d4df68f8ebef6d0e71cd8cc3f2dfa28f0090e07cc043d11cee308aeea480092fe2e93f2d4c58f822a5c013c1c7121964372d28ee4069949a378b4c/download | tar zxf - -C ~/ + wget -q -O - https://data.kitware.com/api/v1/file/hashsum/sha512/bdbb67a10c5f8d1b738cd19cb074f409d4803e8077cb8c1072ef4eaf738fa871a73643f9c8282d58cae28d188df842c82ad6620b6d590b0396a0172a27438dce/download | tar zxf - -C ~/ - name: Setup castxml for Mac if: matrix.os == 'macos-latest' run: | - wget -q -O - https://data.kitware.com/api/v1/file/hashsum/sha512/e5962372508abd295f8a8110a20142bcbd93c235f72afba34b0abb3918a623f27465a7674b5532320e770f56fddb99019f5c47b254cea9f862a2df35630c2879/download | tar zxf - -C ~/ + wget -q -O - https://data.kitware.com/api/v1/file/hashsum/sha512/5d937e938f7b882a3a3e7941e68f8312d0898aaf2082e00003dd362b1ba70b98b0a08706a1be28e71652a6a0f1e66f89768b5eaa20e5a100592d5b3deefec3f0/download | tar zxf - -C ~/ - name: Setup castxml config if: matrix.compiler == 'gcc' && matrix.version == '7' run: mv unittests/configs/gcc7.cfg unittests/xml_generator.cfg; diff --git a/unittests/test_deprecation.py b/unittests/test_deprecation.py index d69f2d4e2..a95bfd067 100644 --- a/unittests/test_deprecation.py +++ b/unittests/test_deprecation.py @@ -1,5 +1,4 @@ -# Copyright 2014-2020 Insight Software Consortium. -# Copyright 2004-2009 Roman Yakovenko. +# Copyright 2021 Insight Software Consortium. # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt @@ -20,14 +19,13 @@ def __init__(self, *args): self.global_ns = None self.config.castxml_epic_version = 1 - def _check_text_content(self, list, comment_decl): - if comment_decl: - self.assertEqual(list, comment_decl) + def _check_text_content(self, desired_text, deprecation_string): + if deprecation_string: + self.assertEqual(desired_text, deprecation_string) else: - print("No text in comment to check") + print("No text in deprecation attribute to check") def setUp(self): - if not self.global_ns: decls = parser.parse([self.header], self.config) Test.global_ns = declarations.get_global_namespace(decls)