Skip to content

Commit a5272f3

Browse files
Merge #934 #937
934: macromatch: Add location to abstract MacroMatch class r=philberty a=CohenArthur Closes #928 This adds location to the all child classes of the `MacroMatch` abstract class. The current locations are as follow, which I believe is what is expected but might be wrong. ```rust test.rs:2:6: error: macro match fragment 2 | ($a:expr, $b:expr) => { $a + $b }; | ^ test.rs:2:15: error: macro match fragment 2 | ($a:expr, $b:expr) => { $a + $b }; | ^ test.rs:2:5: error: macro matcher 2 | ($a:expr, $b:expr) => { $a + $b }; | ^ test.rs:3:8: error: macro match fragment 3 | ($($i:ident)*) => { $($i)* } | ^ test.rs:3:17: error: macro match repetition! 3 | ($($i:ident)*) => { $($i)* } | ^ test.rs:3:5: error: macro matcher 3 | ($($i:ident)*) => { $($i)* } | ^ ``` I think this should be rebased on #932 so that I can remove the FIXME 937: dockerfile: Install cargo-gccrs alongside gccrs r=philberty a=CohenArthur Closes #826 Co-authored-by: Arthur Cohen <[email protected]>
3 parents 733db62 + 3ac1716 + 85d78c1 commit a5272f3

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:latest
1+
FROM ubuntu:latest AS gcc-builder
22

33
RUN apt-get update; \
44
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
@@ -67,5 +67,8 @@ RUN /bin/sh -c set -ex; \
6767
dpkg-divert --divert /usr/bin/g++.orig --rename /usr/bin/g++; \
6868
update-alternatives --install /usr/bin/cc cc /usr/local/bin/gcc 999
6969

70+
FROM rust
71+
COPY --from=gcc-builder /usr/ /usr/
72+
RUN cargo install --git https://github.com/Rust-GCC/cargo-gccrs cargo-gccrs
7073

7174
CMD ["bash"]

gcc/rust/ast/rust-ast.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class MacroMatch
111111
virtual ~MacroMatch () {}
112112

113113
virtual std::string as_string () const = 0;
114+
virtual Location get_match_locus () const = 0;
114115

115116
// Unique pointer custom clone function
116117
std::unique_ptr<MacroMatch> clone_macro_match () const
@@ -217,6 +218,7 @@ class Token : public TokenTree, public MacroMatch
217218
}
218219

219220
std::string as_string () const override;
221+
Location get_match_locus () const override { return tok_ref->get_locus (); };
220222

221223
void accept_vis (ASTVisitor &vis) override;
222224

gcc/rust/ast/rust-macro.h

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,24 @@ class MacroMatchFragment : public MacroMatch
8787
{
8888
Identifier ident;
8989
MacroFragSpec frag_spec;
90-
91-
// TODO: should store location information?
90+
Location locus;
9291

9392
public:
94-
MacroMatchFragment (Identifier ident, MacroFragSpec frag_spec)
95-
: ident (std::move (ident)), frag_spec (frag_spec)
93+
MacroMatchFragment (Identifier ident, MacroFragSpec frag_spec, Location locus)
94+
: ident (std::move (ident)), frag_spec (frag_spec), locus (locus)
9695
{}
9796

9897
// Returns whether macro match fragment is in an error state.
9998
bool is_error () const { return frag_spec == INVALID; }
10099

101100
// Creates an error state macro match fragment.
102-
static MacroMatchFragment create_error ()
101+
static MacroMatchFragment create_error (Location locus)
103102
{
104-
return MacroMatchFragment (std::string (""), INVALID);
103+
return MacroMatchFragment (std::string (""), INVALID, locus);
105104
}
106105

107106
std::string as_string () const override;
107+
Location get_match_locus () const override { return locus; };
108108

109109
void accept_vis (ASTVisitor &vis) override;
110110

@@ -137,20 +137,22 @@ class MacroMatchRepetition : public MacroMatch
137137
typedef Token MacroRepSep;
138138
// any token except delimiters and repetition operators
139139
std::unique_ptr<MacroRepSep> sep;
140-
141-
// TODO: should store location information?
140+
Location locus;
142141

143142
public:
144143
// Returns whether macro match repetition has separator token.
145144
bool has_sep () const { return sep != nullptr; }
146145

147146
MacroMatchRepetition (std::vector<std::unique_ptr<MacroMatch> > matches,
148-
MacroRepOp op, std::unique_ptr<MacroRepSep> sep)
149-
: matches (std::move (matches)), op (op), sep (std::move (sep))
147+
MacroRepOp op, std::unique_ptr<MacroRepSep> sep,
148+
Location locus)
149+
: matches (std::move (matches)), op (op), sep (std::move (sep)),
150+
locus (locus)
150151
{}
151152

152153
// Copy constructor with clone
153-
MacroMatchRepetition (MacroMatchRepetition const &other) : op (other.op)
154+
MacroMatchRepetition (MacroMatchRepetition const &other)
155+
: op (other.op), locus (other.locus)
154156
{
155157
// guard to protect from null pointer dereference
156158
if (other.sep != nullptr)
@@ -165,6 +167,7 @@ class MacroMatchRepetition : public MacroMatch
165167
MacroMatchRepetition &operator= (MacroMatchRepetition const &other)
166168
{
167169
op = other.op;
170+
locus = other.locus;
168171

169172
// guard to protect from null pointer dereference
170173
if (other.sep != nullptr)
@@ -184,6 +187,7 @@ class MacroMatchRepetition : public MacroMatch
184187
MacroMatchRepetition &operator= (MacroMatchRepetition &&other) = default;
185188

186189
std::string as_string () const override;
190+
Location get_match_locus () const override { return locus; };
187191

188192
void accept_vis (ASTVisitor &vis) override;
189193

@@ -201,20 +205,22 @@ class MacroMatcher : public MacroMatch
201205
{
202206
DelimType delim_type;
203207
std::vector<std::unique_ptr<MacroMatch> > matches;
208+
Location locus;
204209

205210
// TODO: think of way to mark invalid that doesn't take up more space
206211
bool is_invalid;
207212

208-
// TODO: should store location information?
209-
210213
public:
211214
MacroMatcher (DelimType delim_type,
212-
std::vector<std::unique_ptr<MacroMatch> > matches)
213-
: delim_type (delim_type), matches (std::move (matches)), is_invalid (false)
215+
std::vector<std::unique_ptr<MacroMatch> > matches,
216+
Location locus)
217+
: delim_type (delim_type), matches (std::move (matches)), locus (locus),
218+
is_invalid (false)
214219
{}
215220

216221
// copy constructor with vector clone
217-
MacroMatcher (MacroMatcher const &other) : delim_type (other.delim_type)
222+
MacroMatcher (MacroMatcher const &other)
223+
: delim_type (other.delim_type), locus (other.locus)
218224
{
219225
matches.reserve (other.matches.size ());
220226
for (const auto &e : other.matches)
@@ -225,6 +231,7 @@ class MacroMatcher : public MacroMatch
225231
MacroMatcher &operator= (MacroMatcher const &other)
226232
{
227233
delim_type = other.delim_type;
234+
locus = other.locus;
228235

229236
matches.reserve (other.matches.size ());
230237
for (const auto &e : other.matches)
@@ -238,10 +245,14 @@ class MacroMatcher : public MacroMatch
238245
MacroMatcher &operator= (MacroMatcher &&other) = default;
239246

240247
// Creates an error state macro matcher.
241-
static MacroMatcher create_error () { return MacroMatcher (true); }
248+
static MacroMatcher create_error (Location locus)
249+
{
250+
return MacroMatcher (true, locus);
251+
}
242252

243253
// Returns whether MacroMatcher is in an error state.
244254
bool is_error () const { return is_invalid; }
255+
Location get_match_locus () const override { return locus; }
245256

246257
std::string as_string () const override;
247258

@@ -256,7 +267,8 @@ class MacroMatcher : public MacroMatch
256267
}
257268

258269
// constructor only used to create error matcher
259-
MacroMatcher (bool is_invalid) : delim_type (PARENS), is_invalid (is_invalid)
270+
MacroMatcher (bool is_invalid, Location locus)
271+
: delim_type (PARENS), locus (locus), is_invalid (is_invalid)
260272
{}
261273
};
262274

@@ -296,7 +308,8 @@ struct MacroRule
296308
// Creates an error state macro rule.
297309
static MacroRule create_error ()
298310
{
299-
return MacroRule (MacroMatcher::create_error (),
311+
// FIXME: Once #928 is merged, give location to MacroMatcher
312+
return MacroRule (MacroMatcher::create_error (Location ()),
300313
MacroTranscriber (DelimTokenTree::create_empty ()));
301314
}
302315

gcc/rust/parse/rust-parse-impl.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
17091709

17101710
// Map tokens to DelimType
17111711
const_TokenPtr t = lexer.peek_token ();
1712+
Location locus = t->get_locus ();
17121713
switch (t->get_id ())
17131714
{
17141715
case LEFT_PAREN:
@@ -1726,7 +1727,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
17261727
"unexpected token %qs - expecting delimiters (for a macro matcher)",
17271728
t->get_token_description ()));
17281729

1729-
return AST::MacroMatcher::create_error ();
1730+
return AST::MacroMatcher::create_error (t->get_locus ());
17301731
}
17311732
lexer.skip_token ();
17321733

@@ -1747,7 +1748,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
17471748
t->get_token_description ());
17481749
add_error (std::move (error));
17491750

1750-
return AST::MacroMatcher::create_error ();
1751+
return AST::MacroMatcher::create_error (t->get_locus ());
17511752
}
17521753

17531754
matches.push_back (std::move (match));
@@ -1765,7 +1766,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
17651766
// tokens match opening delimiter, so skip.
17661767
lexer.skip_token ();
17671768

1768-
return AST::MacroMatcher (delim_type, std::move (matches));
1769+
return AST::MacroMatcher (delim_type, std::move (matches), locus);
17691770
}
17701771
else
17711772
{
@@ -1781,7 +1782,7 @@ Parser<ManagedTokenSource>::parse_macro_matcher ()
17811782

17821783
/* return error macro matcher despite possibly parsing mostly correct one?
17831784
* TODO is this the best idea? */
1784-
return AST::MacroMatcher::create_error ();
1785+
return AST::MacroMatcher::create_error (t->get_locus ());
17851786
}
17861787
}
17871788

@@ -1857,6 +1858,7 @@ template <typename ManagedTokenSource>
18571858
std::unique_ptr<AST::MacroMatchFragment>
18581859
Parser<ManagedTokenSource>::parse_macro_match_fragment ()
18591860
{
1861+
Location fragment_locus = lexer.peek_token ()->get_locus ();
18601862
skip_token (DOLLAR_SIGN);
18611863

18621864
const_TokenPtr ident_tok = expect_token (IDENTIFIER);
@@ -1893,7 +1895,7 @@ Parser<ManagedTokenSource>::parse_macro_match_fragment ()
18931895
}
18941896

18951897
return std::unique_ptr<AST::MacroMatchFragment> (
1896-
new AST::MacroMatchFragment (std::move (ident), frag));
1898+
new AST::MacroMatchFragment (std::move (ident), frag, fragment_locus));
18971899
}
18981900

18991901
// Parses a repetition macro match.
@@ -2002,7 +2004,7 @@ Parser<ManagedTokenSource>::parse_macro_match_repetition ()
20022004

20032005
return std::unique_ptr<AST::MacroMatchRepetition> (
20042006
new AST::MacroMatchRepetition (std::move (matches), op,
2005-
std::move (separator)));
2007+
std::move (separator), t->get_locus ()));
20062008
}
20072009

20082010
/* Parses a visibility syntactical production (i.e. creating a non-default

0 commit comments

Comments
 (0)