diff options
Diffstat (limited to 'gcc/rust/parse')
-rw-r--r-- | gcc/rust/parse/rust-cfg-parser.cc | 75 | ||||
-rw-r--r-- | gcc/rust/parse/rust-cfg-parser.h | 57 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse.cc | 1 |
3 files changed, 132 insertions, 1 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 diff --git a/gcc/rust/parse/rust-cfg-parser.h b/gcc/rust/parse/rust-cfg-parser.h new file mode 100644 index 0000000..a4b860f --- /dev/null +++ b/gcc/rust/parse/rust-cfg-parser.h @@ -0,0 +1,57 @@ +/* This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#ifndef RUST_CFG_PARSER_H +#define RUST_CFG_PARSER_H + +#include "config.h" +#include "system.h" +#include "coretypes.h" + +#include <string> + +namespace Rust { +/** + * Parse a `key` or `key="value"` pair given to the `-frust-cfg` compiler + * option. + * + * The format is as follows: + * + * -frust-cfg=<input> + * + * cfg_input: identifier | identifier '=' '"' identifier '"' + * + * @param input User input given to the -frust-cfg option + * @param key String in which to store the parsed `key`. + * @param value String in which to store the parsed `value` if it exists + * + * @return false if the given input was invalid, true otherwise + */ +bool +parse_cfg_option (const std::string &input, std::string &key, + std::string &value); +} // namespace Rust + +#if CHECKING_P + +namespace selftest { +extern void +rust_cfg_parser_test (void); +} // namespace selftest + +#endif // CHECKING_P + +#endif // RUST_CFG_PARSER_H diff --git a/gcc/rust/parse/rust-parse.cc b/gcc/rust/parse/rust-parse.cc index e78de51..f2c1301 100644 --- a/gcc/rust/parse/rust-parse.cc +++ b/gcc/rust/parse/rust-parse.cc @@ -118,5 +118,4 @@ extract_module_path (const AST::AttrVec &inner_attrs, return path; } - } // namespace Rust |