diff options
author | Hidehiko Abe <hidehiko@google.com> | 2018-02-15 11:09:33 +0900 |
---|---|---|
committer | Hidehiko Abe <hidehiko@google.com> | 2018-02-19 13:31:47 +0900 |
commit | c0d6a3235da44dc29ded5c773db7df0170611cbb (patch) | |
tree | f72f6679ce6e841c2613caf596098be9ea63b89f | |
parent | 24af70ee32b234bfcd6c8dd09030b1f8d5f8348e (diff) | |
download | libchrome-c0d6a3235da44dc29ded5c773db7df0170611cbb.tar.gz |
Deal with -Wunused-parameter in libchrome build rule.
So, now we do not need modification in libchrome to workaround
the error.
Bug: 73270448
Test: Build locally. Treehugger.
Change-Id: I38fe79c1e01fd39a4bdd7121bb58b9b6d9a3da89
-rw-r--r-- | Android.bp | 20 | ||||
-rw-r--r-- | base/threading/thread_restrictions.h | 4 | ||||
-rwxr-xr-x | libchrome_tools/include_generator.py | 93 |
3 files changed, 114 insertions, 3 deletions
diff --git a/Android.bp b/Android.bp index bf4809e30e..202076beec 100644 --- a/Android.bp +++ b/Android.bp @@ -15,6 +15,18 @@ // Common defaults // ======================================================== +// Using Chrome header files directly could cause -Wunused-parameter errors, +// and this is workaround. Please find the document in include_generator.py +// for details. +gensrcs { + name: "libchrome-include", + cmd: "$(location libchrome_tools/include_generator.py) $(in) $(out)", + tool_files: ["libchrome_tools/include_generator.py"], + export_include_dirs: ["."], + srcs: ["**/*.h"], + output_extension: "h", +} + cc_defaults { name: "libchrome-defaults", // Set clang to "true" to force clang or "false" to force gcc. @@ -24,12 +36,18 @@ cc_defaults { "-Wall", "-Werror", "-Wno-deprecated-declarations", + "-Wno-unused-parameter", ], include_dirs: [ "external/valgrind/include", "external/valgrind", ], - export_include_dirs: ["."], + + // Note: Although the generated header files are exported here, in building + // libchrome, "." has priority (unlike building projects using libchrome), + // so the raw header files are used for them. + generated_headers: ["libchrome-include"], + export_generated_headers: ["libchrome-include"], target: { host: { cflags: [ diff --git a/base/threading/thread_restrictions.h b/base/threading/thread_restrictions.h index a86dd452b8..ad8b4ba629 100644 --- a/base/threading/thread_restrictions.h +++ b/base/threading/thread_restrictions.h @@ -163,9 +163,9 @@ class BASE_EXPORT ThreadRestrictions { #else // Inline the empty definitions of these functions so that they can be // compiled out. - static bool SetIOAllowed(bool) { return true; } + static bool SetIOAllowed(bool allowed) { return true; } static void AssertIOAllowed() {} - static bool SetSingletonAllowed(bool) { return true; } + static bool SetSingletonAllowed(bool allowed) { return true; } static void AssertSingletonAllowed() {} static void DisallowWaiting() {} static void AssertWaitAllowed() {} diff --git a/libchrome_tools/include_generator.py b/libchrome_tools/include_generator.py new file mode 100755 index 0000000000..efffd3952c --- /dev/null +++ b/libchrome_tools/include_generator.py @@ -0,0 +1,93 @@ +#!/usr/bin/python + +# Copyright (C) 2018 The Android Open Source Project +# +# 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. + +"""Generates wrapped include files to workaround -Wunused-parameter errors. + +In Chrome repository, "-Wunused-parameter" is disabled, and several header +files in Chrome repository have actually unused-parameter. +One of the typical scenarios is; in Chrome, Observer class is often defined +as follows: + +class Foo { + public: + class Observer { + public: + virtual void OnSomeEvent(EventArg arg) {} + virtual void OnAnotherEvent(EventArg arg) {} + ... + }; + ... +}; + +Here, On...Event() methods do nothing by default, and subclasses will override +only necessary ones. +In this use case, argument names can also work as documentation, and overrides +can use these good interface-defined default names as a starting point for +their implementation. + +On the other hand, in Android, -Wunused-parameter is enabled by default. +Thus, if such a project includes header files from libchrome, it could cause +a compile error (by the warning and "-Werror"). + +To avoid such a situation, libchrome exports include files wrapped by the +pragmas as follows. + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +${actual_include_file_content} +#pragma GCC diagnostic pop + +so, the unused-parameter warning generated by the libchrome include headers +will be ignored. +Note that these GCC pragmas are also supported by clang for compatibility. cf) +https://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas + +Usage: include_generator.py $(in) $(out) +""" + +import sys + + +def _generate(input_path, output_path): + """Generates a include file wrapped by pragmas. + + Reads the file at |input_path| and output the content with wrapping by + #pragma to ignore unused-parameter warning into the file at |output_path|. + If the parent directories of |output_path| do not exist, creates them. + + Args: + input_path: Path to the source file. Expected this is a chrome's header + file. + output_path: Path to the output file. + """ + with open(input_path, 'r') as f: + content = f.read() + + with open(output_path, 'w') as f: + f.writelines([ + '// Generated by %s\n' % sys.argv[0], + '#pragma GCC diagnostic push\n' + '#pragma GCC diagnostic ignored "-Wunused-parameter"\n', + content, + '#pragma GCC diagnostic pop\n']) + + +def main(): + _generate(*sys.argv[1:]) + + +if __name__ == '__main__': + main() |