aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/parse/rust-cfg-parser.cc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-02-17 10:58:07 +0000
committerGitHub <noreply@github.com>2022-02-17 10:58:07 +0000
commit752bf6c80a922e09edf5bcb53e15e08e83057a7f (patch)
tree0968dcfc9a8ccc858a5aa5531463cc50a713eab4 /gcc/rust/parse/rust-cfg-parser.cc
parent6a6c21709314e72f2edb5539913a4b7ce7a1cb66 (diff)
parent766a9002a3d5fb6701de2d84ce689379811eabff (diff)
downloadgcc-752bf6c80a922e09edf5bcb53e15e08e83057a7f.zip
gcc-752bf6c80a922e09edf5bcb53e15e08e83057a7f.tar.gz
gcc-752bf6c80a922e09edf5bcb53e15e08e83057a7f.tar.bz2
Merge #935
935: frust-cfg: Only allow double quoted values r=philberty a=CohenArthur Closes #910 This PR separates the `handle_cfg_option()` function in two, separating the parsing logic from the session logic. The parsing logic is able to be unit tested, and now only allows quoted values. What remains to be done is to only allow `key` and `value` to be proper rust identifiers. We need to figure out if we'd like to spawn a parser here and parse identifiers, or simply sanitize both strings to make sure they do not contain invalid characters. Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust/parse/rust-cfg-parser.cc')
-rw-r--r--gcc/rust/parse/rust-cfg-parser.cc75
1 files changed, 75 insertions, 0 deletions
diff --git a/gcc/rust/parse/rust-cfg-parser.cc b/gcc/rust/parse/rust-cfg-parser.cc
new file mode 100644
index 0000000..a6f34b6
--- /dev/null
+++ b/gcc/rust/parse/rust-cfg-parser.cc
@@ -0,0 +1,75 @@
+#include "rust-cfg-parser.h"
+#include "selftest.h"
+
+namespace Rust {
+bool
+parse_cfg_option (const std::string &input, std::string &key,
+ std::string &value)
+{
+ key.clear ();
+ value.clear ();
+
+ auto equal = input.find ('=');
+
+ // If there is no equal sign, it means there is no value. Clean up the key
+ // and return
+ if (equal == std::string::npos)
+ {
+ key = input;
+
+ // FIXME: Make sure key is a proper identifier
+
+ return true;
+ }
+
+ key = input.substr (0, equal);
+
+ auto remaining_input = input.substr (equal + 1);
+ if (remaining_input[0] != '"' || remaining_input.back () != '"')
+ return false;
+
+ // Remove the quotes around the value, by advancing one character
+ value = remaining_input.substr (1);
+ // And trimming the rightmost character. This is fine since we've already
+ // checked that both the first and last characters were quotes.
+ value.resize (value.size () - 1);
+
+ // FIXME: We need to sanitize here and make sure that both key and value
+ // are proper identifiers
+
+ return true;
+}
+
+} // namespace Rust
+
+#if CHECKING_P
+
+namespace selftest {
+
+void
+rust_cfg_parser_test (void)
+{
+ std::string key;
+ std::string value;
+
+ ASSERT_TRUE (Rust::parse_cfg_option ("key-no-value", key, value));
+ ASSERT_EQ (key, "key-no-value");
+ ASSERT_TRUE (value.empty ());
+
+ ASSERT_TRUE (Rust::parse_cfg_option ("k=\"v\"", key, value));
+ ASSERT_EQ (key, "k");
+ ASSERT_EQ (value, "v");
+
+ // values should be between double quotes
+ ASSERT_FALSE (Rust::parse_cfg_option ("k=v", key, value));
+
+ // No value is an error if there is an equal sign
+ ASSERT_FALSE (Rust::parse_cfg_option ("k=", key, value));
+
+ // No key is an error
+ ASSERT_FALSE (Rust::parse_cfg_option ("=", key, value));
+ ASSERT_FALSE (Rust::parse_cfg_option ("=value", key, value));
+}
+} // namespace selftest
+
+#endif // CHECKING_P