[cfe-dev] Need Help in resolving exception while calling FunctionDecl Method

Apoorva Paneliya via cfe-dev cfe-dev at lists.llvm.org
Tue Dec 27 06:57:49 PST 2016


Hi there,

Just now started with Clang. so, this might silly question.

I am getting exception while calling *FunctionDecl->getNameAsString() or  *
*FunctionDecl->**getName().str()*;

Exception Details:-

Type:-  "Exception thrown: read access violation."

Points to:-

File:- include\clang\AST\DeclarationNames.h

*StoredNameKind getStoredNameKind() const {*
*    return static_cast<StoredNameKind>(Ptr & PtrMask);*
*  }*

Actual Code

*Main.cpp*

static llvm::cl::OptionCategory ToolingSampleCategory("Tooling Sample");

int main(int argc, const char **argv)
{

        CommonOptionsParser OptionsParser(argc, argv,
ToolingSampleCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());

        DeclarationMatcher funcMatcher = functionDecl(
).bind("functionMatcher");

        *FunctionHelper funcHelper;*
funcHelper.setNodeBindName("functionMatcher");
funcHelper.MatchFinder.addMatcher(funcMatcher, &funcHelper);

Tool.run(newFrontendActionFactory(&(funcHelper.MatchFinder)).get());


llvm::SmallVector<const clang::FunctionDecl*, 5U> allFuncDecl =
funcHelper.getAllFunctionDecl();
const FunctionDecl* fnDecl = allFuncDecl[0];
* cout << fnDecl->getName().str();  /* Exception Generated while calling
method.  If same method called from run method then it works. */*

       return 0;
}


*FunctionalHelper.h*


                typedef struct _FunctionInfo
{
std::string funcName;
std::string sourceFileName;
unsigned int paramCount;
}FunctionInfo;


*class FunctionHelper : public
clang::ast_matchers::MatchFinder::MatchCallback* {

std::vector<FunctionInfo> m_vec_FunctionInfo;
std::string m_str_NodeBindName;
llvm::SmallVector<const clang::FunctionDecl*,5> m_vec_FunctionDecl;
private:
void run(const clang::ast_matchers::MatchFinder::MatchResult &Result)
override
{
const clang::FunctionDecl* tempFuncDecl =
Result.Nodes.getNodeAs<clang::FunctionDecl>(m_str_NodeBindName.c_str());

*                                 // tempFuncDecl ->getName().str();
 //from here, it's works.*
if (tempFuncDecl)
{
FunctionInfo tempFuncInfo;
fillFunctionInfo(tempFuncDecl, tempFuncInfo);

m_vec_FunctionInfo.push_back(tempFuncInfo);
m_vec_FunctionDecl.push_back(tempFuncDecl);
}
}

public:
clang::ast_matchers::MatchFinder MatchFinder;
void setNodeBindName(std::string nodeBindName) { m_str_NodeBindName =
nodeBindName; };

std::vector<FunctionInfo> getFuncInfoForAllMatchedNodes()
{
return m_vec_FunctionInfo;
}
llvm::SmallVector<const clang::FunctionDecl*,5> getAllFunctionDecl()
{
return m_vec_FunctionDecl;
}

static std::string getFunctionName(const clang::FunctionDecl* funcDecl)
{
if (!funcDecl) { return ""; }
return funcDecl->getNameAsString();
}

static std::string getSourceFileName(const clang::FunctionDecl* funcDecl)
{
if (!funcDecl) { return ""; }
return
funcDecl->getASTContext().getSourceManager().getFilename(funcDecl->getLocation()).str();
}

static unsigned int getParamsCount(const clang::FunctionDecl* funcDecl)
{
if (!funcDecl) { return 0; }
return funcDecl->getNumParams();
}

static void fillFunctionInfo(const clang::FunctionDecl* funcDecl,
FunctionInfo &funcInfo)
{
if (!funcDecl) { return; }
funcInfo.funcName = FunctionHelper::getFunctionName(funcDecl);
funcInfo.sourceFileName = FunctionHelper::getSourceFileName(funcDecl);
funcInfo.paramCount = FunctionHelper::getParamsCount(funcDecl);
}
};


Config:- 32bit, Debug
OS:- Windows
Compiled through VS2015.
Clang Version:- latest available. [ I guess 4.0]

So, Need help in resolving issue.
Thanks In Advance.!!

Regards,
Apoorva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161227/b9a31247/attachment.html>


More information about the cfe-dev mailing list