Emit fixit for root AST node

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

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).

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).

Thanks. I found https://github.com/llvm-mirror/clang/blob/f3b7928366f63b51ffc97e74f8afcff497c57e8d/lib/CodeGen/MacroPPCallbacks.cpp#L178 and I think this logic is what I need :slight_smile:

so 6. 10. 2018 o 17:42 Artem Dergachev <noqnoqneo@gmail.com> napísal(a):