[cfe-users] Adding a FieldDecl to a CXXRecordDecl

Mani monamimani at gmail.com
Thu Dec 20 19:23:04 PST 2012


Hi everybody,

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:

auto declContext = declaration->getDeclContext();

SourceLocation startLoc = declaration->getLocStart();
const char* startBuf = sourceManager_.getCharacterData(startLoc);
const char* classStartBuf = strchr(startBuf, '{')+1;
SourceLocation insertStartLoc = declaration->getLocStart().getLocWithOffset(std::distance(startBuf, classStartBuf));


llvm::APInt apInt{32, 42, true};
clang::IntegerLiteral* intergerLiteral = clang::IntegerLiteral::Create(*astContext_, apInt, astContext_->IntTy, insertStartLoc);

clang::TypeSourceInfo* typeSourceInfo = astContext_->CreateTypeSourceInfo(astContext_->IntTy);

clang::IdentifierInfo& id = astContext_->Idents.get("test_");
clang::FieldDecl* fieldDecl = clang::FieldDecl::Create(*astContext_, declaration, insertStartLoc, insertStartLoc, &id, astContext_->IntTy, typeSourceInfo, intergerLiteral, false, clang::InClassInitStyle::ICIS_CopyInit);

fieldDecl->setAccess(clang::AccessSpecifier::AS_public);

declContext->addDecl(fieldDecl);


In my main.cpp i just try to do

std::out<< "Hello " << a.test_;

Where a is a ClassA class;

But clang crashes when it tries to compile the a.test_; Because this code compile fine:

std::out<< "Hello " << a.test_;

Here is the crash report:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   clang-3.3                     	0x0000000108c7e0c9 HandleLValueMember((anonymous namespace)::EvalInfo&, clang::Expr const*, (anonymous namespace)::LValue&, clang::FieldDecl const*, clang::ASTRecordLayout const*) + 121
1   clang-3.3                     	0x0000000108ca09c2 clang::StmtVisitorBase<clang::make_const_ptr, (anonymous namespace)::LValueExprEvaluator, bool>::Visit(clang::Stmt const*) + 5058
2   clang-3.3                     	0x0000000108c76cae Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) + 206
3   clang-3.3                     	0x0000000108c72939 EvaluateAsRValue((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::APValue&) + 73
4   clang-3.3                     	0x0000000108c72786 clang::Expr::EvaluateAsRValue(clang::Expr::EvalResult&, clang::ASTContext const&) const + 326
5   clang-3.3                     	0x0000000108c72d16 clang::Expr::EvaluateAsInt(llvm::APSInt&, clang::ASTContext const&, clang::Expr::SideEffectsKind) const + 182
6   clang-3.3                     	0x0000000108dede0d clang::StmtVisitorBase<clang::make_ptr, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) + 5901
7   clang-3.3                     	0x0000000108df1a9e (anonymous namespace)::ScalarExprEmitter::VisitCastExpr(clang::CastExpr*) + 126
8   clang-3.3                     	0x0000000108dec8eb clang::StmtVisitorBase<clang::make_ptr, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) + 491
9   clang-3.3                     	0x0000000108de7a48 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) + 72
10  clang-3.3                     	0x0000000108dc2867 clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) + 135
11  clang-3.3                     	0x0000000108d960d3 clang::CodeGen::CodeGenFunction::EmitCallArg(clang::CodeGen::CallArgList&, clang::Expr const*, clang::QualType) + 243
12  clang-3.3                     	0x0000000108d81f0d void clang::CodeGen::CodeGenFunction::EmitCallArgs<clang::FunctionProtoType>(clang::CodeGen::CallArgList&, clang::FunctionProtoType const*, clang::ConstExprIterator, clang::ConstExprIterator) + 109
13  clang-3.3                     	0x0000000108de1a30 clang::CodeGen::CodeGenFunction::EmitCXXMemberCall(clang::CXXMethodDecl const*, clang::SourceLocation, llvm::Value*, clang::CodeGen::ReturnValueSlot, llvm::Value*, llvm::Value*, clang::ConstExprIterator, clang::ConstExprIterator) + 592
14  clang-3.3                     	0x0000000108de2946 clang::CodeGen::CodeGenFunction::EmitCXXOperatorMemberCallExpr(clang::CXXOperatorCallExpr const*, clang::CXXMethodDecl const*, clang::CodeGen::ReturnValueSlot) + 278
15  clang-3.3                     	0x0000000108dcee45 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) + 597
16  clang-3.3                     	0x0000000108dc6637 clang::CodeGen::CodeGenFunction::EmitCallExprLValue(clang::CallExpr const*) + 39
17  clang-3.3                     	0x0000000108dc295a clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*) + 122
18  clang-3.3                     	0x0000000108dc27c3 clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) + 35
19  clang-3.3                     	0x0000000108e37268 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*) + 232
20  clang-3.3                     	0x0000000108e3ba3b clang::CodeGen::CodeGenFunction::EmitCompoundStmt(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) + 235
21  clang-3.3                     	0x0000000108e37674 clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*) + 260
22  clang-3.3                     	0x0000000108e3719a clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*) + 26
23  clang-3.3                     	0x0000000108e46523 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) + 851
24  clang-3.3                     	0x0000000108e4eb25 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl) + 581
25  clang-3.3                     	0x0000000108e4c20c clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl) + 220
26  clang-3.3                     	0x0000000108e4dff2 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) + 1362
27  clang-3.3                     	0x0000000108e52bb4 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) + 276
28  clang-3.3                     	0x0000000108e6a91f (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) + 95
29  clang-3.3                     	0x0000000108e43a8c clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) + 156
30  clang-3.3                     	0x0000000108f30ff8 clang::MultiplexConsumer::HandleTopLevelDecl(clang::DeclGroupRef) + 72
31  clang-3.3                     	0x0000000108f8c3f3 clang::ParseAST(clang::Sema&, bool, bool) + 419
32  clang-3.3                     	0x0000000108e42aeb clang::CodeGenAction::ExecuteAction() + 91
33  clang-3.3                     	0x0000000108f1e836 clang::FrontendAction::Execute() + 86
34  clang-3.3                     	0x0000000108efe0cd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 893
35  clang-3.3                     	0x0000000108bd4ee7 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 4311
36  clang-3.3                     	0x00000001084920ed cc1_main(char const**, char const**, char const*, void*) + 813
37  clang-3.3                     	0x000000010848f4a2 main + 2898
38  libdyld.dylib                 	0x00007fff89ef57e1 start + 1

Can anybody tell me what is wrong in my code to create the FieldDecl?

Thanks

Emmanuel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20121220/48c249ce/attachment.html>


More information about the cfe-users mailing list