[cfe-dev] Quick question: how to check that Decl belongs to included file

via cfe-dev cfe-dev at lists.llvm.org
Wed Nov 27 09:41:44 PST 2019


Thanks!

27.11.2019, 20:55, "Keane, Erich" <erich.keane at intel.com>:
> Yep! The source location is going to be after macro expansion. You can get the macro locations via the source manager as well.
>
> -----Original Message-----
> From: llvm at dyatkovskiy.com <llvm at dyatkovskiy.com>
> Sent: Wednesday, November 27, 2019 8:53 AM
> To: Keane, Erich <erich.keane at intel.com>; Clang Dev <cfe-dev at lists.llvm.org>
> Cc: Stepan Dyatkovskiy <stepan at dyatkovskiy.com>
> Subject: Re: [cfe-dev] Quick question: how to check that Decl belongs to included file
>
> 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
>
> 27.11.2019, 20:38, "Keane, Erich" <erich.keane at intel.com>:
>>  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.
>>
>>  -----Original Message-----
>>  From: cfe-dev <cfe-dev-bounces at lists.llvm.org> On Behalf Of via cfe-dev
>>  Sent: Wednesday, November 27, 2019 8:31 AM
>>  To: Clang Dev <cfe-dev at lists.llvm.org>
>>  Cc: Stepan Dyatkovskiy <stepan at dyatkovskiy.com>
>>  Subject: [cfe-dev] 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
>>  _______________________________________________
>>  cfe-dev mailing list
>>  cfe-dev at lists.llvm.org
>>  https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list