Get location of #pragma

Hi,

I’m writing a code re-writer involving the use of pragmas. I have my PragmaHandler and every other bit working fine, but when I try to replace the pragma statement it only replaces the parts after the #pragma keyword. HandlePragma provides a SourceLocation to the token after #pragma, but not the keyword itself. Is there any way, preferably from within the PragmaHandler, that I can get a SourceLocation to the #pragma keyword?

Example:

Input: #pragma omp parallel for
Desired output: { char * specLoop; }
Actual output: #pragma { char * specLoop; }

Cheers,

Dan

HandlePragma should take a SourceRange that covers the '#pragma' or '_Pragma', so that this information is available to the pragma handlers. It should be a fairly simple change to Clang; would you like to contribute a patch for it?

  - Doug

Hi,

HandlePragma should take a SourceRange that covers the ‘#pragma’ or ‘_Pragma’, so that this information is available to the pragma handlers. It should be a fairly simple change to Clang; would you like to contribute a patch for it?

Happy to. Just to check, you are suggesting I modify the current HandlePragma method to have the following prototype?
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &FirstToken, SourceLocation IntroducerLoc) = 0;

With the three types of pragma supported (#pragma, _Pragma and __Pragma) their locations only appear available when the preprocessor first discovers the token (HandleDirective, Handle_Pragma and HandleMicrosoft__pragma respectively), and is lost by the time HandlePragmaDirective is called so it’s probably best to pass the location from those places to HandlePragmaDirective and onwards.

_Pragma and __Pragma are easy, they each have PragmaLoc pointing to the keyword. However, by the time #pragma is fully discovered it only has a location pointing to “pragma” not “#pragma”. I could use the getLocWithOffset(-1) method, or catch it at the start of the HandleDirective method, which do you think is best?

I’ll have to update the existing PragmaHandlers too.

Does all this sound sane? First time someone’s asked me to contribute something specific back to open source. :smiley:

Cheers,

Dan

HandlePragma should take a SourceRange that covers the ‘#pragma’ or ‘_Pragma’, so that this information is available to the pragma handlers. It should be a fairly simple change to Clang; would you like to contribute a patch for it?

Happy to. Just to check, you are suggesting I modify the current HandlePragma method to have the following prototype?
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &FirstToken, SourceLocation IntroducerLoc) = 0;

Sorry, make that:
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &FirstToken, SourceRange IntroducerRange) = 0;

Cheers,

Dan

Hi,

HandlePragma should take a SourceRange that covers the ‘#pragma’ or ‘_Pragma’, so that this information is available to the pragma handlers. It should be a fairly simple change to Clang; would you like to contribute a patch for it?

Happy to. Just to check, you are suggesting I modify the current HandlePragma method to have the following prototype?
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &FirstToken, SourceLocation IntroducerLoc) = 0;

With the SourceRange change from your follow-up e-mail, yes.

With the three types of pragma supported (#pragma, _Pragma and __Pragma) their locations only appear available when the preprocessor first discovers the token (HandleDirective, Handle_Pragma and HandleMicrosoft__pragma respectively), and is lost by the time HandlePragmaDirective is called so it’s probably best to pass the location from those places to HandlePragmaDirective and onwards.

_Pragma and __Pragma are easy, they each have PragmaLoc pointing to the keyword. However, by the time #pragma is fully discovered it only has a location pointing to “pragma” not “#pragma”. I could use the getLocWithOffset(-1) method, or catch it at the start of the HandleDirective method, which do you think is best?

Please catch it at the start of HandleDirective and pass it down.

I’ll have to update the existing PragmaHandlers too.

Yes, please!

Does all this sound sane? First time someone’s asked me to contribute something specific back to open source. :smiley:

Yes, this sounds perfectly reasonable. Thanks for looking into it!

  • Doug