From 4dfa0a0383f8efb4ab07b7c6843006bdd4c74ec9 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 4 Dec 2019 17:29:05 +0300 Subject: [PATCH] bindings: basic support for #if preprocessor directives - #if 0 - #ifdef __OPENCV_BUILD --- .../core/misc/java/src/cpp/core_manual.hpp | 4 +-- .../misc/java/src/cpp/features2d_manual.hpp | 2 +- modules/python/src2/hdr_parser.py | 27 +++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/modules/core/misc/java/src/cpp/core_manual.hpp b/modules/core/misc/java/src/cpp/core_manual.hpp index 82d460fa53..4ca87e836a 100644 --- a/modules/core/misc/java/src/cpp/core_manual.hpp +++ b/modules/core/misc/java/src/cpp/core_manual.hpp @@ -9,7 +9,7 @@ CV_EXPORTS_W void setErrorVerbosity(bool verbose); } -#if 0 +#ifdef OPENCV_BINDINGS_PARSER namespace cv { @@ -30,4 +30,4 @@ CV_EXPORTS_W void min(InputArray src1, Scalar src2, OutputArray dst); CV_EXPORTS_W void max(InputArray src1, Scalar src2, OutputArray dst); } -#endif //0 +#endif diff --git a/modules/features2d/misc/java/src/cpp/features2d_manual.hpp b/modules/features2d/misc/java/src/cpp/features2d_manual.hpp index d9fd3fbded..467f5efc99 100644 --- a/modules/features2d/misc/java/src/cpp/features2d_manual.hpp +++ b/modules/features2d/misc/java/src/cpp/features2d_manual.hpp @@ -294,7 +294,7 @@ private: Ptr wrapped; }; -#if 0 +#ifdef OPENCV_BINDINGS_PARSER //DO NOT REMOVE! The block is required for sources parser enum { diff --git a/modules/python/src2/hdr_parser.py b/modules/python/src2/hdr_parser.py index 352ebe3d1f..2bbaf23b8b 100755 --- a/modules/python/src2/hdr_parser.py +++ b/modules/python/src2/hdr_parser.py @@ -793,6 +793,7 @@ class CppHeaderParser(object): COMMENT = 1 # inside a multi-line comment DIRECTIVE = 2 # inside a multi-line preprocessor directive DOCSTRING = 3 # inside a multi-line docstring + DIRECTIVE_IF_0 = 4 # inside a '#if 0' directive state = SCAN @@ -802,6 +803,8 @@ class CppHeaderParser(object): self.lineno = 0 self.wrap_mode = wmode + depth_if_0 = 0 + for l0 in linelist: self.lineno += 1 #print(state, self.lineno, l0) @@ -813,8 +816,28 @@ class CppHeaderParser(object): # fall through to the if state == DIRECTIVE check if state == DIRECTIVE: - if not l.endswith("\\"): - state = SCAN + if l.endswith("\\"): + continue + state = SCAN + l = re.sub(r'//(.+)?', '', l).strip() # drop // comment + if l == '#if 0' or l == '#if defined(__OPENCV_BUILD)' or l == '#ifdef __OPENCV_BUILD': + state = DIRECTIVE_IF_0 + depth_if_0 = 1 + continue + + if state == DIRECTIVE_IF_0: + if l.startswith('#'): + l = l[1:].strip() + if l.startswith("if"): + depth_if_0 += 1 + continue + if l.startswith("endif"): + depth_if_0 -= 1 + if depth_if_0 == 0: + state = SCAN + else: + # print('---- {:30s}:{:5d}: {}'.format(hname[-30:], self.lineno, l)) + pass continue if state == COMMENT: