diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-02-16 15:13:57 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-02-17 11:23:25 +0100 |
commit | 766a9002a3d5fb6701de2d84ce689379811eabff (patch) | |
tree | 3a94ffcbbc033f140cb1ec44cb70ad6e1739faa2 /gcc/rust/rust-session-manager.cc | |
parent | d81ba63f4829c12b89e87564c398e95879c89db1 (diff) | |
download | gcc-766a9002a3d5fb6701de2d84ce689379811eabff.zip gcc-766a9002a3d5fb6701de2d84ce689379811eabff.tar.gz gcc-766a9002a3d5fb6701de2d84ce689379811eabff.tar.bz2 |
frust-cfg: Only allow double quoted values
This commit 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.
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; } |