我正在做论文,我必须将源代码解析并标记为单个函数.对于每个函数,我想提取类型的名称,称为函数名称和类型转换.铿锵声是这种工作的正确工具吗?如果是,我该怎么做?
下面是一个简单的C函数.粗体是我想要的提取项目:
static char func1(unsigned int a, struct foo *b)
{
int c = 0;
struct bar *d;
if (a == 0) {
d = func2((int) a);
} else {
c = func3((struct bar *) b);
}
return c;
}
您好,我正在尝试实现AST Clang访问者,这是我的代码。
class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {
private:
ASTContext *astContext; // used for getting additional AST info
public:
virtual bool VisitVarDecl(VarDecl *var)
{
numVariables++;
string varName = var->getQualifiedNameAsString();
string varType = var->getType().getAsString();
cout << "Found variable declaration: " << varName << " of type " << varType << "\n";
APIs << varType << ", ";
return true;
}
virtual bool VisitFunctionDecl(FunctionDecl *func)
{
numFunctions++;
string funcName = func->getNameInfo().getName().getAsString();
string funcType = func->getResultType().getAsString();
cout << "Found function declaration: " << …Run Code Online (Sandbox Code Playgroud) 我正在尝试确定ASTvisitor中的变量声明是否为数组,如果为数组,则要确定数组的维数。在下面可以找到我的代码。
bool VisitVarDecl(VarDecl *var)
{
if (astContext->getSourceManager().isInMainFile(var->getLocStart())) //checks if the node is in the main = input file.
{
FullSourceLoc FullLocation = astContext->getFullLoc(var->getLocStart());
if((var->hasLocalStorage() || var->isStaticLocal ()))
{
if (!var->isDefinedOutsideFunctionOrMethod())
{
if(avoid == 0)
{
numVariables++;
string varName = var->getQualifiedNameAsString();
string varType = var->getType().getAsString();
const Type *type = var->getType().getTypePtr();
if(type->isConstantArrayType())
{
const ArrayType *Array = type->castAsArrayTypeUnsafe();
cout << "Is array of type: " << Array->getElementType().getAsString() << endl;
}
REPORT << "[" << FullLocation.getSpellingLineNumber() << "," << FullLocation.getSpellingColumnNumber() << "]Variable Declaration: …Run Code Online (Sandbox Code Playgroud)