aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/lex/rust-lex.h
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-03-01 17:26:07 +0000
committerGitHub <noreply@github.com>2022-03-01 17:26:07 +0000
commit6cf9f8c99c5813a23d7cec473fedf00683f409e4 (patch)
treecd590bc5f7b266c043499899e0529ff4c16cc5ec /gcc/rust/lex/rust-lex.h
parente82b59dfc9319d72f891bac099bfa0f46d8b8c99 (diff)
parentf7ff6020f8c68e4fb54c17c4460aa7f8a31f85bd (diff)
downloadgcc-6cf9f8c99c5813a23d7cec473fedf00683f409e4.zip
gcc-6cf9f8c99c5813a23d7cec473fedf00683f409e4.tar.gz
gcc-6cf9f8c99c5813a23d7cec473fedf00683f409e4.tar.bz2
Merge #983
983: Parse proper cfg values r=CohenArthur a=CohenArthur Closes #936 Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust/lex/rust-lex.h')
-rw-r--r--gcc/rust/lex/rust-lex.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h
index 0ae07fe..c50f632 100644
--- a/gcc/rust/lex/rust-lex.h
+++ b/gcc/rust/lex/rust-lex.h
@@ -23,6 +23,7 @@
#include "rust-buffered-queue.h"
#include "rust-token.h"
+#include <cstdio>
#include <utility>
#include <tuple>
@@ -49,6 +50,13 @@ public:
file = fopen (filename, "r");
}
+ /**
+ * Create a RAIIFile from an existing instance of FILE*
+ */
+ RAIIFile (FILE *raw, const char *filename = nullptr)
+ : file (raw), filename (filename)
+ {}
+
RAIIFile (const RAIIFile &other) = delete;
RAIIFile &operator= (const RAIIFile &other) = delete;
@@ -57,6 +65,7 @@ public:
{
other.file = nullptr;
}
+
RAIIFile &operator= (RAIIFile &&other)
{
close ();
@@ -132,6 +141,19 @@ public:
Lexer (const char *filename, RAIIFile input, Linemap *linemap);
~Lexer ();
+ /**
+ * Lex the contents of a string instead of a file
+ */
+ static Lexer lex_string (std::string input)
+ {
+ // We can perform this ugly cast to a non-const char* since we're only
+ // *reading* the string. This would not be valid if we were doing any
+ // modification to it.
+ auto string_file = fmemopen (&input[0], input.length (), "r");
+
+ return Lexer (nullptr, RAIIFile (string_file), nullptr);
+ }
+
// don't allow copy semantics (for now, at least)
Lexer (const Lexer &other) = delete;
Lexer &operator= (const Lexer &other) = delete;