<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hi everybody,</div><div><br></div><div>I am playing with clang plugins to generate some code. In my plugin I do in the visit CXXRecordDecl of my recursive ast visitor this code when i detect that the declaration is a ClassA class:</div><div><span style="font-family: Menlo; font-size: 11px; "><br></span></div><div><font face="Menlo">auto declContext = declaration->getDeclContext();</font></div><div><div style="margin: 0px; min-height: 13px; "><span style="background-color: transparent;"><font face="Menlo"><br></font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">SourceLocation startLoc = declaration->getLocStart();</font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">const char* startBuf = sourceManager_.getCharacterData(startLoc);</font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">const char* classStartBuf = strchr(startBuf, '{')+1;</font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">SourceLocation insertStartLoc = declaration->getLocStart().getLocWithOffset(std::distance(startBuf, classStartBuf));</font></span></div><div style="margin: 0px; min-height: 13px; "><span style="background-color: transparent;"><font face="Menlo"><br></font></span></div><div style="margin: 0px; min-height: 13px; "><span style="background-color: transparent;"><font face="Menlo"><br></font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">llvm::APInt apInt{32, 42, true};</font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">clang::IntegerLiteral* intergerLiteral = clang::IntegerLiteral::Create(*astContext_, apInt, astContext_->IntTy, insertStartLoc);</font></span></div><div style="margin: 0px; min-height: 13px; "><font face="Menlo"><br></font></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">clang::TypeSourceInfo* typeSourceInfo = astContext_->CreateTypeSourceInfo(astContext_->IntTy);</font></span></div><div style="margin: 0px; min-height: 13px; "><font face="Menlo"><br></font></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">clang::IdentifierInfo& id = astContext_->Idents.get("test_");</font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">clang::FieldDecl* fieldDecl = clang::FieldDecl::Create(*astContext_, declaration, insertStartLoc, insertStartLoc, &id, astContext_->IntTy, typeSourceInfo, intergerLiteral, false, clang::InClassInitStyle::ICIS_CopyInit);</font></span></div><div style="margin: 0px; min-height: 13px; "><span style="background-color: transparent;"><font face="Menlo"><br></font></span></div><div style="margin: 0px; "><span style="background-color: transparent;"><font face="Menlo">fieldDecl->setAccess(clang::AccessSpecifier::AS_public);</font></span></div><div style="margin: 0px; min-height: 13px; "><span style="background-color: transparent;"><font face="Menlo"><br></font></span></div><div style="margin: 0px; "><span style="background-color: transparent; "><font face="Menlo">declContext->addDecl(fieldDecl);</font></span></div></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; "><span style="background-color: transparent;"><br></span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; "><span style="background-color: transparent;"><br></span></div><div style="margin: 0px; ">In my main.cpp i just try to do</div><div style="margin: 0px; "><br></div><div style="margin: 0px; "><font face="Menlo">std::out<< "Hello " << a.test_;</font></div><div style="margin: 0px; "><br></div><div style="margin: 0px; ">Where a is a ClassA class;</div><div style="margin: 0px; "><br></div><div style="margin: 0px; ">But clang crashes when it tries to compile the a.test_; Because this code compile fine:</div><div style="margin: 0px; "><br></div><div style="margin: 0px; "><font face="Menlo">std::out<< "Hello " << a.test_;</font></div><div style="margin: 0px; "><br></div><div style="margin: 0px; ">Here is the crash report:</div><div style="margin: 0px; "><br></div><div style="margin: 0px; "><div style="margin: 0px; ">Thread 0 Crashed:: Dispatch queue: com.apple.main-thread</div><div style="margin: 0px; ">0 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108c7e0c9 HandleLValueMember((anonymous namespace)::EvalInfo&, clang::Expr const*, (anonymous namespace)::LValue&, clang::FieldDecl const*, clang::ASTRecordLayout const*) + 121</div><div style="margin: 0px; ">1 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108ca09c2 clang::StmtVisitorBase<clang::make_const_ptr, (anonymous namespace)::LValueExprEvaluator, bool>::Visit(clang::Stmt const*) + 5058</div><div style="margin: 0px; ">2 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108c76cae Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) + 206</div><div style="margin: 0px; ">3 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108c72939 EvaluateAsRValue((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::APValue&) + 73</div><div style="margin: 0px; ">4 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108c72786 clang::Expr::EvaluateAsRValue(clang::Expr::EvalResult&, clang::ASTContext const&) const + 326</div><div style="margin: 0px; ">5 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108c72d16 clang::Expr::EvaluateAsInt(llvm::APSInt&, clang::ASTContext const&, clang::Expr::SideEffectsKind) const + 182</div><div style="margin: 0px; ">6 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108dede0d clang::StmtVisitorBase<clang::make_ptr, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) + 5901</div><div style="margin: 0px; ">7 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108df1a9e (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) + 126</div><div style="margin: 0px; ">8 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108dec8eb clang::StmtVisitorBase<clang::make_ptr, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) + 491</div><div style="margin: 0px; ">9 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108de7a48 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) + 72</div><div style="margin: 0px; ">10 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108dc2867 clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) + 135</div><div style="margin: 0px; ">11 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108d960d3 clang::CodeGen::CodeGenFunction::EmitCallArg(clang::CodeGen::CallArgList&, clang::Expr const*, clang::QualType) + 243</div><div style="margin: 0px; ">12 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108d81f0d void clang::CodeGen::CodeGenFunction::EmitCallArgs<clang::FunctionProtoType>(clang::CodeGen::CallArgList&, clang::FunctionProtoType const*, clang::ConstExprIterator, clang::ConstExprIterator) + 109</div><div style="margin: 0px; ">13 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108de1a30 clang::CodeGen::CodeGenFunction::EmitCXXMemberCall(clang::CXXMethodDecl const*, clang::SourceLocation, llvm::Value*, clang::CodeGen::ReturnValueSlot, llvm::Value*, llvm::Value*, clang::ConstExprIterator, clang::ConstExprIterator) + 592</div><div style="margin: 0px; ">14 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108de2946 clang::CodeGen::CodeGenFunction::EmitCXXOperatorMemberCallExpr(clang::CXXOperatorCallExpr const*, clang::CXXMethodDecl const*, clang::CodeGen::ReturnValueSlot) + 278</div><div style="margin: 0px; ">15 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108dcee45 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) + 597</div><div style="margin: 0px; ">16 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108dc6637 clang::CodeGen::CodeGenFunction::EmitCallExprLValue(clang::CallExpr const*) + 39</div><div style="margin: 0px; ">17 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108dc295a clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*) + 122</div><div style="margin: 0px; ">18 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108dc27c3 clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) + 35</div><div style="margin: 0px; ">19 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e37268 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*) + 232</div><div style="margin: 0px; ">20 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e3ba3b clang::CodeGen::CodeGenFunction::EmitCompoundStmt(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) + 235</div><div style="margin: 0px; ">21 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e37674 clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*) + 260</div><div style="margin: 0px; ">22 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e3719a clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*) + 26</div><div style="margin: 0px; ">23 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e46523 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) + 851</div><div style="margin: 0px; ">24 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e4eb25 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl) + 581</div><div style="margin: 0px; ">25 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e4c20c clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl) + 220</div><div style="margin: 0px; ">26 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e4dff2 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) + 1362</div><div style="margin: 0px; ">27 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e52bb4 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) + 276</div><div style="margin: 0px; ">28 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e6a91f (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) + 95</div><div style="margin: 0px; ">29 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e43a8c clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) + 156</div><div style="margin: 0px; ">30 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108f30ff8 clang::MultiplexConsumer::HandleTopLevelDecl(clang::DeclGroupRef) + 72</div><div style="margin: 0px; ">31 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108f8c3f3 clang::ParseAST(clang::Sema&, bool, bool) + 419</div><div style="margin: 0px; ">32 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108e42aeb clang::CodeGenAction::ExecuteAction() + 91</div><div style="margin: 0px; ">33 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108f1e836 clang::FrontendAction::Execute() + 86</div><div style="margin: 0px; ">34 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108efe0cd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 893</div><div style="margin: 0px; ">35 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x0000000108bd4ee7 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 4311</div><div style="margin: 0px; ">36 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x00000001084920ed cc1_main(char const**, char const**, char const*, void*) + 813</div><div style="margin: 0px; ">37 clang-3.3 <span class="Apple-tab-span" style="white-space:pre"> </span>0x000000010848f4a2 main + 2898</div><div style="margin: 0px; ">38 libdyld.dylib <span class="Apple-tab-span" style="white-space:pre"> </span>0x00007fff89ef57e1 start + 1</div><div style="margin: 0px; "><br></div><div style="margin: 0px; ">Can anybody tell me what is wrong in my code to create the FieldDecl?</div><div style="margin: 0px; "><br></div><div style="margin: 0px; ">Thanks</div><div style="margin: 0px; "><br></div><div style="margin: 0px; ">Emmanuel</div><div style="margin: 0px; "><br></div></div></body></html>