[cfe-dev] Plugin: Pragma with argument and diagnostic error messages

Marcel Schaible via cfe-dev cfe-dev at lists.llvm.org
Mon Jul 10 02:13:40 PDT 2017


Hi,

I need to evaluate a user defined pragma by using the PragmaHandler class in a plugin (see the snippet below). 

The pragma is defined like this: #pragma TEST with mandatory arguments ARG1 and ARG2.

Example:

#pragma TEST ARG1
...
#pragma TEST ARG2


At the moment I am struggling with evaluating the mandatory prama arguments 
in "if (II->isStr("ARG1"))".

The second problem how to define in a plugin its own diagnostic messages in "PP.Diag(PragmaLocation, diag::????????) << PragmaName;"?

Any idea or pointing me into the right direction is welcome.

<--- Code snippet start -->

  class TestPragmaHandler : public PragmaHandler {
  public:
    TESTPragmaHandler() : PragmaHandler("TEST") {}

    void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer ,Token &Tok) 
	{
	  SourceLocation PragmaLocation = Tok.getLocation();
	  StringRef PragmaName = Tok.getIdentifierInfo()->getName();

	  if (Tok.isNot(tok::string_literal)) {
		  PP.Diag(PragmaLocation, diag::????????) << PragmaName;
		  return;
	  }

	  const IdentifierInfo *II = Tok.getIdentifierInfo();

      if (II->isStr("ARG1")) {
		std::cout<< "TEST ARG1 pragma detected" << std::endl;
		PP.Lex(Tok);
      }
      else if (II->isStr("ARG2")) {
		std::cout<< "TEST ARG2  pragma detected" << std::endl;
		PP.Lex(Tok);
      }
    }
  };
  
  static PragmaHandlerRegistry::Add<TESTPragmaHandler> Y("TEST","TEST enables xxx support");
}

<--- Code snippet end -->

Thanks

Marcel



More information about the cfe-dev mailing list