2016-06-22 10 views
1

Hallo, ich versuche, einen AST Clang Besucher zu implementieren, und das ist mein Code.Clang AST Besucher, vermeiden Sie das Durchlaufen Include-Dateien

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: " << funcName << " of type " << funcType << "\n"; 
     APIs << "\n\n" << funcName <<": "; 
     APIs << funcType << ", "; 
     return true; 
    } 

    virtual bool VisitStmt(Stmt *st) 
    { 
     if (CallExpr *call = dyn_cast<CallExpr>(st)) 
     { 
      numFuncCalls++; 
      FunctionDecl *func_decl = call->getDirectCallee(); 
      string funcCall = func_decl->getNameInfo().getName().getAsString(); 
      cout << "Found function call: " << funcCall << " with arguments "; 
      APIs << funcCall << ", "; 
      for(int i=0, j = call->getNumArgs(); i<j; i++) 
      { 
       string TypeS; 
       raw_string_ostream s(TypeS); 
       call->getArg(i)->printPretty(s, 0, Policy); 
       cout<< s.str() << ", "; 
       APIs<< s.str() << ", "; 
      } 
      cout << "\n"; 
     } 
     return true; 
    } 
}; 

Wie kann ich vermeiden, die enthaltenen Header-Dateien zu durchlaufen, aber ohne ihre Informationen zu verlieren. Ich will nur nicht verantwortlich jede mögliche Informationen über diese Dateien drucken, aber ich möchte Klirren über diese Dateien wissen

Danke

Antwort

1

von AST Kontext verwenden, können Sie alle nescecarry Informationen für den Code erhalten Sie parsen. Die Funktion, die zwischen AST-Knoten in der Hauptdatei oder den Header-Dateien unterscheidet, heißt isInMainFile() und kann wie folgt verwendet werden.

bool VisitVarDecl(VarDecl *var) 
{ 
    if (astContext->getSourceManager().isInMainFile(var->getLocStart())) //checks if the node is in the main = input file. 
    { 
     if(var->hasLocalStorage() || var->isStaticLocal()) 
     { 
      //var->dump(); //prints the corresponding line of the AST. 
      FullSourceLoc FullLocation = astContext->getFullLoc(var->getLocStart()); 
      numVariables++; 
      string varName = var->getQualifiedNameAsString(); 
      string varType = var->getType().getAsString(); 
      REPORT << "Variable Declaration [" << FullLocation.getSpellingLineNumber() << "," << FullLocation.getSpellingColumnNumber() << "]: " << varName << " of type " << varType << "\n"; 
      APIs << varType << ","; 
     } 
    } 
    return true; 
} 

Weitere Informationen darüber, wie astContext verwenden, um den offiziellen rekursive ASTVisitor Tutorial in Klirren Webseite folgen.