Emit fixit for root AST node

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

Emit fixit for root AST node

via cfe-dev
Hi,

test.c:
#include <stdio.h>

void foo() {
    bool x;
}

When analyzing type "bool", I would like to emit fixit hint to top of the file, just before "#include <stdio.h>".

So my question is how to find the location of "root/top" AST node.

Thanks

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

Re: Emit fixit for root AST node

via cfe-dev
The "root/top" AST node is TranslationUnitDecl that can be obtained via
ASTContext.getTranslationUnitDecl().

I guess you only need source locations in order to emit a fixit, so you
can use SourceManager directly, eg.
SourceManager.getLocForStartOfFile(SourceManager.getMainFileID()).

The problem is probably harder though, because many files start with
comments, which you'll most likely want to skip, so "top of the file"
and "just before the first #include" will most likely be two different
locations, and this difficulty will require more advanced hacking to
overcome, which i'm not brave enough to reason about. You might find
some inspiration in some of the existing clang-tidy checkers that deal
with #includes, or maybe in the include-what-you-use tool (though it
doesn't seem to be using fixits at all).

On 10/6/18 1:53 AM, Dávid Bolvanský via cfe-dev wrote:

> Hi,
>
> test.c:
> #include <stdio.h>
>
> void foo() {
>     bool x;
> }
>
> When analyzing type "bool", I would like to emit fixit hint to top of
> the file, just before "#include <stdio.h>".
>
> So my question is how to find the location of "root/top" AST node.
>
> Thanks
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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

Re: Emit fixit for root AST node

via cfe-dev
In reply to this post by via cfe-dev
The "root/top" AST node is TranslationUnitDecl that can be obtained via
ASTContext.getTranslationUnitDecl().

I guess you only need source locations in order to emit a fixit, so you
can use SourceManager directly, eg.
SourceManager.getLocForStartOfFile(SourceManager.getMainFileID()).

The problem is probably harder though, because many files start with
comments, which you'll most likely want to skip, so "top of the file"
and "just before the first #include" will most likely be two different
locations, and this difficulty will require more advanced hacking to
overcome, which i'm not brave enough to reason about. You might find
some inspiration in some of the existing clang-tidy checkers that deal
with #includes, or maybe in the include-what-you-use tool (though it
doesn't seem to be using fixits at all).

On 10/6/18 1:53 AM, Dávid Bolvanský via cfe-dev wrote:

> Hi,
>
> test.c:
> #include <stdio.h>
>
> void foo() {
>     bool x;
> }
>
> When analyzing type "bool", I would like to emit fixit hint to top of
> the file, just before "#include <stdio.h>".
>
> So my question is how to find the location of "root/top" AST node.
>
> Thanks
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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

Re: Emit fixit for root AST node

via cfe-dev

so 6. 10. 2018 o 17:42 Artem Dergachev <[hidden email]> napísal(a):
The "root/top" AST node is TranslationUnitDecl that can be obtained via
ASTContext.getTranslationUnitDecl().

I guess you only need source locations in order to emit a fixit, so you
can use SourceManager directly, eg.
SourceManager.getLocForStartOfFile(SourceManager.getMainFileID()).

The problem is probably harder though, because many files start with
comments, which you'll most likely want to skip, so "top of the file"
and "just before the first #include" will most likely be two different
locations, and this difficulty will require more advanced hacking to
overcome, which i'm not brave enough to reason about. You might find
some inspiration in some of the existing clang-tidy checkers that deal
with #includes, or maybe in the include-what-you-use tool (though it
doesn't seem to be using fixits at all).

On 10/6/18 1:53 AM, Dávid Bolvanský via cfe-dev wrote:
> Hi,
>
> test.c:
> #include <stdio.h>
>
> void foo() {
>     bool x;
> }
>
> When analyzing type "bool", I would like to emit fixit hint to top of
> the file, just before "#include <stdio.h>".
>
> So my question is how to find the location of "root/top" AST node.
>
> Thanks
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


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