-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc++] Add a utility for checking the output of commands #65917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
philnik777
wants to merge
1
commit into
llvm:main
Choose a base branch
from
philnik777:add_check_output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
libcxx/test/libcxx/algorithms/alg.modifying.operations/copy_move_codegen.sh.cpp
This file contains hidden or 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,23 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// This test is checking the LLVM IR | ||
// REQUIRES: clang | ||
|
||
// UNSUPPORTED: asan, tsan, ubsan, msan | ||
|
||
// RUN: %{cxx} %s %{flags} %{compile_flags} -O3 -c -S -emit-llvm -o - | %{check-output} | ||
|
||
#include <algorithm> | ||
|
||
int* test1(int* first, int* last, int* out) { | ||
// CHECK: define | ||
// CHECK-SAME: test1 | ||
// CHECK: tail call void @llvm.memmove | ||
return std::copy(first, last, out); | ||
} |
29 changes: 29 additions & 0 deletions
29
libcxx/test/libcxx/selftest/clang_tidy/proper_version_checks.sh.cpp
This file contains hidden or 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,29 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// REQUIRES: has-clang-tidy | ||
|
||
// RUN: %{clang-tidy} %s -header-filter=.* --checks='-*,libcpp-cpp-version-check' --load=%{test-tools-dir}/clang_tidy_checks/libcxx-tidy.plugin -- %{compile_flags} -fno-modules 2>&1 | %{check-output} | ||
|
||
#include <__config> | ||
|
||
// CHECK: warning: _LIBCPP_STD_VER >= version should be used instead of _LIBCPP_STD_VER > prev_version | ||
#if _LIBCPP_STD_VER > 14 | ||
#endif | ||
|
||
// CHECK: warning: Use _LIBCPP_STD_VER instead of __cplusplus to constrain based on the C++ version | ||
#if __cplusplus >= 201103L | ||
#endif | ||
|
||
// CHECK: warning: _LIBCPP_STD_VER >= 11 is always true. Did you mean '#ifndef _LIBCPP_CXX03_LANG'? | ||
#if _LIBCPP_STD_VER >= 11 | ||
#endif | ||
|
||
// CHECK: warning: Not a valid value for _LIBCPP_STD_VER. Use 14, 17, 20, 23, or 26 | ||
#if _LIBCPP_STD_VER >= 12 | ||
#endif |
29 changes: 29 additions & 0 deletions
29
libcxx/test/libcxx/utilities/utility/unreachable.codegen.sh.cpp
This file contains hidden or 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,29 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// This test is checking the LLVM IR | ||
// REQUIRES: clang | ||
|
||
// UNSUPPORTED: asan, tsan, ubsan, msan | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 | ||
|
||
// RUN: %{cxx} %s %{flags} %{compile_flags} -Wno-missing-noreturn -O3 -c -S -emit-llvm -o - | %{check-output} | ||
|
||
// This is an assert instead in debug mode | ||
// UNSUPPORTED: libcpp-hardening-mode=debug | ||
|
||
#include <utility> | ||
|
||
void test() { | ||
// CHECK: define dso_local void | ||
// CHECK-SAME: test | ||
// CHECK-NEXT: unreachable | ||
// CHECK-NEXT: } | ||
std::unreachable(); | ||
} |
This file contains hidden or 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 hidden or 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,17 @@ | ||
|
||
add_executable(check_output check_output.cpp) | ||
|
||
# put the binary into <build>/bin | ||
set_target_properties(check_output PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${LIBCXX_BINARY_DIR}/bin") | ||
|
||
# set the language mode | ||
set_target_properties(check_output PROPERTIES | ||
CXX_STANDARD 23 | ||
CXX_STANDARD_REQUIRED YES | ||
CXX_EXTENSIONS NO) | ||
|
||
# The standard library we link against is most likely not built against MSan, so we have to disable it. | ||
target_compile_options(check_output PRIVATE -Werror=missing-prototypes -fno-sanitize=memory) | ||
target_link_options(check_output PRIVATE -fno-sanitize=memory) | ||
|
||
add_dependencies(cxx-test-depends check_output) |
This file contains hidden or 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,129 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <algorithm> | ||
#include <fstream> | ||
#include <functional> | ||
#include <iostream> | ||
#include <iterator> | ||
#include <ranges> | ||
#include <string> | ||
|
||
using namespace std::string_view_literals; | ||
|
||
enum class Result { | ||
success, | ||
mismatch, | ||
no_match_found, | ||
unknown_matcher, | ||
invalid_use, | ||
}; | ||
|
||
namespace co { | ||
namespace { | ||
[[noreturn]] void exit(Result result) { std::exit(static_cast<int>(result)); } | ||
|
||
[[noreturn]] void print_failure(int line, std::string_view stdin_content, std::string_view matcher) { | ||
std::cout << "Failed to match: `" << matcher << "`\nRemaining data:\n" << stdin_content << '\n'; | ||
co::exit(Result::mismatch); | ||
} | ||
|
||
bool is_newline(char c) { return c == '\n'; } | ||
|
||
bool isblank(char c) { return std::isblank(c); } | ||
|
||
bool consume_front(std::string_view& sv, std::string_view start) { | ||
if (!sv.starts_with(start)) | ||
return false; | ||
sv.remove_prefix(start.size()); | ||
return true; | ||
} | ||
} // namespace | ||
} // namespace co | ||
|
||
int main(int argc, char** argv) { | ||
if (argc != 2) { | ||
std::cerr << "check_output has to be used as `<command> | ./check_output %s`\n"; | ||
co::exit(Result::invalid_use); | ||
} | ||
|
||
std::string file_content_data = [&] { | ||
std::ifstream file(argv[1]); | ||
if (!file) { | ||
std::cerr << "Failed to open file: " << argv[1] << '\n'; | ||
co::exit(Result::invalid_use); | ||
} | ||
return std::string{std::istreambuf_iterator<char>{file}, {}}; | ||
}(); | ||
std::string_view file_content = file_content_data; // Don't copy the data around all the time | ||
|
||
std::string stdin_content_data = [&] { | ||
std::cin >> std::noskipws; | ||
return std::string{std::istream_iterator<char>{std::cin}, {}}; | ||
}(); | ||
std::string_view stdin_content = stdin_content_data; // Don't copy the data around all the time | ||
|
||
size_t match_count = 0; | ||
auto drop_blanks = std::views::drop_while(co::isblank); | ||
|
||
while (!file_content.empty()) { | ||
auto marker = std::ranges::search(file_content, "// CHECK"sv); | ||
if (marker.empty()) { | ||
if (match_count == 0) { | ||
std::cerr << "No matcher found!\n"; | ||
co::exit(Result::no_match_found); | ||
} | ||
co::exit(Result::success); | ||
} | ||
file_content.remove_prefix(marker.end() - file_content.begin()); | ||
|
||
const auto get_match = [&]() { | ||
return std::string_view(file_content.begin(), std::ranges::find(file_content, '\n')); | ||
}; | ||
|
||
if (co::consume_front(file_content, ":")) { | ||
auto match = get_match(); | ||
auto found = std::ranges::search( | ||
stdin_content | std::views::drop_while(std::not_fn(co::is_newline)) | std::views::drop(1), | ||
match | drop_blanks); | ||
if (found.empty()) { | ||
co::print_failure(1, stdin_content, match); | ||
} | ||
++match_count; | ||
stdin_content.remove_prefix(found.end() - stdin_content.begin()); | ||
} else if (co::consume_front(file_content, "-SAME:")) { | ||
auto match = get_match(); | ||
auto haystack = std::string_view(stdin_content.begin(), std::ranges::find(stdin_content, '\n')); | ||
auto found = std::ranges::search(haystack, match | drop_blanks); | ||
if (found.empty()) { | ||
co::print_failure(1, stdin_content, match); | ||
} | ||
stdin_content.remove_prefix(found.end() - stdin_content.begin()); | ||
} else if (co::consume_front(file_content, "-NEXT:")) { | ||
auto match = get_match(); | ||
auto haystack = [&] { | ||
auto begin = std::ranges::find(stdin_content, '\n'); | ||
if (begin == stdin_content.end()) { | ||
co::print_failure(1, stdin_content, match); | ||
} | ||
++begin; | ||
return std::string_view(begin, std::ranges::find(begin, stdin_content.end(), '\n')); | ||
}(); | ||
auto found = std::ranges::search(haystack, match | drop_blanks); | ||
if (found.empty()) | ||
co::print_failure(1, stdin_content, match); | ||
stdin_content.remove_prefix(found.end() - stdin_content.begin()); | ||
} else { | ||
std::cerr << "Unkown matcher type " | ||
<< std::string_view(file_content.begin(), std::ranges::find(file_content, ':')) << " found\n"; | ||
co::exit(Result::unknown_matcher); | ||
} | ||
} | ||
|
||
co::exit(Result::success); | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.