[cfe-dev] Emit fixit for root AST node

Artem Dergachev via cfe-dev cfe-dev at lists.llvm.org
Sat Oct 6 08:42:06 PDT 2018


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
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list