forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: API boosting support for rewriting elaborated types. (envoypro…
…xy#9375) This PR introduces API boosting support for ElaboratedTypeLoc, one of a number of Clang AST nodes that we need to analyze and rewrite to move to the latest type according to the type database. The approach taken is to generate a replacements YAML file suitable for consumption by clang-apply-replacements; this is how clang-tidy works internally. Replacements are a series of patch like file transformation operations, Clang has nice support for building replacement sets and serializing to YAML. This PR also starts to split out api_booster into more modules (just some utils to start with), introduces some more unit tests and a golden C++ source file test framework. Risk level: Low Testing: New unit and e2e golden tests added. Part of envoyproxy#8082 Signed-off-by: Harvey Tuch <htuch@google.com> Signed-off-by: Prakhar <prakhar_au@yahoo.com>
- Loading branch information
Showing
14 changed files
with
409 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Golden C++ source tests for API boosting. This is effectively a test for the | ||
# combination of api_boost.py, the Clang libtooling-based | ||
# tools/clang_tools/api_booster, as well as the type whisperer and API type | ||
# database. | ||
|
||
import logging | ||
import os | ||
import pathlib | ||
import shutil | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
import api_boost | ||
|
||
# List of test in the form [(file_name, explanation)] | ||
TESTS = [ | ||
('elaborated_type.cc', 'ElaboratedTypeLoc type upgrades'), | ||
] | ||
|
||
TESTDATA_PATH = 'tools/api_boost/testdata' | ||
|
||
|
||
def Diff(some_path, other_path): | ||
result = subprocess.run(['diff', '-u', some_path, other_path], capture_output=True) | ||
if result.returncode == 0: | ||
return None | ||
return result.stdout.decode('utf-8') + result.stderr.decode('utf-8') | ||
|
||
|
||
if __name__ == '__main__': | ||
# Accumulated error messages. | ||
logging.basicConfig(format='%(message)s') | ||
messages = [] | ||
|
||
# Run API booster against test artifacts in a directory relative to workspace. | ||
# We use a temporary copy as the API booster does in-place rewriting. | ||
with tempfile.TemporaryDirectory(dir=pathlib.Path.cwd()) as path: | ||
# Setup temporary tree. | ||
shutil.copy(os.path.join(TESTDATA_PATH, 'BUILD'), path) | ||
for filename, _ in TESTS: | ||
shutil.copy(os.path.join(TESTDATA_PATH, filename), path) | ||
|
||
# Run API booster. | ||
api_boost.ApiBoostTree([str(pathlib.Path(path).relative_to(pathlib.Path.cwd()))], | ||
generate_compilation_database=True, | ||
build_api_booster=True, | ||
debug_log=True) | ||
|
||
# Validate output against golden files. | ||
for filename, description in TESTS: | ||
delta = Diff(os.path.join(TESTDATA_PATH, filename + '.gold'), os.path.join(path, filename)) | ||
if delta is not None: | ||
messages.append('Non-empty diff for %s (%s):\n%s\n' % (filename, description, delta)) | ||
|
||
if len(messages) > 0: | ||
logging.error('FAILED:\n{}'.format('\n'.join(messages))) | ||
sys.exit(1) | ||
logging.warning('PASS') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_library", | ||
"envoy_package", | ||
) | ||
|
||
envoy_package() | ||
|
||
envoy_cc_library( | ||
name = "elaborated_type", | ||
srcs = ["elaborated_type.cc"], | ||
deps = ["@envoy_api//envoy/config/overload/v2alpha:pkg_cc_proto"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "envoy/config/overload/v2alpha/overload.pb.h" | ||
|
||
class ThresholdTriggerImpl { | ||
public: | ||
ThresholdTriggerImpl(const envoy::config::overload::v2alpha::ThresholdTrigger& /*config*/) {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "envoy/config/overload/v3alpha/overload.pb.h" | ||
|
||
class ThresholdTriggerImpl { | ||
public: | ||
ThresholdTriggerImpl(const envoy::config::overload::v3alpha::ThresholdTrigger& /*config*/) {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
load("//clang_tools/support:clang_tools.bzl", "envoy_clang_tools_cc_binary") | ||
load( | ||
"//clang_tools/support:clang_tools.bzl", | ||
"clang_tools_cc_binary", | ||
"clang_tools_cc_library", | ||
"clang_tools_cc_test", | ||
) | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_clang_tools_cc_binary( | ||
clang_tools_cc_binary( | ||
name = "api_booster", | ||
srcs = ["main.cc"], | ||
deps = [ | ||
":proto_cxx_utils_lib", | ||
"@clang_tools//:clang_astmatchers", | ||
"@clang_tools//:clang_basic", | ||
"@clang_tools//:clang_tooling", | ||
"@envoy//tools/type_whisperer:api_type_db_lib", | ||
], | ||
) | ||
|
||
clang_tools_cc_library( | ||
name = "proto_cxx_utils_lib", | ||
hdrs = ["proto_cxx_utils.h"], | ||
deps = ["@com_google_absl//absl/strings"], | ||
) | ||
|
||
clang_tools_cc_test( | ||
name = "proto_cxx_utils_test", | ||
srcs = ["proto_cxx_utils_test.cc"], | ||
deps = [":proto_cxx_utils_lib"], | ||
) |
Oops, something went wrong.