preprocessor #21

Open
opened 2025-10-22 07:24:32 +02:00 by alexander.nutz · 0 comments

also introduce "preprocessor AST":

bad example:

#include <a>
#include "b"

#ifdef __GNUC__
# define assert(a,cond) \
      ....
# if _some_cond
#  defien thisandthat 2
# endif
#else
# define assert(a, cond) ....
# if _some_cond
#  undef assert
# endif
#endif

the idea is that the preprocessor represents the preprocessor elements in the document at the positions before preprocessing, but is generated after preprocessing, which means that we don't always have enough information to construct the preprocessor AST for the cases that were not chosen

preprocessor ast:

; loc points to the whole #include statement
(included
  ; loc points to "<a>"
  path: (include_path "/path/to/a)
  document: @1231
  ..... ; preprocessor ast continuing with everything that was included
  )  ; those included files will get a `click_document` too, which will contain their PP ast, but note that that might not always make sense on it's own
(included
  path: (includ_path "/path/to/b")
  document: @4211
  .....)


note that includes won't always look that clean.
if you have a.h:

#ifdef something

and b.h:

#define x 1
#endif

and includde a then b, then the preprocesso ast will look like this:

(included @0
  ; loc points to "<a>"
  path: (include_path "/path/to/a)
  document: @1231

  (ifdef @1231
    what: "something"
    then: (included @0
                path: (includ_path "/path/to/b")
                document: @4211
                (define_var @4211
                   name: "x"
                   value: (tokens  @4211 ....)
                ))
  ))
also introduce "preprocessor AST": bad example: ```c #include <a> #include "b" #ifdef __GNUC__ # define assert(a,cond) \ .... # if _some_cond # defien thisandthat 2 # endif #else # define assert(a, cond) .... # if _some_cond # undef assert # endif #endif ``` the idea is that the preprocessor represents the preprocessor elements in the document at the positions before preprocessing, but is generated after preprocessing, which means that we don't always have enough information to construct the preprocessor AST for the cases that were not chosen preprocessor ast: ``` ; loc points to the whole #include statement (included ; loc points to "<a>" path: (include_path "/path/to/a) document: @1231 ..... ; preprocessor ast continuing with everything that was included ) ; those included files will get a `click_document` too, which will contain their PP ast, but note that that might not always make sense on it's own (included path: (includ_path "/path/to/b") document: @4211 .....) ``` note that includes won't always look that clean. if you have `a.h`: ``` #ifdef something ``` and `b.h`: ``` #define x 1 #endif ``` and includde a then b, then the preprocesso ast will look like this: ``` (included @0 ; loc points to "<a>" path: (include_path "/path/to/a) document: @1231 (ifdef @1231 what: "something" then: (included @0 path: (includ_path "/path/to/b") document: @4211 (define_var @4211 name: "x" value: (tokens @4211 ....) )) )) ```
alexander.nutz changed title from pre-processor to preprocessor 2025-10-22 07:25:17 +02:00
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
click-project/click#21
No description provided.