diff options
author | Ayke van Laethem <[email protected]> | 2024-07-12 19:15:32 +0200 |
---|---|---|
committer | Ayke van Laethem <[email protected]> | 2024-07-12 20:09:17 +0200 |
commit | bedc2375958aa124c464405ec0b7557d06513f3b (patch) | |
tree | 899e86c94213802e32a682464b9c7f439a2ddf72 /cgo/const.go | |
parent | ac396708da17271ec48bffefb57d1185dd8cadb1 (diff) | |
download | tinygo-cgo-macro-via-cmdline.tar.gz tinygo-cgo-macro-via-cmdline.zip |
cgo: support preprocessor macros passed on the command linecgo-macro-via-cmdline
Go code might sometimes want to use preprocessor macros that were passed
on the command line. This wasn't working before and resulted in the
following error:
internal error: could not find file where macro is defined
This is now supported, though location information isn't available
(which makes sense: the command line is not a file).
I had to use the `clang_tokenize` API for this and reconstruct the
original source location. Apparently this is the only way to do it:
https://stackoverflow.com/a/19074846/559350
In the future we could consider replacing our own tokenization with the
tokenizer that's built into Clang directly. This should reduce the
possibility of bugs a bit.
Diffstat (limited to 'cgo/const.go')
-rw-r--r-- | cgo/const.go | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/cgo/const.go b/cgo/const.go index 2d0e29e10..f4707c80a 100644 --- a/cgo/const.go +++ b/cgo/const.go @@ -195,7 +195,9 @@ func (t *tokenizer) Next() { t.curValue = t.peekValue // Parse the next peek token. - t.peekPos += token.Pos(len(t.curValue)) + if t.peekPos != token.NoPos { + t.peekPos += token.Pos(len(t.curValue)) + } for { if len(t.buf) == 0 { t.peekToken = token.EOF @@ -207,7 +209,9 @@ func (t *tokenizer) Next() { // Skip whitespace. // Based on this source, not sure whether it represents C whitespace: // https://en.cppreference.com/w/cpp/string/byte/isspace - t.peekPos++ + if t.peekPos != token.NoPos { + t.peekPos++ + } t.buf = t.buf[1:] case len(t.buf) >= 2 && (string(t.buf[:2]) == "||" || string(t.buf[:2]) == "&&" || string(t.buf[:2]) == "<<" || string(t.buf[:2]) == ">>"): // Two-character tokens. |