Quick question: how to check that Decl belongs to included file

Hi folks! I'm currently trying to check whether declaration belongs to included file by #include directive.

I'm currently about to call Decl::getLocation() and run through bunch of checks with FileID and MacroID. But perhaps there is exist similar function or method?

Thanks!
-Stepan

If you have access to the SourceManager (ASTContext has a reference to it), you can check whether it is the 'main file':
https://clang.llvm.org/doxygen/classclang_1_1SourceManager.html

check isInMainFile to make sure it isn't in the original .c/.cpp/.whatever file. You can also check whether it is in a system header or in the 'builtin' 'file'.

Anything that isn't in the Main or 'builtin' files I would expect to have come in via a module import or #include.

Thanks! That works. I was afraid it won't work for decls produced by macro expansions, but it works even in this case. So I defined header like this:

// Header.h

#define DEF_CLASS_C(A) class C { \
public: \
  static int get() { return A; } \
};

DEF_CLASS_C(777);

and then used it in main file:

// main.cpp

#include "Header.h"
//...

And somehow it still detects that macro expansion was not from main file. It seems that macro expansion also belongs to file it was expanded in.

Thanks!
-Stepan

Yep! The source location is going to be after macro expansion. You can get the macro locations via the source manager as well.

Thanks!