extend C syntax in clang plug-in?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

extend C syntax in clang plug-in?

hongborong
I want to experiment with some syntax extensions to C, including some new built-in types and new statements. Can it be done completely in a plugin? Any related examples to look at?

For an extremely simple example, let us say we have
        int A;
        A, B = 1, [1.0, 2.0];
Here B is untyped, and [1.0, 2.0] is not an expected expression. Instead of throwing errors, can we write a clang plugin to do the following? (1) Pre-process the token stream. For example, the second statement can be broken into two statements: A=1; B=[1.0, 2.0]; That gives us a new token stream. (2) Parse. For the statement “B=[1.0, 2.0]”, build a customized AST node, and insert it to the function’s AST. (3) Some additional analysis may be done on the function’s AST. (4) Finally, transform the function's AST so that it completely conforms to standard C.

Thanks,
Hongbo
Reply | Threaded
Open this post in threaded view
|

Re: extend C syntax in clang plug-in?

Nico Weber
This isn't possible. The plugin API allows you to add an ASTConsumer that runs either instead of or after codegen. The ASTConsumer interface [1] only has hooks that tell you about the AST that clang is building – so a plugin can only do things in response to receiving an AST. Plugins can't modify parsing, or add new AST nodes.


On Mon, Nov 17, 2014 at 7:00 PM, hongborong <[hidden email]> wrote:
I want to experiment with some syntax extensions to C, including some new
built-in types and new statements. Can it be done completely in a plugin?
Any related examples to look at?

For an extremely simple example, let us say we have
        int A;
        A, B = 1, [1.0, 2.0];
Here B is untyped, and [1.0, 2.0] is not an expected expression. Instead of
throwing errors, can we write a clang plugin to do the following? (1)
Pre-process the token stream. For example, the second statement can be
broken into two statements: A=1; B=[1.0, 2.0]; That gives us a new token
stream. (2) Parse. For the statement “B=[1.0, 2.0]”, build a customized AST
node, and insert it to the function’s AST. (3) Some additional analysis may
be done on the function’s AST. (4) Finally, transform the function's AST so
that it completely conforms to standard C.

Thanks,
Hongbo



--
View this message in context: http://clang-developers.42468.n3.nabble.com/extend-C-syntax-in-clang-plug-in-tp4042499.html
Sent from the Clang Developers mailing list archive at Nabble.com.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: extend C syntax in clang plug-in?

Sean Silva
In reply to this post by hongborong


On Mon, Nov 17, 2014 at 7:00 PM, hongborong <[hidden email]> wrote:
I want to experiment with some syntax extensions to C, including some new
built-in types and new statements. Can it be done completely in a plugin?

No. Currently clang's plugin functionality is very limited.

-- Sean Silva
 
Any related examples to look at?

For an extremely simple example, let us say we have
        int A;
        A, B = 1, [1.0, 2.0];
Here B is untyped, and [1.0, 2.0] is not an expected expression. Instead of
throwing errors, can we write a clang plugin to do the following? (1)
Pre-process the token stream. For example, the second statement can be
broken into two statements: A=1; B=[1.0, 2.0]; That gives us a new token
stream. (2) Parse. For the statement “B=[1.0, 2.0]”, build a customized AST
node, and insert it to the function’s AST. (3) Some additional analysis may
be done on the function’s AST. (4) Finally, transform the function's AST so
that it completely conforms to standard C.

Thanks,
Hongbo



--
View this message in context: http://clang-developers.42468.n3.nabble.com/extend-C-syntax-in-clang-plug-in-tp4042499.html
Sent from the Clang Developers mailing list archive at Nabble.com.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: extend C syntax in clang plug-in?

Sean Silva


On Mon, Nov 17, 2014 at 7:48 PM, Sean Silva <[hidden email]> wrote:


On Mon, Nov 17, 2014 at 7:00 PM, hongborong <[hidden email]> wrote:
I want to experiment with some syntax extensions to C, including some new
built-in types and new statements. Can it be done completely in a plugin?

No. Currently clang's plugin functionality is very limited.

Sorry, I should have added a link to what you *can* do: http://clang.llvm.org/docs/ClangPlugins.html

Basically you can run a FrontendAction which operates on the AST that clang's parser generates.

-- Sean Silva
 

-- Sean Silva
 
Any related examples to look at?

For an extremely simple example, let us say we have
        int A;
        A, B = 1, [1.0, 2.0];
Here B is untyped, and [1.0, 2.0] is not an expected expression. Instead of
throwing errors, can we write a clang plugin to do the following? (1)
Pre-process the token stream. For example, the second statement can be
broken into two statements: A=1; B=[1.0, 2.0]; That gives us a new token
stream. (2) Parse. For the statement “B=[1.0, 2.0]”, build a customized AST
node, and insert it to the function’s AST. (3) Some additional analysis may
be done on the function’s AST. (4) Finally, transform the function's AST so
that it completely conforms to standard C.

Thanks,
Hongbo



--
View this message in context: http://clang-developers.42468.n3.nabble.com/extend-C-syntax-in-clang-plug-in-tp4042499.html
Sent from the Clang Developers mailing list archive at Nabble.com.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: extend C syntax in clang plug-in?

hongborong
Thanks to Nico's and your replies. It looks I have to modify Clang to achieve this purpose. What are the main places to change in clang?
Reply | Threaded
Open this post in threaded view
|

Re: extend C syntax in clang plug-in?

Shi Ningning
I think you should to modify the libclangParse and libclangSema.
The source code in the clang/lib/Parse and clang/lib/Sema.
And in your extensions, maybe also need to modify the libLLVMCodeGen.

On 2014年11月18日 12:15, hongborong wrote:

> Thanks to Nico's and your replies. It looks I have to modify Clang to achieve
> this purpose. What are the main places to change in clang?
>
>
>
> --
> View this message in context: http://clang-developers.42468.n3.nabble.com/extend-C-syntax-in-clang-plug-in-tp4042499p4042505.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev