How to get SourceLocation of a trivia? #989
-
module top;
reg [7:0] data;
integer code;
always_comb begin
case (data) // synopsys full_case
8'b00000001: code = 32'd0;
8'b00000010: code = 32'd1;
8'b00000100: code = 32'd2;
8'b00001000: code = 32'd3;
8'b00010000: code = 32'd4;
8'b00100000: code = 32'd5;
8'b01000000: code = 32'd6;
8'b10000000: code = 32'd7;
default: code = 32'd0;
endcase
end
endmodule I get the LineComment trivia of "// synopsis full_case", but How to get the start SourceLocation of this trivia? I used trivia.getExplicitLocation(), but the result is nullopt. Question 1: when getExplicitLocation() is not nullopt? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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. |
Beta Was this translation helpful? Give feedback.
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.