Skip to content

How to get SourceLocation of a trivia? #989

Answered by MikePopoloski
yanggeorge asked this question in Q&A
Discussion options

You must be logged in to vote

Most trivia is relative to the parent token's location, so the way to figure it out is to walk backward through the trivia list and simply subtract the trivia's raw text length (the reason for this is to save a lot of memory storing locations for each piece of trivia). The exception is for cases where getExplicitLocation returns a valid location, which happens in a few rare corner cases: compilation directives / macro expansions that get turned into trivia, skipped tokens, and trivia that was at the end of an include file and so got merged into the next token from the including source file.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yanggeorge
Comment options

Answer selected by yanggeorge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants