Wrong lexical context

Hello,
I’m making an operator that adds attributes of a base class into a derived one, hidding the base class attribute, but when i run it I get the following error:

IHI_operator: /home/daniel/llvm/tools/clang/lib/AST/DeclBase.cpp:1094: void clang::DeclContext::addHiddenDecl(clang::Decl*): Assertion `D->getLexicalDeclContext() == this && “Decl inserted into wrong lexical context”’ failed.
Stack dump:
0. parser at end of file

My code for the matcher is:

DeclarationMatcher IHI_Matcher = recordDecl(
isDerivedFrom(
recordDecl(
forEach(
fieldDecl(
anyOf(
isPublic(),
isProtected()
),
unless(
hasType(
isConstQualified()
)
)
).bind(“ParentVar”)
)
)
)
).bind(“ChildClass”);

And this is the run function:

virtual void run(const MatchFinder::MatchResult &Result) {
ASTContext *Context = Result.Context;
Rewriter Rewrite;
Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts());

RecordDecl *FS;
const FieldDecl *FS1;

if ((FS = const_cast<RecordDecl*>(Result.Nodes.getNodeAsclang::RecordDecl(“ChildClass”)))
&& (FS1 = Result.Nodes.getNodeAsclang::FieldDecl(“ParentVar”))){

FullSourceLoc FullLocation = Context->getFullLoc(FS->getLocStart());
FullSourceLoc FullLocation1 = Context->getFullLoc(FS1->getLocStart());

if ((FullLocation.isValid())){

FieldDecl* field = const_cast<FieldDecl*>(FS1);

if (FS->hasAttrs()){
for(RecordDecl::field_iterator i = FS->field_begin(); i != FS->field_end(); i++){
if ((field->getName() != (*i)->getName()) && (field->getType() != (*i)->getType())){
FS->dump();
llvm::outs() << "Found declaration at "
<< FullLocation1.getSpellingLineNumber() << “:”
<< FullLocation1.getSpellingColumnNumber() << “\n”;
FS->addDecl(field);
const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(FullLocation.getFileID());
llvm::outs() << std::string(RewriteBuf->begin(), RewriteBuf->end());
}
}
}
else {
FS->dump();
llvm::outs() << "Found declaration at "
<< FullLocation1.getSpellingLineNumber() << “:”
<< FullLocation1.getSpellingColumnNumber() << “\n”;
FS->addDecl(field);
const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(FullLocation.getFileID());
llvm::outs() << std::string(RewriteBuf->begin(), RewriteBuf->end());
}
}
}

}
};

I’m not quite sure about what is happening here, can someone help me? Thank you.