Implementing __builtin_LINE(x) using a clang plugin

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

Implementing __builtin_LINE(x) using a clang plugin

David Zarzycki via cfe-dev
Hi all,

I have  C++ DSL that defines its own primitive types and operators and I want to provide some debugging facilities during compilation to display the source location (filename, line number) for uninitialized DSL objects.
To capture the source line information for a given DSL object, I'm using __builtin_LINE(x) inside the class constructor, where 'x' is the function nesting depth (see below).
I would like to implement this functionality using a clang plugging such that the feature is automatically used during compilation.
I don't know if this is feasible with the current Clang plugin API.
I was wondering someone has some experience with the plugin and how this can be done.
Thank you so much.

eg: DSLObject  myobj;

--------------------------

class DSLObject {
public:
    DSLObject() {
        lineno_ = __builtin_LINE(1);
    }
private:
    int lineno_;
};



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Implementing __builtin_LINE(x) using a clang plugin

David Zarzycki via cfe-dev

On 22 Dec 2019, at 3:17, Blaise Tine via cfe-dev wrote:

Hi all,
I have  C++ DSL that defines its own primitive types and operators and I want to provide some debugging facilities during compilation to display the source location (filename, line number) for uninitialized DSL objects.To capture the source line information for a given DSL object, I'm using __builtin_LINE(x) inside the class constructor, where 'x' is the function nesting depth (see below).

__builtin_LINE() does not take an argument in either Clang or GCC, as far as I can tell, so I’ll assume you’re asking whether that would be a reasonable extension. And I don’t know why a nesting depth (i.e. the number of functions/structs/whatever that the current source location is nested within) would affect the result of __builtin_LINE(), so I’ll also assume you actually mean a stack depth (i.e. you would like to return the line number of the caller). If that’s correct, then the answer is simple: it is not reasonable to implement such a feature in either a plugin or in the normal compiler. I can see two ways to implement it: (1) emit a specialized version of the function for every caller or (2) dynamically look up line-table information associated with the return address. Both approaches would be highly complex and have serious drawbacks.

Fortunately, you don’t really need any of that, because the whole point of __builtin_LINE() is to be used in a default argument expression, where it will be evaluated in the context of the caller:

class DSLObject {
public:
  DSLObject(unsigned lineno = __builtin_LINE()) : lineno_(lineno) {}
private:
  int lineno_;
};

John.


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Modifying clang AST using a plugin

David Zarzycki via cfe-dev
In reply to this post by David Zarzycki via cfe-dev
Hi all,

I would like to modify the clang AST using a plugin.
I have following the suggestions of using clang Rewriter object.
I use compiler.createDefaultOutputFile() to obtain the output stream to write the changed file into.
the compilation works fine but the generated object file is broken. 
I get the following linker error: "Wrong file format".

Question:
- Is it actually possible to update the AST using a clang plugin?
- What am I doing wrong with my solution?
- Can someone point me to an example program that does that?

Thanks,
-Blaise

 

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Modifying clang AST using a plugin

David Zarzycki via cfe-dev
This is generally explicitly not supported: https://clang.llvm.org/docs/InternalsManual.html#immutability

In some cases you can hack around that: https://www.youtube.com/watch?v=_rUwW8Awc5s

On Mon, Jan 13, 2020 at 1:16 PM Blaise Tine via cfe-dev <[hidden email]> wrote:
Hi all,

I would like to modify the clang AST using a plugin.
I have following the suggestions of using clang Rewriter object.
I use compiler.createDefaultOutputFile() to obtain the output stream to write the changed file into.
the compilation works fine but the generated object file is broken. 
I get the following linker error: "Wrong file format".

Question:
- Is it actually possible to update the AST using a clang plugin?
- What am I doing wrong with my solution?
- Can someone point me to an example program that does that?

Thanks,
-Blaise

 
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Modifying clang AST using a plugin

David Zarzycki via cfe-dev
Thank you Nico,


On Monday, January 13, 2020, 02:58:09 PM EST, Nico Weber <[hidden email]> wrote:


This is generally explicitly not supported: https://clang.llvm.org/docs/InternalsManual.html#immutability

In some cases you can hack around that: https://www.youtube.com/watch?v=_rUwW8Awc5s

On Mon, Jan 13, 2020 at 1:16 PM Blaise Tine via cfe-dev <[hidden email]> wrote:
Hi all,

I would like to modify the clang AST using a plugin.
I have following the suggestions of using clang Rewriter object.
I use compiler.createDefaultOutputFile() to obtain the output stream to write the changed file into.
the compilation works fine but the generated object file is broken. 
I get the following linker error: "Wrong file format".

Question:
- Is it actually possible to update the AST using a clang plugin?
- What am I doing wrong with my solution?
- Can someone point me to an example program that does that?

Thanks,
-Blaise

 
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev