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

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

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

Tom Stellard via cfe-dev
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
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

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

Tom Stellard via cfe-dev
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 <[hidden email]> On Behalf Of via cfe-dev
Sent: Wednesday, November 27, 2019 8:31 AM
To: Clang Dev <[hidden email]>
Cc: Stepan Dyatkovskiy <[hidden email]>
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
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

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

Tom Stellard via cfe-dev
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" <[hidden email]>:

> 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 <[hidden email]> On Behalf Of via cfe-dev
> Sent: Wednesday, November 27, 2019 8:31 AM
> To: Clang Dev <[hidden email]>
> Cc: Stepan Dyatkovskiy <[hidden email]>
> 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
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

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

Tom Stellard via cfe-dev
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: [hidden email] <[hidden email]>
Sent: Wednesday, November 27, 2019 8:53 AM
To: Keane, Erich <[hidden email]>; Clang Dev <[hidden email]>
Cc: Stepan Dyatkovskiy <[hidden email]>
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" <[hidden email]>:

> 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 <[hidden email]> On Behalf Of via cfe-dev
> Sent: Wednesday, November 27, 2019 8:31 AM
> To: Clang Dev <[hidden email]>
> Cc: Stepan Dyatkovskiy <[hidden email]>
> 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
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

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

Tom Stellard via cfe-dev
Thanks!

27.11.2019, 20:55, "Keane, Erich" <[hidden email]>:

> 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: [hidden email] <[hidden email]>
> Sent: Wednesday, November 27, 2019 8:53 AM
> To: Keane, Erich <[hidden email]>; Clang Dev <[hidden email]>
> Cc: Stepan Dyatkovskiy <[hidden email]>
> 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" <[hidden email]>:
>>  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 <[hidden email]> On Behalf Of via cfe-dev
>>  Sent: Wednesday, November 27, 2019 8:31 AM
>>  To: Clang Dev <[hidden email]>
>>  Cc: Stepan Dyatkovskiy <[hidden email]>
>>  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
>>  [hidden email]
>>  https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev