From 1be24d945ca347c472a8fe28ffa8024397fd60f7 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Fri, 26 Jul 2019 07:46:43 -0700 Subject: [PATCH] Improve formatting of the generated code PiperOrigin-RevId: 260142999 Change-Id: Id170e212012c7e10e3525d031d620a01f90fb388 --- sandboxed_api/tools/generator2/code.py | 21 ++++------- .../tools/generator2/code_test_util.py | 37 +++++-------------- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/sandboxed_api/tools/generator2/code.py b/sandboxed_api/tools/generator2/code.py index 9d539ce..2b913d0 100644 --- a/sandboxed_api/tools/generator2/code.py +++ b/sandboxed_api/tools/generator2/code.py @@ -666,7 +666,7 @@ class Generator(object): def __init__(self, translation_units): # type: (List[cindex.TranslationUnit]) -> None - """Initialize Generator. + """Initializes the generator. Args: translation_units: list of translation_units for analyzed files, @@ -876,20 +876,17 @@ class Generator(object): if embed_dir and embed_name: result.append(Generator.EMBED_INCLUDE.format(embed_dir, embed_name)) - result.append('') if namespaces: + result.append('') for n in namespaces: result.append('namespace {} {{'.format(n)) - result.append('') - result.append('') - if related_types: + result.append('') for t in related_types: result.append(t) result.append('') - result.append('') if embed_dir and embed_name: result.append( @@ -899,15 +896,13 @@ class Generator(object): result.append(' public:') result.append(' explicit {}Api(::sapi::Sandbox* sandbox)' ' : sandbox_(sandbox) {{}}'.format(name)) - result.append(' ::sapi::Sandbox* GetSandbox() {') - result.append(' return sandbox_;') - result.append(' }') - result.append('') - result.append('') + result.append(' // Deprecated') + result.append(' ::sapi::Sandbox* GetSandbox() const { return sandbox(); }') + result.append(' ::sapi::Sandbox* sandbox() const { return sandbox_; }') for f in functions: - result.append(self._format_function(f)) result.append('') + result.append(self._format_function(f)) result.append('') result.append(' private:') @@ -923,7 +918,5 @@ class Generator(object): result.append(Generator.GUARD_END.format(header_guard)) result.append('') - result.append('') - result.append('') return '\n'.join(result) diff --git a/sandboxed_api/tools/generator2/code_test_util.py b/sandboxed_api/tools/generator2/code_test_util.py index 3315cb8..2281ae9 100644 --- a/sandboxed_api/tools/generator2/code_test_util.py +++ b/sandboxed_api/tools/generator2/code_test_util.py @@ -24,16 +24,12 @@ CODE_GOLD = """// AUTO-GENERATED by the Sandboxed API generator. namespace sapi { namespace Tests { - - - class TestApi { public: explicit TestApi(::sapi::Sandbox* sandbox) : sandbox_(sandbox) {} - ::sapi::Sandbox* GetSandbox() { - return sandbox_; - } - + // Deprecated + ::sapi::Sandbox* GetSandbox() const { return sandbox(); } + ::sapi::Sandbox* sandbox() const { return sandbox_; } // int function_a(int, int) ::sapi::StatusOr function_a(int x, int y) { @@ -112,15 +108,12 @@ class TestApi { return ::sapi::OkStatus(); } - private: ::sapi::Sandbox* sandbox_; }; } // namespace Tests } // namespace sapi - - """ CODE_GOLD_MAPPED = """// AUTO-GENERATED by the Sandboxed API generator. @@ -132,18 +125,15 @@ CODE_GOLD_MAPPED = """// AUTO-GENERATED by the Sandboxed API generator. namespace sapi { namespace Tests { - typedef unsigned int uint; typedef uint * uintp; - class TestApi { public: explicit TestApi(::sapi::Sandbox* sandbox) : sandbox_(sandbox) {} - ::sapi::Sandbox* GetSandbox() { - return sandbox_; - } - + // Deprecated + ::sapi::Sandbox* GetSandbox() const { return sandbox(); } + ::sapi::Sandbox* sandbox() const { return sandbox_; } // uint function(uintp) ::sapi::StatusOr function(::sapi::v::Ptr* a) { @@ -153,15 +143,12 @@ class TestApi { return ret.GetValue(); } - private: ::sapi::Sandbox* sandbox_; }; } // namespace Tests } // namespace sapi - - """ CODE_ENUM_GOLD = """// AUTO-GENERATED by the Sandboxed API generator. @@ -173,20 +160,17 @@ CODE_ENUM_GOLD = """// AUTO-GENERATED by the Sandboxed API generator. namespace sapi { namespace Tests { - enum ProcessStatus { \tOK = 0 , \tERROR = 1 , }; - class TestApi { public: explicit TestApi(::sapi::Sandbox* sandbox) : sandbox_(sandbox) {} - ::sapi::Sandbox* GetSandbox() { - return sandbox_; - } - + // Deprecated + ::sapi::Sandbox* GetSandbox() const { return sandbox(); } + ::sapi::Sandbox* sandbox() const { return sandbox_; } // ProcessStatus ProcessDatapoint(ProcessStatus) ::sapi::StatusOr ProcessDatapoint(ProcessStatus status) { @@ -197,13 +181,10 @@ class TestApi { return static_cast(ret.GetValue()); } - private: ::sapi::Sandbox* sandbox_; }; } // namespace Tests } // namespace sapi - - """