-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
[libc++] Improve diagnostic when failing to parse the tzdb #122125
Open
ldionne
wants to merge
3
commits into
llvm:main
Choose a base branch
from
ldionne:review/tzdb-improve-error
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.
+28
−12
Conversation
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
Providing the character that we failed on is helpful for figuring out what's going wrong in the tztb.
llvmbot
added
the
libc++
libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
label
Jan 8, 2025
@llvm/pr-subscribers-libcxx Author: Louis Dionne (ldionne) ChangesProviding the character that we failed on is helpful for figuring out what's going wrong in the tztb. Full diff: https://github.com/llvm/llvm-project/pull/122125.diff 3 Files Affected:
diff --git a/libcxx/src/experimental/tzdb.cpp b/libcxx/src/experimental/tzdb.cpp
index 638d45f69e033e..10f7001d74806b 100644
--- a/libcxx/src/experimental/tzdb.cpp
+++ b/libcxx/src/experimental/tzdb.cpp
@@ -8,6 +8,7 @@
// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
+#include <__assert>
#include <algorithm>
#include <cctype>
#include <chrono>
@@ -97,14 +98,23 @@ static void __skip(istream& __input, string_view __suffix) {
}
static void __matches(istream& __input, char __expected) {
- if (std::tolower(__input.get()) != __expected)
- std::__throw_runtime_error((string("corrupt tzdb: expected character '") + __expected + '\'').c_str());
+ _LIBCPP_ASSERT_INTERNAL(std::islower(__expected), "lowercase characters only here!");
+ char __c = __input.get();
+ if (std::tolower(__c) != __expected)
+ std::__throw_runtime_error(
+ (string("corrupt tzdb: expected character '") + __expected + "', got '" + __c + "' instead").c_str());
}
static void __matches(istream& __input, string_view __expected) {
- for (auto __c : __expected)
- if (std::tolower(__input.get()) != __c)
- std::__throw_runtime_error((string("corrupt tzdb: expected string '") + string(__expected) + '\'').c_str());
+ for (auto __c : __expected) {
+ _LIBCPP_ASSERT_INTERNAL(std::islower(__c), "lowercase strings only here!");
+ char __actual = __input.get();
+ if (std::tolower(__actual) != __c)
+ std::__throw_runtime_error(
+ (string("corrupt tzdb: expected character '") + __c + "' from string '" + string(__expected) + "', got '" +
+ __actual + "' instead")
+ .c_str());
+ }
}
[[nodiscard]] static string __parse_string(istream& __input) {
diff --git a/libcxx/test/libcxx/time/time.zone/time.zone.db/rules.pass.cpp b/libcxx/test/libcxx/time/time.zone/time.zone.db/rules.pass.cpp
index 7d9759320c535b..237a206b3a95bd 100644
--- a/libcxx/test/libcxx/time/time.zone/time.zone.db/rules.pass.cpp
+++ b/libcxx/test/libcxx/time/time.zone/time.zone.db/rules.pass.cpp
@@ -20,9 +20,10 @@
// ADDITIONAL_COMPILE_FLAGS: -I %{libcxx-dir}/src/experimental/include
#include <chrono>
+#include <cstdio>
#include <fstream>
-#include <string>
#include <string_view>
+#include <string>
#include <variant>
#include "assert_macros.h"
@@ -96,7 +97,7 @@ static void test_invalid() {
test_exception("R r 0 mix", "corrupt tzdb: expected whitespace");
test_exception("R r 0 1", "corrupt tzdb: expected whitespace");
- test_exception("R r 0 1 X", "corrupt tzdb: expected character '-'");
+ test_exception("R r 0 1 X", "corrupt tzdb: expected character '-', got 'X' instead");
test_exception("R r 0 1 -", "corrupt tzdb: expected whitespace");
@@ -106,13 +107,17 @@ static void test_invalid() {
test_exception("R r 0 1 - Ja +", "corrupt tzdb weekday: invalid name");
test_exception("R r 0 1 - Ja 32", "corrupt tzdb day: value too large");
- test_exception("R r 0 1 - Ja l", "corrupt tzdb: expected string 'last'");
+ test_exception(
+ "R r 0 1 - Ja l",
+ std::string{"corrupt tzdb: expected character 'a' from string 'last', got '"} + (char)EOF + "' instead");
test_exception("R r 0 1 - Ja last", "corrupt tzdb weekday: invalid name");
test_exception("R r 0 1 - Ja lastS", "corrupt tzdb weekday: invalid name");
test_exception("R r 0 1 - Ja S", "corrupt tzdb weekday: invalid name");
test_exception("R r 0 1 - Ja Su", "corrupt tzdb on: expected '>=' or '<='");
- test_exception("R r 0 1 - Ja Su>", "corrupt tzdb: expected character '='");
- test_exception("R r 0 1 - Ja Su<", "corrupt tzdb: expected character '='");
+ test_exception(
+ "R r 0 1 - Ja Su>", std::string{"corrupt tzdb: expected character '=', got '"} + (char)EOF + "' instead");
+ test_exception(
+ "R r 0 1 - Ja Su<", std::string{"corrupt tzdb: expected character '=', got '"} + (char)EOF + "' instead");
test_exception("R r 0 1 - Ja Su>=+", "corrupt tzdb: expected a non-zero digit");
test_exception("R r 0 1 - Ja Su>=0", "corrupt tzdb: expected a non-zero digit");
test_exception("R r 0 1 - Ja Su>=32", "corrupt tzdb day: value too large");
diff --git a/libcxx/test/libcxx/time/time.zone/time.zone.db/version.pass.cpp b/libcxx/test/libcxx/time/time.zone/time.zone.db/version.pass.cpp
index b4f32a1b6fd785..ca3a890f1fa548 100644
--- a/libcxx/test/libcxx/time/time.zone/time.zone.db/version.pass.cpp
+++ b/libcxx/test/libcxx/time/time.zone/time.zone.db/version.pass.cpp
@@ -18,9 +18,10 @@
// This is not part of the public tzdb interface.
#include <chrono>
+#include <cstdio>
#include <fstream>
-#include <string>
#include <string_view>
+#include <string>
#include "assert_macros.h"
#include "concat_macros.h"
@@ -60,7 +61,7 @@ static void test_exception(std::string_view input, [[maybe_unused]] std::string_
}
int main(int, const char**) {
- test_exception("", "corrupt tzdb: expected character '#'");
+ test_exception("", std::string{"corrupt tzdb: expected character '#', got '"} + (char)EOF + "' instead");
test_exception("#version", "corrupt tzdb: expected whitespace");
test("#version \t ABCD", "ABCD");
test("#Version \t ABCD", "ABCD");
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
libc++
libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
pending-ci
Merging the PR is only pending completion of CI
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.
Providing the character that we failed on is helpful for figuring out what's going wrong in the tztb.