GrieferAtWork / tpp

Tiny PreProcessor
Other
17 stars 1 forks source link

question: how to get/enable the token's column and line information #5

Closed asmwarrior closed 7 months ago

asmwarrior commented 7 months ago

Hi, I see some code

struct TPPLCInfo {
    /* TPP Line/Column information. */
#ifdef __INTELLISENSE__
        line_t  lc_line; /* zero-based line index in the associated file. */
        col_t   lc_col;  /* zero-based column index in the associated file (NOTE: Tabs are already expanded in this). */
#else /* __INTELLISENSE__ */
    TPP(line_t) lc_line; /* zero-based line index in the associated file. */
    TPP(col_t)  lc_col;  /* zero-based column index in the associated file (NOTE: Tabs are already expanded in this). */
#endif /* !__INTELLISENSE__ */
};

I don't really understand what dose the __INTELLISENSE__ mean, so I think some document or sample code about this feature is needed. Thanks.

GrieferAtWork commented 7 months ago

__INTELLISENSE__ isn't a feature. It's just a check for the IDE (Visual Studio) I like to use. Said IDE doesn't like the "TPP(...)" namespace macro (which adds a TPP_ prefix to various symbols), so I hide it in a couple of spots as a hacky work-around. It is never defined during compilation.

Line/column information is always available, and there are 2 different values you can (easily) query:

Note that these *_LC() functions return 0-based numbers, so if you want to display them to a user, you'll have to add +1.

Example:

struct TPPLCInfo lc;
TPPLexer_LC(&lc);
printf("%s:%d:%d: Here\n", TPPLexer_FILE(NULL), (int)lc.lc_line + 1, (int)lc.lc_col + 1);
GrieferAtWork commented 7 months ago

You can also enumerate the file/line/column info for every macro on the macro-expansion (or even keep going and print #include locations from the include-stack). An example of this can be found in the code here (where it's used for generating tracebacks when warnings are printed):

https://github.com/GrieferAtWork/tpp/blob/31ccfa65eb7543ce4c0db654c5c4b2082f1141fb/src/tpp.c#L15362

GrieferAtWork commented 7 months ago

@asmwarrior I've now extended the "advanced" sample to print token file positions. When inside of a macro, it also prints the name of the currently top-most macro, as well as where the token currently being generated originates from.

https://github.com/GrieferAtWork/tpp/blob/8171b9d49988102a68a5f934ecb42c661c82ab56/samples/advanced/main.c#L60

asmwarrior commented 7 months ago

__INTELLISENSE__ isn't a feature. It's just a check for the IDE (Visual Studio) I like to use. Said IDE doesn't like the "TPP(...)" namespace macro (which adds a TPP_ prefix to various symbols), so I hide it in a couple of spots as a hacky work-around. It is never defined during compilation.

OK, thanks for the explanation. I think this should be document somewhere, so others won't ask similar question again.

Also, thanks for the demo code in the "advanced" samples, I learned a lot from that code.

asmwarrior commented 7 months ago

Can you add some comments about the macro definition __INTELLISENSE__?

This is not known from the non msvc people.

See document here: Microsoft-specific predefined macros