<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Hello everyone,<br>
</p>
<p>I am still struggling with my tool and I need some advice:</p>
<p> I have defined my own pragma, which enables/disables certain
functionality.</p>
<p>The PragmaHandler is called, but I have no idea how to pass the
state of the pragma into my RecursiveASTVisitor.</p>
<p>From my understanding the PragmaHandler is called by the
Preprocessor for all found #pragma instances and after</p>
<p> that my Visitor is executed. But the pragma state changes
PUSH/POP/enable are lost at this point.<br>
</p>
<p>How can I pass the pragma state to my Visitor? I want to attach
an implicit attribute to the affected AStNodes.</p>
<p>I have attached a stripped version of the problem.<br>
</p>
<p>Thanks <br>
</p>
<p>Marcel<br>
</p>
<p><font face="Courier New, Courier, monospace"><-- code snippet
--></font></p>
<p><font face="Courier New, Courier, monospace">static bool
mypragma_enabled = false;<br>
<br>
// #pragma mypragma PUSH<br>
// #pragma mypragma <br>
// #pragma mypragma POP<br>
<br>
class MyPragmaHandler : public PragmaHandler {<br>
public:<br>
MyPragmaHandler() : PragmaHandler("mypragma") {}<br>
<br>
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind
Introducer, Token &Tok)<br>
{<br>
SourceLocation PragmaLocation = Tok.getLocation();<br>
StringRef PragmaName =
Tok.getIdentifierInfo()->getName();<font face="Courier New,
Courier, monospace"><br>
</font></font></p>
<p><font face="Courier New, Courier, monospace"><font face="Courier
New, Courier, monospace"> mypragma_enabled = true;</font></font></p>
<p><font face="Courier New, Courier, monospace"> // how
to attach the pragma information to an ASTContext here?<br>
}<br>
};<br>
<br>
class MyASTVisitor : public
RecursiveASTVisitor<MyASTVisitor> {<br>
public:<br>
MyASTVisitor(Rewriter &R) : TheRewriter(R) {}<br>
<br>
bool VisitFunctionDecl(FunctionDecl *f) {</font></p>
<p><font face="Courier New, Courier, monospace"> // How the
get current state of the pragma here?</font></p>
<p><font face="Courier New, Courier, monospace"> // This is
not working:<br>
</font></p>
<p><font face="Courier New, Courier, monospace"> if
(!mypragma_enabled) {<br>
llvm::errs() << "VisitFunctionDecl: skip
function\n";<br>
return false;<br>
}<br>
<br>
// processing...<br>
<br>
return true;<br>
}<br>
<br>
private:<br>
Rewriter &TheRewriter;<br>
};<br>
<br>
class MyASTConsumer : public ASTConsumer {<br>
public:<br>
MyASTConsumer(Rewriter &R) : Visitor(R) {}<br>
<br>
// Override the method that gets called for each parsed
top-level<br>
// declaration.<br>
bool HandleTopLevelDecl(DeclGroupRef DR) override {<br>
for (DeclGroupRef::iterator b = DR.begin(), e =
DR.end(); b != e; ++b) {<br>
// Traverse the declaration using our AST visitor.<br>
Visitor.TraverseDecl(*b);<br>
(*b)->dump();<br>
}<br>
<br>
return true;<br>
}<br>
<br>
private:<br>
MyASTVisitor Visitor;<br>
};<br>
<br>
class MyFrontendAction : public ASTFrontendAction {<br>
public:<br>
MyFrontendAction() {}<br>
void EndSourceFileAction() override {<br>
SourceManager &SM = TheRewriter.getSourceMgr();<br>
// Now emit the rewritten buffer.<br>
TheRewriter.getEditBuffer(SM.getMainFileID()).write(llvm::outs());<br>
}<br>
<br>
std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance &CI,<br>
StringRef file) override {<br>
TheRewriter.setSourceMgr(CI.getSourceManager(),
CI.getLangOpts());<br>
return
llvm::make_unique<MyASTConsumer>(TheRewriter);<br>
}<br>
<br>
private:<br>
Rewriter TheRewriter;<br>
};<br>
<br>
int main(int argc, const char **argv) {<br>
CommonOptionsParser op(argc, argv, MyCategory);<br>
ClangTool Tool(op.getCompilations(),
op.getSourcePathList());<br>
return
Tool.run(newFrontendActionFactory<MyFrontendAction>().get());<br>
}<br>
<br>
static PragmaHandlerRegistry::Add<MyPragmaHandler><br>
Y("mypragma", "mypragma enables ...");<br>
</font><br>
</p>
</body>
</html>