diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-02-17 10:58:07 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-17 10:58:07 +0000 |
commit | 752bf6c80a922e09edf5bcb53e15e08e83057a7f (patch) | |
tree | 0968dcfc9a8ccc858a5aa5531463cc50a713eab4 /gcc/rust/rust-session-manager.cc | |
parent | 6a6c21709314e72f2edb5539913a4b7ce7a1cb66 (diff) | |
parent | 766a9002a3d5fb6701de2d84ce689379811eabff (diff) | |
download | gcc-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/rust-session-manager.cc')
-rw-r--r-- | gcc/rust/rust-session-manager.cc | 86 |
1 files changed, 11 insertions, 75 deletions
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index ea933d63..cd2c590 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -29,6 +29,7 @@ #include "rust-tycheck-dump.h" #include "rust-ast-resolve-unused.h" #include "rust-compile.h" +#include "rust-cfg-parser.h" #include "diagnostic.h" #include "input.h" @@ -382,87 +383,22 @@ Session::handle_cfg_option (const std::string &input) std::string key; std::string value; - enum pstate - { - KEY, - EQ, - VAL, - DONE, - ERROR - }; - - // FIXME - // we need to use the GCC self_test framework to unit-test this its - // likely got a bunch of bugs. This simple parser could be extracted to a - // helper function to be more easily unit-tested or it could be tested via - // checking what the target_options contain - bool expect_quote = false; - pstate s = KEY; - for (const auto &ch : input) + // Refactor this if needed + if (!parse_cfg_option (input, key, value)) { - if (ch == ' ') - { - if (!key.empty ()) - s = EQ; - else if (!value.empty ()) - s = DONE; - else - { - s = ERROR; - break; - } - } - else if (ch == '"') - { - expect_quote = !expect_quote; - } - else if (ch == '=') - { - if (key.empty ()) - { - s = ERROR; - break; - } - - s = VAL; - } - else - { - if (s == KEY) - key.push_back (ch); - else if (s == VAL) - value.push_back (ch); - else - { - s = ERROR; - break; - } - } - } - - if (key.empty () && value.empty ()) - s = ERROR; - - if (expect_quote) - s = ERROR; - - if (s == ERROR) - { - rust_error_at (Location (), - "invalid %<-frust-cfg=option%> expected %<key%> or " - "key=%<value%> got %<%s%>", - input.c_str ()); + rust_error_at ( + Location (), + "invalid argument to %<-frust-cfg%>: Accepted formats are " + "%<-frust-cfg=key%> or %<-frust-cfg=key=\"value\"%> (quoted)"); return false; } if (value.empty ()) - { - // rustc does not seem to error on dup key - options.target_data.insert_key (key); - return true; - } + // rustc does not seem to error on dup key + options.target_data.insert_key (key); + else + options.target_data.insert_key_value_pair (key, value); - options.target_data.insert_key_value_pair (key, value); return true; } |